ggml-metal-embed.metal 397 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994
  1. /**
  2. * llama.cpp - commit ba1cb19cdd0d92e012e0f6e009e0620f854b6afd - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #define GGML_COMMON_DECL_METAL
  27. #define GGML_COMMON_IMPL_METAL
  28. #if defined(GGML_METAL_EMBED_LIBRARY)
  29. /**
  30. * llama.cpp - commit ba1cb19cdd0d92e012e0f6e009e0620f854b6afd - 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_U
  60. #define GGML_COMMON_AGGR_S
  61. #define GGML_COMMON_DECL
  62. #elif defined(GGML_COMMON_DECL_CPP)
  63. #include <cstdint>
  64. typedef uint16_t ggml_half;
  65. typedef uint32_t ggml_half2;
  66. // std-c++ allow anonymous unions but some compiler warn on it
  67. #define GGML_COMMON_AGGR_U data
  68. // std-c++ do not allow it.
  69. #define GGML_COMMON_AGGR_S data
  70. #define GGML_COMMON_DECL
  71. #elif defined(GGML_COMMON_DECL_METAL)
  72. #include <metal_stdlib>
  73. typedef half ggml_half;
  74. typedef half2 ggml_half2;
  75. #define GGML_COMMON_AGGR_U
  76. #define GGML_COMMON_AGGR_S
  77. #define GGML_COMMON_DECL
  78. #elif defined(GGML_COMMON_DECL_CUDA)
  79. #if defined(GGML_COMMON_DECL_MUSA)
  80. #include <musa_fp16.h>
  81. #else
  82. #include <cuda_fp16.h>
  83. #endif
  84. #include <cstdint>
  85. typedef half ggml_half;
  86. typedef half2 ggml_half2;
  87. #define GGML_COMMON_AGGR_U
  88. #define GGML_COMMON_AGGR_S data
  89. #define GGML_COMMON_DECL
  90. #elif defined(GGML_COMMON_DECL_HIP)
  91. #include <hip/hip_fp16.h>
  92. #include <cstdint>
  93. typedef half ggml_half;
  94. typedef half2 ggml_half2;
  95. #define GGML_COMMON_AGGR_U
  96. #define GGML_COMMON_AGGR_S data
  97. #define GGML_COMMON_DECL
  98. #elif defined(GGML_COMMON_DECL_SYCL)
  99. #include <sycl/half_type.hpp>
  100. #include <cstdint>
  101. typedef sycl::half ggml_half;
  102. typedef sycl::half2 ggml_half2;
  103. #define GGML_COMMON_AGGR_U
  104. #define GGML_COMMON_AGGR_S data
  105. #define GGML_COMMON_DECL
  106. #endif
  107. #if defined(GGML_COMMON_DECL)
  108. #ifndef __cplusplus
  109. #ifndef static_assert
  110. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  111. #define static_assert(cond, msg) _Static_assert(cond, msg)
  112. #else
  113. #define static_assert(cond, msg) struct global_scope_noop_trick
  114. #endif
  115. #endif
  116. #endif // __cplusplus
  117. // QK = number of values after dequantization
  118. // QK_K = super-block size
  119. #define QK_K 256
  120. #define K_SCALE_SIZE 12
  121. #if defined(GGML_COMMON_DECL_CUDA) || defined(GGML_COMMON_DECL_HIP) || defined(GGML_COMMON_DECL_SYCL)
  122. // QR = QK / number of values before dequantization
  123. // QI = number of 32 bit integers before dequantization
  124. #define QI4_0 (QK4_0 / (4 * QR4_0))
  125. #define QR4_0 2
  126. #define QI4_1 (QK4_1 / (4 * QR4_1))
  127. #define QR4_1 2
  128. #define QI5_0 (QK5_0 / (4 * QR5_0))
  129. #define QR5_0 2
  130. #define QI5_1 (QK5_1 / (4 * QR5_1))
  131. #define QR5_1 2
  132. #define QI8_0 (QK8_0 / (4 * QR8_0))
  133. #define QR8_0 1
  134. #define QI8_1 (QK8_1 / (4 * QR8_1))
  135. #define QR8_1 1
  136. #define QI2_K (QK_K / (4*QR2_K))
  137. #define QR2_K 4
  138. #define QI3_K (QK_K / (4*QR3_K))
  139. #define QR3_K 4
  140. #define QI4_K (QK_K / (4*QR4_K))
  141. #define QR4_K 2
  142. #define QI5_K (QK_K / (4*QR5_K))
  143. #define QR5_K 2
  144. #define QI6_K (QK_K / (4*QR6_K))
  145. #define QR6_K 2
  146. #define QI2_XXS (QK_K / (4*QR2_XXS))
  147. #define QR2_XXS 4
  148. #define QI2_XS (QK_K / (4*QR2_XS))
  149. #define QR2_XS 4
  150. #define QI2_S (QK_K / (4*QR2_S))
  151. #define QR2_S 4
  152. #define QI3_XXS (QK_K / (4*QR3_XXS))
  153. #define QR3_XXS 4
  154. #define QI3_XS (QK_K / (4*QR3_XS))
  155. #define QR3_XS 4
  156. #define QI1_S (QK_K / (4*QR1_S))
  157. #define QR1_S 8
  158. #define QI1_M (QK_K / (4*QR1_M))
  159. #define QR1_M 8
  160. #define QI4_NL (QK4_NL / (4*QR4_NL))
  161. #define QR4_NL 2
  162. #define QI4_XS (QK_K / (4*QR4_XS))
  163. #define QR4_XS 2
  164. #define QI3_S (QK_K / (4*QR3_S))
  165. #define QR3_S 4
  166. #endif // GGML_COMMON_DECL_CUDA || GGML_COMMON_DECL_HIP
  167. #define QK4_0 32
  168. typedef struct {
  169. ggml_half d; // delta
  170. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  171. } block_q4_0;
  172. static_assert(sizeof(block_q4_0) == sizeof(ggml_half) + QK4_0 / 2, "wrong q4_0 block size/padding");
  173. #define QK4_1 32
  174. typedef struct {
  175. union {
  176. struct {
  177. ggml_half d; // delta
  178. ggml_half m; // min
  179. } GGML_COMMON_AGGR_S;
  180. ggml_half2 dm;
  181. } GGML_COMMON_AGGR_U;
  182. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  183. } block_q4_1;
  184. static_assert(sizeof(block_q4_1) == 2 * sizeof(ggml_half) + QK4_1 / 2, "wrong q4_1 block size/padding");
  185. #define QK5_0 32
  186. typedef struct {
  187. ggml_half d; // delta
  188. uint8_t qh[4]; // 5-th bit of quants
  189. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  190. } block_q5_0;
  191. static_assert(sizeof(block_q5_0) == sizeof(ggml_half) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  192. #define QK5_1 32
  193. typedef struct {
  194. union {
  195. struct {
  196. ggml_half d; // delta
  197. ggml_half m; // min
  198. } GGML_COMMON_AGGR_S;
  199. ggml_half2 dm;
  200. } GGML_COMMON_AGGR_U;
  201. uint8_t qh[4]; // 5-th bit of quants
  202. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  203. } block_q5_1;
  204. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_half) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  205. #define QK8_0 32
  206. typedef struct {
  207. ggml_half d; // delta
  208. int8_t qs[QK8_0]; // quants
  209. } block_q8_0;
  210. static_assert(sizeof(block_q8_0) == sizeof(ggml_half) + QK8_0, "wrong q8_0 block size/padding");
  211. #define QK8_1 32
  212. typedef struct {
  213. union {
  214. struct {
  215. ggml_half d; // delta
  216. ggml_half s; // d * sum(qs[i])
  217. } GGML_COMMON_AGGR_S;
  218. ggml_half2 ds;
  219. } GGML_COMMON_AGGR_U;
  220. int8_t qs[QK8_1]; // quants
  221. } block_q8_1;
  222. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_half) + QK8_1, "wrong q8_1 block size/padding");
  223. //
  224. // Ternary quantization
  225. //
  226. // 1.6875 bpw
  227. typedef struct {
  228. uint8_t qs[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)
  229. uint8_t qh[QK_K/64]; // 4 elements per byte
  230. ggml_half d;
  231. } block_tq1_0;
  232. 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");
  233. // 2.0625 bpw
  234. typedef struct {
  235. uint8_t qs[QK_K/4]; // 2 bits per element
  236. ggml_half d;
  237. } block_tq2_0;
  238. static_assert(sizeof(block_tq2_0) == sizeof(ggml_half) + QK_K / 4, "wrong tq2_0 block size/padding");
  239. //
  240. // Super-block quantization structures
  241. //
  242. // 2-bit quantization
  243. // weight is represented as x = a * q + b
  244. // 16 blocks of 16 elements each
  245. // Effectively 2.625 bits per weight
  246. typedef struct {
  247. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  248. uint8_t qs[QK_K/4]; // quants
  249. union {
  250. struct {
  251. ggml_half d; // super-block scale for quantized scales
  252. ggml_half dmin; // super-block scale for quantized mins
  253. } GGML_COMMON_AGGR_S;
  254. ggml_half2 dm;
  255. } GGML_COMMON_AGGR_U;
  256. } block_q2_K;
  257. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_half) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  258. // 3-bit quantization
  259. // weight is represented as x = a * q
  260. // 16 blocks of 16 elements each
  261. // Effectively 3.4375 bits per weight
  262. typedef struct {
  263. uint8_t hmask[QK_K/8]; // quants - high bit
  264. uint8_t qs[QK_K/4]; // quants - low 2 bits
  265. uint8_t scales[12]; // scales, quantized with 6 bits
  266. ggml_half d; // super-block scale
  267. } block_q3_K;
  268. static_assert(sizeof(block_q3_K) == sizeof(ggml_half) + QK_K / 4 + QK_K / 8 + 12, "wrong q3_K block size/padding");
  269. // 4-bit quantization
  270. // 8 blocks of 32 elements each
  271. // weight is represented as x = a * q + b
  272. // Effectively 4.5 bits per weight
  273. typedef struct {
  274. union {
  275. struct {
  276. ggml_half d; // super-block scale for quantized scales
  277. ggml_half dmin; // super-block scale for quantized mins
  278. } GGML_COMMON_AGGR_S;
  279. ggml_half2 dm;
  280. } GGML_COMMON_AGGR_U;
  281. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  282. uint8_t qs[QK_K/2]; // 4--bit quants
  283. } block_q4_K;
  284. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2, "wrong q4_K block size/padding");
  285. // 5-bit quantization
  286. // 8 blocks of 32 elements each
  287. // weight is represented as x = a * q + b
  288. // Effectively 5.5 bits per weight
  289. typedef struct {
  290. union {
  291. struct {
  292. ggml_half d; // super-block scale for quantized scales
  293. ggml_half dmin; // super-block scale for quantized mins
  294. } GGML_COMMON_AGGR_S;
  295. ggml_half2 dm;
  296. } GGML_COMMON_AGGR_U;
  297. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  298. uint8_t qh[QK_K/8]; // quants, high bit
  299. uint8_t qs[QK_K/2]; // quants, low 4 bits
  300. } block_q5_K;
  301. 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");
  302. // 6-bit quantization
  303. // weight is represented as x = a * q
  304. // 16 blocks of 16 elements each
  305. // Effectively 6.5625 bits per weight
  306. typedef struct {
  307. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  308. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  309. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  310. ggml_half d; // super-block scale
  311. } block_q6_K;
  312. static_assert(sizeof(block_q6_K) == sizeof(ggml_half) + QK_K / 16 + 3*QK_K/4, "wrong q6_K block size/padding");
  313. // This is only used for intermediate quantization and dot products
  314. typedef struct {
  315. float d; // delta
  316. int8_t qs[QK_K]; // quants
  317. int16_t bsums[QK_K/16]; // sum of quants in groups of 16
  318. } block_q8_K;
  319. static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_t), "wrong q8_K block size/padding");
  320. // (Almost) "true" 2-bit quantization.
  321. // Due to the need to use blocks as per ggml design, it ends up using
  322. // 2.0625 bpw because of the 16-bit scale for each block of 256.
  323. typedef struct {
  324. ggml_half d;
  325. uint16_t qs[QK_K/8];
  326. } block_iq2_xxs;
  327. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  328. // 2.3125 bpw quants
  329. typedef struct {
  330. ggml_half d;
  331. uint16_t qs[QK_K/8];
  332. uint8_t scales[QK_K/32];
  333. } block_iq2_xs;
  334. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  335. // 2.5625 bpw quants
  336. typedef struct {
  337. ggml_half d;
  338. uint8_t qs[QK_K/4];
  339. uint8_t qh[QK_K/32];
  340. uint8_t scales[QK_K/32];
  341. } block_iq2_s;
  342. static_assert(sizeof(block_iq2_s) == sizeof(ggml_half) + QK_K/4 + QK_K/16, "wrong iq2_s block size/padding");
  343. // (Almost) "true" 3-bit quantization.
  344. // Due to the need to use blocks as per ggml design, it ends up using
  345. // 3.0625 bpw because of the 16-bit scale for each block of 256.
  346. typedef struct {
  347. ggml_half d;
  348. uint8_t qs[3*QK_K/8];
  349. } block_iq3_xxs;
  350. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_half) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  351. // 3.4375 bpw
  352. #define IQ3S_N_SCALE QK_K/64
  353. typedef struct {
  354. ggml_half d;
  355. uint8_t qs[QK_K/4];
  356. uint8_t qh[QK_K/32];
  357. uint8_t signs[QK_K/8];
  358. uint8_t scales[IQ3S_N_SCALE];
  359. } block_iq3_s;
  360. static_assert(sizeof(block_iq3_s) == sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
  361. // 1.5625 bpw
  362. typedef struct {
  363. ggml_half d;
  364. uint8_t qs[QK_K/8];
  365. uint16_t qh[QK_K/32];
  366. } block_iq1_s;
  367. static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  368. // 1.75 bpw
  369. typedef struct {
  370. uint8_t qs[QK_K/8]; // grid index, low 8 bits
  371. uint8_t qh[QK_K/16]; // grid index, high 3 bits + grid shift bit (for two groups of 8)
  372. uint8_t scales[QK_K/32]; // 3-bit block scales (4-bit if QK_K == 64)
  373. } block_iq1_m;
  374. static_assert(sizeof(block_iq1_m) == QK_K/8 + QK_K/16 + QK_K/32, "wrong iq1_m block size/padding");
  375. // Used by IQ1_M quants
  376. typedef union {
  377. ggml_half f16;
  378. uint16_t u16;
  379. } iq1m_scale_t;
  380. // Non-linear quants
  381. #define QK4_NL 32
  382. typedef struct {
  383. ggml_half d;
  384. uint8_t qs[QK4_NL/2];
  385. } block_iq4_nl;
  386. static_assert(sizeof(block_iq4_nl) == sizeof(ggml_half) + QK4_NL/2, "wrong iq4_nl block size/padding");
  387. typedef struct {
  388. ggml_half d;
  389. uint16_t scales_h;
  390. uint8_t scales_l[QK_K/64];
  391. uint8_t qs[QK_K/2];
  392. } block_iq4_xs;
  393. static_assert(sizeof(block_iq4_xs) == sizeof(ggml_half) + sizeof(uint16_t) + QK_K/64 + QK_K/2, "wrong iq4_xs block size/padding");
  394. #endif // GGML_COMMON_DECL
  395. #endif // GGML_COMMON_DECL
  396. ////////////////////////////////////////////////////////////////////////////////
  397. #ifndef GGML_COMMON_IMPL
  398. #if defined(GGML_COMMON_IMPL_C)
  399. #include <stdint.h>
  400. #define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
  401. #define GGML_TABLE_END() };
  402. #define GGML_COMMON_IMPL
  403. #elif defined(GGML_COMMON_IMPL_CPP)
  404. #include <cstdint>
  405. #define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
  406. #define GGML_TABLE_END() };
  407. #define GGML_COMMON_IMPL
  408. #elif defined(GGML_COMMON_IMPL_METAL)
  409. #include <metal_stdlib>
  410. #define GGML_TABLE_BEGIN(type, name, size) static const constant type name[size] = {
  411. #define GGML_TABLE_END() };
  412. #define GGML_COMMON_IMPL
  413. #elif defined(GGML_COMMON_IMPL_CUDA) || defined(GGML_COMMON_IMPL_HIP) || defined(GGML_COMMON_IMPL_MUSA)
  414. #include <cstdint>
  415. #define GGML_TABLE_BEGIN(type, name, size) static const __device__ type name[size] = {
  416. #define GGML_TABLE_END() };
  417. #define GGML_COMMON_IMPL
  418. #elif defined(GGML_COMMON_IMPL_SYCL)
  419. #include <cstdint>
  420. #define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
  421. #define GGML_TABLE_END() };
  422. #define GGML_COMMON_IMPL
  423. #endif
  424. #if defined(GGML_COMMON_IMPL)
  425. GGML_TABLE_BEGIN(uint8_t, kmask_iq2xs, 8)
  426. 1, 2, 4, 8, 16, 32, 64, 128
  427. GGML_TABLE_END()
  428. GGML_TABLE_BEGIN(uint8_t, ksigns_iq2xs, 128)
  429. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  430. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  431. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  432. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  433. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  434. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  435. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  436. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  437. GGML_TABLE_END()
  438. //#if __CUDA_ARCH__ >= GGML_CUDA_CC_DP4A // lowest compute capability for integer intrinsics
  439. GGML_TABLE_BEGIN(uint64_t, ksigns64, 128)
  440. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  441. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  442. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  443. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  444. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  445. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  446. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  447. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  448. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  449. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  450. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  451. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  452. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  453. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  454. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  455. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  456. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  457. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  458. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  459. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  460. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  461. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  462. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  463. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  464. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  465. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  466. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  467. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  468. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  469. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  470. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  471. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  472. GGML_TABLE_END()
  473. //#endif
  474. GGML_TABLE_BEGIN(uint64_t, iq2xxs_grid, 256)
  475. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  476. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  477. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  478. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  479. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  480. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  481. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  482. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  483. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  484. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  485. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  486. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  487. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  488. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  489. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  490. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  491. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  492. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  493. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  494. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  495. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  496. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  497. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  498. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  499. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  500. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  501. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  502. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  503. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  504. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  505. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  506. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  507. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  508. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  509. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  510. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  511. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  512. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  513. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  514. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  515. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  516. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  517. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  518. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  519. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  520. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  521. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  522. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  523. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  524. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  525. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  526. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  527. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  528. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  529. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  530. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  531. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  532. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  533. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  534. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  535. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  536. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  537. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  538. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  539. GGML_TABLE_END()
  540. GGML_TABLE_BEGIN(uint64_t, iq2xs_grid, 512)
  541. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  542. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  543. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  544. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  545. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  546. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  547. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  548. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  549. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  550. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  551. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  552. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  553. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  554. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  555. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  556. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  557. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  558. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  559. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  560. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  561. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  562. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  563. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  564. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  565. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  566. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  567. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  568. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  569. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  570. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  571. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  572. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  573. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  574. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  575. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  576. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  577. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  578. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  579. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  580. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  581. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  582. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  583. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  584. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  585. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  586. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  587. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  588. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  589. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  590. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  591. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  592. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  593. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  594. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  595. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  596. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  597. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  598. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  599. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  600. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  601. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  602. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  603. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  604. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  605. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  606. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  607. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  608. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  609. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  610. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  611. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  612. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  613. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  614. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  615. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  616. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  617. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  618. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  619. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  620. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  621. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  622. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  623. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  624. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  625. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  626. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  627. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  628. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  629. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  630. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  631. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  632. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  633. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  634. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  635. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  636. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  637. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  638. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  639. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  640. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  641. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  642. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  643. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  644. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  645. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  646. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  647. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  648. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  649. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  650. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  651. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  652. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  653. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  654. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  655. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  656. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  657. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  658. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  659. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  660. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  661. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  662. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  663. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  664. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  665. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  666. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  667. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  668. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  669. GGML_TABLE_END()
  670. GGML_TABLE_BEGIN(uint64_t, iq2s_grid, 1024)
  671. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  672. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  673. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  674. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  675. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  676. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  677. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  678. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  679. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  680. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  681. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  682. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  683. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  684. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  685. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  686. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  687. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  688. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  689. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  690. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  691. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  692. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  693. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  694. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  695. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  696. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  697. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  698. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  699. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  700. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  701. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  702. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  703. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  704. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  705. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  706. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  707. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  708. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  709. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  710. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  711. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  712. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  713. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  714. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  715. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  716. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  717. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  718. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  719. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  720. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  721. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  722. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  723. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  724. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  725. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  726. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  727. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  728. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  729. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  730. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  731. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  732. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  733. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  734. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  735. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  736. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  737. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  738. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  739. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  740. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  741. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  742. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  743. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  744. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  745. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  746. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  747. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  748. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  749. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  750. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  751. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  752. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  753. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  754. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  755. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  756. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  757. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  758. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  759. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  760. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  761. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  762. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  763. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  764. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  765. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  766. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  767. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  768. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  769. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  770. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  771. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  772. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  773. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  774. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  775. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  776. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  777. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  778. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  779. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  780. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  781. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  782. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  783. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  784. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  785. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  786. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  787. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  788. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  789. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  790. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  791. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  792. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  793. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  794. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  795. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  796. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  797. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  798. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  799. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  800. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  801. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  802. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  803. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  804. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  805. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  806. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  807. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  808. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  809. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  810. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  811. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  812. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  813. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  814. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  815. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  816. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  817. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  818. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  819. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  820. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  821. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  822. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  823. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  824. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  825. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  826. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  827. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  828. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  829. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  830. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  831. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  832. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  833. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  834. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  835. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  836. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  837. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  838. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  839. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  840. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  841. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  842. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  843. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  844. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  845. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  846. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  847. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  848. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  849. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  850. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  851. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  852. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  853. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  854. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  855. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  856. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  857. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  858. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  859. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  860. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  861. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  862. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  863. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  864. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  865. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  866. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  867. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  868. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  869. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  870. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  871. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  872. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  873. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  874. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  875. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  876. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  877. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  878. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  879. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  880. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  881. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  882. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  883. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  884. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  885. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  886. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  887. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  888. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  889. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  890. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  891. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  892. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  893. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  894. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  895. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  896. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  897. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  898. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  899. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  900. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  901. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  902. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  903. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  904. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  905. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  906. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  907. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  908. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  909. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  910. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  911. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  912. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  913. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  914. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  915. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  916. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  917. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  918. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  919. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  920. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  921. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  922. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  923. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  924. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  925. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  926. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  927. GGML_TABLE_END()
  928. GGML_TABLE_BEGIN(uint32_t, iq3xxs_grid, 256)
  929. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  930. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  931. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  932. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  933. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  934. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  935. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  936. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  937. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  938. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  939. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  940. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  941. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  942. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  943. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  944. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  945. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  946. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  947. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  948. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  949. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  950. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  951. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  952. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  953. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  954. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  955. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  956. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  957. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  958. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  959. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  960. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  961. GGML_TABLE_END()
  962. GGML_TABLE_BEGIN(uint32_t, iq3s_grid, 512)
  963. 0x01010101, 0x01010103, 0x01010105, 0x0101010b, 0x0101010f, 0x01010301, 0x01010303, 0x01010305,
  964. 0x01010309, 0x0101030d, 0x01010501, 0x01010503, 0x0101050b, 0x01010707, 0x01010901, 0x01010905,
  965. 0x0101090b, 0x0101090f, 0x01010b03, 0x01010b07, 0x01010d01, 0x01010d05, 0x01010f03, 0x01010f09,
  966. 0x01010f0f, 0x01030101, 0x01030103, 0x01030105, 0x01030109, 0x01030301, 0x01030303, 0x0103030b,
  967. 0x01030501, 0x01030507, 0x0103050f, 0x01030703, 0x0103070b, 0x01030909, 0x01030d03, 0x01030d0b,
  968. 0x01030f05, 0x01050101, 0x01050103, 0x0105010b, 0x0105010f, 0x01050301, 0x01050307, 0x0105030d,
  969. 0x01050503, 0x0105050b, 0x01050701, 0x01050709, 0x01050905, 0x0105090b, 0x0105090f, 0x01050b03,
  970. 0x01050b07, 0x01050f01, 0x01050f07, 0x01070107, 0x01070303, 0x0107030b, 0x01070501, 0x01070505,
  971. 0x01070703, 0x01070707, 0x0107070d, 0x01070909, 0x01070b01, 0x01070b05, 0x01070d0f, 0x01070f03,
  972. 0x01070f0b, 0x01090101, 0x01090307, 0x0109030f, 0x01090503, 0x01090509, 0x01090705, 0x01090901,
  973. 0x01090907, 0x01090b03, 0x01090f01, 0x010b0105, 0x010b0109, 0x010b0501, 0x010b0505, 0x010b050d,
  974. 0x010b0707, 0x010b0903, 0x010b090b, 0x010b090f, 0x010b0d0d, 0x010b0f07, 0x010d010d, 0x010d0303,
  975. 0x010d0307, 0x010d0703, 0x010d0b05, 0x010d0f03, 0x010f0101, 0x010f0105, 0x010f0109, 0x010f0501,
  976. 0x010f0505, 0x010f050d, 0x010f0707, 0x010f0b01, 0x010f0b09, 0x03010101, 0x03010103, 0x03010105,
  977. 0x03010109, 0x03010301, 0x03010303, 0x03010307, 0x0301030b, 0x0301030f, 0x03010501, 0x03010505,
  978. 0x03010703, 0x03010709, 0x0301070d, 0x03010b09, 0x03010b0d, 0x03010d03, 0x03010f05, 0x03030101,
  979. 0x03030103, 0x03030107, 0x0303010d, 0x03030301, 0x03030309, 0x03030503, 0x03030701, 0x03030707,
  980. 0x03030903, 0x03030b01, 0x03030b05, 0x03030f01, 0x03030f0d, 0x03050101, 0x03050305, 0x0305030b,
  981. 0x0305030f, 0x03050501, 0x03050509, 0x03050705, 0x03050901, 0x03050907, 0x03050b0b, 0x03050d01,
  982. 0x03050f05, 0x03070103, 0x03070109, 0x0307010f, 0x03070301, 0x03070307, 0x03070503, 0x0307050f,
  983. 0x03070701, 0x03070709, 0x03070903, 0x03070d05, 0x03070f01, 0x03090107, 0x0309010b, 0x03090305,
  984. 0x03090309, 0x03090703, 0x03090707, 0x03090905, 0x0309090d, 0x03090b01, 0x03090b09, 0x030b0103,
  985. 0x030b0301, 0x030b0307, 0x030b0503, 0x030b0701, 0x030b0705, 0x030b0b03, 0x030d0501, 0x030d0509,
  986. 0x030d050f, 0x030d0909, 0x030d090d, 0x030f0103, 0x030f0107, 0x030f0301, 0x030f0305, 0x030f0503,
  987. 0x030f070b, 0x030f0903, 0x030f0d05, 0x030f0f01, 0x05010101, 0x05010103, 0x05010107, 0x0501010b,
  988. 0x0501010f, 0x05010301, 0x05010305, 0x05010309, 0x0501030d, 0x05010503, 0x05010507, 0x0501050f,
  989. 0x05010701, 0x05010705, 0x05010903, 0x05010907, 0x0501090b, 0x05010b01, 0x05010b05, 0x05010d0f,
  990. 0x05010f01, 0x05010f07, 0x05010f0b, 0x05030101, 0x05030105, 0x05030301, 0x05030307, 0x0503030f,
  991. 0x05030505, 0x0503050b, 0x05030703, 0x05030709, 0x05030905, 0x05030b03, 0x05050103, 0x05050109,
  992. 0x0505010f, 0x05050503, 0x05050507, 0x05050701, 0x0505070f, 0x05050903, 0x05050b07, 0x05050b0f,
  993. 0x05050f03, 0x05050f09, 0x05070101, 0x05070105, 0x0507010b, 0x05070303, 0x05070505, 0x05070509,
  994. 0x05070703, 0x05070707, 0x05070905, 0x05070b01, 0x05070d0d, 0x05090103, 0x0509010f, 0x05090501,
  995. 0x05090507, 0x05090705, 0x0509070b, 0x05090903, 0x05090f05, 0x05090f0b, 0x050b0109, 0x050b0303,
  996. 0x050b0505, 0x050b070f, 0x050b0901, 0x050b0b07, 0x050b0f01, 0x050d0101, 0x050d0105, 0x050d010f,
  997. 0x050d0503, 0x050d0b0b, 0x050d0d03, 0x050f010b, 0x050f0303, 0x050f050d, 0x050f0701, 0x050f0907,
  998. 0x050f0b01, 0x07010105, 0x07010303, 0x07010307, 0x0701030b, 0x0701030f, 0x07010505, 0x07010703,
  999. 0x07010707, 0x0701070b, 0x07010905, 0x07010909, 0x0701090f, 0x07010b03, 0x07010d07, 0x07010f03,
  1000. 0x07030103, 0x07030107, 0x0703010b, 0x07030309, 0x07030503, 0x07030507, 0x07030901, 0x07030d01,
  1001. 0x07030f05, 0x07030f0d, 0x07050101, 0x07050305, 0x07050501, 0x07050705, 0x07050709, 0x07050b01,
  1002. 0x07070103, 0x07070301, 0x07070309, 0x07070503, 0x07070507, 0x0707050f, 0x07070701, 0x07070903,
  1003. 0x07070907, 0x0707090f, 0x07070b0b, 0x07070f07, 0x07090107, 0x07090303, 0x0709030d, 0x07090505,
  1004. 0x07090703, 0x07090b05, 0x07090d01, 0x07090d09, 0x070b0103, 0x070b0301, 0x070b0305, 0x070b050b,
  1005. 0x070b0705, 0x070b0909, 0x070b0b0d, 0x070b0f07, 0x070d030d, 0x070d0903, 0x070f0103, 0x070f0107,
  1006. 0x070f0501, 0x070f0505, 0x070f070b, 0x09010101, 0x09010109, 0x09010305, 0x09010501, 0x09010509,
  1007. 0x0901050f, 0x09010705, 0x09010903, 0x09010b01, 0x09010f01, 0x09030105, 0x0903010f, 0x09030303,
  1008. 0x09030307, 0x09030505, 0x09030701, 0x0903070b, 0x09030907, 0x09030b03, 0x09030b0b, 0x09050103,
  1009. 0x09050107, 0x09050301, 0x0905030b, 0x09050503, 0x09050707, 0x09050901, 0x09050b0f, 0x09050d05,
  1010. 0x09050f01, 0x09070109, 0x09070303, 0x09070307, 0x09070501, 0x09070505, 0x09070703, 0x0907070b,
  1011. 0x09090101, 0x09090105, 0x09090509, 0x0909070f, 0x09090901, 0x09090f03, 0x090b010b, 0x090b010f,
  1012. 0x090b0503, 0x090b0d05, 0x090d0307, 0x090d0709, 0x090d0d01, 0x090f0301, 0x090f030b, 0x090f0701,
  1013. 0x090f0907, 0x090f0b03, 0x0b010105, 0x0b010301, 0x0b010309, 0x0b010505, 0x0b010901, 0x0b010909,
  1014. 0x0b01090f, 0x0b010b05, 0x0b010d0d, 0x0b010f09, 0x0b030103, 0x0b030107, 0x0b03010b, 0x0b030305,
  1015. 0x0b030503, 0x0b030705, 0x0b030f05, 0x0b050101, 0x0b050303, 0x0b050507, 0x0b050701, 0x0b05070d,
  1016. 0x0b050b07, 0x0b070105, 0x0b07010f, 0x0b070301, 0x0b07050f, 0x0b070909, 0x0b070b03, 0x0b070d0b,
  1017. 0x0b070f07, 0x0b090103, 0x0b090109, 0x0b090501, 0x0b090705, 0x0b09090d, 0x0b0b0305, 0x0b0b050d,
  1018. 0x0b0b0b03, 0x0b0b0b07, 0x0b0d0905, 0x0b0f0105, 0x0b0f0109, 0x0b0f0505, 0x0d010303, 0x0d010307,
  1019. 0x0d01030b, 0x0d010703, 0x0d010707, 0x0d010d01, 0x0d030101, 0x0d030501, 0x0d03050f, 0x0d030d09,
  1020. 0x0d050305, 0x0d050709, 0x0d050905, 0x0d050b0b, 0x0d050d05, 0x0d050f01, 0x0d070101, 0x0d070309,
  1021. 0x0d070503, 0x0d070901, 0x0d09050b, 0x0d090907, 0x0d090d05, 0x0d0b0101, 0x0d0b0107, 0x0d0b0709,
  1022. 0x0d0b0d01, 0x0d0d010b, 0x0d0d0901, 0x0d0f0303, 0x0d0f0307, 0x0f010101, 0x0f010109, 0x0f01010f,
  1023. 0x0f010501, 0x0f010505, 0x0f01070d, 0x0f010901, 0x0f010b09, 0x0f010d05, 0x0f030105, 0x0f030303,
  1024. 0x0f030509, 0x0f030907, 0x0f03090b, 0x0f050103, 0x0f050109, 0x0f050301, 0x0f05030d, 0x0f050503,
  1025. 0x0f050701, 0x0f050b03, 0x0f070105, 0x0f070705, 0x0f07070b, 0x0f070b07, 0x0f090103, 0x0f09010b,
  1026. 0x0f090307, 0x0f090501, 0x0f090b01, 0x0f0b0505, 0x0f0b0905, 0x0f0d0105, 0x0f0d0703, 0x0f0f0101,
  1027. GGML_TABLE_END()
  1028. #define NGRID_IQ1S 2048
  1029. #define IQ1S_DELTA 0.125f
  1030. #define IQ1M_DELTA 0.125f
  1031. #if defined(GGML_COMMON_IMPL_C)
  1032. GGML_TABLE_BEGIN(uint64_t, iq1s_grid, NGRID_IQ1S)
  1033. 0xffffffffffffffff, 0xffffffffffffff01, 0xffffffffffff0000, 0xffffffffffff01ff,
  1034. 0xffffffffffff0101, 0xffffffffff00ff00, 0xffffffffff000000, 0xffffffffff01ffff,
  1035. 0xffffffffff01ff01, 0xffffffffff0101ff, 0xffffffffff010101, 0xffffffff00ff0000,
  1036. 0xffffffff0000ff00, 0xffffffff000000ff, 0xffffffff00000001, 0xffffffff00010000,
  1037. 0xffffffff01ffffff, 0xffffffff01ffff01, 0xffffffff01ff01ff, 0xffffffff01ff0101,
  1038. 0xffffffff01000000, 0xffffffff0101ffff, 0xffffffff0101ff01, 0xffffffff010101ff,
  1039. 0xffffffff01010101, 0xffffff00ffff00ff, 0xffffff00ffff0000, 0xffffff00ff00ff00,
  1040. 0xffffff00ff0000ff, 0xffffff00ff000001, 0xffffff00ff000100, 0xffffff00ff000101,
  1041. 0xffffff00ff010000, 0xffffff0000ffff00, 0xffffff0000ff0001, 0xffffff0000ff0100,
  1042. 0xffffff000000ff01, 0xffffff0000000000, 0xffffff0000000101, 0xffffff000001ff00,
  1043. 0xffffff00000100ff, 0xffffff0000010001, 0xffffff00000101ff, 0xffffff0001ff0000,
  1044. 0xffffff000100ff00, 0xffffff00010000ff, 0xffffff0001000001, 0xffffff0001010000,
  1045. 0xffffff01ffffffff, 0xffffff01ffffff01, 0xffffff01ffff01ff, 0xffffff01ffff0101,
  1046. 0xffffff01ff000000, 0xffffff01ff01ffff, 0xffffff01ff01ff01, 0xffffff01ff0101ff,
  1047. 0xffffff01ff010101, 0xffffff0100ff0000, 0xffffff010000ff00, 0xffffff0100000100,
  1048. 0xffffff01000100ff, 0xffffff0100010100, 0xffffff0101ffffff, 0xffffff0101ffff01,
  1049. 0xffffff0101ff01ff, 0xffffff0101ff0101, 0xffffff010100ff00, 0xffffff0101000000,
  1050. 0xffffff0101000100, 0xffffff010101ffff, 0xffffff010101ff01, 0xffffff01010101ff,
  1051. 0xffffff0101010101, 0xffff00ffff00ff00, 0xffff00ffff0000ff, 0xffff00ffff000001,
  1052. 0xffff00ffff010000, 0xffff00ff00ffff00, 0xffff00ff00ff0100, 0xffff00ff00000000,
  1053. 0xffff00ff00000101, 0xffff00ff000100ff, 0xffff00ff00010000, 0xffff00ff0100ff00,
  1054. 0xffff00ff01000100, 0xffff00ff01010000, 0xffff0000ffffff00, 0xffff0000ffff00ff,
  1055. 0xffff0000ffff0000, 0xffff0000ffff0001, 0xffff0000ff000000, 0xffff0000ff0001ff,
  1056. 0xffff0000ff000101, 0xffff0000ff010100, 0xffff000000ffffff, 0xffff000000ff0000,
  1057. 0xffff000000ff0101, 0xffff00000000ffff, 0xffff00000000ff00, 0xffff0000000000ff,
  1058. 0xffff000000000000, 0xffff000000000001, 0xffff000000000100, 0xffff00000001ffff,
  1059. 0xffff00000001ff01, 0xffff000000010000, 0xffff0000000101ff, 0xffff000000010101,
  1060. 0xffff000001ffff00, 0xffff00000100ff00, 0xffff000001000000, 0xffff0000010001ff,
  1061. 0xffff000001000101, 0xffff00000101ff00, 0xffff0000010100ff, 0xffff000001010000,
  1062. 0xffff000001010001, 0xffff000001010100, 0xffff0001ff0000ff, 0xffff0001ff000100,
  1063. 0xffff000100ffff00, 0xffff000100ff00ff, 0xffff00010000ffff, 0xffff00010000ff01,
  1064. 0xffff000100000000, 0xffff0001000001ff, 0xffff00010001ffff, 0xffff00010001ff00,
  1065. 0xffff000100010001, 0xffff000100010100, 0xffff000101ff0000, 0xffff00010100ff00,
  1066. 0xffff0001010000ff, 0xffff000101000100, 0xffff01ffffffffff, 0xffff01ffffffff01,
  1067. 0xffff01ffffff01ff, 0xffff01ffffff0101, 0xffff01ffff000000, 0xffff01ffff01ffff,
  1068. 0xffff01ffff01ff01, 0xffff01ffff0101ff, 0xffff01ffff010101, 0xffff01ff00ff0000,
  1069. 0xffff01ff0000ff00, 0xffff01ff00000001, 0xffff01ff00010000, 0xffff01ff01ffffff,
  1070. 0xffff01ff01ffff01, 0xffff01ff01ff01ff, 0xffff01ff01ff0101, 0xffff01ff01000000,
  1071. 0xffff01ff0101ffff, 0xffff01ff0101ff01, 0xffff01ff010101ff, 0xffff01ff01010101,
  1072. 0xffff0100ffff0000, 0xffff0100ff00ff00, 0xffff0100ff0000ff, 0xffff0100ff000100,
  1073. 0xffff0100ff0100ff, 0xffff0100ff010000, 0xffff010000ffff00, 0xffff01000000ffff,
  1074. 0xffff01000000ff00, 0xffff010000000000, 0xffff01000001ff00, 0xffff0100000100ff,
  1075. 0xffff010000010100, 0xffff01000100ff00, 0xffff0100010000ff, 0xffff010001000001,
  1076. 0xffff010001000100, 0xffff010001010000, 0xffff0101ffffffff, 0xffff0101ffffff01,
  1077. 0xffff0101ffff01ff, 0xffff0101ffff0101, 0xffff0101ff000000, 0xffff0101ff01ffff,
  1078. 0xffff0101ff01ff01, 0xffff0101ff0101ff, 0xffff0101ff010101, 0xffff010100ff0000,
  1079. 0xffff01010000ff00, 0xffff010100000100, 0xffff01010001ff00, 0xffff010100010000,
  1080. 0xffff010101ffffff, 0xffff010101ffff01, 0xffff010101ff0000, 0xffff010101ff01ff,
  1081. 0xffff010101ff0101, 0xffff010101000000, 0xffff01010101ffff, 0xffff01010101ff01,
  1082. 0xffff0101010101ff, 0xffff010101010101, 0xff00ffffff00ffff, 0xff00ffffff00ff00,
  1083. 0xff00ffffff0000ff, 0xff00ffffff000100, 0xff00ffffff0100ff, 0xff00ffffff010000,
  1084. 0xff00ffff00ffff00, 0xff00ffff00ff00ff, 0xff00ffff0000ffff, 0xff00ffff00000000,
  1085. 0xff00ffff000001ff, 0xff00ffff0001ff00, 0xff00ffff000100ff, 0xff00ffff00010000,
  1086. 0xff00ffff00010100, 0xff00ffff0100ff00, 0xff00ffff010000ff, 0xff00ffff01000001,
  1087. 0xff00ffff0101ff00, 0xff00ffff01010000, 0xff00ff00ffffff00, 0xff00ff00ffff00ff,
  1088. 0xff00ff00ffff0001, 0xff00ff00ffff0100, 0xff00ff00ff00ffff, 0xff00ff00ff00ff01,
  1089. 0xff00ff00ff000000, 0xff00ff00ff0001ff, 0xff00ff00ff01ff00, 0xff00ff00ff0100ff,
  1090. 0xff00ff00ff010100, 0xff00ff0000ff0000, 0xff00ff0000ff0101, 0xff00ff000000ffff,
  1091. 0xff00ff000000ff00, 0xff00ff000000ff01, 0xff00ff00000000ff, 0xff00ff0000000000,
  1092. 0xff00ff0000000001, 0xff00ff0000000100, 0xff00ff000001ffff, 0xff00ff0000010000,
  1093. 0xff00ff0001ff00ff, 0xff00ff000100ff01, 0xff00ff0001000000, 0xff00ff000101ff00,
  1094. 0xff00ff00010100ff, 0xff00ff01ff00ff00, 0xff00ff01ff0000ff, 0xff00ff01ff000001,
  1095. 0xff00ff01ff010000, 0xff00ff0100ffffff, 0xff00ff0100ff0001, 0xff00ff0100ff0100,
  1096. 0xff00ff010000ff01, 0xff00ff0100000000, 0xff00ff01000001ff, 0xff00ff0100000101,
  1097. 0xff00ff01000100ff, 0xff00ff0100010001, 0xff00ff0101ff0000, 0xff00ff010100ff00,
  1098. 0xff00ff01010000ff, 0xff00ff0101000001, 0xff00ff0101010000, 0xff0000ffffffff00,
  1099. 0xff0000ffffff0001, 0xff0000ffffff0100, 0xff0000ffff0000ff, 0xff0000ffff000000,
  1100. 0xff0000ffff0001ff, 0xff0000ffff000100, 0xff0000ffff01ff00, 0xff0000ffff010001,
  1101. 0xff0000ff00ffff00, 0xff0000ff00ff0000, 0xff0000ff00ff0001, 0xff0000ff00ff01ff,
  1102. 0xff0000ff00ff0101, 0xff0000ff0000ff00, 0xff0000ff000000ff, 0xff0000ff00000000,
  1103. 0xff0000ff00000001, 0xff0000ff00000100, 0xff0000ff0001ff01, 0xff0000ff00010000,
  1104. 0xff0000ff000101ff, 0xff0000ff01ff00ff, 0xff0000ff01ff0100, 0xff0000ff0100ffff,
  1105. 0xff0000ff010000ff, 0xff0000ff01000000, 0xff0000ff010001ff, 0xff0000ff01000100,
  1106. 0xff0000ff01000101, 0xff0000ff0101ff00, 0xff0000ff010100ff, 0xff0000ff01010000,
  1107. 0xff0000ff01010100, 0xff000000ffffff01, 0xff000000ffff0000, 0xff000000ffff0101,
  1108. 0xff000000ff00ff00, 0xff000000ff0000ff, 0xff000000ff000000, 0xff000000ff000001,
  1109. 0xff000000ff000100, 0xff000000ff01ffff, 0xff000000ff01ff01, 0xff000000ff010000,
  1110. 0xff000000ff0101ff, 0xff000000ff010101, 0xff00000000ffff00, 0xff00000000ff00ff,
  1111. 0xff00000000ff0000, 0xff00000000ff0001, 0xff0000000000ff00, 0xff0000000000ff01,
  1112. 0xff000000000000ff, 0xff00000000000000, 0xff00000000000001, 0xff00000000000100,
  1113. 0xff00000000000101, 0xff0000000001ff00, 0xff000000000100ff, 0xff00000000010000,
  1114. 0xff00000000010001, 0xff00000000010100, 0xff00000001ffffff, 0xff00000001ffff01,
  1115. 0xff00000001ff00ff, 0xff00000001ff0000, 0xff00000001ff01ff, 0xff00000001ff0101,
  1116. 0xff0000000100ffff, 0xff0000000100ff00, 0xff000000010000ff, 0xff00000001000000,
  1117. 0xff00000001000001, 0xff00000001000100, 0xff00000001000101, 0xff0000000101ffff,
  1118. 0xff0000000101ff01, 0xff00000001010000, 0xff000001ffffff00, 0xff000001ffff00ff,
  1119. 0xff000001ffff0000, 0xff000001ffff0001, 0xff000001ff000000, 0xff000001ff000001,
  1120. 0xff000001ff0001ff, 0xff000001ff000101, 0xff000001ff01ff00, 0xff000001ff010001,
  1121. 0xff00000100ffffff, 0xff00000100ffff01, 0xff00000100ff00ff, 0xff00000100ff0000,
  1122. 0xff00000100ff01ff, 0xff00000100ff0101, 0xff0000010000ff00, 0xff00000100000000,
  1123. 0xff00000100000001, 0xff000001000001ff, 0xff00000100000100, 0xff0000010001ff00,
  1124. 0xff000001000100ff, 0xff00000100010000, 0xff000001000101ff, 0xff00000100010100,
  1125. 0xff00000100010101, 0xff00000101ff0001, 0xff00000101ff0101, 0xff0000010100ff01,
  1126. 0xff00000101000000, 0xff000001010100ff, 0xff00000101010100, 0xff0001ffff00ff00,
  1127. 0xff0001ffff000001, 0xff0001ffff010000, 0xff0001ff00ffff00, 0xff0001ff00ff00ff,
  1128. 0xff0001ff00ff0001, 0xff0001ff00ff0100, 0xff0001ff0000ffff, 0xff0001ff00000000,
  1129. 0xff0001ff000001ff, 0xff0001ff00000101, 0xff0001ff0001ffff, 0xff0001ff0001ff00,
  1130. 0xff0001ff000100ff, 0xff0001ff00010001, 0xff0001ff00010100, 0xff0001ff01ff0000,
  1131. 0xff0001ff0100ff00, 0xff0001ff010000ff, 0xff0001ff01010000, 0xff000100ff00ffff,
  1132. 0xff000100ff00ff01, 0xff000100ff000000, 0xff000100ff000101, 0xff000100ff01ff00,
  1133. 0xff000100ff010000, 0xff00010000ffff01, 0xff00010000ff00ff, 0xff00010000ff0000,
  1134. 0xff00010000ff01ff, 0xff0001000000ff00, 0xff000100000000ff, 0xff00010000000000,
  1135. 0xff00010000000001, 0xff00010000000100, 0xff00010000000101, 0xff0001000001ffff,
  1136. 0xff00010000010000, 0xff00010000010101, 0xff00010001ff0100, 0xff0001000100ff00,
  1137. 0xff0001000100ff01, 0xff00010001000000, 0xff000100010001ff, 0xff0001000101ff00,
  1138. 0xff00010001010001, 0xff00010001010100, 0xff000101ffff0100, 0xff000101ff000001,
  1139. 0xff000101ff0100ff, 0xff000101ff010001, 0xff00010100ff00ff, 0xff00010100ff0001,
  1140. 0xff00010100ff0100, 0xff0001010000ffff, 0xff0001010000ff01, 0xff00010100000000,
  1141. 0xff000101000001ff, 0xff0001010001ff00, 0xff00010100010001, 0xff00010100010100,
  1142. 0xff00010101ff0000, 0xff0001010100ff00, 0xff00010101000001, 0xff00010101000101,
  1143. 0xff01ffffffffffff, 0xff01ffffffffff01, 0xff01ffffffff01ff, 0xff01ffffffff0101,
  1144. 0xff01ffffff000000, 0xff01ffffff01ffff, 0xff01ffffff01ff01, 0xff01ffffff010000,
  1145. 0xff01ffffff0101ff, 0xff01ffffff010101, 0xff01ffff00ff0000, 0xff01ffff0000ff00,
  1146. 0xff01ffff00000100, 0xff01ffff0001ff00, 0xff01ffff00010000, 0xff01ffff01ffffff,
  1147. 0xff01ffff01ffff01, 0xff01ffff01ff01ff, 0xff01ffff01ff0101, 0xff01ffff01000000,
  1148. 0xff01ffff0101ffff, 0xff01ffff0101ff01, 0xff01ffff01010000, 0xff01ffff010101ff,
  1149. 0xff01ffff01010101, 0xff01ff00ffff0000, 0xff01ff00ff00ff00, 0xff01ff00ff0000ff,
  1150. 0xff01ff00ff000100, 0xff01ff00ff010000, 0xff01ff0000ffff01, 0xff01ff0000ff00ff,
  1151. 0xff01ff0000ff0100, 0xff01ff0000000000, 0xff01ff00000001ff, 0xff01ff0000000101,
  1152. 0xff01ff000001ff00, 0xff01ff00000100ff, 0xff01ff0000010000, 0xff01ff0000010001,
  1153. 0xff01ff0001ff0000, 0xff01ff000100ffff, 0xff01ff0001000001, 0xff01ff0001000100,
  1154. 0xff01ff0001010000, 0xff01ff01ffffff00, 0xff01ff01ffff01ff, 0xff01ff01ffff0101,
  1155. 0xff01ff01ff00ff00, 0xff01ff01ff000000, 0xff01ff01ff01ffff, 0xff01ff01ff01ff01,
  1156. 0xff01ff01ff0101ff, 0xff01ff01ff010101, 0xff01ff0100ff0000, 0xff01ff010000ff00,
  1157. 0xff01ff0100000001, 0xff01ff0100000100, 0xff01ff0100010000, 0xff01ff0101ffff00,
  1158. 0xff01ff0101ff01ff, 0xff01ff0101ff0101, 0xff01ff010100ff00, 0xff01ff0101000000,
  1159. 0xff01ff010101ffff, 0xff01ff010101ff01, 0xff01ff01010101ff, 0xff01ff0101010101,
  1160. 0xff0100ffffff0000, 0xff0100ffff0000ff, 0xff0100ffff000001, 0xff0100ffff000100,
  1161. 0xff0100ffff010000, 0xff0100ff00ff00ff, 0xff0100ff00ff0000, 0xff0100ff00ff0001,
  1162. 0xff0100ff00ff0100, 0xff0100ff0000ff01, 0xff0100ff00000000, 0xff0100ff000001ff,
  1163. 0xff0100ff00000101, 0xff0100ff00010001, 0xff0100ff01ff0000, 0xff0100ff0100ff00,
  1164. 0xff0100ff010000ff, 0xff0100ff01000100, 0xff0100ff0101ff00, 0xff0100ff01010000,
  1165. 0xff010000ffff0100, 0xff010000ff000000, 0xff010000ff01ff00, 0xff010000ff010100,
  1166. 0xff01000000ffffff, 0xff01000000ff0000, 0xff01000000ff01ff, 0xff0100000000ff00,
  1167. 0xff010000000000ff, 0xff01000000000000, 0xff01000000000100, 0xff0100000001ff01,
  1168. 0xff01000000010000, 0xff010000000101ff, 0xff01000001ff0100, 0xff0100000100ffff,
  1169. 0xff010000010000ff, 0xff01000001000000, 0xff010000010001ff, 0xff01000001000101,
  1170. 0xff0100000101ff00, 0xff010000010100ff, 0xff01000001010001, 0xff01000001010100,
  1171. 0xff010001ffff0000, 0xff010001ff00ffff, 0xff010001ff00ff01, 0xff010001ff000100,
  1172. 0xff010001ff010000, 0xff01000100ffff00, 0xff01000100ff0100, 0xff01000100000000,
  1173. 0xff0100010001ffff, 0xff0100010001ff00, 0xff01000100010100, 0xff01000101ff00ff,
  1174. 0xff01000101ff0001, 0xff0100010100ffff, 0xff01000101000101, 0xff0101ffffffffff,
  1175. 0xff0101ffffffff01, 0xff0101ffffff01ff, 0xff0101ffffff0101, 0xff0101ffff000000,
  1176. 0xff0101ffff01ffff, 0xff0101ffff01ff01, 0xff0101ffff0101ff, 0xff0101ffff010101,
  1177. 0xff0101ff00ff0000, 0xff0101ff0000ff00, 0xff0101ff000000ff, 0xff0101ff00010000,
  1178. 0xff0101ff01ffffff, 0xff0101ff01ffff01, 0xff0101ff01ff01ff, 0xff0101ff01ff0101,
  1179. 0xff0101ff0101ffff, 0xff0101ff0101ff01, 0xff0101ff010101ff, 0xff0101ff01010101,
  1180. 0xff010100ffff0100, 0xff010100ff00ff00, 0xff010100ff0000ff, 0xff010100ff000100,
  1181. 0xff010100ff010000, 0xff01010000ff0001, 0xff01010000ff0100, 0xff0101000000ff01,
  1182. 0xff01010000000000, 0xff0101000001ff00, 0xff010100000100ff, 0xff01010000010001,
  1183. 0xff01010000010100, 0xff01010001ff0000, 0xff0101000100ffff, 0xff01010001000001,
  1184. 0xff01010001000100, 0xff010100010100ff, 0xff01010001010000, 0xff010101ffffffff,
  1185. 0xff010101ffffff01, 0xff010101ffff01ff, 0xff010101ffff0101, 0xff010101ff01ffff,
  1186. 0xff010101ff01ff01, 0xff010101ff0101ff, 0xff010101ff010101, 0xff01010100ff0000,
  1187. 0xff0101010000ff00, 0xff01010100000001, 0xff01010100000100, 0xff01010100010000,
  1188. 0xff01010101ffffff, 0xff01010101ffff01, 0xff01010101ff01ff, 0xff01010101ff0101,
  1189. 0xff01010101000000, 0xff0101010101ffff, 0xff0101010101ff01, 0xff010101010101ff,
  1190. 0xff01010101010101, 0x00ffffffffff0000, 0x00ffffffff00ff00, 0x00ffffffff000001,
  1191. 0x00ffffffff010000, 0x00ffffff00ff0100, 0x00ffffff0000ff01, 0x00ffffff00000000,
  1192. 0x00ffffff000001ff, 0x00ffffff00000101, 0x00ffffff0001ff00, 0x00ffffff000100ff,
  1193. 0x00ffffff00010001, 0x00ffffff010000ff, 0x00ffffff01000100, 0x00ffffff0101ff00,
  1194. 0x00ffffff01010001, 0x00ffff00ffffffff, 0x00ffff00ffffff00, 0x00ffff00ffff00ff,
  1195. 0x00ffff00ffff0001, 0x00ffff00ffff0100, 0x00ffff00ff00ff01, 0x00ffff00ff000000,
  1196. 0x00ffff00ff000001, 0x00ffff00ff0001ff, 0x00ffff00ff000101, 0x00ffff00ff01ff00,
  1197. 0x00ffff00ff010001, 0x00ffff00ff010100, 0x00ffff0000ff0000, 0x00ffff0000ff01ff,
  1198. 0x00ffff0000ff0101, 0x00ffff000000ff00, 0x00ffff00000000ff, 0x00ffff0000000000,
  1199. 0x00ffff0000000001, 0x00ffff0000000100, 0x00ffff0000000101, 0x00ffff0000010000,
  1200. 0x00ffff00000101ff, 0x00ffff0000010101, 0x00ffff0001ffff00, 0x00ffff0001ff00ff,
  1201. 0x00ffff0001ff0001, 0x00ffff000100ffff, 0x00ffff000100ff01, 0x00ffff0001000000,
  1202. 0x00ffff000101ffff, 0x00ffff000101ff00, 0x00ffff000101ff01, 0x00ffff01ffff0000,
  1203. 0x00ffff01ff00ff00, 0x00ffff01ff0000ff, 0x00ffff01ff000001, 0x00ffff01ff010000,
  1204. 0x00ffff0100ffff00, 0x00ffff010000ff01, 0x00ffff0100000000, 0x00ffff0100000101,
  1205. 0x00ffff01000100ff, 0x00ffff0100010100, 0x00ffff0101ff0100, 0x00ffff01010000ff,
  1206. 0x00ffff0101010000, 0x00ff00ffffffff00, 0x00ff00ffff000000, 0x00ff00ffff000100,
  1207. 0x00ff00ffff010100, 0x00ff00ff00ff0000, 0x00ff00ff00ff01ff, 0x00ff00ff00ff0101,
  1208. 0x00ff00ff0000ff00, 0x00ff00ff000000ff, 0x00ff00ff00000000, 0x00ff00ff00000001,
  1209. 0x00ff00ff0001ff00, 0x00ff00ff0001ff01, 0x00ff00ff00010000, 0x00ff00ff000101ff,
  1210. 0x00ff00ff00010101, 0x00ff00ff01ffff00, 0x00ff00ff01ff0001, 0x00ff00ff01ff0100,
  1211. 0x00ff00ff0100ffff, 0x00ff00ff0100ff01, 0x00ff00ff01000000, 0x00ff00ff0101ffff,
  1212. 0x00ff00ff0101ff00, 0x00ff00ff01010100, 0x00ff0000ffffff00, 0x00ff0000ffffff01,
  1213. 0x00ff0000ffff0000, 0x00ff0000ffff0101, 0x00ff0000ff00ff00, 0x00ff0000ff0000ff,
  1214. 0x00ff0000ff000000, 0x00ff0000ff000001, 0x00ff0000ff000100, 0x00ff0000ff01ffff,
  1215. 0x00ff0000ff010000, 0x00ff0000ff010101, 0x00ff000000ffff00, 0x00ff000000ff00ff,
  1216. 0x00ff000000ff0000, 0x00ff000000ff0001, 0x00ff000000ff0100, 0x00ff00000000ffff,
  1217. 0x00ff00000000ff00, 0x00ff0000000000ff, 0x00ff000000000000, 0x00ff000000000001,
  1218. 0x00ff0000000001ff, 0x00ff000000000100, 0x00ff00000001ff00, 0x00ff0000000100ff,
  1219. 0x00ff000000010000, 0x00ff000000010001, 0x00ff000000010100, 0x00ff000001ffff01,
  1220. 0x00ff000001ff00ff, 0x00ff000001ff0000, 0x00ff000001ff01ff, 0x00ff00000100ff00,
  1221. 0x00ff0000010000ff, 0x00ff000001000000, 0x00ff000001000001, 0x00ff000001000100,
  1222. 0x00ff000001000101, 0x00ff000001010000, 0x00ff0000010101ff, 0x00ff000001010101,
  1223. 0x00ff0001ffffff00, 0x00ff0001ffff0000, 0x00ff0001ffff0100, 0x00ff0001ff0000ff,
  1224. 0x00ff0001ff000000, 0x00ff0001ff0001ff, 0x00ff0001ff000101, 0x00ff0001ff01ff00,
  1225. 0x00ff0001ff0100ff, 0x00ff0001ff010100, 0x00ff000100ffffff, 0x00ff000100ffff01,
  1226. 0x00ff000100ff0000, 0x00ff000100ff01ff, 0x00ff00010000ffff, 0x00ff00010000ff00,
  1227. 0x00ff00010000ff01, 0x00ff000100000000, 0x00ff000100000001, 0x00ff000100000100,
  1228. 0x00ff00010001ff01, 0x00ff000100010000, 0x00ff0001000101ff, 0x00ff000101ffff00,
  1229. 0x00ff000101ff0000, 0x00ff000101ff0101, 0x00ff0001010000ff, 0x00ff000101000000,
  1230. 0x00ff00010101ff00, 0x00ff0001010100ff, 0x00ff000101010001, 0x00ff01ffffff0000,
  1231. 0x00ff01ffff00ff00, 0x00ff01ffff000000, 0x00ff01ffff000101, 0x00ff01ffff010000,
  1232. 0x00ff01ff00ffff01, 0x00ff01ff00ff0100, 0x00ff01ff0000ffff, 0x00ff01ff00000000,
  1233. 0x00ff01ff000001ff, 0x00ff01ff0001ff00, 0x00ff01ff000100ff, 0x00ff01ff00010001,
  1234. 0x00ff01ff00010100, 0x00ff01ff01ff0000, 0x00ff01ff0100ff00, 0x00ff01ff010000ff,
  1235. 0x00ff01ff01000001, 0x00ff01ff01000100, 0x00ff01ff01010000, 0x00ff0100ffffff00,
  1236. 0x00ff0100ffff0000, 0x00ff0100ffff0001, 0x00ff0100ffff0101, 0x00ff0100ff00ffff,
  1237. 0x00ff0100ff0000ff, 0x00ff0100ff000000, 0x00ff0100ff0001ff, 0x00ff0100ff01ff00,
  1238. 0x00ff0100ff0100ff, 0x00ff0100ff010001, 0x00ff010000ffffff, 0x00ff010000ff0000,
  1239. 0x00ff010000ff0101, 0x00ff01000000ff00, 0x00ff01000000ff01, 0x00ff0100000000ff,
  1240. 0x00ff010000000000, 0x00ff010000000001, 0x00ff010000000100, 0x00ff01000001ffff,
  1241. 0x00ff01000001ff01, 0x00ff010000010000, 0x00ff010000010001, 0x00ff010000010101,
  1242. 0x00ff010001ff0001, 0x00ff010001ff0100, 0x00ff01000100ff01, 0x00ff010001000000,
  1243. 0x00ff010001000001, 0x00ff0100010001ff, 0x00ff01000101ff00, 0x00ff0100010100ff,
  1244. 0x00ff010001010001, 0x00ff010001010100, 0x00ff0101ff000001, 0x00ff010100ff00ff,
  1245. 0x00ff010100ff0001, 0x00ff010100ff0100, 0x00ff010100000000, 0x00ff0101000001ff,
  1246. 0x00ff010100000101, 0x00ff0101000100ff, 0x00ff010100010100, 0x00ff0101010000ff,
  1247. 0x00ff010101010000, 0x0000ffffffffff00, 0x0000ffffffff00ff, 0x0000ffffffff0000,
  1248. 0x0000ffffffff0001, 0x0000ffffffff0100, 0x0000ffffff00ff01, 0x0000ffffff000000,
  1249. 0x0000ffffff000101, 0x0000ffffff01ff00, 0x0000ffffff0100ff, 0x0000ffffff010100,
  1250. 0x0000ffff00ffffff, 0x0000ffff00ff0000, 0x0000ffff00ff01ff, 0x0000ffff0000ff00,
  1251. 0x0000ffff000000ff, 0x0000ffff00000000, 0x0000ffff00000001, 0x0000ffff00000100,
  1252. 0x0000ffff00010000, 0x0000ffff000101ff, 0x0000ffff01ff0001, 0x0000ffff01ff0100,
  1253. 0x0000ffff01000000, 0x0000ffff010001ff, 0x0000ffff0101ffff, 0x0000ffff0101ff00,
  1254. 0x0000ffff01010001, 0x0000ffff01010100, 0x0000ff00ffff0000, 0x0000ff00ffff01ff,
  1255. 0x0000ff00ffff0100, 0x0000ff00ffff0101, 0x0000ff00ff00ff00, 0x0000ff00ff0000ff,
  1256. 0x0000ff00ff000000, 0x0000ff00ff000001, 0x0000ff00ff0001ff, 0x0000ff00ff000100,
  1257. 0x0000ff00ff01ffff, 0x0000ff00ff010000, 0x0000ff00ff010001, 0x0000ff00ff0101ff,
  1258. 0x0000ff00ff010101, 0x0000ff0000ffff00, 0x0000ff0000ff00ff, 0x0000ff0000ff0000,
  1259. 0x0000ff0000ff0001, 0x0000ff0000ff0100, 0x0000ff000000ffff, 0x0000ff000000ff00,
  1260. 0x0000ff000000ff01, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1261. 0x0000ff00000001ff, 0x0000ff0000000100, 0x0000ff0000000101, 0x0000ff000001ff00,
  1262. 0x0000ff00000100ff, 0x0000ff0000010000, 0x0000ff0000010001, 0x0000ff0000010100,
  1263. 0x0000ff0001ffff01, 0x0000ff0001ff0000, 0x0000ff000100ff00, 0x0000ff00010000ff,
  1264. 0x0000ff0001000000, 0x0000ff0001000001, 0x0000ff0001000100, 0x0000ff000101ffff,
  1265. 0x0000ff0001010000, 0x0000ff0001010101, 0x0000ff01ffffff00, 0x0000ff01ffff0001,
  1266. 0x0000ff01ff00ff01, 0x0000ff01ff000000, 0x0000ff01ff000101, 0x0000ff01ff01ff00,
  1267. 0x0000ff01ff0100ff, 0x0000ff0100ffff01, 0x0000ff0100ff0000, 0x0000ff0100ff0101,
  1268. 0x0000ff010000ff00, 0x0000ff01000000ff, 0x0000ff0100000000, 0x0000ff0100000001,
  1269. 0x0000ff0100000100, 0x0000ff010001ff01, 0x0000ff0100010000, 0x0000ff0101ff0000,
  1270. 0x0000ff010100ffff, 0x0000ff010100ff01, 0x0000ff0101000000, 0x0000ff0101000100,
  1271. 0x0000ff0101000101, 0x0000ff01010100ff, 0x000000ffffff00ff, 0x000000ffffff0000,
  1272. 0x000000ffff00ff00, 0x000000ffff0000ff, 0x000000ffff000000, 0x000000ffff000001,
  1273. 0x000000ffff0001ff, 0x000000ffff000100, 0x000000ffff01ff00, 0x000000ffff010000,
  1274. 0x000000ffff0101ff, 0x000000ffff010101, 0x000000ff00ffff00, 0x000000ff00ff00ff,
  1275. 0x000000ff00ff0000, 0x000000ff00ff0001, 0x000000ff00ff0100, 0x000000ff00ff0101,
  1276. 0x000000ff0000ffff, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1277. 0x000000ff00000001, 0x000000ff000001ff, 0x000000ff00000100, 0x000000ff00000101,
  1278. 0x000000ff0001ff00, 0x000000ff0001ff01, 0x000000ff000100ff, 0x000000ff00010000,
  1279. 0x000000ff00010001, 0x000000ff00010100, 0x000000ff01ffffff, 0x000000ff01ff01ff,
  1280. 0x000000ff01ff0101, 0x000000ff0100ff00, 0x000000ff010000ff, 0x000000ff01000000,
  1281. 0x000000ff01000001, 0x000000ff01000100, 0x000000ff0101ff00, 0x000000ff010100ff,
  1282. 0x000000ff01010000, 0x000000ff01010101, 0x00000000ffffff00, 0x00000000ffffff01,
  1283. 0x00000000ffff00ff, 0x00000000ffff0000, 0x00000000ffff0001, 0x00000000ffff0100,
  1284. 0x00000000ff00ffff, 0x00000000ff00ff00, 0x00000000ff00ff01, 0x00000000ff0000ff,
  1285. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff000101,
  1286. 0x00000000ff01ff00, 0x00000000ff0100ff, 0x00000000ff010000, 0x00000000ff010001,
  1287. 0x00000000ff010100, 0x0000000000ffffff, 0x0000000000ffff00, 0x0000000000ffff01,
  1288. 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001, 0x0000000000ff01ff,
  1289. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1290. 0x00000000000000ff, 0x0000000000000000, 0x0000000000000001, 0x00000000000001ff,
  1291. 0x0000000000000100, 0x0000000000000101, 0x000000000001ffff, 0x000000000001ff00,
  1292. 0x00000000000100ff, 0x0000000000010000, 0x0000000000010001, 0x00000000000101ff,
  1293. 0x0000000000010100, 0x0000000000010101, 0x0000000001ffff00, 0x0000000001ff00ff,
  1294. 0x0000000001ff0000, 0x0000000001ff0100, 0x0000000001ff0101, 0x000000000100ffff,
  1295. 0x000000000100ff00, 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001,
  1296. 0x00000000010001ff, 0x0000000001000100, 0x000000000101ff00, 0x00000000010100ff,
  1297. 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x00000001ffffffff,
  1298. 0x00000001ffffff00, 0x00000001ffffff01, 0x00000001ffff00ff, 0x00000001ffff0001,
  1299. 0x00000001ffff01ff, 0x00000001ffff0100, 0x00000001ff00ff00, 0x00000001ff0000ff,
  1300. 0x00000001ff000000, 0x00000001ff0001ff, 0x00000001ff000100, 0x00000001ff01ffff,
  1301. 0x00000001ff01ff00, 0x00000001ff01ff01, 0x00000001ff0100ff, 0x00000001ff010000,
  1302. 0x00000001ff010001, 0x00000001ff0101ff, 0x00000001ff010100, 0x0000000100ffff00,
  1303. 0x0000000100ff0000, 0x0000000100ff0001, 0x0000000100ff01ff, 0x0000000100ff0100,
  1304. 0x0000000100ff0101, 0x000000010000ffff, 0x000000010000ff00, 0x000000010000ff01,
  1305. 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001, 0x00000001000001ff,
  1306. 0x0000000100000100, 0x0000000100000101, 0x000000010001ff00, 0x00000001000100ff,
  1307. 0x0000000100010000, 0x0000000100010100, 0x0000000101ffff01, 0x0000000101ff0000,
  1308. 0x0000000101ff0001, 0x0000000101ff01ff, 0x0000000101ff0100, 0x0000000101ff0101,
  1309. 0x000000010100ff00, 0x0000000101000000, 0x0000000101000101, 0x000000010101ff01,
  1310. 0x0000000101010000, 0x0000000101010001, 0x00000001010101ff, 0x0000000101010100,
  1311. 0x000001ffffff00ff, 0x000001ffffff0000, 0x000001ffffff0001, 0x000001ffffff0100,
  1312. 0x000001ffff00ffff, 0x000001ffff000000, 0x000001ffff0001ff, 0x000001ffff01ff00,
  1313. 0x000001ffff010101, 0x000001ff00ff0000, 0x000001ff00ff01ff, 0x000001ff00ff0101,
  1314. 0x000001ff0000ff00, 0x000001ff000000ff, 0x000001ff00000000, 0x000001ff00000001,
  1315. 0x000001ff000001ff, 0x000001ff00000100, 0x000001ff0001ffff, 0x000001ff0001ff01,
  1316. 0x000001ff000100ff, 0x000001ff00010000, 0x000001ff01ffff01, 0x000001ff01ff0100,
  1317. 0x000001ff0100ffff, 0x000001ff0100ff01, 0x000001ff01000000, 0x000001ff010001ff,
  1318. 0x000001ff0101ff00, 0x000001ff01010100, 0x00000100ffffff00, 0x00000100ffffff01,
  1319. 0x00000100ffff0000, 0x00000100ffff0101, 0x00000100ff00ff00, 0x00000100ff0000ff,
  1320. 0x00000100ff000000, 0x00000100ff000001, 0x00000100ff000100, 0x00000100ff010000,
  1321. 0x0000010000ffff00, 0x0000010000ff00ff, 0x0000010000ff0000, 0x0000010000ff0001,
  1322. 0x0000010000ff0100, 0x000001000000ffff, 0x000001000000ff00, 0x000001000000ff01,
  1323. 0x00000100000000ff, 0x0000010000000000, 0x0000010000000001, 0x00000100000001ff,
  1324. 0x0000010000000100, 0x0000010000000101, 0x000001000001ff00, 0x00000100000100ff,
  1325. 0x0000010000010000, 0x0000010000010001, 0x0000010000010100, 0x0000010001ffff00,
  1326. 0x0000010001ff0000, 0x0000010001ff0100, 0x000001000100ff00, 0x00000100010000ff,
  1327. 0x0000010001000000, 0x0000010001000001, 0x00000100010001ff, 0x0000010001000100,
  1328. 0x0000010001010000, 0x00000101ffff00ff, 0x00000101ffff01ff, 0x00000101ff000000,
  1329. 0x00000101ff000101, 0x00000101ff01ffff, 0x00000101ff010000, 0x00000101ff010001,
  1330. 0x00000101ff010100, 0x0000010100ff0000, 0x0000010100ff01ff, 0x0000010100ff0100,
  1331. 0x000001010000ff00, 0x0000010100000000, 0x0000010100000001, 0x00000101000001ff,
  1332. 0x0000010100000100, 0x000001010001ff01, 0x0000010100010000, 0x00000101000101ff,
  1333. 0x0000010100010101, 0x0000010101ffff00, 0x0000010101ff0101, 0x000001010100ff01,
  1334. 0x0000010101000000, 0x0000010101000001, 0x00000101010001ff, 0x0000010101000101,
  1335. 0x000001010101ff00, 0x0001ffffffff0000, 0x0001ffffff0000ff, 0x0001ffffff000001,
  1336. 0x0001ffffff000100, 0x0001ffffff010000, 0x0001ffff00ff00ff, 0x0001ffff0000ffff,
  1337. 0x0001ffff00000000, 0x0001ffff00000001, 0x0001ffff000001ff, 0x0001ffff00000101,
  1338. 0x0001ffff0001ff00, 0x0001ffff000100ff, 0x0001ffff00010001, 0x0001ffff00010100,
  1339. 0x0001ffff01ffff00, 0x0001ffff01000001, 0x0001ffff01010000, 0x0001ff00ffffff00,
  1340. 0x0001ff00ffff00ff, 0x0001ff00ffff0001, 0x0001ff00ffff0100, 0x0001ff00ff00ff01,
  1341. 0x0001ff00ff000000, 0x0001ff00ff01ff00, 0x0001ff00ff01ff01, 0x0001ff00ff010001,
  1342. 0x0001ff00ff010100, 0x0001ff0000ff0000, 0x0001ff0000ff0100, 0x0001ff000000ff00,
  1343. 0x0001ff0000000000, 0x0001ff0000000001, 0x0001ff0000000100, 0x0001ff0000010000,
  1344. 0x0001ff0000010001, 0x0001ff0000010101, 0x0001ff0001ff00ff, 0x0001ff0001ff0101,
  1345. 0x0001ff000100ff01, 0x0001ff0001000000, 0x0001ff000101ff00, 0x0001ff0001010001,
  1346. 0x0001ff0001010100, 0x0001ff01ff00ff00, 0x0001ff01ff000001, 0x0001ff01ff000100,
  1347. 0x0001ff0100ffffff, 0x0001ff0100ffff00, 0x0001ff0100ff0001, 0x0001ff0100000000,
  1348. 0x0001ff0100000001, 0x0001ff01000001ff, 0x0001ff010001ffff, 0x0001ff0101ff0000,
  1349. 0x0001ff010100ff00, 0x0001ff0101000001, 0x0001ff0101010000, 0x000100ffff00ff00,
  1350. 0x000100ffff00ff01, 0x000100ffff000000, 0x000100ffff000001, 0x000100ffff000101,
  1351. 0x000100ffff01ff00, 0x000100ffff010001, 0x000100ffff010100, 0x000100ff00ffffff,
  1352. 0x000100ff00ffff01, 0x000100ff00ff0000, 0x000100ff00ff01ff, 0x000100ff00ff0101,
  1353. 0x000100ff0000ff00, 0x000100ff000000ff, 0x000100ff00000000, 0x000100ff00000001,
  1354. 0x000100ff00000100, 0x000100ff00000101, 0x000100ff0001ffff, 0x000100ff0001ff01,
  1355. 0x000100ff00010000, 0x000100ff01ff00ff, 0x000100ff01ff0000, 0x000100ff01ff0100,
  1356. 0x000100ff0100ffff, 0x000100ff0100ff01, 0x000100ff010000ff, 0x000100ff01000000,
  1357. 0x000100ff01000001, 0x000100ff010001ff, 0x000100ff01000101, 0x000100ff0101ff00,
  1358. 0x000100ff010100ff, 0x000100ff01010100, 0x00010000ffff0000, 0x00010000ffff01ff,
  1359. 0x00010000ffff0101, 0x00010000ff00ff00, 0x00010000ff000000, 0x00010000ff000001,
  1360. 0x00010000ff000100, 0x0001000000ff00ff, 0x0001000000ff0000, 0x0001000000ff0001,
  1361. 0x0001000000ff0100, 0x000100000000ffff, 0x000100000000ff00, 0x00010000000000ff,
  1362. 0x0001000000000000, 0x0001000000000001, 0x0001000000000100, 0x000100000001ff00,
  1363. 0x00010000000100ff, 0x0001000000010000, 0x0001000000010001, 0x0001000000010100,
  1364. 0x0001000001ff0001, 0x0001000001ff0100, 0x0001000001ff0101, 0x000100000100ff00,
  1365. 0x0001000001000000, 0x0001000001000001, 0x0001000001000100, 0x0001000001000101,
  1366. 0x000100000101ff01, 0x0001000001010000, 0x0001000001010001, 0x00010000010101ff,
  1367. 0x00010001ffffff01, 0x00010001ffff0100, 0x00010001ff000000, 0x00010001ff01ffff,
  1368. 0x00010001ff010001, 0x00010001ff0101ff, 0x00010001ff010100, 0x0001000100ffffff,
  1369. 0x0001000100ff0000, 0x0001000100ff01ff, 0x0001000100ff0101, 0x000100010000ff00,
  1370. 0x00010001000000ff, 0x0001000100000000, 0x0001000100000001, 0x00010001000001ff,
  1371. 0x0001000100000101, 0x000100010001ffff, 0x0001000100010000, 0x00010001000101ff,
  1372. 0x0001000101ffffff, 0x0001000101ffff01, 0x0001000101ff0000, 0x0001000101ff0101,
  1373. 0x00010001010000ff, 0x0001000101000001, 0x00010001010001ff, 0x0001000101000100,
  1374. 0x000100010101ffff, 0x00010001010100ff, 0x0001000101010001, 0x0001000101010101,
  1375. 0x000101ffff000001, 0x000101ffff000100, 0x000101ffff010000, 0x000101ff00ffff00,
  1376. 0x000101ff0000ff01, 0x000101ff00000000, 0x000101ff00000101, 0x000101ff0001ff00,
  1377. 0x000101ff00010100, 0x000101ff01ff0000, 0x000101ff0100ff00, 0x000101ff010001ff,
  1378. 0x000101ff01010001, 0x00010100ffffff00, 0x00010100ffff00ff, 0x00010100ff00ffff,
  1379. 0x00010100ff000000, 0x00010100ff01ff00, 0x00010100ff0100ff, 0x00010100ff010001,
  1380. 0x00010100ff010100, 0x0001010000ffffff, 0x0001010000ffff00, 0x0001010000ff0000,
  1381. 0x0001010000ff0001, 0x0001010000ff01ff, 0x000101000000ff00, 0x00010100000000ff,
  1382. 0x0001010000000000, 0x0001010000000001, 0x0001010000000100, 0x000101000001ffff,
  1383. 0x0001010000010000, 0x0001010000010101, 0x0001010001ffff01, 0x0001010001ff00ff,
  1384. 0x0001010001ff0101, 0x0001010001000000, 0x000101000101ff00, 0x00010100010100ff,
  1385. 0x0001010001010000, 0x0001010001010100, 0x00010101ff00ff00, 0x00010101ff000001,
  1386. 0x00010101ff0001ff, 0x0001010100ffff00, 0x0001010100ff00ff, 0x0001010100ff0100,
  1387. 0x000101010000ffff, 0x0001010100000000, 0x00010101000001ff, 0x0001010100000101,
  1388. 0x00010101000100ff, 0x0001010100010000, 0x0001010100010100, 0x0001010101ff0001,
  1389. 0x00010101010000ff, 0x00010101010001ff, 0x0001010101000101, 0x0001010101010001,
  1390. 0x01ffffffffffffff, 0x01ffffffffffff01, 0x01ffffffffff01ff, 0x01ffffffffff0101,
  1391. 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff, 0x01ffffffff010101,
  1392. 0x01ffffff00ff0000, 0x01ffffff0000ffff, 0x01ffffff0000ff00, 0x01ffffff000000ff,
  1393. 0x01ffffff00000001, 0x01ffffff00000100, 0x01ffffff00010000, 0x01ffffff01ffffff,
  1394. 0x01ffffff01ffff01, 0x01ffffff01ff01ff, 0x01ffffff01ff0101, 0x01ffffff01000000,
  1395. 0x01ffffff0101ffff, 0x01ffffff0101ff01, 0x01ffffff010101ff, 0x01ffffff01010101,
  1396. 0x01ffff00ffff0000, 0x01ffff00ff00ff00, 0x01ffff00ff0000ff, 0x01ffff00ff000001,
  1397. 0x01ffff00ff000100, 0x01ffff00ff010000, 0x01ffff0000ffff00, 0x01ffff0000ff00ff,
  1398. 0x01ffff0000ff0100, 0x01ffff000000ffff, 0x01ffff000000ff01, 0x01ffff0000000000,
  1399. 0x01ffff0000000001, 0x01ffff00000001ff, 0x01ffff0000000100, 0x01ffff00000100ff,
  1400. 0x01ffff0000010001, 0x01ffff0000010100, 0x01ffff0001ff0000, 0x01ffff0001ff0100,
  1401. 0x01ffff00010000ff, 0x01ffff0001000001, 0x01ffff0001000100, 0x01ffff0001010000,
  1402. 0x01ffff01ffffffff, 0x01ffff01ffffff01, 0x01ffff01ffff01ff, 0x01ffff01ffff0101,
  1403. 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff01ff01, 0x01ffff01ff0101ff,
  1404. 0x01ffff01ff010101, 0x01ffff010000ff00, 0x01ffff01000000ff, 0x01ffff0100000100,
  1405. 0x01ffff0100010000, 0x01ffff0101ffffff, 0x01ffff0101ffff01, 0x01ffff0101ff01ff,
  1406. 0x01ffff0101ff0101, 0x01ffff0101000000, 0x01ffff010101ffff, 0x01ffff010101ff01,
  1407. 0x01ffff01010101ff, 0x01ffff0101010101, 0x01ff00ffff0000ff, 0x01ff00ffff000100,
  1408. 0x01ff00ff00ffff00, 0x01ff00ff00ff00ff, 0x01ff00ff0000ff00, 0x01ff00ff00000000,
  1409. 0x01ff00ff00000101, 0x01ff00ff0001ff00, 0x01ff00ff000100ff, 0x01ff00ff00010100,
  1410. 0x01ff00ff010000ff, 0x01ff00ff01000100, 0x01ff0000ffffff00, 0x01ff0000ffff0100,
  1411. 0x01ff0000ff00ff01, 0x01ff0000ff000000, 0x01ff0000ff000101, 0x01ff0000ff010001,
  1412. 0x01ff0000ff010100, 0x01ff000000ffffff, 0x01ff000000ffff00, 0x01ff000000ff0000,
  1413. 0x01ff000000ff01ff, 0x01ff00000000ff00, 0x01ff0000000000ff, 0x01ff000000000000,
  1414. 0x01ff000000000001, 0x01ff000000000100, 0x01ff000000000101, 0x01ff000000010000,
  1415. 0x01ff000000010001, 0x01ff0000000101ff, 0x01ff000000010101, 0x01ff000001ffff00,
  1416. 0x01ff000001ff00ff, 0x01ff000001ff0001, 0x01ff000001ff0100, 0x01ff00000100ffff,
  1417. 0x01ff00000100ff01, 0x01ff000001000000, 0x01ff0000010001ff, 0x01ff000001010001,
  1418. 0x01ff0001ff00ff00, 0x01ff0001ff000001, 0x01ff0001ff000100, 0x01ff0001ff010000,
  1419. 0x01ff000100ffff00, 0x01ff000100ff00ff, 0x01ff000100ff0100, 0x01ff000100ff0101,
  1420. 0x01ff00010000ffff, 0x01ff000100000000, 0x01ff000100000100, 0x01ff000100000101,
  1421. 0x01ff00010001ff00, 0x01ff000100010001, 0x01ff000100010101, 0x01ff000101ff0000,
  1422. 0x01ff00010100ff00, 0x01ff000101000101, 0x01ff0001010100ff, 0x01ff01ffffffffff,
  1423. 0x01ff01ffffffff01, 0x01ff01ffffff01ff, 0x01ff01ffffff0101, 0x01ff01ffff000000,
  1424. 0x01ff01ffff01ffff, 0x01ff01ffff01ff01, 0x01ff01ffff0101ff, 0x01ff01ffff010101,
  1425. 0x01ff01ff00ffff00, 0x01ff01ff00ff0000, 0x01ff01ff0000ff00, 0x01ff01ff000000ff,
  1426. 0x01ff01ff00000100, 0x01ff01ff00010000, 0x01ff01ff00010100, 0x01ff01ff01ffffff,
  1427. 0x01ff01ff01ffff01, 0x01ff01ff01ff01ff, 0x01ff01ff01ff0101, 0x01ff01ff01000000,
  1428. 0x01ff01ff0101ffff, 0x01ff01ff0101ff01, 0x01ff01ff010101ff, 0x01ff01ff01010101,
  1429. 0x01ff0100ffff0000, 0x01ff0100ffff0001, 0x01ff0100ff00ff00, 0x01ff0100ff0000ff,
  1430. 0x01ff0100ff000001, 0x01ff0100ff010000, 0x01ff010000ffff00, 0x01ff010000ff00ff,
  1431. 0x01ff010000ff0001, 0x01ff010000ff0100, 0x01ff01000000ffff, 0x01ff01000000ff01,
  1432. 0x01ff010000000000, 0x01ff010000000101, 0x01ff01000001ff00, 0x01ff0100000100ff,
  1433. 0x01ff010001ff0000, 0x01ff010001000001, 0x01ff010001000100, 0x01ff010001010000,
  1434. 0x01ff0101ffffffff, 0x01ff0101ffffff01, 0x01ff0101ffff01ff, 0x01ff0101ffff0101,
  1435. 0x01ff0101ff000000, 0x01ff0101ff01ffff, 0x01ff0101ff01ff01, 0x01ff0101ff0101ff,
  1436. 0x01ff0101ff010101, 0x01ff010100ff0000, 0x01ff01010000ff00, 0x01ff0101000000ff,
  1437. 0x01ff010100000001, 0x01ff010101ffffff, 0x01ff010101ffff01, 0x01ff010101ff01ff,
  1438. 0x01ff010101ff0101, 0x01ff010101000000, 0x01ff01010101ffff, 0x01ff01010101ff01,
  1439. 0x01ff0101010101ff, 0x01ff010101010101, 0x0100ffffffff0000, 0x0100ffffff00ff00,
  1440. 0x0100ffffff000001, 0x0100ffffff0001ff, 0x0100ffffff000100, 0x0100ffffff010000,
  1441. 0x0100ffff00ffff00, 0x0100ffff00ff0001, 0x0100ffff00ff0100, 0x0100ffff00000000,
  1442. 0x0100ffff000001ff, 0x0100ffff00000101, 0x0100ffff00010100, 0x0100ffff00010101,
  1443. 0x0100ffff01ff0000, 0x0100ffff0100ff00, 0x0100ffff010000ff, 0x0100ffff01000001,
  1444. 0x0100ffff01000100, 0x0100ffff01010000, 0x0100ff00ffffff00, 0x0100ff00ffff00ff,
  1445. 0x0100ff00ffff0001, 0x0100ff00ffff0100, 0x0100ff00ff00ffff, 0x0100ff00ff000000,
  1446. 0x0100ff00ff0001ff, 0x0100ff00ff000101, 0x0100ff00ff01ff00, 0x0100ff00ff0100ff,
  1447. 0x0100ff00ff010001, 0x0100ff00ff010100, 0x0100ff0000ffffff, 0x0100ff0000ff0000,
  1448. 0x0100ff000000ffff, 0x0100ff000000ff00, 0x0100ff00000000ff, 0x0100ff0000000000,
  1449. 0x0100ff0000000001, 0x0100ff0000000100, 0x0100ff000001ff01, 0x0100ff0000010000,
  1450. 0x0100ff0001ff00ff, 0x0100ff0001ff0001, 0x0100ff000100ff01, 0x0100ff0001000000,
  1451. 0x0100ff00010001ff, 0x0100ff000101ff00, 0x0100ff00010100ff, 0x0100ff0001010001,
  1452. 0x0100ff0001010100, 0x0100ff01ffff0000, 0x0100ff01ff00ff00, 0x0100ff01ff0000ff,
  1453. 0x0100ff01ff000100, 0x0100ff01ff010000, 0x0100ff0100ff00ff, 0x0100ff0100ff0001,
  1454. 0x0100ff0100ff0100, 0x0100ff010000ffff, 0x0100ff010000ff01, 0x0100ff0100000000,
  1455. 0x0100ff01000001ff, 0x0100ff0100010001, 0x0100ff0100010100, 0x0100ff0101ff0000,
  1456. 0x0100ff01010000ff, 0x0100ff0101000001, 0x0100ff0101010100, 0x010000ffffffff00,
  1457. 0x010000ffffff00ff, 0x010000ffffff0001, 0x010000ffff00ffff, 0x010000ffff000000,
  1458. 0x010000ffff0001ff, 0x010000ffff010001, 0x010000ff00ffffff, 0x010000ff00ff0101,
  1459. 0x010000ff0000ff00, 0x010000ff000000ff, 0x010000ff00000000, 0x010000ff00000001,
  1460. 0x010000ff000001ff, 0x010000ff00000100, 0x010000ff0001ffff, 0x010000ff0001ff00,
  1461. 0x010000ff0001ff01, 0x010000ff00010000, 0x010000ff01ff00ff, 0x010000ff01ff0001,
  1462. 0x010000ff0100ff01, 0x010000ff010000ff, 0x010000ff01000000, 0x010000ff010001ff,
  1463. 0x010000ff0101ff00, 0x010000ff01010100, 0x01000000ffffffff, 0x01000000ffff0000,
  1464. 0x01000000ffff01ff, 0x01000000ffff0101, 0x01000000ff00ffff, 0x01000000ff00ff00,
  1465. 0x01000000ff0000ff, 0x01000000ff000000, 0x01000000ff000001, 0x01000000ff000100,
  1466. 0x01000000ff01ff00, 0x01000000ff010000, 0x01000000ff010100, 0x01000000ff010101,
  1467. 0x0100000000ffff00, 0x0100000000ff00ff, 0x0100000000ff0000, 0x0100000000ff0001,
  1468. 0x0100000000ff0100, 0x010000000000ffff, 0x010000000000ff00, 0x010000000000ff01,
  1469. 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001, 0x01000000000001ff,
  1470. 0x0100000000000100, 0x0100000000000101, 0x010000000001ff00, 0x01000000000100ff,
  1471. 0x0100000000010000, 0x0100000000010001, 0x0100000000010100, 0x0100000001ffff00,
  1472. 0x0100000001ff0000, 0x0100000001ff01ff, 0x010000000100ff00, 0x010000000100ff01,
  1473. 0x01000000010000ff, 0x0100000001000000, 0x0100000001000001, 0x0100000001000100,
  1474. 0x0100000001000101, 0x010000000101ffff, 0x010000000101ff01, 0x0100000001010000,
  1475. 0x01000000010101ff, 0x0100000001010101, 0x01000001ffffff00, 0x01000001ffff00ff,
  1476. 0x01000001ff00ffff, 0x01000001ff000000, 0x01000001ff000100, 0x01000001ff01ffff,
  1477. 0x01000001ff010001, 0x01000001ff010100, 0x0100000100ff0000, 0x0100000100ff01ff,
  1478. 0x0100000100ff0100, 0x010000010000ff00, 0x010000010000ff01, 0x0100000100000000,
  1479. 0x0100000100000001, 0x0100000100000100, 0x0100000100010000, 0x01000001000101ff,
  1480. 0x0100000101ffff01, 0x0100000101ff00ff, 0x0100000101ff0100, 0x0100000101ff0101,
  1481. 0x010000010100ff01, 0x01000001010000ff, 0x0100000101000000, 0x01000001010100ff,
  1482. 0x0100000101010001, 0x0100000101010100, 0x010001ffffff0000, 0x010001ffff000001,
  1483. 0x010001ffff000100, 0x010001ffff010000, 0x010001ff00ffff00, 0x010001ff00ff0001,
  1484. 0x010001ff0000ffff, 0x010001ff0000ff01, 0x010001ff00000000, 0x010001ff00000001,
  1485. 0x010001ff00000101, 0x010001ff000100ff, 0x010001ff00010000, 0x010001ff01ff0000,
  1486. 0x010001ff0100ff00, 0x010001ff01000001, 0x010001ff01000100, 0x010001ff01010000,
  1487. 0x01000100ffff00ff, 0x01000100ffff0001, 0x01000100ffff0100, 0x01000100ff00ffff,
  1488. 0x01000100ff00ff01, 0x01000100ff000000, 0x01000100ff0001ff, 0x01000100ff000101,
  1489. 0x01000100ff01ffff, 0x01000100ff01ff00, 0x01000100ff0100ff, 0x01000100ff010001,
  1490. 0x0100010000ffffff, 0x0100010000ffff01, 0x0100010000ff0000, 0x0100010000ff01ff,
  1491. 0x0100010000ff0101, 0x010001000000ff00, 0x01000100000000ff, 0x0100010000000000,
  1492. 0x0100010000000001, 0x0100010000000100, 0x010001000001ff01, 0x0100010000010000,
  1493. 0x0100010000010001, 0x0100010000010101, 0x0100010001ffff00, 0x0100010001ff00ff,
  1494. 0x010001000100ffff, 0x010001000100ff01, 0x0100010001000000, 0x0100010001000101,
  1495. 0x010001000101ff00, 0x0100010001010001, 0x01000101ffff0000, 0x01000101ff000000,
  1496. 0x01000101ff010000, 0x0100010100ff00ff, 0x0100010100ff0001, 0x0100010100ff0100,
  1497. 0x010001010000ffff, 0x0100010100000000, 0x01000101000001ff, 0x010001010001ff00,
  1498. 0x0100010101ff0000, 0x010001010100ff00, 0x01000101010000ff, 0x0100010101000000,
  1499. 0x0100010101000001, 0x0101ffffffffffff, 0x0101ffffffffff01, 0x0101ffffffff01ff,
  1500. 0x0101ffffffff0101, 0x0101ffffff000000, 0x0101ffffff01ffff, 0x0101ffffff01ff01,
  1501. 0x0101ffffff0101ff, 0x0101ffffff010101, 0x0101ffff00ff0000, 0x0101ffff0000ff00,
  1502. 0x0101ffff000000ff, 0x0101ffff00000001, 0x0101ffff00000100, 0x0101ffff01ffffff,
  1503. 0x0101ffff01ffff01, 0x0101ffff01ff01ff, 0x0101ffff01ff0101, 0x0101ffff01000000,
  1504. 0x0101ffff0101ffff, 0x0101ffff0101ff01, 0x0101ffff010101ff, 0x0101ffff01010101,
  1505. 0x0101ff00ffff0000, 0x0101ff00ffff0100, 0x0101ff00ff00ff00, 0x0101ff00ff0000ff,
  1506. 0x0101ff00ff000001, 0x0101ff00ff000100, 0x0101ff00ff000101, 0x0101ff0000ff0001,
  1507. 0x0101ff0000ff0100, 0x0101ff000000ff00, 0x0101ff0000000000, 0x0101ff00000001ff,
  1508. 0x0101ff0000000101, 0x0101ff000001ff00, 0x0101ff00000100ff, 0x0101ff0001ff0000,
  1509. 0x0101ff000100ffff, 0x0101ff000100ff01, 0x0101ff0001000001, 0x0101ff0001000100,
  1510. 0x0101ff01ffffff01, 0x0101ff01ffff01ff, 0x0101ff01ffff0101, 0x0101ff01ff00ffff,
  1511. 0x0101ff01ff000100, 0x0101ff01ff01ff01, 0x0101ff01ff0101ff, 0x0101ff01ff010101,
  1512. 0x0101ff0100ff0000, 0x0101ff010000ff00, 0x0101ff0100000001, 0x0101ff0100000100,
  1513. 0x0101ff0100010000, 0x0101ff0101ffffff, 0x0101ff0101ffff01, 0x0101ff0101ff01ff,
  1514. 0x0101ff0101ff0101, 0x0101ff0101000000, 0x0101ff010101ffff, 0x0101ff010101ff01,
  1515. 0x0101ff01010101ff, 0x0101ff0101010101, 0x010100ffff000100, 0x010100ffff010000,
  1516. 0x010100ff00ffff00, 0x010100ff00ff00ff, 0x010100ff0000ffff, 0x010100ff000000ff,
  1517. 0x010100ff00000000, 0x010100ff000001ff, 0x010100ff00000101, 0x010100ff0001ff00,
  1518. 0x010100ff00010000, 0x010100ff00010001, 0x010100ff000101ff, 0x010100ff00010100,
  1519. 0x010100ff01ff0000, 0x01010000ffff0001, 0x01010000ffff0100, 0x01010000ff00ffff,
  1520. 0x01010000ff00ff01, 0x01010000ff000000, 0x01010000ff0001ff, 0x01010000ff010001,
  1521. 0x01010000ff010100, 0x0101000000ffff01, 0x0101000000ff0000, 0x010100000000ff00,
  1522. 0x01010000000000ff, 0x0101000000000000, 0x0101000000000001, 0x0101000000000100,
  1523. 0x0101000000010000, 0x0101000000010101, 0x0101000001ffff00, 0x0101000001ff00ff,
  1524. 0x0101000001ff0000, 0x0101000001ff0001, 0x0101000001ff0100, 0x010100000100ff01,
  1525. 0x0101000001000000, 0x01010000010001ff, 0x01010001ffff0000, 0x01010001ff00ff00,
  1526. 0x01010001ff000001, 0x01010001ff000101, 0x01010001ff01ff00, 0x01010001ff010000,
  1527. 0x0101000100ff00ff, 0x0101000100ff0001, 0x0101000100ff0101, 0x010100010000ff01,
  1528. 0x0101000100000000, 0x0101000100000001, 0x01010001000001ff, 0x010100010001ffff,
  1529. 0x010100010001ff01, 0x0101000101ff0001, 0x010100010100ffff, 0x0101000101000000,
  1530. 0x0101000101000001, 0x0101000101000100, 0x010100010101ff00, 0x01010001010100ff,
  1531. 0x0101000101010001, 0x010101ffffffffff, 0x010101ffffffff01, 0x010101ffffff01ff,
  1532. 0x010101ffffff0101, 0x010101ffff01ffff, 0x010101ffff01ff01, 0x010101ffff0101ff,
  1533. 0x010101ffff010101, 0x010101ff0000ff00, 0x010101ff000000ff, 0x010101ff00000001,
  1534. 0x010101ff00000100, 0x010101ff01ffffff, 0x010101ff01ffff01, 0x010101ff01ff01ff,
  1535. 0x010101ff01ff0101, 0x010101ff01000000, 0x010101ff0101ffff, 0x010101ff0101ff01,
  1536. 0x010101ff010101ff, 0x010101ff01010101, 0x01010100ffff0000, 0x01010100ff0000ff,
  1537. 0x01010100ff000100, 0x01010100ff01ff00, 0x01010100ff010000, 0x0101010000ffff00,
  1538. 0x010101000000ffff, 0x0101010000000000, 0x0101010000000101, 0x010101000001ff00,
  1539. 0x0101010000010001, 0x0101010000010100, 0x010101000100ffff, 0x0101010001000001,
  1540. 0x01010101ffffffff, 0x01010101ffffff01, 0x01010101ffff01ff, 0x01010101ffff0101,
  1541. 0x01010101ff01ffff, 0x01010101ff01ff01, 0x01010101ff0101ff, 0x01010101ff010101,
  1542. 0x010101010000ff00, 0x01010101000000ff, 0x0101010100000001, 0x0101010101ffffff,
  1543. 0x0101010101ffff01, 0x0101010101ff01ff, 0x0101010101ff0101, 0x0101010101000000,
  1544. 0x010101010101ffff, 0x010101010101ff01, 0x01010101010101ff, 0x0101010101010101,
  1545. GGML_TABLE_END()
  1546. #else
  1547. GGML_TABLE_BEGIN(uint32_t, iq1s_grid_gpu, NGRID_IQ1S)
  1548. 0x00000000, 0x00000002, 0x00000101, 0x00000200, 0x00000202, 0x00010001, 0x00010101, 0x00020000,
  1549. 0x00020002, 0x00020200, 0x00020202, 0x01000101, 0x01010001, 0x01010100, 0x01010102, 0x01020101,
  1550. 0x02000000, 0x02000002, 0x02000200, 0x02000202, 0x02010101, 0x02020000, 0x02020002, 0x02020200,
  1551. 0x02020202, 0x00000110, 0x00000111, 0x00010011, 0x00010110, 0x00010112, 0x00010211, 0x00010212,
  1552. 0x00020111, 0x01000011, 0x01000112, 0x01000211, 0x01010012, 0x01010111, 0x01010212, 0x01020011,
  1553. 0x01020110, 0x01020112, 0x01020210, 0x02000111, 0x02010011, 0x02010110, 0x02010112, 0x02020111,
  1554. 0x00000020, 0x00000022, 0x00000220, 0x00000222, 0x00010121, 0x00020020, 0x00020022, 0x00020220,
  1555. 0x00020222, 0x01000121, 0x01010021, 0x01010221, 0x01020120, 0x01020221, 0x02000020, 0x02000022,
  1556. 0x02000220, 0x02000222, 0x02010021, 0x02010121, 0x02010221, 0x02020020, 0x02020022, 0x02020220,
  1557. 0x02020222, 0x00011001, 0x00011100, 0x00011102, 0x00021101, 0x01001001, 0x01001201, 0x01011101,
  1558. 0x01011202, 0x01021100, 0x01021101, 0x02011001, 0x02011201, 0x02021101, 0x00001011, 0x00001110,
  1559. 0x00001111, 0x00001112, 0x00011111, 0x00011210, 0x00011212, 0x00021211, 0x01001010, 0x01001111,
  1560. 0x01001212, 0x01011010, 0x01011011, 0x01011110, 0x01011111, 0x01011112, 0x01011211, 0x01021010,
  1561. 0x01021012, 0x01021111, 0x01021210, 0x01021212, 0x02001011, 0x02011011, 0x02011111, 0x02011210,
  1562. 0x02011212, 0x02021011, 0x02021110, 0x02021111, 0x02021112, 0x02021211, 0x00011120, 0x00011221,
  1563. 0x01001021, 0x01001120, 0x01011020, 0x01011022, 0x01011121, 0x01011220, 0x01021020, 0x01021021,
  1564. 0x01021122, 0x01021221, 0x02001121, 0x02011021, 0x02011120, 0x02011221, 0x00002000, 0x00002002,
  1565. 0x00002200, 0x00002202, 0x00012101, 0x00022000, 0x00022002, 0x00022200, 0x00022202, 0x01002101,
  1566. 0x01012001, 0x01012102, 0x01022101, 0x02002000, 0x02002002, 0x02002200, 0x02002202, 0x02012101,
  1567. 0x02022000, 0x02022002, 0x02022200, 0x02022202, 0x00002111, 0x00012011, 0x00012110, 0x00012211,
  1568. 0x00022110, 0x00022111, 0x01002011, 0x01012010, 0x01012011, 0x01012111, 0x01022011, 0x01022110,
  1569. 0x01022211, 0x02012011, 0x02012110, 0x02012112, 0x02012211, 0x02022111, 0x00002020, 0x00002022,
  1570. 0x00002220, 0x00002222, 0x00012121, 0x00022020, 0x00022022, 0x00022220, 0x00022222, 0x01002121,
  1571. 0x01012021, 0x01012221, 0x01022021, 0x01022121, 0x02002020, 0x02002022, 0x02002121, 0x02002220,
  1572. 0x02002222, 0x02012121, 0x02022020, 0x02022022, 0x02022220, 0x02022222, 0x00110000, 0x00110001,
  1573. 0x00110100, 0x00110201, 0x00120100, 0x00120101, 0x01100001, 0x01100100, 0x01110000, 0x01110101,
  1574. 0x01110200, 0x01120001, 0x01120100, 0x01120101, 0x01120201, 0x02110001, 0x02110100, 0x02110102,
  1575. 0x02120001, 0x02120101, 0x00100011, 0x00100110, 0x00100112, 0x00100211, 0x00110010, 0x00110012,
  1576. 0x00110111, 0x00110210, 0x00120011, 0x00120110, 0x00120211, 0x01100111, 0x01100212, 0x01110010,
  1577. 0x01110011, 0x01110012, 0x01110110, 0x01110111, 0x01110112, 0x01110211, 0x01120010, 0x01120111,
  1578. 0x02100110, 0x02110012, 0x02110111, 0x02120011, 0x02120110, 0x00110021, 0x00110120, 0x00110122,
  1579. 0x00120121, 0x01100020, 0x01100122, 0x01100221, 0x01110022, 0x01110121, 0x01110220, 0x01110222,
  1580. 0x01120120, 0x01120122, 0x02100121, 0x02110021, 0x02110120, 0x02110122, 0x02120121, 0x00101001,
  1581. 0x00101102, 0x00101201, 0x00111100, 0x00111101, 0x00111200, 0x00111201, 0x00121001, 0x00121102,
  1582. 0x01101001, 0x01101101, 0x01101102, 0x01101200, 0x01101202, 0x01111001, 0x01111100, 0x01111101,
  1583. 0x01111102, 0x01111201, 0x01121002, 0x01121101, 0x01121200, 0x02101100, 0x02101201, 0x02111000,
  1584. 0x02111100, 0x02111101, 0x02111200, 0x02111201, 0x02111202, 0x02121001, 0x02121100, 0x02121101,
  1585. 0x02121201, 0x00101012, 0x00101111, 0x00101212, 0x00111011, 0x00111110, 0x00111111, 0x00111112,
  1586. 0x00111211, 0x00121010, 0x00121012, 0x00121111, 0x00121210, 0x00121212, 0x01101011, 0x01101110,
  1587. 0x01101111, 0x01101112, 0x01111011, 0x01111012, 0x01111110, 0x01111111, 0x01111112, 0x01111211,
  1588. 0x01111212, 0x01121011, 0x01121110, 0x01121111, 0x01121112, 0x01121211, 0x02101010, 0x02101012,
  1589. 0x02101110, 0x02101111, 0x02101210, 0x02101212, 0x02111010, 0x02111011, 0x02111110, 0x02111111,
  1590. 0x02111112, 0x02111211, 0x02111212, 0x02121010, 0x02121012, 0x02121111, 0x00101021, 0x00101120,
  1591. 0x00101121, 0x00101122, 0x00111121, 0x00111122, 0x00111220, 0x00111222, 0x00121021, 0x00121122,
  1592. 0x01101020, 0x01101022, 0x01101120, 0x01101121, 0x01101220, 0x01101222, 0x01111021, 0x01111121,
  1593. 0x01111122, 0x01111220, 0x01111221, 0x01121021, 0x01121120, 0x01121121, 0x01121220, 0x01121221,
  1594. 0x01121222, 0x02101122, 0x02101222, 0x02111022, 0x02111121, 0x02121120, 0x02121221, 0x00112001,
  1595. 0x00112102, 0x00122101, 0x01102001, 0x01102100, 0x01102102, 0x01102201, 0x01112000, 0x01112101,
  1596. 0x01112200, 0x01112202, 0x01122000, 0x01122001, 0x01122100, 0x01122102, 0x01122201, 0x02102101,
  1597. 0x02112001, 0x02112100, 0x02122101, 0x00112010, 0x00112012, 0x00112111, 0x00112212, 0x00122011,
  1598. 0x00122111, 0x01102012, 0x01102110, 0x01102111, 0x01102210, 0x01112011, 0x01112110, 0x01112111,
  1599. 0x01112112, 0x01112211, 0x01112212, 0x01122010, 0x01122111, 0x01122212, 0x02102211, 0x02112011,
  1600. 0x02112012, 0x02112111, 0x02112210, 0x02122011, 0x02122112, 0x02122211, 0x00102221, 0x00112122,
  1601. 0x00122120, 0x00122122, 0x01102120, 0x01102122, 0x01102221, 0x01112020, 0x01112022, 0x01112121,
  1602. 0x01112220, 0x01122021, 0x01122122, 0x01122221, 0x02102121, 0x02112021, 0x02112122, 0x02112222,
  1603. 0x00200000, 0x00200002, 0x00200200, 0x00200202, 0x00210101, 0x00220000, 0x00220002, 0x00220101,
  1604. 0x00220200, 0x00220202, 0x01200101, 0x01210001, 0x01210201, 0x01220001, 0x01220101, 0x02200000,
  1605. 0x02200002, 0x02200200, 0x02200202, 0x02210101, 0x02220000, 0x02220002, 0x02220101, 0x02220200,
  1606. 0x02220202, 0x00200111, 0x00210011, 0x00210110, 0x00210211, 0x00220111, 0x01200012, 0x01200110,
  1607. 0x01200211, 0x01210111, 0x01210210, 0x01210212, 0x01220011, 0x01220110, 0x01220111, 0x01220112,
  1608. 0x02200111, 0x02210010, 0x02210112, 0x02210211, 0x02220111, 0x00200021, 0x00200220, 0x00200222,
  1609. 0x00210021, 0x00210121, 0x00220020, 0x00220022, 0x00220220, 0x00220222, 0x01200121, 0x01210021,
  1610. 0x01210122, 0x01210221, 0x01220121, 0x02200021, 0x02200220, 0x02200222, 0x02210021, 0x02210121,
  1611. 0x02220020, 0x02220022, 0x02220220, 0x02220222, 0x00201101, 0x00211100, 0x00211102, 0x00211201,
  1612. 0x00221101, 0x01201100, 0x01201101, 0x01201102, 0x01201201, 0x01211002, 0x01211101, 0x01211200,
  1613. 0x01211202, 0x01221102, 0x02201101, 0x02211001, 0x02211100, 0x02211201, 0x02221001, 0x02221101,
  1614. 0x00201211, 0x00211111, 0x00221011, 0x00221211, 0x01201010, 0x01201111, 0x01201210, 0x01211011,
  1615. 0x01211110, 0x01211111, 0x01211211, 0x01221012, 0x01221111, 0x01221210, 0x02201211, 0x02211010,
  1616. 0x02211110, 0x02211111, 0x02211210, 0x02211212, 0x02221011, 0x02221110, 0x02221112, 0x02221211,
  1617. 0x00201121, 0x00211020, 0x00211022, 0x00211221, 0x00221121, 0x01201021, 0x01201221, 0x01211121,
  1618. 0x01221020, 0x01221021, 0x01221221, 0x02201120, 0x02201122, 0x02211020, 0x02211222, 0x00202000,
  1619. 0x00202002, 0x00202200, 0x00202202, 0x00212101, 0x00222000, 0x00222002, 0x00222200, 0x00222202,
  1620. 0x01202101, 0x01212001, 0x01212100, 0x01222101, 0x02202000, 0x02202002, 0x02202200, 0x02202202,
  1621. 0x02222000, 0x02222002, 0x02222200, 0x02222202, 0x00202211, 0x00212011, 0x00212110, 0x00212211,
  1622. 0x00222111, 0x01202112, 0x01202211, 0x01212012, 0x01212111, 0x01222011, 0x01222110, 0x01222112,
  1623. 0x01222211, 0x02202111, 0x02212010, 0x02212112, 0x02212211, 0x02222110, 0x02222111, 0x00202020,
  1624. 0x00202022, 0x00202220, 0x00202222, 0x00222020, 0x00222022, 0x00222220, 0x00222222, 0x01202121,
  1625. 0x01212021, 0x01212122, 0x01212221, 0x01222121, 0x02202020, 0x02202022, 0x02202220, 0x02202222,
  1626. 0x02212121, 0x02222020, 0x02222022, 0x02222220, 0x02222222, 0x10000101, 0x10010001, 0x10010102,
  1627. 0x10020101, 0x11000201, 0x11010002, 0x11010101, 0x11010200, 0x11010202, 0x11020001, 0x11020100,
  1628. 0x11020102, 0x12010100, 0x12010201, 0x12020001, 0x12020102, 0x10000010, 0x10000011, 0x10000110,
  1629. 0x10000112, 0x10000211, 0x10010012, 0x10010111, 0x10010112, 0x10010210, 0x10010212, 0x10020011,
  1630. 0x10020112, 0x10020211, 0x11000111, 0x11000210, 0x11000212, 0x11010011, 0x11010110, 0x11010111,
  1631. 0x11010112, 0x11010211, 0x11010212, 0x11020111, 0x11020210, 0x11020212, 0x12000011, 0x12000110,
  1632. 0x12000112, 0x12010010, 0x12010012, 0x12010111, 0x12020010, 0x12020011, 0x12020012, 0x10000121,
  1633. 0x10010021, 0x10010120, 0x10010122, 0x10020121, 0x11000021, 0x11010022, 0x11010121, 0x11010222,
  1634. 0x11020120, 0x11020221, 0x12000221, 0x12010120, 0x12020121, 0x10001001, 0x10011101, 0x10011201,
  1635. 0x10021201, 0x11001101, 0x11001200, 0x11001202, 0x11011001, 0x11011100, 0x11011101, 0x11011102,
  1636. 0x11021001, 0x11021002, 0x11021101, 0x11021200, 0x11021202, 0x12001001, 0x12001102, 0x12001201,
  1637. 0x12011000, 0x12011002, 0x12011101, 0x12021000, 0x12021001, 0x12021201, 0x10001011, 0x10001012,
  1638. 0x10001111, 0x10001212, 0x10011011, 0x10011110, 0x10011111, 0x10011112, 0x10011211, 0x10021010,
  1639. 0x10021111, 0x10021212, 0x11001011, 0x11001110, 0x11001111, 0x11001112, 0x11001211, 0x11011010,
  1640. 0x11011011, 0x11011110, 0x11011111, 0x11011112, 0x11011210, 0x11011211, 0x11021011, 0x11021110,
  1641. 0x11021111, 0x11021112, 0x11021211, 0x12001012, 0x12001110, 0x12001111, 0x12001210, 0x12011011,
  1642. 0x12011110, 0x12011111, 0x12011112, 0x12011211, 0x12011212, 0x12021111, 0x12021210, 0x12021212,
  1643. 0x10001021, 0x10001121, 0x10001221, 0x10011120, 0x10011121, 0x10011220, 0x10011222, 0x10021021,
  1644. 0x10021120, 0x10021221, 0x11001020, 0x11001022, 0x11001121, 0x11001220, 0x11011020, 0x11011021,
  1645. 0x11011022, 0x11011121, 0x11011122, 0x11011221, 0x11021022, 0x11021121, 0x11021220, 0x12001021,
  1646. 0x12001121, 0x12001222, 0x12011120, 0x12011121, 0x12021021, 0x12021120, 0x12021122, 0x10002101,
  1647. 0x10012001, 0x10012101, 0x10012202, 0x10022101, 0x11002002, 0x11002201, 0x11012000, 0x11012101,
  1648. 0x11012200, 0x11022001, 0x11022100, 0x11022102, 0x11022201, 0x12002101, 0x12012001, 0x12012100,
  1649. 0x12012102, 0x12012201, 0x12022101, 0x10002011, 0x10002111, 0x10002112, 0x10002212, 0x10012010,
  1650. 0x10012110, 0x10012111, 0x10012210, 0x10022011, 0x10022110, 0x10022112, 0x11002010, 0x11002111,
  1651. 0x11002212, 0x11012011, 0x11012012, 0x11012110, 0x11012111, 0x11012112, 0x11012211, 0x11022010,
  1652. 0x11022012, 0x11022111, 0x11022112, 0x11022212, 0x12002112, 0x12002211, 0x12012012, 0x12012111,
  1653. 0x12012112, 0x12012210, 0x12022011, 0x12022110, 0x12022112, 0x12022211, 0x10012122, 0x11002120,
  1654. 0x11002122, 0x11002221, 0x11012121, 0x11012220, 0x11012222, 0x11022120, 0x11022221, 0x12012120,
  1655. 0x12022121, 0x10100001, 0x10100100, 0x10100101, 0x10100102, 0x10100201, 0x10110002, 0x10110101,
  1656. 0x10110202, 0x10120001, 0x10120100, 0x10120201, 0x11100000, 0x11100101, 0x11100200, 0x11110001,
  1657. 0x11110100, 0x11110101, 0x11110102, 0x11110201, 0x11120101, 0x11120200, 0x12100102, 0x12100201,
  1658. 0x12110101, 0x12110200, 0x12120000, 0x12120001, 0x12120102, 0x12120201, 0x10100111, 0x10100210,
  1659. 0x10100211, 0x10100212, 0x10110011, 0x10110110, 0x10110111, 0x10110112, 0x10110210, 0x10110211,
  1660. 0x10120010, 0x10120111, 0x10120112, 0x10120210, 0x10120212, 0x11100011, 0x11100110, 0x11100111,
  1661. 0x11100112, 0x11100211, 0x11110010, 0x11110011, 0x11110012, 0x11110110, 0x11110111, 0x11110112,
  1662. 0x11110210, 0x11110211, 0x11110212, 0x11120011, 0x11120110, 0x11120111, 0x11120112, 0x11120211,
  1663. 0x12100012, 0x12100111, 0x12110011, 0x12110110, 0x12110111, 0x12110112, 0x12110211, 0x12120010,
  1664. 0x12120111, 0x12120212, 0x10100021, 0x10100122, 0x10110022, 0x10110121, 0x10110222, 0x10120021,
  1665. 0x10120120, 0x11100022, 0x11100121, 0x11100222, 0x11110021, 0x11110120, 0x11110121, 0x11110122,
  1666. 0x11110221, 0x11120022, 0x11120121, 0x12100121, 0x12110020, 0x12110022, 0x12110121, 0x12110221,
  1667. 0x12110222, 0x12120120, 0x10101100, 0x10101101, 0x10111001, 0x10111100, 0x10111101, 0x10111102,
  1668. 0x10111200, 0x10111201, 0x10121001, 0x10121101, 0x10121200, 0x10121202, 0x11101001, 0x11101100,
  1669. 0x11101101, 0x11101102, 0x11101201, 0x11101202, 0x11111000, 0x11111001, 0x11111100, 0x11111101,
  1670. 0x11111102, 0x11111200, 0x11111201, 0x11111202, 0x11121001, 0x11121002, 0x11121100, 0x11121101,
  1671. 0x11121102, 0x11121201, 0x12101000, 0x12101200, 0x12101202, 0x12111001, 0x12111100, 0x12111101,
  1672. 0x12111102, 0x12111201, 0x12121001, 0x12121100, 0x12121101, 0x12121202, 0x10101011, 0x10101012,
  1673. 0x10101110, 0x10101111, 0x10101112, 0x10101211, 0x10111010, 0x10111011, 0x10111012, 0x10111110,
  1674. 0x10111111, 0x10111112, 0x10111211, 0x10111212, 0x10121011, 0x10121110, 0x10121111, 0x10121112,
  1675. 0x10121211, 0x11101010, 0x11101011, 0x11101012, 0x11101110, 0x11101111, 0x11101112, 0x11101210,
  1676. 0x11101211, 0x11111010, 0x11111011, 0x11111012, 0x11111110, 0x11111111, 0x11111112, 0x11111210,
  1677. 0x11111211, 0x11111212, 0x11121010, 0x11121011, 0x11121110, 0x11121111, 0x11121112, 0x11121210,
  1678. 0x11121211, 0x11121212, 0x12101011, 0x12101110, 0x12101111, 0x12101211, 0x12101212, 0x12111010,
  1679. 0x12111011, 0x12111110, 0x12111111, 0x12111112, 0x12111210, 0x12111211, 0x12121011, 0x12121110,
  1680. 0x12121111, 0x12121112, 0x12121211, 0x10101020, 0x10101021, 0x10101022, 0x10101120, 0x10101122,
  1681. 0x10101220, 0x10101221, 0x10111021, 0x10111120, 0x10111121, 0x10111220, 0x10111221, 0x10121020,
  1682. 0x10121021, 0x10121022, 0x10121120, 0x10121121, 0x10121122, 0x10121220, 0x10121221, 0x11101021,
  1683. 0x11101121, 0x11101122, 0x11101220, 0x11101221, 0x11101222, 0x11111020, 0x11111021, 0x11111022,
  1684. 0x11111120, 0x11111121, 0x11111122, 0x11111220, 0x11111221, 0x11111222, 0x11121021, 0x11121120,
  1685. 0x11121121, 0x11121221, 0x12101022, 0x12101121, 0x12101122, 0x12101220, 0x12101221, 0x12101222,
  1686. 0x12111021, 0x12111121, 0x12111222, 0x12121022, 0x12121121, 0x12121122, 0x12121220, 0x12121221,
  1687. 0x10102100, 0x10102101, 0x10102102, 0x10102201, 0x10112000, 0x10112101, 0x10112200, 0x10122001,
  1688. 0x10122202, 0x11102101, 0x11102200, 0x11102202, 0x11112001, 0x11112100, 0x11112101, 0x11112102,
  1689. 0x11112200, 0x11112201, 0x11122000, 0x11122002, 0x11122100, 0x11122101, 0x12102002, 0x12102201,
  1690. 0x12112000, 0x12112002, 0x12112101, 0x12112200, 0x12122001, 0x12122201, 0x10102011, 0x10102012,
  1691. 0x10102111, 0x10102212, 0x10112011, 0x10112110, 0x10112111, 0x10112112, 0x10112211, 0x10122111,
  1692. 0x11102011, 0x11102110, 0x11102111, 0x11102112, 0x11102211, 0x11112010, 0x11112011, 0x11112012,
  1693. 0x11112110, 0x11112111, 0x11112112, 0x11112210, 0x11112211, 0x11112212, 0x11122011, 0x11122110,
  1694. 0x11122111, 0x11122112, 0x11122211, 0x12102011, 0x12102111, 0x12102211, 0x12112011, 0x12112110,
  1695. 0x12112111, 0x12112112, 0x12112210, 0x12112211, 0x12122111, 0x10102120, 0x10102220, 0x10112121,
  1696. 0x10112222, 0x10122020, 0x10122121, 0x10122122, 0x10122221, 0x11102121, 0x11102220, 0x11102221,
  1697. 0x11112021, 0x11112121, 0x11112122, 0x11112220, 0x11112221, 0x11122022, 0x11122121, 0x11122220,
  1698. 0x11122222, 0x12102021, 0x12102222, 0x12112022, 0x12112121, 0x12112122, 0x12112220, 0x12112222,
  1699. 0x12122021, 0x10200101, 0x10210100, 0x10210102, 0x10210201, 0x10220101, 0x11200100, 0x11210000,
  1700. 0x11210101, 0x11210102, 0x11210200, 0x11210202, 0x11220001, 0x11220100, 0x11220102, 0x11220201,
  1701. 0x12200001, 0x12210102, 0x12220101, 0x10200011, 0x10200110, 0x10200112, 0x10200211, 0x10210012,
  1702. 0x10210111, 0x10220011, 0x10220012, 0x10220112, 0x10220211, 0x11200111, 0x11200211, 0x11210011,
  1703. 0x11210111, 0x11210112, 0x11210211, 0x11220111, 0x11220112, 0x11220212, 0x12200110, 0x12200212,
  1704. 0x12210012, 0x12210111, 0x12220011, 0x12220112, 0x12220211, 0x10210021, 0x10210122, 0x10210221,
  1705. 0x11200020, 0x11200021, 0x11200122, 0x11210121, 0x11210122, 0x11210220, 0x11220020, 0x12200121,
  1706. 0x12210021, 0x12210122, 0x12220121, 0x10211001, 0x10211002, 0x10211101, 0x10211102, 0x10211202,
  1707. 0x10221001, 0x10221102, 0x10221201, 0x11201000, 0x11201002, 0x11201101, 0x11201200, 0x11201202,
  1708. 0x11211001, 0x11211100, 0x11211101, 0x11211102, 0x11211201, 0x11211202, 0x11221000, 0x11221002,
  1709. 0x11221101, 0x12201100, 0x12201101, 0x12201201, 0x12211000, 0x12211002, 0x12211100, 0x12211101,
  1710. 0x12211102, 0x12211200, 0x12211202, 0x12221001, 0x12221100, 0x12221201, 0x10201111, 0x10201210,
  1711. 0x10201212, 0x10211011, 0x10211111, 0x10211112, 0x10211211, 0x11201110, 0x11201111, 0x11201112,
  1712. 0x11201211, 0x11211010, 0x11211011, 0x11211110, 0x11211111, 0x11211112, 0x11211211, 0x11221011,
  1713. 0x11221110, 0x11221111, 0x11221112, 0x11221211, 0x12201112, 0x12201211, 0x12201212, 0x12211011,
  1714. 0x12211111, 0x12211112, 0x12211211, 0x12211212, 0x12221012, 0x12221111, 0x12221112, 0x12221210,
  1715. 0x10201022, 0x10201221, 0x10211121, 0x10221020, 0x10221122, 0x10221220, 0x10221221, 0x11201020,
  1716. 0x11201121, 0x11201220, 0x11201222, 0x11211021, 0x11211120, 0x11211121, 0x11211122, 0x11211220,
  1717. 0x11211222, 0x11221020, 0x11221121, 0x11221220, 0x12201020, 0x12201022, 0x12201121, 0x12201222,
  1718. 0x12211120, 0x12211122, 0x12211220, 0x12211221, 0x12221020, 0x12221120, 0x12221122, 0x12221222,
  1719. 0x10212102, 0x10212201, 0x10222101, 0x11202001, 0x11212002, 0x11212101, 0x11212202, 0x11222001,
  1720. 0x11222201, 0x12202101, 0x12212001, 0x12212200, 0x12222102, 0x10202011, 0x10202110, 0x10212010,
  1721. 0x10212111, 0x10222011, 0x10222110, 0x10222112, 0x10222211, 0x11202010, 0x11202011, 0x11202111,
  1722. 0x11202112, 0x11202210, 0x11212011, 0x11212110, 0x11212111, 0x11212112, 0x11212211, 0x11222010,
  1723. 0x11222111, 0x11222212, 0x12202012, 0x12202110, 0x12202212, 0x12212111, 0x12222011, 0x12222110,
  1724. 0x12222111, 0x12222211, 0x10212021, 0x10212122, 0x10212220, 0x11202021, 0x11202120, 0x11202221,
  1725. 0x11212020, 0x11212121, 0x11212220, 0x11212222, 0x11222120, 0x11222121, 0x11222221, 0x12202122,
  1726. 0x12212120, 0x12212220, 0x12212222, 0x12222122, 0x20000000, 0x20000002, 0x20000200, 0x20000202,
  1727. 0x20020000, 0x20020002, 0x20020200, 0x20020202, 0x21000101, 0x21010000, 0x21010001, 0x21010100,
  1728. 0x21010102, 0x21010201, 0x21020101, 0x22000000, 0x22000002, 0x22000200, 0x22000202, 0x22010101,
  1729. 0x22020000, 0x22020002, 0x22020200, 0x22020202, 0x20000111, 0x20010011, 0x20010110, 0x20010112,
  1730. 0x20010211, 0x20020111, 0x21000011, 0x21000110, 0x21000211, 0x21010010, 0x21010012, 0x21010111,
  1731. 0x21010112, 0x21010210, 0x21010211, 0x21020110, 0x21020112, 0x21020211, 0x22000111, 0x22000211,
  1732. 0x22010110, 0x22010112, 0x22010211, 0x22020111, 0x20000020, 0x20000022, 0x20000220, 0x20000222,
  1733. 0x20010121, 0x20020020, 0x20020022, 0x20020220, 0x20020222, 0x21010021, 0x21010120, 0x21010221,
  1734. 0x21020121, 0x22000020, 0x22000022, 0x22000220, 0x22000222, 0x22010121, 0x22020020, 0x22020022,
  1735. 0x22020220, 0x22020222, 0x20011100, 0x20011201, 0x21001001, 0x21001100, 0x21011001, 0x21011101,
  1736. 0x21011202, 0x21021001, 0x21021100, 0x21021201, 0x22011100, 0x22011201, 0x20001011, 0x20001211,
  1737. 0x20011012, 0x20011111, 0x20011212, 0x20021112, 0x20021211, 0x21001010, 0x21001011, 0x21001111,
  1738. 0x21001210, 0x21011011, 0x21011110, 0x21011111, 0x21011112, 0x21011211, 0x21011212, 0x21021111,
  1739. 0x21021112, 0x21021210, 0x21021212, 0x22001011, 0x22001110, 0x22001112, 0x22001211, 0x22011010,
  1740. 0x22011012, 0x22011111, 0x22011210, 0x22021112, 0x20011021, 0x20011122, 0x20011221, 0x20021121,
  1741. 0x21001021, 0x21001120, 0x21001221, 0x21001222, 0x21011020, 0x21011121, 0x21011221, 0x21011222,
  1742. 0x21021021, 0x21021122, 0x21021222, 0x22001121, 0x22011021, 0x22011222, 0x22021120, 0x20002000,
  1743. 0x20002002, 0x20002200, 0x20002202, 0x20012101, 0x20022000, 0x20022002, 0x20022200, 0x20022202,
  1744. 0x21002001, 0x21002101, 0x21012001, 0x21012100, 0x21012201, 0x21022101, 0x21022201, 0x22002000,
  1745. 0x22002002, 0x22002200, 0x22002202, 0x22012101, 0x22022000, 0x22022002, 0x22022200, 0x22022202,
  1746. 0x20002111, 0x20002112, 0x20012011, 0x20012110, 0x20012112, 0x20022111, 0x21002011, 0x21002110,
  1747. 0x21002112, 0x21002211, 0x21012010, 0x21012012, 0x21012111, 0x21012212, 0x21022011, 0x21022110,
  1748. 0x22002111, 0x22012112, 0x22012211, 0x22022111, 0x20002020, 0x20002022, 0x20002220, 0x20002222,
  1749. 0x20012121, 0x20022020, 0x20022022, 0x20022220, 0x20022222, 0x21002121, 0x21012021, 0x21012120,
  1750. 0x21012122, 0x22002020, 0x22002022, 0x22002220, 0x22002222, 0x22012121, 0x22022020, 0x22022022,
  1751. 0x22022220, 0x22022222, 0x20100101, 0x20110001, 0x20110102, 0x20110200, 0x20110201, 0x20120101,
  1752. 0x21100001, 0x21100102, 0x21100201, 0x21110101, 0x21110200, 0x21110202, 0x21120201, 0x21120202,
  1753. 0x22100101, 0x22110001, 0x22110100, 0x22110102, 0x22110201, 0x22120101, 0x20100011, 0x20100110,
  1754. 0x20100112, 0x20100211, 0x20110010, 0x20110111, 0x20110210, 0x20110212, 0x20120011, 0x20120110,
  1755. 0x20120112, 0x20120211, 0x21100010, 0x21100111, 0x21110010, 0x21110011, 0x21110110, 0x21110111,
  1756. 0x21110112, 0x21110211, 0x21120012, 0x21120111, 0x22100110, 0x22100112, 0x22110012, 0x22110111,
  1757. 0x22110210, 0x22120011, 0x22120110, 0x22120112, 0x22120211, 0x20100121, 0x20110021, 0x20110120,
  1758. 0x20110221, 0x20120121, 0x21100120, 0x21100122, 0x21100221, 0x21110020, 0x21110022, 0x21110121,
  1759. 0x21110220, 0x21120122, 0x21120221, 0x22100121, 0x22110120, 0x22110122, 0x22120221, 0x20101001,
  1760. 0x20101100, 0x20101102, 0x20111000, 0x20111101, 0x20111200, 0x20121102, 0x21101000, 0x21101202,
  1761. 0x21111001, 0x21111100, 0x21111101, 0x21111102, 0x21111200, 0x21111201, 0x21121000, 0x21121001,
  1762. 0x21121002, 0x21121101, 0x22101100, 0x22101102, 0x22111002, 0x22111100, 0x22111101, 0x22111200,
  1763. 0x22121001, 0x22121201, 0x20101010, 0x20101111, 0x20101210, 0x20101212, 0x20111010, 0x20111011,
  1764. 0x20111110, 0x20111111, 0x20111112, 0x20111211, 0x20121011, 0x20121111, 0x20121211, 0x20121212,
  1765. 0x21101011, 0x21101110, 0x21101111, 0x21101112, 0x21101211, 0x21111010, 0x21111011, 0x21111012,
  1766. 0x21111110, 0x21111111, 0x21111112, 0x21111210, 0x21111211, 0x21111212, 0x21121011, 0x21121110,
  1767. 0x21121111, 0x21121112, 0x21121211, 0x22101011, 0x22101111, 0x22101210, 0x22111011, 0x22111012,
  1768. 0x22111110, 0x22111111, 0x22111112, 0x22111211, 0x22111212, 0x22121010, 0x22121012, 0x22121111,
  1769. 0x22121210, 0x22121212, 0x20101021, 0x20101120, 0x20111020, 0x20111121, 0x20111221, 0x20121020,
  1770. 0x20121122, 0x20121221, 0x21101121, 0x21101220, 0x21101221, 0x21111021, 0x21111022, 0x21111121,
  1771. 0x21111122, 0x21111221, 0x21121121, 0x21121220, 0x22101022, 0x22101120, 0x22101221, 0x22101222,
  1772. 0x22111022, 0x22111120, 0x22111121, 0x22121120, 0x22121122, 0x22121221, 0x20102101, 0x20112102,
  1773. 0x20112201, 0x20122101, 0x21102001, 0x21102102, 0x21112000, 0x21112002, 0x21112101, 0x21112102,
  1774. 0x21112202, 0x21122100, 0x21122101, 0x22102101, 0x22112001, 0x22112102, 0x22112201, 0x22122101,
  1775. 0x20102110, 0x20102112, 0x20102211, 0x20112010, 0x20112012, 0x20112111, 0x20112210, 0x20112212,
  1776. 0x20122010, 0x20122011, 0x20122110, 0x20122112, 0x21102010, 0x21102012, 0x21102111, 0x21102210,
  1777. 0x21102212, 0x21112011, 0x21112110, 0x21112111, 0x21112112, 0x21112211, 0x21122012, 0x21122111,
  1778. 0x21122112, 0x21122212, 0x22102011, 0x22102110, 0x22112010, 0x22112012, 0x22112111, 0x22112212,
  1779. 0x22122011, 0x22122112, 0x20102121, 0x20112121, 0x20122121, 0x21102120, 0x21102122, 0x21102221,
  1780. 0x21112020, 0x21112121, 0x21112220, 0x21122021, 0x22102121, 0x22112021, 0x22112120, 0x22112121,
  1781. 0x22112122, 0x20200000, 0x20200002, 0x20200200, 0x20200202, 0x20210101, 0x20220000, 0x20220002,
  1782. 0x20220200, 0x20220202, 0x21200101, 0x21210001, 0x21210100, 0x21210102, 0x21210201, 0x22200000,
  1783. 0x22200002, 0x22200200, 0x22200202, 0x22210101, 0x22220000, 0x22220002, 0x22220200, 0x22220202,
  1784. 0x20200111, 0x20200211, 0x20210011, 0x20210110, 0x20210112, 0x20210211, 0x20210212, 0x21200112,
  1785. 0x21200211, 0x21210011, 0x21210111, 0x21210210, 0x21210212, 0x21220011, 0x21220110, 0x22200111,
  1786. 0x22210010, 0x22210012, 0x22210112, 0x22210211, 0x20200022, 0x20200220, 0x20200222, 0x20210020,
  1787. 0x20210221, 0x20220022, 0x20220220, 0x20220222, 0x21200121, 0x21210021, 0x21210122, 0x21210221,
  1788. 0x21220121, 0x22200020, 0x22200022, 0x22200220, 0x22200222, 0x22210121, 0x22220020, 0x22220022,
  1789. 0x22220220, 0x22220222, 0x20211201, 0x20221101, 0x21201001, 0x21201100, 0x21211000, 0x21211100,
  1790. 0x21211101, 0x21211200, 0x21211202, 0x21221001, 0x21221101, 0x21221102, 0x21221200, 0x21221201,
  1791. 0x22201101, 0x20201112, 0x20201211, 0x20211010, 0x20211012, 0x20211111, 0x20211210, 0x20221112,
  1792. 0x20221211, 0x21201012, 0x21201111, 0x21211011, 0x21211110, 0x21211111, 0x21211112, 0x21211211,
  1793. 0x21221111, 0x21221212, 0x22201011, 0x22201110, 0x22201111, 0x22201112, 0x22201211, 0x22211012,
  1794. 0x22211111, 0x22211210, 0x20201121, 0x20211021, 0x20211122, 0x20211222, 0x20221021, 0x20221121,
  1795. 0x21201120, 0x21201122, 0x21201222, 0x21211022, 0x21211121, 0x21211122, 0x21211220, 0x21221020,
  1796. 0x21221022, 0x22201122, 0x22211020, 0x22211121, 0x22211122, 0x22211221, 0x22221021, 0x22221120,
  1797. 0x22221122, 0x20202000, 0x20202002, 0x20202200, 0x20202202, 0x20222000, 0x20222002, 0x20222200,
  1798. 0x20222202, 0x21212001, 0x21212100, 0x21212102, 0x21212201, 0x22202000, 0x22202002, 0x22202200,
  1799. 0x22202202, 0x22212101, 0x22222000, 0x22222002, 0x22222200, 0x22222202, 0x20202111, 0x20212110,
  1800. 0x20212211, 0x20222011, 0x20222111, 0x21202011, 0x21212010, 0x21212111, 0x21212212, 0x21222011,
  1801. 0x21222112, 0x21222211, 0x22212010, 0x22212112, 0x20202020, 0x20202022, 0x20202220, 0x20202222,
  1802. 0x20222020, 0x20222022, 0x20222220, 0x20222222, 0x21212021, 0x21212120, 0x21212122, 0x22202020,
  1803. 0x22202022, 0x22202220, 0x22202222, 0x22212121, 0x22222020, 0x22222022, 0x22222220, 0x22222222,
  1804. GGML_TABLE_END()
  1805. #endif
  1806. #endif // GGML_COMMON_IMPL
  1807. #endif // GGML_COMMON_IMPL
  1808. #else
  1809. // TODO: this should not be a relative path, but can't figure out how to set Metal include paths in Package.swift
  1810. #include "../ggml-common.h"
  1811. #endif
  1812. /**
  1813. * llama.cpp - commit ba1cb19cdd0d92e012e0f6e009e0620f854b6afd - do not edit this file
  1814. *
  1815. * MIT License
  1816. *
  1817. * Copyright (c) 2023-2024 The ggml authors
  1818. *
  1819. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1820. * of this software and associated documentation files (the "Software"), to deal
  1821. * in the Software without restriction, including without limitation the rights
  1822. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1823. * copies of the Software, and to permit persons to whom the Software is
  1824. * furnished to do so, subject to the following conditions:
  1825. *
  1826. * The above copyright notice and this permission notice shall be included in all
  1827. * copies or substantial portions of the Software.
  1828. *
  1829. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1830. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1831. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1832. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1833. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1834. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1835. * SOFTWARE.
  1836. */
  1837. #ifndef GGML_METAL_IMPL
  1838. #define GGML_METAL_IMPL
  1839. // kernel argument structs
  1840. //
  1841. // - element counters (e.g. ne00) typically use int32_t to reduce register usage
  1842. // however, be careful from int overflows when using those in the kernel implementation
  1843. //
  1844. // - strides (e.g. nb00) use uint64_t
  1845. typedef struct {
  1846. int32_t ne00;
  1847. int32_t ne01;
  1848. int32_t ne02;
  1849. int32_t ne03;
  1850. uint64_t nb00;
  1851. uint64_t nb01;
  1852. uint64_t nb02;
  1853. uint64_t nb03;
  1854. int32_t ne10;
  1855. int32_t ne11;
  1856. int32_t ne12;
  1857. int32_t ne13;
  1858. uint64_t nb10;
  1859. uint64_t nb11;
  1860. uint64_t nb12;
  1861. uint64_t nb13;
  1862. int32_t ne0;
  1863. int32_t ne1;
  1864. int32_t ne2;
  1865. int32_t ne3;
  1866. uint64_t nb0;
  1867. uint64_t nb1;
  1868. uint64_t nb2;
  1869. uint64_t nb3;
  1870. int32_t dim;
  1871. } ggml_metal_kargs_concat;
  1872. typedef struct {
  1873. int32_t ne00;
  1874. int32_t ne01;
  1875. int32_t ne02;
  1876. int32_t ne03;
  1877. uint64_t nb00;
  1878. uint64_t nb01;
  1879. uint64_t nb02;
  1880. uint64_t nb03;
  1881. int32_t ne10;
  1882. int32_t ne11;
  1883. int32_t ne12;
  1884. int32_t ne13;
  1885. uint64_t nb10;
  1886. uint64_t nb11;
  1887. uint64_t nb12;
  1888. uint64_t nb13;
  1889. int32_t ne0;
  1890. int32_t ne1;
  1891. int32_t ne2;
  1892. int32_t ne3;
  1893. uint64_t nb0;
  1894. uint64_t nb1;
  1895. uint64_t nb2;
  1896. uint64_t nb3;
  1897. uint64_t offs;
  1898. } ggml_metal_kargs_bin;
  1899. typedef struct {
  1900. int32_t ne00;
  1901. int32_t ne01;
  1902. int32_t ne02;
  1903. int32_t ne03;
  1904. uint64_t nb00;
  1905. uint64_t nb01;
  1906. uint64_t nb02;
  1907. uint64_t nb03;
  1908. int32_t ne0;
  1909. int32_t ne1;
  1910. int32_t ne2;
  1911. int32_t ne3;
  1912. uint64_t nb0;
  1913. uint64_t nb1;
  1914. uint64_t nb2;
  1915. uint64_t nb3;
  1916. } ggml_metal_kargs_repeat;
  1917. typedef struct {
  1918. int64_t ne00;
  1919. int64_t ne01;
  1920. int64_t ne02;
  1921. int64_t ne03;
  1922. uint64_t nb00;
  1923. uint64_t nb01;
  1924. uint64_t nb02;
  1925. uint64_t nb03;
  1926. int64_t ne0;
  1927. int64_t ne1;
  1928. int64_t ne2;
  1929. int64_t ne3;
  1930. uint64_t nb0;
  1931. uint64_t nb1;
  1932. uint64_t nb2;
  1933. uint64_t nb3;
  1934. } ggml_metal_kargs_cpy;
  1935. typedef struct {
  1936. int64_t ne10;
  1937. int64_t ne11;
  1938. int64_t ne12;
  1939. uint64_t nb10;
  1940. uint64_t nb11;
  1941. uint64_t nb12;
  1942. uint64_t nb13;
  1943. uint64_t nb1;
  1944. uint64_t nb2;
  1945. uint64_t nb3;
  1946. uint64_t offs;
  1947. bool inplace;
  1948. } ggml_metal_kargs_set;
  1949. typedef struct {
  1950. int32_t ne00;
  1951. int32_t ne01;
  1952. int32_t ne02;
  1953. int32_t ne03;
  1954. uint64_t nb00;
  1955. uint64_t nb01;
  1956. uint64_t nb02;
  1957. uint64_t nb03;
  1958. int32_t ne0;
  1959. int32_t ne1;
  1960. int32_t ne2;
  1961. int32_t ne3;
  1962. uint64_t nb0;
  1963. uint64_t nb1;
  1964. uint64_t nb2;
  1965. uint64_t nb3;
  1966. int32_t n_past;
  1967. int32_t n_dims;
  1968. int32_t n_ctx_orig;
  1969. float freq_base;
  1970. float freq_scale;
  1971. float ext_factor;
  1972. float attn_factor;
  1973. float beta_fast;
  1974. float beta_slow;
  1975. } ggml_metal_kargs_rope;
  1976. typedef struct {
  1977. int32_t ne01;
  1978. int32_t ne02;
  1979. int32_t ne03;
  1980. uint64_t nb01;
  1981. uint64_t nb02;
  1982. uint64_t nb03;
  1983. int32_t ne11;
  1984. int32_t ne_12_2; // assume K and V are same shape
  1985. int32_t ne_12_3;
  1986. uint64_t nb_12_1;
  1987. uint64_t nb_12_2;
  1988. uint64_t nb_12_3;
  1989. uint64_t nb31;
  1990. int32_t ne1;
  1991. int32_t ne2;
  1992. float scale;
  1993. float max_bias;
  1994. float m0;
  1995. float m1;
  1996. uint16_t n_head_log2;
  1997. float logit_softcap;
  1998. } ggml_metal_kargs_flash_attn_ext;
  1999. typedef struct {
  2000. int32_t ne00;
  2001. int32_t ne02;
  2002. uint64_t nb01;
  2003. uint64_t nb02;
  2004. uint64_t nb03;
  2005. int32_t ne12;
  2006. uint64_t nb10;
  2007. uint64_t nb11;
  2008. uint64_t nb12;
  2009. uint64_t nb13;
  2010. int32_t ne0;
  2011. int32_t ne1;
  2012. int16_t r2;
  2013. int16_t r3;
  2014. } ggml_metal_kargs_mul_mm;
  2015. typedef struct {
  2016. int32_t ne00;
  2017. int32_t ne01;
  2018. int32_t ne02;
  2019. uint64_t nb00;
  2020. uint64_t nb01;
  2021. uint64_t nb02;
  2022. uint64_t nb03;
  2023. int32_t ne10;
  2024. int32_t ne11;
  2025. int32_t ne12;
  2026. uint64_t nb10;
  2027. uint64_t nb11;
  2028. uint64_t nb12;
  2029. uint64_t nb13;
  2030. int32_t ne0;
  2031. int32_t ne1;
  2032. int16_t r2;
  2033. int16_t r3;
  2034. } ggml_metal_kargs_mul_mv;
  2035. typedef struct {
  2036. int32_t ne00;
  2037. int32_t ne01;
  2038. int32_t ne02;
  2039. uint64_t nb00;
  2040. uint64_t nb01;
  2041. uint64_t nb02;
  2042. uint64_t nb03;
  2043. int32_t ne10;
  2044. int32_t ne11;
  2045. int32_t ne12;
  2046. uint64_t nb10;
  2047. uint64_t nb11;
  2048. uint64_t nb12;
  2049. uint64_t nb13;
  2050. int32_t ne0;
  2051. int32_t ne1;
  2052. int16_t r2;
  2053. int16_t r3;
  2054. int16_t nsg;
  2055. int16_t nxpsg;
  2056. int16_t r1ptg;
  2057. } ggml_metal_kargs_mul_mv_ext;
  2058. typedef struct {
  2059. int32_t nei0;
  2060. int32_t nei1;
  2061. uint64_t nbi1;
  2062. int32_t ne00;
  2063. int32_t ne02;
  2064. uint64_t nb01;
  2065. uint64_t nb02;
  2066. int32_t ne11;
  2067. int32_t ne12;
  2068. int32_t ne13;
  2069. uint64_t nb10;
  2070. uint64_t nb11;
  2071. uint64_t nb12;
  2072. int32_t ne0;
  2073. int32_t ne1;
  2074. } ggml_metal_kargs_mul_mm_id;
  2075. typedef struct {
  2076. int32_t nei0;
  2077. int32_t nei1;
  2078. uint64_t nbi1;
  2079. int32_t ne00;
  2080. int32_t ne01;
  2081. int32_t ne02;
  2082. uint64_t nb00;
  2083. uint64_t nb01;
  2084. uint64_t nb02;
  2085. int32_t ne10;
  2086. int32_t ne11;
  2087. int32_t ne12;
  2088. int32_t ne13;
  2089. uint64_t nb10;
  2090. uint64_t nb11;
  2091. uint64_t nb12;
  2092. int32_t ne0;
  2093. int32_t ne1;
  2094. uint64_t nb1;
  2095. } ggml_metal_kargs_mul_mv_id;
  2096. typedef struct {
  2097. int32_t ne00;
  2098. int32_t ne00_4;
  2099. uint64_t nb01;
  2100. float eps;
  2101. } ggml_metal_kargs_norm;
  2102. typedef struct {
  2103. int32_t ne00;
  2104. int32_t ne00_4;
  2105. uint64_t nb01;
  2106. float eps;
  2107. } ggml_metal_kargs_rms_norm;
  2108. #endif // GGML_METAL_IMPL
  2109. #include <metal_stdlib>
  2110. using namespace metal;
  2111. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  2112. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  2113. #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; }
  2114. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  2115. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  2116. //
  2117. // cmd:
  2118. // .../usr/bin/metal -dM -E -c ggml/src/ggml-metal/ggml-metal.metal
  2119. // .../usr/bin/metal -dM -E -c -target air64-apple-ios14.0 ggml/src/ggml-metal/ggml-metal.metal
  2120. //
  2121. #if __METAL_VERSION__ < 310 && defined(GGML_METAL_USE_BF16)
  2122. #undef GGML_METAL_USE_BF16
  2123. #endif
  2124. #if defined(GGML_METAL_USE_BF16)
  2125. typedef matrix<bfloat, 4, 4> bfloat4x4;
  2126. #endif
  2127. constexpr constant static float kvalues_iq4nl_f[16] = {
  2128. -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
  2129. };
  2130. // NOTE: this is not dequantizing - we are simply fitting the template
  2131. template <typename type4x4>
  2132. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  2133. reg = (type4x4)(*src);
  2134. }
  2135. template <typename type4x4>
  2136. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  2137. reg = (type4x4)(*src);
  2138. }
  2139. template <typename type4>
  2140. void dequantize_f16_t4(device const half4 * src, short il, thread type4 & reg) {
  2141. reg = (type4)(*(src + il));
  2142. }
  2143. #if defined(GGML_METAL_USE_BF16)
  2144. template <typename type4x4>
  2145. void dequantize_bf16(device const bfloat4x4 * src, short il, thread type4x4 & reg) {
  2146. reg = (type4x4)(*src);
  2147. }
  2148. #endif
  2149. template <typename type4x4>
  2150. void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) {
  2151. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  2152. const float d1 = il ? (xb->d / 16.h) : xb->d;
  2153. const float d2 = d1 / 256.f;
  2154. const float md = -8.h * xb->d;
  2155. const ushort mask0 = il ? 0x00F0 : 0x000F;
  2156. const ushort mask1 = mask0 << 8;
  2157. float4x4 reg_f;
  2158. for (int i = 0; i < 8; i++) {
  2159. reg_f[i/2][2*(i%2) + 0] = d1 * (qs[i] & mask0) + md;
  2160. reg_f[i/2][2*(i%2) + 1] = d2 * (qs[i] & mask1) + md;
  2161. }
  2162. reg = (type4x4) reg_f;
  2163. }
  2164. template <typename type4>
  2165. void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & reg) {
  2166. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  2167. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  2168. const float d2 = d1 / 256.f;
  2169. const float md = -8.h * xb->d;
  2170. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  2171. const ushort mask1 = mask0 << 8;
  2172. for (int i = 0; i < 2; i++) {
  2173. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + md;
  2174. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + md;
  2175. }
  2176. }
  2177. template <typename type4x4>
  2178. void dequantize_q4_1(device const block_q4_1 * xb, short il, thread type4x4 & reg) {
  2179. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  2180. const float d1 = il ? (xb->d / 16.h) : xb->d;
  2181. const float d2 = d1 / 256.f;
  2182. const float m = xb->m;
  2183. const ushort mask0 = il ? 0x00F0 : 0x000F;
  2184. const ushort mask1 = mask0 << 8;
  2185. float4x4 reg_f;
  2186. for (int i = 0; i < 8; i++) {
  2187. reg_f[i/2][2*(i%2) + 0] = ((qs[i] & mask0) * d1) + m;
  2188. reg_f[i/2][2*(i%2) + 1] = ((qs[i] & mask1) * d2) + m;
  2189. }
  2190. reg = (type4x4) reg_f;
  2191. }
  2192. template <typename type4>
  2193. void dequantize_q4_1_t4(device const block_q4_1 * xb, short il, thread type4 & reg) {
  2194. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  2195. const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
  2196. const float d2 = d1 / 256.f;
  2197. const float m = xb->m;
  2198. const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
  2199. const ushort mask1 = mask0 << 8;
  2200. for (int i = 0; i < 2; i++) {
  2201. reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + m;
  2202. reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + m;
  2203. }
  2204. }
  2205. template <typename type4x4>
  2206. void dequantize_q5_0(device const block_q5_0 * xb, short il, thread type4x4 & reg) {
  2207. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  2208. const float d = xb->d;
  2209. const float md = -16.h * xb->d;
  2210. const ushort mask = il ? 0x00F0 : 0x000F;
  2211. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2212. const int x_mv = il ? 4 : 0;
  2213. const int gh_mv = il ? 12 : 0;
  2214. const int gh_bk = il ? 0 : 4;
  2215. float4x4 reg_f;
  2216. for (int i = 0; i < 8; i++) {
  2217. // extract the 5-th bits for x0 and x1
  2218. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2219. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2220. // combine the 4-bits from qs with the 5th bit
  2221. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2222. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2223. reg_f[i/2][2*(i%2) + 0] = d * x0 + md;
  2224. reg_f[i/2][2*(i%2) + 1] = d * x1 + md;
  2225. }
  2226. reg = (type4x4) reg_f;
  2227. }
  2228. template <typename type4>
  2229. void dequantize_q5_0_t4(device const block_q5_0 * xb, short il, thread type4 & reg) {
  2230. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  2231. const float d = xb->d;
  2232. const float md = -16.h * xb->d;
  2233. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  2234. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2235. const int x_mv = (il/4) ? 4 : 0;
  2236. const int gh_mv = (il/4) ? 12 : 0;
  2237. const int gh_bk = (il/4) ? 0 : 4;
  2238. for (int ii = 0; ii < 2; ii++) {
  2239. int i = 2*(il%4) + ii;
  2240. // extract the 5-th bits for x0 and x1
  2241. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2242. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2243. // combine the 4-bits from qs with the 5th bit
  2244. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2245. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2246. reg[2*ii + 0] = d * x0 + md;
  2247. reg[2*ii + 1] = d * x1 + md;
  2248. }
  2249. }
  2250. template <typename type4x4>
  2251. void dequantize_q5_1(device const block_q5_1 * xb, short il, thread type4x4 & reg) {
  2252. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  2253. const float d = xb->d;
  2254. const float m = xb->m;
  2255. const ushort mask = il ? 0x00F0 : 0x000F;
  2256. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2257. const int x_mv = il ? 4 : 0;
  2258. const int gh_mv = il ? 12 : 0;
  2259. const int gh_bk = il ? 0 : 4;
  2260. float4x4 reg_f;
  2261. for (int i = 0; i < 8; i++) {
  2262. // extract the 5-th bits for x0 and x1
  2263. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2264. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2265. // combine the 4-bits from qs with the 5th bit
  2266. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2267. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2268. reg_f[i/2][2*(i%2) + 0] = d * x0 + m;
  2269. reg_f[i/2][2*(i%2) + 1] = d * x1 + m;
  2270. }
  2271. reg = (type4x4) reg_f;
  2272. }
  2273. template <typename type4>
  2274. void dequantize_q5_1_t4(device const block_q5_1 * xb, short il, thread type4 & reg) {
  2275. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  2276. const float d = xb->d;
  2277. const float m = xb->m;
  2278. const ushort mask = (il/4) ? 0x00F0 : 0x000F;
  2279. const uint32_t qh = *((device const uint32_t *)xb->qh);
  2280. const int x_mv = (il/4) ? 4 : 0;
  2281. const int gh_mv = (il/4) ? 12 : 0;
  2282. const int gh_bk = (il/4) ? 0 : 4;
  2283. for (int ii = 0; ii < 2; ii++) {
  2284. int i = 2*(il%4) + ii;
  2285. // extract the 5-th bits for x0 and x1
  2286. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  2287. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  2288. // combine the 4-bits from qs with the 5th bit
  2289. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  2290. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  2291. reg[2*ii + 0] = d * x0 + m;
  2292. reg[2*ii + 1] = d * x1 + m;
  2293. }
  2294. }
  2295. template <typename type4x4>
  2296. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  2297. device const int8_t * qs = ((device const int8_t *)xb->qs);
  2298. const float d = xb->d;
  2299. float4x4 reg_f;
  2300. for (int i = 0; i < 16; i++) {
  2301. reg_f[i/4][i%4] = (qs[i + 16*il] * d);
  2302. }
  2303. reg = (type4x4) reg_f;
  2304. }
  2305. template <typename type4>
  2306. void dequantize_q8_0_t4(device const block_q8_0 *xb, short il, thread type4 & reg) {
  2307. device const int8_t * qs = ((device const int8_t *)xb->qs);
  2308. const float d = xb->d;
  2309. for (int i = 0; i < 4; i++) {
  2310. reg[i] = (qs[4*(il%4) + i + 16*(il/4)] * d);
  2311. }
  2312. }
  2313. template <typename type4x4>
  2314. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  2315. const float d = xb->d;
  2316. const float min = xb->dmin;
  2317. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2318. float dl, ml;
  2319. uint8_t sc = xb->scales[il];
  2320. q = q + 32*(il/8) + 16*(il&1);
  2321. il = (il/2)%4;
  2322. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2323. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2324. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  2325. for (int i = 0; i < 16; ++i) {
  2326. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2327. }
  2328. }
  2329. template <typename type4x4>
  2330. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  2331. const half d_all = xb->d;
  2332. device const uint8_t * q = (device const uint8_t *)xb->qs;
  2333. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  2334. device const int8_t * scales = (device const int8_t *)xb->scales;
  2335. q = q + 32 * (il/8) + 16 * (il&1);
  2336. h = h + 16 * (il&1);
  2337. uint8_t m = 1 << (il/2);
  2338. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  2339. ((il/4)>0 ? 12 : 3);
  2340. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  2341. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  2342. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  2343. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  2344. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  2345. const float ml = 4.f * dl;
  2346. il = (il/2) & 3;
  2347. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  2348. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2349. dl *= coef;
  2350. for (int i = 0; i < 16; ++i) {
  2351. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  2352. }
  2353. }
  2354. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  2355. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  2356. : 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))};
  2357. }
  2358. template <typename type4x4>
  2359. void dequantize_q4_K(device const block_q4_K * xb, short il, thread type4x4 & reg) {
  2360. device const uchar * q = xb->qs;
  2361. short is = (il/4) * 2;
  2362. q = q + (il/4) * 32 + 16 * (il&1);
  2363. il = il & 3;
  2364. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2365. const float d = il < 2 ? xb->d : xb->d / 16.h;
  2366. const float min = xb->dmin;
  2367. const float dl = d * sc[0];
  2368. const float ml = min * sc[1];
  2369. const ushort mask = il < 2 ? 0x0F : 0xF0;
  2370. for (int i = 0; i < 16; ++i) {
  2371. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  2372. }
  2373. }
  2374. template <typename type4x4>
  2375. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  2376. device const uint8_t * q = xb->qs;
  2377. device const uint8_t * qh = xb->qh;
  2378. short is = (il/4) * 2;
  2379. q = q + 32 * (il/4) + 16 * (il&1);
  2380. qh = qh + 16 * (il&1);
  2381. uint8_t ul = 1 << (il/2);
  2382. il = il & 3;
  2383. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  2384. const float d = il < 2 ? xb->d : xb->d / 16.f;
  2385. const float min = xb->dmin;
  2386. const float dl = d * sc[0];
  2387. const float ml = min * sc[1];
  2388. const ushort mask = il<2 ? 0x0F : 0xF0;
  2389. const float qh_val = il<2 ? 16.f : 256.f;
  2390. for (int i = 0; i < 16; ++i) {
  2391. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  2392. }
  2393. }
  2394. template <typename type4x4>
  2395. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  2396. const half d_all = xb->d;
  2397. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  2398. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  2399. device const int8_t * scales = (device const int8_t *)xb->scales;
  2400. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  2401. qh = qh + 32*(il/8) + 16*(il&1);
  2402. float sc = scales[(il%2) + 2 * ((il/2))];
  2403. il = (il/2) & 3;
  2404. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  2405. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  2406. const float coef = il>1 ? 1.f/16.f : 1.f;
  2407. const float ml = d_all * sc * 32.f;
  2408. const float dl = d_all * sc * coef;
  2409. for (int i = 0; i < 16; ++i) {
  2410. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  2411. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  2412. reg[i/4][i%4] = dl * q - ml;
  2413. }
  2414. }
  2415. template <typename type4x4>
  2416. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  2417. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2418. const float d = xb->d;
  2419. const int ib32 = il/2;
  2420. il = il%2;
  2421. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2422. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  2423. device const uint16_t * q2 = xb->qs + 4*ib32;
  2424. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  2425. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  2426. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  2427. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  2428. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  2429. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  2430. for (int i = 0; i < 8; ++i) {
  2431. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2432. }
  2433. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  2434. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  2435. for (int i = 0; i < 8; ++i) {
  2436. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2437. }
  2438. }
  2439. template <typename type4x4>
  2440. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  2441. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2442. const float d = xb->d;
  2443. const int ib32 = il/2;
  2444. il = il%2;
  2445. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2446. device const uint16_t * q2 = xb->qs + 4*ib32;
  2447. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  2448. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  2449. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  2450. for (int i = 0; i < 8; ++i) {
  2451. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2452. }
  2453. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  2454. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  2455. for (int i = 0; i < 8; ++i) {
  2456. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  2457. }
  2458. }
  2459. template <typename type4x4>
  2460. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  2461. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2462. const float d = xb->d;
  2463. const int ib32 = il/2;
  2464. il = il%2;
  2465. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2466. device const uint8_t * q3 = xb->qs + 8*ib32;
  2467. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  2468. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  2469. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2470. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  2471. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  2472. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  2473. for (int i = 0; i < 4; ++i) {
  2474. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  2475. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  2476. }
  2477. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  2478. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  2479. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  2480. for (int i = 0; i < 4; ++i) {
  2481. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  2482. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  2483. }
  2484. }
  2485. template <typename type4x4>
  2486. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  2487. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2488. const float d = xb->d;
  2489. const int ib32 = il/2;
  2490. il = il%2;
  2491. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2492. device const uint8_t * qs = xb->qs + 8*ib32;
  2493. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  2494. const uint8_t qh = xb->qh[ib32] >> 4*il;
  2495. const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
  2496. constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  2497. constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  2498. for (int i = 0; i < 4; ++i) {
  2499. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  2500. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  2501. }
  2502. grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  2503. grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  2504. for (int i = 0; i < 4; ++i) {
  2505. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  2506. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  2507. }
  2508. }
  2509. template <typename type4x4>
  2510. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  2511. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2512. const float d = xb->d;
  2513. const int ib32 = il/2;
  2514. il = il%2;
  2515. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2516. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2517. device const uint8_t * signs = qs + QK_K/8;
  2518. const uint8_t qh = xb->qh[ib32] >> 4*il;
  2519. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  2520. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  2521. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  2522. for (int i = 0; i < 8; ++i) {
  2523. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  2524. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  2525. }
  2526. }
  2527. template <typename type4x4>
  2528. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  2529. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2530. const int ib32 = il/2;
  2531. il = il%2;
  2532. const float d = xb->d;
  2533. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2534. device const uint16_t * qh = xb->qh;
  2535. const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1);
  2536. const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA);
  2537. const uint16_t h = qh[ib32] >> 6*il;
  2538. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700)));
  2539. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700)));
  2540. for (int i = 0; i < 4; ++i) {
  2541. reg[0][i] = dl * (grid1[i] & 0xf) + ml;
  2542. reg[1][i] = dl * (grid1[i] >> 4) + ml;
  2543. reg[2][i] = dl * (grid2[i] & 0xf) + ml;
  2544. reg[3][i] = dl * (grid2[i] >> 4) + ml;
  2545. }
  2546. }
  2547. template <typename type4x4>
  2548. void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) {
  2549. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2550. const int ib32 = il/2;
  2551. il = il%2;
  2552. device const uint16_t * sc = (device const uint16_t *)xb->scales;
  2553. iq1m_scale_t scale;
  2554. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2555. const float d = scale.f16;
  2556. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  2557. device const uint8_t * qh = xb->qh + 2*ib32 + il;
  2558. const float dl = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1);
  2559. const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  2560. const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  2561. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  2562. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  2563. for (int i = 0; i < 4; ++i) {
  2564. reg[0][i] = dl * (grid1[i] & 0xf) + ml1;
  2565. reg[1][i] = dl * (grid1[i] >> 4) + ml1;
  2566. reg[2][i] = dl * (grid2[i] & 0xf) + ml2;
  2567. reg[3][i] = dl * (grid2[i] >> 4) + ml2;
  2568. }
  2569. }
  2570. template <typename type4x4>
  2571. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  2572. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  2573. const float d = xb->d;
  2574. uint32_t aux32;
  2575. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2576. for (int i = 0; i < 4; ++i) {
  2577. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  2578. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  2579. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  2580. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  2581. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  2582. }
  2583. }
  2584. template <typename type4>
  2585. void dequantize_iq4_nl_t4(device const block_iq4_nl * xb, short il, thread type4 & reg) {
  2586. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  2587. const float d = xb->d;
  2588. uint32_t aux32;
  2589. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2590. aux32 = ((q4[2*(il%4)] | (q4[2*(il%4)+1] << 16)) >> 4*(il/4)) & 0x0f0f0f0f;
  2591. reg[0] = d * kvalues_iq4nl_f[q8[0]];
  2592. reg[1] = d * kvalues_iq4nl_f[q8[1]];
  2593. reg[2] = d * kvalues_iq4nl_f[q8[2]];
  2594. reg[3] = d * kvalues_iq4nl_f[q8[3]];
  2595. }
  2596. template <typename type4x4>
  2597. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  2598. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  2599. const int ib32 = il/2;
  2600. il = il%2;
  2601. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  2602. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  2603. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  2604. const float d = (float)xb->d * (ls - 32);
  2605. uint32_t aux32;
  2606. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  2607. for (int i = 0; i < 4; ++i) {
  2608. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  2609. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  2610. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  2611. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  2612. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  2613. }
  2614. }
  2615. enum ggml_sort_order {
  2616. GGML_SORT_ORDER_ASC,
  2617. GGML_SORT_ORDER_DESC,
  2618. };
  2619. // general-purpose kernel for addition, subtraction, multiplication and division of two tensors
  2620. // pros: works for non-contiguous tensors, supports broadcast across all dims
  2621. // cons: not very efficient
  2622. kernel void kernel_add(
  2623. constant ggml_metal_kargs_bin & args,
  2624. device const char * src0,
  2625. device const char * src1,
  2626. device char * dst,
  2627. uint3 tgpig[[threadgroup_position_in_grid]],
  2628. ushort3 tpitg[[thread_position_in_threadgroup]],
  2629. ushort3 ntg[[threads_per_threadgroup]]) {
  2630. const int i03 = tgpig.z;
  2631. const int i02 = tgpig.y;
  2632. const int i01 = tgpig.x;
  2633. const int i13 = i03%args.ne13;
  2634. const int i12 = i02%args.ne12;
  2635. const int i11 = i01%args.ne11;
  2636. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  2637. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2638. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  2639. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2640. const int i10 = i0%args.ne10;
  2641. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) + *((device float *)(src1_ptr + i10*args.nb10));
  2642. }
  2643. }
  2644. kernel void kernel_sub(
  2645. constant ggml_metal_kargs_bin & args,
  2646. device const char * src0,
  2647. device const char * src1,
  2648. device char * dst,
  2649. uint3 tgpig[[threadgroup_position_in_grid]],
  2650. ushort3 tpitg[[thread_position_in_threadgroup]],
  2651. ushort3 ntg[[threads_per_threadgroup]]) {
  2652. const int i03 = tgpig.z;
  2653. const int i02 = tgpig.y;
  2654. const int i01 = tgpig.x;
  2655. const int i13 = i03%args.ne13;
  2656. const int i12 = i02%args.ne12;
  2657. const int i11 = i01%args.ne11;
  2658. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs;
  2659. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2660. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs;
  2661. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2662. const int i10 = i0%args.ne10;
  2663. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) - *((device float *)(src1_ptr + i10*args.nb10));
  2664. }
  2665. }
  2666. kernel void kernel_mul(
  2667. constant ggml_metal_kargs_bin & args,
  2668. device const char * src0,
  2669. device const char * src1,
  2670. device char * dst,
  2671. uint3 tgpig[[threadgroup_position_in_grid]],
  2672. ushort3 tpitg[[thread_position_in_threadgroup]],
  2673. ushort3 ntg[[threads_per_threadgroup]]) {
  2674. const int i03 = tgpig.z;
  2675. const int i02 = tgpig.y;
  2676. const int i01 = tgpig.x;
  2677. const int i13 = i03%args.ne13;
  2678. const int i12 = i02%args.ne12;
  2679. const int i11 = i01%args.ne11;
  2680. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2681. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2682. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  2683. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2684. const int i10 = i0%args.ne10;
  2685. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) * *((device float *)(src1_ptr + i10*args.nb10));
  2686. }
  2687. }
  2688. kernel void kernel_div(
  2689. constant ggml_metal_kargs_bin & args,
  2690. device const char * src0,
  2691. device const char * src1,
  2692. device char * dst,
  2693. uint3 tgpig[[threadgroup_position_in_grid]],
  2694. ushort3 tpitg[[thread_position_in_threadgroup]],
  2695. ushort3 ntg[[threads_per_threadgroup]]) {
  2696. const int i03 = tgpig.z;
  2697. const int i02 = tgpig.y;
  2698. const int i01 = tgpig.x;
  2699. const int i13 = i03%args.ne13;
  2700. const int i12 = i02%args.ne12;
  2701. const int i11 = i01%args.ne11;
  2702. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2703. device const char * src1_ptr = src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11;
  2704. device char * dst_ptr = dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1;
  2705. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2706. const int i10 = i0%args.ne10;
  2707. *((device float *)(dst_ptr + i0*args.nb0)) = *((device float *)(src0_ptr + i0*args.nb00)) / *((device float *)(src1_ptr + i10*args.nb10));
  2708. }
  2709. }
  2710. template<typename T>
  2711. kernel void kernel_repeat(
  2712. constant ggml_metal_kargs_repeat & args,
  2713. device const char * src0,
  2714. device char * dst,
  2715. uint3 tgpig[[threadgroup_position_in_grid]],
  2716. ushort3 tpitg[[thread_position_in_threadgroup]],
  2717. ushort3 ntg[[threads_per_threadgroup]]) {
  2718. const int i3 = tgpig.z;
  2719. const int i2 = tgpig.y;
  2720. const int i1 = tgpig.x;
  2721. const int i03 = i3%args.ne03;
  2722. const int i02 = i2%args.ne02;
  2723. const int i01 = i1%args.ne01;
  2724. device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
  2725. device char * dst_ptr = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1;
  2726. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  2727. const int i00 = i0%args.ne00;
  2728. *((device T *)(dst_ptr + i0*args.nb0)) = *((device T *)(src0_ptr + i00*args.nb00));
  2729. }
  2730. }
  2731. typedef decltype(kernel_repeat<float>) kernel_repeat_t;
  2732. template [[host_name("kernel_repeat_f32")]] kernel kernel_repeat_t kernel_repeat<float>;
  2733. template [[host_name("kernel_repeat_f16")]] kernel kernel_repeat_t kernel_repeat<half>;
  2734. template [[host_name("kernel_repeat_i32")]] kernel kernel_repeat_t kernel_repeat<int>;
  2735. template [[host_name("kernel_repeat_i16")]] kernel kernel_repeat_t kernel_repeat<short>;
  2736. // assumption: src1 is a row
  2737. // broadcast src1 into src0
  2738. kernel void kernel_add_row(
  2739. constant ggml_metal_kargs_bin & args,
  2740. device const float4 * src0,
  2741. device const float4 * src1,
  2742. device float4 * dst,
  2743. uint tpig[[thread_position_in_grid]]) {
  2744. const uint nb = args.ne00/4;
  2745. dst[tpig] = src0[tpig] + src1[tpig % nb];
  2746. }
  2747. kernel void kernel_sub_row(
  2748. constant ggml_metal_kargs_bin & args,
  2749. device const float4 * src0,
  2750. device const float4 * src1,
  2751. device float4 * dst,
  2752. uint tpig[[thread_position_in_grid]]) {
  2753. const uint nb = args.ne00/4;
  2754. dst[tpig] = src0[tpig] - src1[tpig % nb];
  2755. }
  2756. kernel void kernel_mul_row(
  2757. constant ggml_metal_kargs_bin & args,
  2758. device const float4 * src0,
  2759. device const float4 * src1,
  2760. device float4 * dst,
  2761. uint tpig[[thread_position_in_grid]]) {
  2762. const uint nb = args.ne00/4;
  2763. dst[tpig] = src0[tpig] * src1[tpig % nb];
  2764. }
  2765. kernel void kernel_div_row(
  2766. constant ggml_metal_kargs_bin & args,
  2767. device const float4 * src0,
  2768. device const float4 * src1,
  2769. device float4 * dst,
  2770. uint tpig[[thread_position_in_grid]]) {
  2771. const uint nb = args.ne00/4;
  2772. dst[tpig] = src0[tpig] / src1[tpig % nb];
  2773. }
  2774. kernel void kernel_scale(
  2775. device const float * src0,
  2776. device float * dst,
  2777. constant float & scale,
  2778. uint tpig[[thread_position_in_grid]]) {
  2779. dst[tpig] = src0[tpig] * scale;
  2780. }
  2781. kernel void kernel_scale_4(
  2782. device const float4 * src0,
  2783. device float4 * dst,
  2784. constant float & scale,
  2785. uint tpig[[thread_position_in_grid]]) {
  2786. dst[tpig] = src0[tpig] * scale;
  2787. }
  2788. kernel void kernel_clamp(
  2789. device const float * src0,
  2790. device float * dst,
  2791. constant float & min,
  2792. constant float & max,
  2793. uint tpig[[thread_position_in_grid]]) {
  2794. dst[tpig] = src0[tpig] < min ? min : (src0[tpig] > max ? max : src0[tpig]);
  2795. }
  2796. kernel void kernel_relu(
  2797. device const float * src0,
  2798. device float * dst,
  2799. uint tpig[[thread_position_in_grid]]) {
  2800. dst[tpig] = max(0.0f, src0[tpig]);
  2801. }
  2802. kernel void kernel_sigmoid(
  2803. device const float * src0,
  2804. device float * dst,
  2805. uint tpig[[thread_position_in_grid]]) {
  2806. dst[tpig] = 1.0f / (1.0f + exp(-src0[tpig]));
  2807. }
  2808. kernel void kernel_tanh(
  2809. device const float * src0,
  2810. device float * dst,
  2811. uint tpig[[thread_position_in_grid]]) {
  2812. device const float & x = src0[tpig];
  2813. dst[tpig] = precise::tanh(x);
  2814. }
  2815. constant float GELU_COEF_A = 0.044715f;
  2816. constant float GELU_QUICK_COEF = -1.702f;
  2817. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  2818. kernel void kernel_gelu(
  2819. device const float * src0,
  2820. device float * dst,
  2821. uint tpig[[thread_position_in_grid]]) {
  2822. device const float & x = src0[tpig];
  2823. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  2824. }
  2825. kernel void kernel_gelu_4(
  2826. device const float4 * src0,
  2827. device float4 * dst,
  2828. uint tpig[[thread_position_in_grid]]) {
  2829. device const float4 & x = src0[tpig];
  2830. // BEWARE !!!
  2831. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  2832. // This was observed with Falcon 7B and 40B models
  2833. //
  2834. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  2835. }
  2836. kernel void kernel_gelu_quick(
  2837. device const float * src0,
  2838. device float * dst,
  2839. uint tpig[[thread_position_in_grid]]) {
  2840. device const float & x = src0[tpig];
  2841. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  2842. }
  2843. kernel void kernel_gelu_quick_4(
  2844. device const float4 * src0,
  2845. device float4 * dst,
  2846. uint tpig[[thread_position_in_grid]]) {
  2847. device const float4 & x = src0[tpig];
  2848. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  2849. }
  2850. kernel void kernel_silu(
  2851. device const float * src0,
  2852. device float * dst,
  2853. uint tpig[[thread_position_in_grid]]) {
  2854. device const float & x = src0[tpig];
  2855. dst[tpig] = x / (1.0f + exp(-x));
  2856. }
  2857. kernel void kernel_silu_4(
  2858. device const float4 * src0,
  2859. device float4 * dst,
  2860. uint tpig[[thread_position_in_grid]]) {
  2861. device const float4 & x = src0[tpig];
  2862. dst[tpig] = x / (1.0f + exp(-x));
  2863. }
  2864. kernel void kernel_elu(
  2865. device const float * src0,
  2866. device float * dst,
  2867. uint tpig[[thread_position_in_grid]]) {
  2868. device const float & x = src0[tpig];
  2869. dst[tpig] = (x > 0.0f) ? x : (exp(x) - 1.0f);
  2870. }
  2871. kernel void kernel_sqr(
  2872. device const float * src0,
  2873. device float * dst,
  2874. uint tpig[[thread_position_in_grid]]) {
  2875. dst[tpig] = src0[tpig] * src0[tpig];
  2876. }
  2877. kernel void kernel_sqrt(
  2878. device const float * src0,
  2879. device float * dst,
  2880. uint tpig[[thread_position_in_grid]]) {
  2881. dst[tpig] = sqrt(src0[tpig]);
  2882. }
  2883. kernel void kernel_sin(
  2884. device const float * src0,
  2885. device float * dst,
  2886. uint tpig[[thread_position_in_grid]]) {
  2887. dst[tpig] = sin(src0[tpig]);
  2888. }
  2889. kernel void kernel_cos(
  2890. device const float * src0,
  2891. device float * dst,
  2892. uint tpig[[thread_position_in_grid]]) {
  2893. dst[tpig] = cos(src0[tpig]);
  2894. }
  2895. kernel void kernel_sum_rows(
  2896. device const float * src0,
  2897. device float * dst,
  2898. constant int64_t & ne00,
  2899. constant int64_t & ne01,
  2900. constant int64_t & ne02,
  2901. constant int64_t & ne03,
  2902. constant uint64_t & nb00,
  2903. constant uint64_t & nb01,
  2904. constant uint64_t & nb02,
  2905. constant uint64_t & nb03,
  2906. constant int64_t & ne10,
  2907. constant int64_t & ne11,
  2908. constant int64_t & ne12,
  2909. constant int64_t & ne13,
  2910. constant uint64_t & nb10,
  2911. constant uint64_t & nb11,
  2912. constant uint64_t & nb12,
  2913. constant uint64_t & nb13,
  2914. constant int64_t & ne0,
  2915. constant int64_t & ne1,
  2916. constant int64_t & ne2,
  2917. constant int64_t & ne3,
  2918. constant uint64_t & nb0,
  2919. constant uint64_t & nb1,
  2920. constant uint64_t & nb2,
  2921. constant uint64_t & nb3,
  2922. uint3 tpig[[thread_position_in_grid]]) {
  2923. int64_t i3 = tpig.z;
  2924. int64_t i2 = tpig.y;
  2925. int64_t i1 = tpig.x;
  2926. if (i3 >= ne03 || i2 >= ne02 || i1 >= ne01) {
  2927. return;
  2928. }
  2929. device const float * src_row = (device const float *) ((device const char *) src0 + i1*nb01 + i2*nb02 + i3*nb03);
  2930. device float * dst_row = (device float *) ((device char *) dst + i1*nb1 + i2*nb2 + i3*nb3);
  2931. float row_sum = 0;
  2932. for (int64_t i0 = 0; i0 < ne00; i0++) {
  2933. row_sum += src_row[i0];
  2934. }
  2935. dst_row[0] = row_sum;
  2936. }
  2937. template<typename T>
  2938. kernel void kernel_soft_max(
  2939. device const char * src0,
  2940. device const char * src1,
  2941. device char * dst,
  2942. constant int64_t & ne00,
  2943. constant int64_t & ne01,
  2944. constant int64_t & ne02,
  2945. constant float & scale,
  2946. constant float & max_bias,
  2947. constant float & m0,
  2948. constant float & m1,
  2949. constant uint32_t & n_head_log2,
  2950. threadgroup float * buf [[threadgroup(0)]],
  2951. uint tgpig[[threadgroup_position_in_grid]],
  2952. uint tpitg[[thread_position_in_threadgroup]],
  2953. uint sgitg[[simdgroup_index_in_threadgroup]],
  2954. uint tiisg[[thread_index_in_simdgroup]],
  2955. uint ntg[[threads_per_threadgroup]]) {
  2956. const int64_t i03 = (tgpig) / (ne02*ne01);
  2957. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  2958. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  2959. device const float * psrc0 = (device const float *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  2960. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00 : nullptr;
  2961. device float * pdst = (device float *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  2962. float slope = 1.0f;
  2963. // ALiBi
  2964. if (max_bias > 0.0f) {
  2965. const int64_t h = i02;
  2966. const float base = h < n_head_log2 ? m0 : m1;
  2967. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  2968. slope = pow(base, exp);
  2969. }
  2970. // parallel max
  2971. float lmax = -INFINITY;
  2972. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  2973. lmax = MAX(lmax, psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f));
  2974. }
  2975. // find the max value in the block
  2976. float max_val = simd_max(lmax);
  2977. if (ntg > N_SIMDWIDTH) {
  2978. if (sgitg == 0) {
  2979. buf[tiisg] = -INFINITY;
  2980. }
  2981. threadgroup_barrier(mem_flags::mem_threadgroup);
  2982. if (tiisg == 0) {
  2983. buf[sgitg] = max_val;
  2984. }
  2985. threadgroup_barrier(mem_flags::mem_threadgroup);
  2986. max_val = buf[tiisg];
  2987. max_val = simd_max(max_val);
  2988. }
  2989. // parallel sum
  2990. float lsum = 0.0f;
  2991. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  2992. const float exp_psrc0 = exp((psrc0[i00]*scale + (pmask ? slope*pmask[i00] : 0.0f)) - max_val);
  2993. lsum += exp_psrc0;
  2994. pdst[i00] = exp_psrc0;
  2995. }
  2996. // This barrier fixes a failing test
  2997. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  2998. threadgroup_barrier(mem_flags::mem_none);
  2999. float sum = simd_sum(lsum);
  3000. if (ntg > N_SIMDWIDTH) {
  3001. if (sgitg == 0) {
  3002. buf[tiisg] = 0.0f;
  3003. }
  3004. threadgroup_barrier(mem_flags::mem_threadgroup);
  3005. if (tiisg == 0) {
  3006. buf[sgitg] = sum;
  3007. }
  3008. threadgroup_barrier(mem_flags::mem_threadgroup);
  3009. sum = buf[tiisg];
  3010. sum = simd_sum(sum);
  3011. }
  3012. const float inv_sum = 1.0f/sum;
  3013. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  3014. pdst[i00] *= inv_sum;
  3015. }
  3016. }
  3017. template<typename T>
  3018. kernel void kernel_soft_max_4(
  3019. device const char * src0,
  3020. device const char * src1,
  3021. device char * dst,
  3022. constant int64_t & ne00,
  3023. constant int64_t & ne01,
  3024. constant int64_t & ne02,
  3025. constant float & scale,
  3026. constant float & max_bias,
  3027. constant float & m0,
  3028. constant float & m1,
  3029. constant uint32_t & n_head_log2,
  3030. threadgroup float * buf [[threadgroup(0)]],
  3031. uint tgpig[[threadgroup_position_in_grid]],
  3032. uint tpitg[[thread_position_in_threadgroup]],
  3033. uint sgitg[[simdgroup_index_in_threadgroup]],
  3034. uint tiisg[[thread_index_in_simdgroup]],
  3035. uint ntg[[threads_per_threadgroup]]) {
  3036. const int64_t i03 = (tgpig) / (ne02*ne01);
  3037. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  3038. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  3039. device const float4 * psrc4 = (device const float4 *) src0 + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  3040. device const T * pmask = src1 != src0 ? (device const T *) src1 + i01*ne00/4 : nullptr;
  3041. device float4 * pdst4 = (device float4 *) dst + (i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00)/4;
  3042. float slope = 1.0f;
  3043. if (max_bias > 0.0f) {
  3044. const int64_t h = i02;
  3045. const float base = h < n_head_log2 ? m0 : m1;
  3046. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  3047. slope = pow(base, exp);
  3048. }
  3049. // parallel max
  3050. float4 lmax4 = -INFINITY;
  3051. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3052. lmax4 = fmax(lmax4, psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f)));
  3053. }
  3054. const float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  3055. float max_val = simd_max(lmax);
  3056. if (ntg > N_SIMDWIDTH) {
  3057. if (sgitg == 0) {
  3058. buf[tiisg] = -INFINITY;
  3059. }
  3060. threadgroup_barrier(mem_flags::mem_threadgroup);
  3061. if (tiisg == 0) {
  3062. buf[sgitg] = max_val;
  3063. }
  3064. threadgroup_barrier(mem_flags::mem_threadgroup);
  3065. max_val = buf[tiisg];
  3066. max_val = simd_max(max_val);
  3067. }
  3068. // parallel sum
  3069. float4 lsum4 = 0.0f;
  3070. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3071. const float4 exp_psrc4 = exp((psrc4[i00]*scale + (float4)((pmask ? slope*pmask[i00] : 0.0f))) - max_val);
  3072. lsum4 += exp_psrc4;
  3073. pdst4[i00] = exp_psrc4;
  3074. }
  3075. const float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  3076. // This barrier fixes a failing test
  3077. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  3078. threadgroup_barrier(mem_flags::mem_none);
  3079. float sum = simd_sum(lsum);
  3080. if (ntg > N_SIMDWIDTH) {
  3081. if (sgitg == 0) {
  3082. buf[tiisg] = 0.0f;
  3083. }
  3084. threadgroup_barrier(mem_flags::mem_threadgroup);
  3085. if (tiisg == 0) {
  3086. buf[sgitg] = sum;
  3087. }
  3088. threadgroup_barrier(mem_flags::mem_threadgroup);
  3089. sum = buf[tiisg];
  3090. sum = simd_sum(sum);
  3091. }
  3092. const float inv_sum = 1.0f/sum;
  3093. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  3094. pdst4[i00] *= inv_sum;
  3095. }
  3096. }
  3097. typedef decltype(kernel_soft_max<float>) kernel_soft_max_t;
  3098. typedef decltype(kernel_soft_max_4<float4>) kernel_soft_max_4_t;
  3099. template [[host_name("kernel_soft_max_f16")]] kernel kernel_soft_max_t kernel_soft_max<half>;
  3100. template [[host_name("kernel_soft_max_f32")]] kernel kernel_soft_max_t kernel_soft_max<float>;
  3101. template [[host_name("kernel_soft_max_f16_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<half4>;
  3102. template [[host_name("kernel_soft_max_f32_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<float4>;
  3103. kernel void kernel_diag_mask_inf(
  3104. device const float * src0,
  3105. device float * dst,
  3106. constant int64_t & ne00,
  3107. constant int64_t & ne01,
  3108. constant int & n_past,
  3109. uint3 tpig[[thread_position_in_grid]]) {
  3110. const int64_t i02 = tpig[2];
  3111. const int64_t i01 = tpig[1];
  3112. const int64_t i00 = tpig[0];
  3113. if (i00 > n_past + i01) {
  3114. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  3115. } else {
  3116. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  3117. }
  3118. }
  3119. kernel void kernel_diag_mask_inf_8(
  3120. device const float4 * src0,
  3121. device float4 * dst,
  3122. constant int64_t & ne00,
  3123. constant int64_t & ne01,
  3124. constant int & n_past,
  3125. uint3 tpig[[thread_position_in_grid]]) {
  3126. const int64_t i = 2*tpig[0];
  3127. dst[i+0] = src0[i+0];
  3128. dst[i+1] = src0[i+1];
  3129. int64_t i4 = 4*i;
  3130. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  3131. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  3132. const int64_t i00 = i4;
  3133. for (int k = 3; k >= 0; --k) {
  3134. if (i00 + 4 + k <= n_past + i01) {
  3135. break;
  3136. }
  3137. dst[i+1][k] = -INFINITY;
  3138. if (i00 + k > n_past + i01) {
  3139. dst[i][k] = -INFINITY;
  3140. }
  3141. }
  3142. }
  3143. // ref: ggml.c:ggml_compute_forward_ssm_conv_f32
  3144. // TODO: optimize
  3145. kernel void kernel_ssm_conv_f32(
  3146. device const void * src0,
  3147. device const void * src1,
  3148. device float * dst,
  3149. constant int64_t & ne00,
  3150. constant int64_t & ne01,
  3151. constant int64_t & ne02,
  3152. constant uint64_t & nb00,
  3153. constant uint64_t & nb01,
  3154. constant uint64_t & nb02,
  3155. constant int64_t & ne10,
  3156. constant int64_t & ne11,
  3157. constant uint64_t & nb10,
  3158. constant uint64_t & nb11,
  3159. constant int64_t & ne0,
  3160. constant int64_t & ne1,
  3161. constant int64_t & ne2,
  3162. constant uint64_t & nb0,
  3163. constant uint64_t & nb1,
  3164. constant uint64_t & nb2,
  3165. uint3 tgpig[[threadgroup_position_in_grid]],
  3166. uint3 tpitg[[thread_position_in_threadgroup]],
  3167. uint3 ntg[[threads_per_threadgroup]]) {
  3168. const int64_t ir = tgpig.x;
  3169. const int64_t i2 = tgpig.y;
  3170. const int64_t i3 = tgpig.z;
  3171. const int64_t nc = ne10;
  3172. //const int64_t ncs = ne00;
  3173. //const int64_t nr = ne01;
  3174. //const int64_t n_t = ne1;
  3175. //const int64_t n_s = ne2;
  3176. device const float * s = (device const float *) ((device const char *) src0 + ir*nb01 + i2*nb00 + i3*nb02);
  3177. device const float * c = (device const float *) ((device const char *) src1 + ir*nb11);
  3178. device float * x = (device float *) ((device char *) dst + ir*nb0 + i2*nb1 + i3*nb2);
  3179. float sumf = 0.0f;
  3180. for (int64_t i0 = 0; i0 < nc; ++i0) {
  3181. sumf += s[i0] * c[i0];
  3182. }
  3183. x[0] = sumf;
  3184. }
  3185. // ref: ggml.c:ggml_compute_forward_ssm_scan_f32
  3186. // TODO: optimize
  3187. kernel void kernel_ssm_scan_f32(
  3188. device const void * src0,
  3189. device const void * src1,
  3190. device const void * src2,
  3191. device const void * src3,
  3192. device const void * src4,
  3193. device const void * src5,
  3194. device float * dst,
  3195. constant int64_t & d_state,
  3196. constant int64_t & d_inner,
  3197. constant int64_t & n_seq_tokens,
  3198. constant int64_t & n_seqs,
  3199. constant uint64_t & nb00,
  3200. constant uint64_t & nb01,
  3201. constant uint64_t & nb02,
  3202. constant uint64_t & nb10,
  3203. constant uint64_t & nb11,
  3204. constant uint64_t & nb12,
  3205. constant uint64_t & nb13,
  3206. constant uint64_t & nb20,
  3207. constant uint64_t & nb21,
  3208. constant uint64_t & nb22,
  3209. constant uint64_t & nb30,
  3210. constant uint64_t & nb31,
  3211. constant uint64_t & nb40,
  3212. constant uint64_t & nb41,
  3213. constant uint64_t & nb42,
  3214. constant uint64_t & nb50,
  3215. constant uint64_t & nb51,
  3216. constant uint64_t & nb52,
  3217. uint3 tgpig[[threadgroup_position_in_grid]],
  3218. uint3 tpitg[[thread_position_in_threadgroup]],
  3219. uint3 ntg[[threads_per_threadgroup]]) {
  3220. const int64_t ir = tgpig.x;
  3221. const int64_t i3 = tgpig.y;
  3222. const int64_t nc = d_state;
  3223. //const int64_t nr = d_inner;
  3224. const int64_t n_t = n_seq_tokens;
  3225. //const int64_t n_s = n_seqs;
  3226. for (int64_t i2 = 0; i2 < n_t; ++i2) {
  3227. device const float * s0 = (device const float *) ((device const char *) src0 + ir*nb01 + i3*nb02);
  3228. device const float * x = (device const float *) ((device const char *) src1 + ir*nb10 + i2*nb11 + i3*nb12);
  3229. device const float * dt = (device const float *) ((device const char *) src2 + ir*nb20 + i2*nb21 + i3*nb22);
  3230. device const float * A = (device const float *) ((device const char *) src3 + ir*nb31);
  3231. device const float * B = (device const float *) ((device const char *) src4 + i2*nb41 + i3*nb42);
  3232. device const float * C = (device const float *) ((device const char *) src5 + i2*nb51 + i3*nb52);
  3233. device float * y = (device float *) ((device char *) dst + ir*nb10 + i2*nb11 + i3*nb12); // TODO: do not use src1 strides
  3234. device float * s = (device float *) ((device char *) dst + ir*nb01 + i3*nb02 + nb13);
  3235. if (i2 > 0) {
  3236. s0 = s;
  3237. }
  3238. // i1 == 0
  3239. float dt_soft_plus = dt[0] <= 20.0f ? log(1.0f + exp(dt[0])) : dt[0];
  3240. float x_dt = x[0] * dt_soft_plus;
  3241. float sumf = 0.0f;
  3242. for (int64_t i0 = 0; i0 < nc; ++i0) {
  3243. int64_t i = i0;
  3244. float state = (s0[i] * exp(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  3245. sumf += state * C[i0];
  3246. s[i] = state;
  3247. }
  3248. y[0] = sumf;
  3249. }
  3250. }
  3251. kernel void kernel_argmax(
  3252. device const void * x,
  3253. device int32_t * dst,
  3254. constant int64_t & ncols,
  3255. constant uint64_t & nb01,
  3256. threadgroup float * shared_maxval [[threadgroup(0)]],
  3257. threadgroup int32_t * shared_argmax [[threadgroup(1)]],
  3258. uint tgpig[[threadgroup_position_in_grid]],
  3259. uint tpitg[[thread_position_in_threadgroup]],
  3260. uint sgitg[[simdgroup_index_in_threadgroup]],
  3261. uint tiisg[[thread_index_in_simdgroup]],
  3262. uint ntg[[threads_per_threadgroup]]) {
  3263. device const float * x_row = (device const float *) ((device const char *) x + tgpig * nb01);
  3264. float lmax = -INFINITY;
  3265. int32_t larg = -1;
  3266. for (int i00 = tpitg; i00 < ncols; i00 += ntg) {
  3267. if (x_row[i00] > lmax) {
  3268. lmax = x_row[i00];
  3269. larg = i00;
  3270. }
  3271. }
  3272. // find the argmax value in the block
  3273. float max_val = simd_max(lmax);
  3274. int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
  3275. if (ntg > N_SIMDWIDTH) {
  3276. if (sgitg == 0) {
  3277. shared_maxval[tiisg] = -INFINITY;
  3278. shared_argmax[tiisg] = -1;
  3279. }
  3280. threadgroup_barrier(mem_flags::mem_threadgroup);
  3281. if (tiisg == 0) {
  3282. shared_maxval[sgitg] = max_val;
  3283. shared_argmax[sgitg] = arg_val;
  3284. }
  3285. threadgroup_barrier(mem_flags::mem_threadgroup);
  3286. max_val = shared_maxval[tiisg];
  3287. arg_val = shared_argmax[tiisg];
  3288. float max_val_reduced = simd_max(max_val);
  3289. int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
  3290. dst[tgpig] = arg_val_reduced;
  3291. return;
  3292. }
  3293. dst[tgpig] = arg_val;
  3294. }
  3295. kernel void kernel_norm(
  3296. constant ggml_metal_kargs_norm & args,
  3297. device const char * src0,
  3298. device char * dst,
  3299. threadgroup float * shmem_f32 [[threadgroup(0)]],
  3300. uint tgpig[[threadgroup_position_in_grid]],
  3301. ushort tpitg[[thread_position_in_threadgroup]],
  3302. ushort sgitg[[simdgroup_index_in_threadgroup]],
  3303. ushort tiisg[[thread_index_in_simdgroup]],
  3304. ushort ntg[[threads_per_threadgroup]]) {
  3305. if (sgitg == 0) {
  3306. shmem_f32[tiisg] = 0.0f;
  3307. }
  3308. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  3309. float4 sumf4(0.0f);
  3310. float sumf = 0.0f;
  3311. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3312. sumf4 += x[i00];
  3313. }
  3314. sumf = sumf4[0] + sumf4[1] + sumf4[2] + sumf4[3];
  3315. sumf = simd_sum(sumf);
  3316. threadgroup_barrier(mem_flags::mem_threadgroup);
  3317. if (tiisg == 0) {
  3318. shmem_f32[sgitg] = sumf;
  3319. }
  3320. threadgroup_barrier(mem_flags::mem_threadgroup);
  3321. sumf = shmem_f32[tiisg];
  3322. sumf = simd_sum(sumf);
  3323. const float mean = sumf/args.ne00;
  3324. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  3325. sumf = 0.0f;
  3326. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3327. y[i00] = x[i00] - mean;
  3328. sumf += dot(y[i00], y[i00]);
  3329. }
  3330. sumf = simd_sum(sumf);
  3331. threadgroup_barrier(mem_flags::mem_threadgroup);
  3332. if (tiisg == 0) {
  3333. shmem_f32[sgitg] = sumf;
  3334. }
  3335. threadgroup_barrier(mem_flags::mem_threadgroup);
  3336. sumf = shmem_f32[tiisg];
  3337. sumf = simd_sum(sumf);
  3338. const float variance = sumf/args.ne00;
  3339. const float scale = 1.0f/sqrt(variance + args.eps);
  3340. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3341. y[i00] = y[i00] * scale;
  3342. }
  3343. }
  3344. kernel void kernel_rms_norm(
  3345. constant ggml_metal_kargs_rms_norm & args,
  3346. device const char * src0,
  3347. device char * dst,
  3348. threadgroup float * shmem_f32 [[threadgroup(0)]],
  3349. uint tgpig[[threadgroup_position_in_grid]],
  3350. ushort tpitg[[thread_position_in_threadgroup]],
  3351. ushort sgitg[[simdgroup_index_in_threadgroup]],
  3352. ushort tiisg[[thread_index_in_simdgroup]],
  3353. ushort ntg[[threads_per_threadgroup]]) {
  3354. if (sgitg == 0) {
  3355. shmem_f32[tiisg] = 0.0f;
  3356. }
  3357. device const float4 * x = (device const float4 *) (src0 + tgpig*args.nb01);
  3358. float sumf = 0.0f;
  3359. // parallel sum
  3360. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3361. sumf += dot(x[i00], x[i00]);
  3362. }
  3363. sumf = simd_sum(sumf);
  3364. threadgroup_barrier(mem_flags::mem_threadgroup);
  3365. if (tiisg == 0) {
  3366. shmem_f32[sgitg] = sumf;
  3367. }
  3368. threadgroup_barrier(mem_flags::mem_threadgroup);
  3369. sumf = shmem_f32[tiisg];
  3370. sumf = simd_sum(sumf);
  3371. const float mean = sumf/args.ne00;
  3372. const float scale = 1.0f/sqrt(mean + args.eps);
  3373. device float4 * y = (device float4 *) dst + tgpig*args.ne00_4;
  3374. for (int i00 = tpitg; i00 < args.ne00_4; i00 += ntg) {
  3375. y[i00] = x[i00] * scale;
  3376. }
  3377. }
  3378. kernel void kernel_group_norm(
  3379. device const float * src0,
  3380. device float * dst,
  3381. constant int64_t & ne00,
  3382. constant int64_t & ne01,
  3383. constant int64_t & ne02,
  3384. constant uint64_t & nb00,
  3385. constant uint64_t & nb01,
  3386. constant uint64_t & nb02,
  3387. constant int32_t & n_groups,
  3388. constant float & eps,
  3389. threadgroup float * buf [[threadgroup(0)]],
  3390. uint tgpig[[threadgroup_position_in_grid]],
  3391. uint tpitg[[thread_position_in_threadgroup]],
  3392. uint sgitg[[simdgroup_index_in_threadgroup]],
  3393. uint tiisg[[thread_index_in_simdgroup]],
  3394. uint ntg[[threads_per_threadgroup]]) {
  3395. const int64_t ne = ne00*ne01*ne02;
  3396. const int64_t gs = ne00*ne01*((ne02 + n_groups - 1) / n_groups);
  3397. int start = tgpig * gs;
  3398. int end = start + gs;
  3399. start += tpitg;
  3400. if (end >= ne) {
  3401. end = ne;
  3402. }
  3403. float tmp = 0.0f; // partial sum for thread in warp
  3404. for (int j = start; j < end; j += ntg) {
  3405. tmp += src0[j];
  3406. }
  3407. threadgroup_barrier(mem_flags::mem_threadgroup);
  3408. tmp = simd_sum(tmp);
  3409. if (ntg > N_SIMDWIDTH) {
  3410. if (sgitg == 0) {
  3411. buf[tiisg] = 0.0f;
  3412. }
  3413. threadgroup_barrier(mem_flags::mem_threadgroup);
  3414. if (tiisg == 0) {
  3415. buf[sgitg] = tmp;
  3416. }
  3417. threadgroup_barrier(mem_flags::mem_threadgroup);
  3418. tmp = buf[tiisg];
  3419. tmp = simd_sum(tmp);
  3420. }
  3421. const float mean = tmp / gs;
  3422. tmp = 0.0f;
  3423. for (int j = start; j < end; j += ntg) {
  3424. float xi = src0[j] - mean;
  3425. dst[j] = xi;
  3426. tmp += xi * xi;
  3427. }
  3428. tmp = simd_sum(tmp);
  3429. if (ntg > N_SIMDWIDTH) {
  3430. if (sgitg == 0) {
  3431. buf[tiisg] = 0.0f;
  3432. }
  3433. threadgroup_barrier(mem_flags::mem_threadgroup);
  3434. if (tiisg == 0) {
  3435. buf[sgitg] = tmp;
  3436. }
  3437. threadgroup_barrier(mem_flags::mem_threadgroup);
  3438. tmp = buf[tiisg];
  3439. tmp = simd_sum(tmp);
  3440. }
  3441. const float variance = tmp / gs;
  3442. const float scale = 1.0f/sqrt(variance + eps);
  3443. for (int j = start; j < end; j += ntg) {
  3444. dst[j] *= scale;
  3445. }
  3446. }
  3447. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  3448. // il indicates where the q4 quants begin (0 or QK4_0/4)
  3449. // we assume that the yl's have been multiplied with the appropriate scale factor
  3450. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3451. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  3452. float d = qb_curr->d;
  3453. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3454. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 1 + il/2);
  3455. for (int i = 0; i < 8; i += 2) {
  3456. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  3457. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  3458. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  3459. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  3460. }
  3461. return d * (sumy * -8.f + acc[0] + acc[1] + acc[2] + acc[3]);
  3462. }
  3463. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  3464. // il indicates where the q4 quants begin (0 or QK4_0/4)
  3465. // we assume that the yl's have been multiplied with the appropriate scale factor
  3466. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3467. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  3468. float d = qb_curr->d;
  3469. float m = qb_curr->m;
  3470. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3471. device const uint16_t * qs = ((device const uint16_t *) qb_curr + 2 + il/2);
  3472. for (int i = 0; i < 8; i+=2) {
  3473. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F);
  3474. acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00);
  3475. acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0);
  3476. acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000);
  3477. }
  3478. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  3479. }
  3480. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  3481. // il indicates where the q5 quants begin (0 or QK5_0/4)
  3482. // we assume that the yl's have been multiplied with the appropriate scale factor
  3483. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3484. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  3485. float d = qb_curr->d;
  3486. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3487. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  3488. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  3489. for (int i = 0; i < 8; i+=2) {
  3490. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  3491. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  3492. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  3493. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  3494. }
  3495. return d * (sumy * -16.f + acc[0] + acc[1] + acc[2] + acc[3]);
  3496. }
  3497. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  3498. // il indicates where the q5 quants begin (0 or QK5_1/4)
  3499. // we assume that the yl's have been multiplied with the appropriate scale factor
  3500. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  3501. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  3502. float d = qb_curr->d;
  3503. float m = qb_curr->m;
  3504. float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  3505. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  3506. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  3507. for (int i = 0; i < 8; i+=2) {
  3508. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010));
  3509. acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  3510. acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100));
  3511. acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  3512. }
  3513. return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m;
  3514. }
  3515. // putting them in the kernel cause a significant performance penalty
  3516. #define N_DST 4 // each SIMD group works on 4 rows
  3517. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  3518. //Note: This is a template, but strictly speaking it only applies to
  3519. // quantizations where the block size is 32. It also does not
  3520. // guard against the number of rows not being divisible by
  3521. // N_DST, so this is another explicit assumption of the implementation.
  3522. template<typename block_q_type, int nr, int nsg, int nw, typename args_t>
  3523. void mul_vec_q_n_f32_impl(
  3524. args_t args,
  3525. device const char * src0,
  3526. device const char * src1,
  3527. device char * dst,
  3528. threadgroup char * shmem,
  3529. uint3 tgpig,
  3530. ushort tiisg,
  3531. ushort sgitg) {
  3532. const int nb = args.ne00/QK4_0;
  3533. const int r0 = tgpig.x;
  3534. const int r1 = tgpig.y;
  3535. const int im = tgpig.z;
  3536. const int first_row = (r0 * nsg + sgitg) * nr;
  3537. const uint i12 = im%args.ne12;
  3538. const uint i13 = im/args.ne12;
  3539. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3540. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3541. //device const block_q_type * x = (device const block_q_type *) (src0 + offset0);
  3542. device const float * y = (device const float *) (src1 + offset1);
  3543. // pointers to src0 rows
  3544. device const block_q_type * ax[nr];
  3545. for (int row = 0; row < nr; ++row) {
  3546. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3547. ax[row] = (device const block_q_type *) ((device char *) src0 + offset0);
  3548. }
  3549. float yl[16]; // src1 vector cache
  3550. float sumf[nr] = {0.f};
  3551. const short ix = (tiisg/2);
  3552. const short il = (tiisg%2)*8;
  3553. device const float * yb = y + ix*QK4_0 + il;
  3554. // each thread in a SIMD group deals with half a block.
  3555. for (int ib = ix; ib < nb; ib += nw/2) {
  3556. float sumy[2] = { 0.f, 0.f };
  3557. #pragma unroll
  3558. for (int i = 0; i < 8; i += 2) {
  3559. sumy[0] += yb[i + 0] + yb[i + 1];
  3560. yl[i + 0] = yb[i + 0];
  3561. yl[i + 1] = yb[i + 1]/256.f;
  3562. sumy[1] += yb[i + 16] + yb[i + 17];
  3563. yl[i + 8] = yb[i + 16]/16.f;
  3564. yl[i + 9] = yb[i + 17]/4096.f;
  3565. }
  3566. #pragma unroll
  3567. for (int row = 0; row < nr; row++) {
  3568. sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy[0] + sumy[1], yl, il);
  3569. }
  3570. yb += QK4_0 * 16;
  3571. }
  3572. device float * dst_f32 = (device float *) dst + im*args.ne0*args.ne1 + r1*args.ne0;
  3573. for (int row = 0; row < nr; ++row) {
  3574. const float tot = simd_sum(sumf[row]);
  3575. if (tiisg == 0 && first_row + row < args.ne01) {
  3576. dst_f32[first_row + row] = tot;
  3577. }
  3578. }
  3579. }
  3580. kernel void kernel_mul_mv_q4_0_f32(
  3581. constant ggml_metal_kargs_mul_mv & args,
  3582. device const char * src0,
  3583. device const char * src1,
  3584. device char * dst,
  3585. uint3 tgpig[[threadgroup_position_in_grid]],
  3586. ushort tiisg[[thread_index_in_simdgroup]],
  3587. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3588. 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);
  3589. }
  3590. kernel void kernel_mul_mv_q4_1_f32(
  3591. constant ggml_metal_kargs_mul_mv & args,
  3592. device const char * src0,
  3593. device const char * src1,
  3594. device char * dst,
  3595. uint3 tgpig[[threadgroup_position_in_grid]],
  3596. ushort tiisg[[thread_index_in_simdgroup]],
  3597. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3598. 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);
  3599. }
  3600. kernel void kernel_mul_mv_q5_0_f32(
  3601. constant ggml_metal_kargs_mul_mv & args,
  3602. device const char * src0,
  3603. device const char * src1,
  3604. device char * dst,
  3605. uint3 tgpig[[threadgroup_position_in_grid]],
  3606. ushort tiisg[[thread_index_in_simdgroup]],
  3607. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3608. 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);
  3609. }
  3610. kernel void kernel_mul_mv_q5_1_f32(
  3611. constant ggml_metal_kargs_mul_mv & args,
  3612. device const char * src0,
  3613. device const char * src1,
  3614. device char * dst,
  3615. uint3 tgpig[[threadgroup_position_in_grid]],
  3616. ushort tiisg[[thread_index_in_simdgroup]],
  3617. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3618. 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);
  3619. }
  3620. #define NB_Q8_0 8
  3621. template<typename args_t>
  3622. void kernel_mul_mv_q8_0_f32_impl(
  3623. args_t args,
  3624. device const char * src0,
  3625. device const char * src1,
  3626. device char * dst,
  3627. threadgroup char * shmem,
  3628. uint3 tgpig,
  3629. ushort tiisg,
  3630. ushort sgitg) {
  3631. const int nr = N_DST;
  3632. const int nsg = N_SIMDGROUP;
  3633. const int nw = N_SIMDWIDTH;
  3634. const int nb = args.ne00/QK8_0;
  3635. const int r0 = tgpig.x;
  3636. const int r1 = tgpig.y;
  3637. const int im = tgpig.z;
  3638. const int first_row = (r0*nsg + sgitg)*nr;
  3639. const uint i12 = im%args.ne12;
  3640. const uint i13 = im/args.ne12;
  3641. //const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3642. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3643. //device const block_q8_0 * x = (device const block_q8_0 *) (src0 + offset0);
  3644. device const float * y = (device const float *) (src1 + offset1);
  3645. // pointers to src0 rows
  3646. device const block_q8_0 * ax[nr];
  3647. for (int row = 0; row < nr; ++row) {
  3648. const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3649. ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
  3650. }
  3651. float yl[NB_Q8_0];
  3652. float sumf[nr] = { 0.f };
  3653. const short ix = tiisg/4;
  3654. const short il = tiisg%4;
  3655. device const float * yb = y + ix*QK8_0 + il*NB_Q8_0;
  3656. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  3657. for (int ib = ix; ib < nb; ib += nw/4) {
  3658. for (short i = 0; i < NB_Q8_0; ++i) {
  3659. yl[i] = yb[i];
  3660. }
  3661. for (int row = 0; row < nr; row++) {
  3662. device const int8_t * qs = ax[row][ib].qs + il*NB_Q8_0;
  3663. float sumq = 0.f;
  3664. for (short iq = 0; iq < NB_Q8_0; ++iq) {
  3665. sumq += qs[iq] * yl[iq];
  3666. }
  3667. sumf[row] += sumq*ax[row][ib].d;
  3668. }
  3669. yb += nw*NB_Q8_0;
  3670. }
  3671. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  3672. for (int row = 0; row < nr; ++row) {
  3673. const float tot = simd_sum(sumf[row]);
  3674. if (tiisg == 0 && first_row + row < args.ne01) {
  3675. dst_f32[first_row + row] = tot;
  3676. }
  3677. }
  3678. }
  3679. [[host_name("kernel_mul_mv_q8_0_f32")]]
  3680. kernel void kernel_mul_mv_q8_0_f32(
  3681. constant ggml_metal_kargs_mul_mv & args,
  3682. device const char * src0,
  3683. device const char * src1,
  3684. device char * dst,
  3685. uint3 tgpig[[threadgroup_position_in_grid]],
  3686. ushort tiisg[[thread_index_in_simdgroup]],
  3687. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3688. kernel_mul_mv_q8_0_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  3689. }
  3690. // mat-vec kernel processing in chunks of float4
  3691. // chpb - chunks per quantization block
  3692. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4)(device const q_t *, short, thread float4 &) >
  3693. void kernel_mul_mv_ext_q4_f32_impl(
  3694. constant ggml_metal_kargs_mul_mv_ext & args,
  3695. device const char * src0,
  3696. device const char * src1,
  3697. device char * dst,
  3698. uint3 tgpig[[threadgroup_position_in_grid]],
  3699. ushort tiisg[[thread_index_in_simdgroup]],
  3700. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3701. const short chpt = 4; // chunks per thread
  3702. //const short nxpsg = (32);
  3703. const short nypsg = (32/nxpsg);
  3704. const short tx = tiisg%nxpsg;
  3705. const short ty = tiisg/nxpsg;
  3706. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  3707. const int i11 = tgpig.y*r1ptg;
  3708. const int i1m = tgpig.z;
  3709. const int i12 = i1m%args.ne12;
  3710. const int i13 = i1m/args.ne12;
  3711. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3712. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3713. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  3714. device const float4 * y4[r1ptg];
  3715. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  3716. y4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4 *) src1;
  3717. }
  3718. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  3719. short cch = tx%chpb; // current chunk index
  3720. for (int ich = tx; 4*ich < args.ne00; ich += chpt*nxpsg) {
  3721. float4 lx[chpt];
  3722. #pragma unroll(chpt)
  3723. for (short ch = 0; ch < chpt; ++ch) {
  3724. deq_t4(xq, cch, lx[ch]);
  3725. cch += nxpsg;
  3726. if (cch >= chpb) {
  3727. xq += cch/chpb;
  3728. cch %= chpb;
  3729. }
  3730. }
  3731. #pragma unroll(chpt)
  3732. for (short ch = 0; ch < chpt; ++ch) {
  3733. #pragma unroll(r1ptg)
  3734. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3735. sumf[ir1] += dot(lx[ch], y4[ir1][ch*nxpsg]);
  3736. }
  3737. }
  3738. #pragma unroll(r1ptg)
  3739. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3740. y4[ir1] += chpt*nxpsg;
  3741. }
  3742. }
  3743. // reduce only the threads in each row
  3744. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3745. if (nxpsg >= 32) {
  3746. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  3747. }
  3748. if (nxpsg >= 16) {
  3749. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  3750. }
  3751. if (nxpsg >= 8) {
  3752. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  3753. }
  3754. if (nxpsg >= 4) {
  3755. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  3756. }
  3757. if (nxpsg >= 2) {
  3758. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  3759. }
  3760. //sumf[ir1] = simd_sum(sumf[ir1]);
  3761. }
  3762. if (tx == 0) {
  3763. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  3764. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  3765. if (i01 < args.ne01) {
  3766. dst_f32[i01] = sumf[ir1];
  3767. }
  3768. }
  3769. }
  3770. }
  3771. // mat-vec kernel processing in chunks of float4x4
  3772. template<short nxpsg, short r1ptg, typename q_t, short chpb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &) >
  3773. void kernel_mul_mv_ext_q4x4_f32_impl(
  3774. constant ggml_metal_kargs_mul_mv_ext & args,
  3775. device const char * src0,
  3776. device const char * src1,
  3777. device char * dst,
  3778. uint3 tgpig[[threadgroup_position_in_grid]],
  3779. ushort tiisg[[thread_index_in_simdgroup]],
  3780. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3781. const short chpt = 1;
  3782. //const short nxpsg = (32);
  3783. const short nypsg = (32/nxpsg);
  3784. const short tx = tiisg%nxpsg;
  3785. const short ty = tiisg/nxpsg;
  3786. const int i01 = tgpig.x*(nypsg*args.nsg) + nypsg*sgitg + ty;
  3787. const int i11 = tgpig.y*r1ptg;
  3788. const int i1m = tgpig.z;
  3789. const int i12 = i1m%args.ne12;
  3790. const int i13 = i1m/args.ne12;
  3791. const uint64_t offset0 = i01*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3792. const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3793. device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0;
  3794. device const float4x4 * y4x4[r1ptg];
  3795. for (int ir1 = 0; ir1 < r1ptg; ++ir1) {
  3796. y4x4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4x4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4x4 *) src1;
  3797. }
  3798. float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f };
  3799. short cch = tx%chpb;
  3800. for (int ich = tx; 16*ich < args.ne00; ich += chpt*nxpsg) {
  3801. float4x4 lx[chpt];
  3802. #pragma unroll(chpt)
  3803. for (short ch = 0; ch < chpt; ++ch) {
  3804. deq_t4x4(xq, cch, lx[ch]);
  3805. cch += nxpsg;
  3806. if (cch >= chpb) {
  3807. xq += cch/chpb;
  3808. cch %= chpb;
  3809. }
  3810. }
  3811. #pragma unroll(chpt)
  3812. for (short ch = 0; ch < chpt; ++ch) {
  3813. #pragma unroll(r1ptg)
  3814. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3815. sumf[ir1] +=
  3816. dot(lx[ch][0], y4x4[ir1][ch*nxpsg][0]) +
  3817. dot(lx[ch][1], y4x4[ir1][ch*nxpsg][1]) +
  3818. dot(lx[ch][2], y4x4[ir1][ch*nxpsg][2]) +
  3819. dot(lx[ch][3], y4x4[ir1][ch*nxpsg][3]);
  3820. }
  3821. }
  3822. #pragma unroll(r1ptg)
  3823. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3824. y4x4[ir1] += chpt*nxpsg;
  3825. }
  3826. }
  3827. for (short ir1 = 0; ir1 < r1ptg; ++ir1) {
  3828. if (nxpsg >= 32) {
  3829. sumf[ir1] += simd_shuffle_down(sumf[ir1], 16);
  3830. }
  3831. if (nxpsg >= 16) {
  3832. sumf[ir1] += simd_shuffle_down(sumf[ir1], 8);
  3833. }
  3834. if (nxpsg >= 8) {
  3835. sumf[ir1] += simd_shuffle_down(sumf[ir1], 4);
  3836. }
  3837. if (nxpsg >= 4) {
  3838. sumf[ir1] += simd_shuffle_down(sumf[ir1], 2);
  3839. }
  3840. if (nxpsg >= 2) {
  3841. sumf[ir1] += simd_shuffle_down(sumf[ir1], 1);
  3842. }
  3843. //sumf[ir1] = simd_sum(sumf[ir1]);
  3844. }
  3845. if (tx == 0) {
  3846. for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) {
  3847. device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0;
  3848. if (i01 < args.ne01) {
  3849. dst_f32[i01] = sumf[ir1];
  3850. }
  3851. }
  3852. }
  3853. }
  3854. // dispatchers needed for compile-time nxpsg
  3855. // epb - elements per quantization block
  3856. template<short r1ptg, typename q_t, short epb, void (*deq_t4)(device const q_t *, short, thread float4 &)>
  3857. kernel void kernel_mul_mv_ext_q4_f32_disp(
  3858. constant ggml_metal_kargs_mul_mv_ext & args,
  3859. device const char * src0,
  3860. device const char * src1,
  3861. device char * dst,
  3862. uint3 tgpig[[threadgroup_position_in_grid]],
  3863. ushort tiisg[[thread_index_in_simdgroup]],
  3864. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3865. switch (args.nxpsg) {
  3866. 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;
  3867. 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;
  3868. 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;
  3869. 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;
  3870. }
  3871. }
  3872. template<short r1ptg, typename q_t, short epb, void (*deq_t4x4)(device const q_t *, short, thread float4x4 &)>
  3873. kernel void kernel_mul_mv_ext_q4x4_f32_disp(
  3874. constant ggml_metal_kargs_mul_mv_ext & args,
  3875. device const char * src0,
  3876. device const char * src1,
  3877. device char * dst,
  3878. uint3 tgpig[[threadgroup_position_in_grid]],
  3879. ushort tiisg[[thread_index_in_simdgroup]],
  3880. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  3881. switch (args.nxpsg) {
  3882. 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;
  3883. 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;
  3884. 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;
  3885. 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;
  3886. }
  3887. }
  3888. typedef decltype(kernel_mul_mv_ext_q4_f32_disp <2, block_q8_0, 32, dequantize_q8_0_t4>) mul_mv_ext_q4_f32_t;
  3889. typedef decltype(kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>) mul_mv_ext_q4x4_f32_t;
  3890. 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>;
  3891. 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>;
  3892. 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>;
  3893. 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>;
  3894. 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>;
  3895. 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>;
  3896. 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>;
  3897. 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>;
  3898. 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>;
  3899. 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>;
  3900. 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>;
  3901. 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>;
  3902. 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>;
  3903. 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>;
  3904. 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>;
  3905. 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>;
  3906. 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>;
  3907. 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>;
  3908. 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>;
  3909. 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>;
  3910. 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>;
  3911. 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>;
  3912. 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>;
  3913. 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>;
  3914. 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>;
  3915. 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>;
  3916. 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>;
  3917. 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>;
  3918. 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>;
  3919. 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>;
  3920. 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>;
  3921. 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>;
  3922. 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>;
  3923. 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>;
  3924. 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>;
  3925. 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>;
  3926. 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>;
  3927. 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>;
  3928. 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>;
  3929. 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>;
  3930. #define N_MV_T_T 4
  3931. template<typename T0, typename T04, typename T1, typename T14, typename args_t>
  3932. void kernel_mul_mv_impl(
  3933. args_t args,
  3934. device const char * src0,
  3935. device const char * src1,
  3936. device char * dst,
  3937. uint3 tgpig,
  3938. ushort tiisg) {
  3939. const int r0 = tgpig.x;
  3940. const int rb = tgpig.y*N_MV_T_T;
  3941. const int im = tgpig.z;
  3942. const uint i12 = im%args.ne12;
  3943. const uint i13 = im/args.ne12;
  3944. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  3945. device const T0 * x = (device const T0 *) (src0 + offset0);
  3946. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  3947. if (args.ne00 < 128) {
  3948. for (int row = 0; row < N_MV_T_T; ++row) {
  3949. int r1 = rb + row;
  3950. if (r1 >= args.ne11) {
  3951. break;
  3952. }
  3953. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3954. device const T1 * y = (device const T1 *) (src1 + offset1);
  3955. float sumf = 0;
  3956. for (int i = tiisg; i < args.ne00; i += 32) {
  3957. sumf += (T0) x[i] * (T1) y[i];
  3958. }
  3959. float all_sum = simd_sum(sumf);
  3960. if (tiisg == 0) {
  3961. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  3962. }
  3963. }
  3964. } else {
  3965. device const T04 * x4 = (device const T04 *) x;
  3966. for (int row = 0; row < N_MV_T_T; ++row) {
  3967. int r1 = rb + row;
  3968. if (r1 >= args.ne11) {
  3969. break;
  3970. }
  3971. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  3972. device const T1 * y = (device const T1 *) (src1 + offset1);
  3973. device const T14 * y4 = (device const T14 *) y;
  3974. float sumf = 0;
  3975. for (int i = tiisg; i < args.ne00/4; i += 32) {
  3976. sumf += dot((float4) x4[i], (float4) y4[i]);
  3977. }
  3978. float all_sum = simd_sum(sumf);
  3979. if (tiisg == 0) {
  3980. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  3981. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  3982. }
  3983. }
  3984. }
  3985. }
  3986. template<typename T0, typename T04, typename T1, typename T14>
  3987. kernel void kernel_mul_mv(
  3988. constant ggml_metal_kargs_mul_mv & args,
  3989. device const char * src0,
  3990. device const char * src1,
  3991. device char * dst,
  3992. uint3 tgpig[[threadgroup_position_in_grid]],
  3993. ushort tiisg[[thread_index_in_simdgroup]]) {
  3994. kernel_mul_mv_impl<T0, T04, T1, T14, constant ggml_metal_kargs_mul_mv &>(
  3995. args,
  3996. src0,
  3997. src1,
  3998. dst,
  3999. tgpig,
  4000. tiisg);
  4001. }
  4002. typedef decltype(kernel_mul_mv<half, half4, half, half4>) mul_mv_t;
  4003. template [[host_name("kernel_mul_mv_f32_f32")]] kernel mul_mv_t kernel_mul_mv<float, float4, float, float4>;
  4004. template [[host_name("kernel_mul_mv_f16_f32")]] kernel mul_mv_t kernel_mul_mv<half, half4, float, float4>;
  4005. template [[host_name("kernel_mul_mv_f16_f16")]] kernel mul_mv_t kernel_mul_mv<half, half4, half, half4>;
  4006. #if defined(GGML_METAL_USE_BF16)
  4007. template [[host_name("kernel_mul_mv_bf16_f32")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, float, float4>;
  4008. template [[host_name("kernel_mul_mv_bf16_bf16")]] kernel mul_mv_t kernel_mul_mv<bfloat, bfloat4, bfloat, bfloat4>;
  4009. #endif
  4010. template<typename T, typename T4>
  4011. kernel void kernel_mul_mv_1row(
  4012. constant ggml_metal_kargs_mul_mv & args,
  4013. device const char * src0,
  4014. device const char * src1,
  4015. device char * dst,
  4016. uint3 tgpig[[threadgroup_position_in_grid]],
  4017. ushort tiisg[[thread_index_in_simdgroup]]) {
  4018. const int r0 = tgpig.x;
  4019. const int r1 = tgpig.y;
  4020. const int im = tgpig.z;
  4021. const uint i12 = im%args.ne12;
  4022. const uint i13 = im/args.ne12;
  4023. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4024. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4025. device const T * x = (device const T *) (src0 + offset0);
  4026. device const float * y = (device const float *) (src1 + offset1);
  4027. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  4028. float sumf = 0;
  4029. if (args.ne00 < 128) {
  4030. for (int i = tiisg; i < args.ne00; i += 32) {
  4031. sumf += (float) x[i] * (float) y[i];
  4032. }
  4033. float all_sum = simd_sum(sumf);
  4034. if (tiisg == 0) {
  4035. dst_f32[r0] = all_sum;
  4036. }
  4037. } else {
  4038. device const T4 * x4 = (device const T4 *) x;
  4039. device const float4 * y4 = (device const float4 *) y;
  4040. for (int i = tiisg; i < args.ne00/4; i += 32) {
  4041. sumf += dot((float4) x4[i], y4[i]);
  4042. }
  4043. float all_sum = simd_sum(sumf);
  4044. if (tiisg == 0) {
  4045. for (int i = 4*(args.ne00/4); i < args.ne00; ++i) all_sum += (float) (x[i] * y[i]);
  4046. dst_f32[r0] = all_sum;
  4047. }
  4048. }
  4049. }
  4050. typedef decltype(kernel_mul_mv_1row<half, half4>) mul_mv_1row_t;
  4051. template [[host_name("kernel_mul_mv_f16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<half, half4>;
  4052. #if defined(GGML_METAL_USE_BF16)
  4053. template [[host_name("kernel_mul_mv_bf16_f32_1row")]] kernel mul_mv_1row_t kernel_mul_mv_1row<bfloat, bfloat4>;
  4054. #endif
  4055. // Assumes row size (ne00) is a multiple of 4
  4056. template<typename T, typename T4>
  4057. kernel void kernel_mul_mv_l4(
  4058. constant ggml_metal_kargs_mul_mv & args,
  4059. device const char * src0,
  4060. device const char * src1,
  4061. device char * dst,
  4062. uint3 tgpig[[threadgroup_position_in_grid]],
  4063. ushort tiisg[[thread_index_in_simdgroup]]) {
  4064. const int nrows = args.ne11;
  4065. const int r0 = tgpig.x;
  4066. const int im = tgpig.z;
  4067. const uint i12 = im%args.ne12;
  4068. const uint i13 = im/args.ne12;
  4069. const uint64_t offset0 = r0*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  4070. device const T4 * x4 = (device const T4 *) (src0 + offset0);
  4071. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1;
  4072. for (int r1 = 0; r1 < nrows; ++r1) {
  4073. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  4074. device const float4 * y4 = (device const float4 *) (src1 + offset1);
  4075. float sumf = 0;
  4076. for (int i = tiisg; i < args.ne00/4; i += 32) {
  4077. sumf += dot((float4) x4[i], y4[i]);
  4078. }
  4079. float all_sum = simd_sum(sumf);
  4080. if (tiisg == 0) {
  4081. dst_f32[(uint64_t)r1*args.ne0 + r0] = all_sum;
  4082. }
  4083. }
  4084. }
  4085. typedef decltype(kernel_mul_mv_l4<half, half4>) mul_mv_l4_t;
  4086. template [[host_name("kernel_mul_mv_f16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<half, half4>;
  4087. #if defined(GGML_METAL_USE_BF16)
  4088. template [[host_name("kernel_mul_mv_bf16_f32_l4")]] kernel mul_mv_l4_t kernel_mul_mv_l4<bfloat, bfloat4>;
  4089. #endif
  4090. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  4091. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4092. return 1.0f - min(1.0f, max(0.0f, y));
  4093. }
  4094. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4095. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4096. static void rope_yarn(
  4097. float theta_extrap, float freq_scale, float corr_dims[2], int i0, float ext_factor, float mscale,
  4098. thread float * cos_theta, thread float * sin_theta) {
  4099. // Get n-d rotational scaling corrected for extrapolation
  4100. float theta_interp = freq_scale * theta_extrap;
  4101. float theta = theta_interp;
  4102. if (ext_factor != 0.0f) {
  4103. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  4104. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4105. // Get n-d magnitude scaling corrected for interpolation
  4106. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  4107. }
  4108. *cos_theta = cos(theta) * mscale;
  4109. *sin_theta = sin(theta) * mscale;
  4110. }
  4111. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  4112. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  4113. static float rope_yarn_corr_factor(int n_dims, int n_ctx_orig, float n_rot, float base) {
  4114. return n_dims * log(n_ctx_orig / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  4115. }
  4116. static void rope_yarn_corr_dims(
  4117. int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]
  4118. ) {
  4119. // start and end correction dims
  4120. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_fast, freq_base)));
  4121. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_slow, freq_base)));
  4122. }
  4123. template<typename T>
  4124. kernel void kernel_rope_norm(
  4125. constant ggml_metal_kargs_rope & args,
  4126. device const char * src0,
  4127. device const char * src1,
  4128. device const char * src2,
  4129. device char * dst,
  4130. ushort tiitg[[thread_index_in_threadgroup]],
  4131. ushort3 tptg [[threads_per_threadgroup]],
  4132. uint3 tgpig[[threadgroup_position_in_grid]]) {
  4133. const int i3 = tgpig[2];
  4134. const int i2 = tgpig[1];
  4135. const int i1 = tgpig[0];
  4136. float corr_dims[2];
  4137. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  4138. device const int32_t * pos = (device const int32_t *) src1;
  4139. const float theta_base = (float) pos[i2];
  4140. const float inv_ndims = -1.f/args.n_dims;
  4141. float cos_theta;
  4142. float sin_theta;
  4143. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  4144. if (i0 < args.n_dims) {
  4145. const int ic = i0/2;
  4146. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  4147. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  4148. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  4149. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4150. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4151. const float x0 = src[0];
  4152. const float x1 = src[1];
  4153. dst_data[0] = x0*cos_theta - x1*sin_theta;
  4154. dst_data[1] = x0*sin_theta + x1*cos_theta;
  4155. } else {
  4156. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4157. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4158. dst_data[0] = src[0];
  4159. dst_data[1] = src[1];
  4160. }
  4161. }
  4162. }
  4163. template<typename T>
  4164. kernel void kernel_rope_neox(
  4165. constant ggml_metal_kargs_rope & args,
  4166. device const char * src0,
  4167. device const char * src1,
  4168. device const char * src2,
  4169. device char * dst,
  4170. ushort tiitg[[thread_index_in_threadgroup]],
  4171. ushort3 tptg [[threads_per_threadgroup]],
  4172. uint3 tgpig[[threadgroup_position_in_grid]]) {
  4173. const int i3 = tgpig[2];
  4174. const int i2 = tgpig[1];
  4175. const int i1 = tgpig[0];
  4176. float corr_dims[2];
  4177. rope_yarn_corr_dims(args.n_dims, args.n_ctx_orig, args.freq_base, args.beta_fast, args.beta_slow, corr_dims);
  4178. device const int32_t * pos = (device const int32_t *) src1;
  4179. const float theta_base = (float) pos[i2];
  4180. const float inv_ndims = -1.f/args.n_dims;
  4181. float cos_theta;
  4182. float sin_theta;
  4183. for (int i0 = 2*tiitg; i0 < args.ne0; i0 += 2*tptg.x) {
  4184. if (i0 < args.n_dims) {
  4185. const int ic = i0/2;
  4186. const float theta = theta_base * pow(args.freq_base, inv_ndims*i0);
  4187. const float freq_factor = src2 != src0 ? ((device const float *) src2)[ic] : 1.0f;
  4188. rope_yarn(theta/freq_factor, args.freq_scale, corr_dims, i0, args.ext_factor, args.attn_factor, &cos_theta, &sin_theta);
  4189. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + ic*args.nb00);
  4190. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + ic*args.nb0);
  4191. const float x0 = src[0];
  4192. const float x1 = src[args.n_dims/2];
  4193. dst_data[0] = x0*cos_theta - x1*sin_theta;
  4194. dst_data[args.n_dims/2] = x0*sin_theta + x1*cos_theta;
  4195. } else {
  4196. device const T * const src = (device T *)(src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01 + i0*args.nb00);
  4197. device T * dst_data = (device T *)( dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  4198. dst_data[0] = src[0];
  4199. dst_data[1] = src[1];
  4200. }
  4201. }
  4202. }
  4203. typedef decltype(kernel_rope_norm<float>) kernel_rope_norm_t;
  4204. typedef decltype(kernel_rope_neox<float>) kernel_rope_neox_t;
  4205. template [[host_name("kernel_rope_norm_f32")]] kernel kernel_rope_norm_t kernel_rope_norm<float>;
  4206. template [[host_name("kernel_rope_norm_f16")]] kernel kernel_rope_norm_t kernel_rope_norm<half>;
  4207. template [[host_name("kernel_rope_neox_f32")]] kernel kernel_rope_neox_t kernel_rope_neox<float>;
  4208. template [[host_name("kernel_rope_neox_f16")]] kernel kernel_rope_neox_t kernel_rope_neox<half>;
  4209. typedef void (im2col_t)(
  4210. device const float * x,
  4211. device char * dst,
  4212. constant int32_t & ofs0,
  4213. constant int32_t & ofs1,
  4214. constant int32_t & IW,
  4215. constant int32_t & IH,
  4216. constant int32_t & CHW,
  4217. constant int32_t & s0,
  4218. constant int32_t & s1,
  4219. constant int32_t & p0,
  4220. constant int32_t & p1,
  4221. constant int32_t & d0,
  4222. constant int32_t & d1,
  4223. uint3 tgpig[[threadgroup_position_in_grid]],
  4224. uint3 tgpg[[threadgroups_per_grid]],
  4225. uint3 tpitg[[thread_position_in_threadgroup]],
  4226. uint3 ntg[[threads_per_threadgroup]]);
  4227. template <typename T>
  4228. kernel void kernel_im2col(
  4229. device const float * x,
  4230. device char * dst,
  4231. constant int32_t & ofs0,
  4232. constant int32_t & ofs1,
  4233. constant int32_t & IW,
  4234. constant int32_t & IH,
  4235. constant int32_t & CHW,
  4236. constant int32_t & s0,
  4237. constant int32_t & s1,
  4238. constant int32_t & p0,
  4239. constant int32_t & p1,
  4240. constant int32_t & d0,
  4241. constant int32_t & d1,
  4242. uint3 tgpig[[threadgroup_position_in_grid]],
  4243. uint3 tgpg[[threadgroups_per_grid]],
  4244. uint3 tpitg[[thread_position_in_threadgroup]],
  4245. uint3 ntg[[threads_per_threadgroup]]) {
  4246. // const int64_t IC = tgpg[0];
  4247. const int64_t OH = tgpg[1];
  4248. const int64_t OW = tgpg[2];
  4249. // const int64_t N = ntg[0];
  4250. const int64_t KH = ntg[1];
  4251. const int64_t KW = ntg[2];
  4252. const int64_t in = tpitg[0];
  4253. const int64_t ikh = tpitg[1];
  4254. const int64_t ikw = tpitg[2];
  4255. const int64_t iic = tgpig[0];
  4256. const int64_t ioh = tgpig[1];
  4257. const int64_t iow = tgpig[2];
  4258. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  4259. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  4260. const int64_t offset_dst = (in*OH*OW + ioh*OW + iow)*CHW + (iic*(KH*KW) + ikh*KW + ikw);
  4261. device T * pdst = (device T *) (dst);
  4262. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4263. pdst[offset_dst] = 0.0f;
  4264. } else {
  4265. const int64_t offset_src = in*ofs0 + iic*ofs1 + iih*IW + iiw;
  4266. pdst[offset_dst] = x[offset_src];
  4267. }
  4268. }
  4269. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  4270. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  4271. typedef void (im2col_ext_t)(
  4272. device const float * x,
  4273. device char * dst,
  4274. constant int32_t & ofs0,
  4275. constant int32_t & ofs1,
  4276. constant int32_t & IW,
  4277. constant int32_t & IH,
  4278. constant int32_t & CHW,
  4279. constant int32_t & s0,
  4280. constant int32_t & s1,
  4281. constant int32_t & p0,
  4282. constant int32_t & p1,
  4283. constant int32_t & d0,
  4284. constant int32_t & d1,
  4285. constant int32_t & N,
  4286. constant int32_t & KH,
  4287. constant int32_t & KW,
  4288. uint3 tgpig[[threadgroup_position_in_grid]],
  4289. uint3 tgpg[[threadgroups_per_grid]],
  4290. uint3 tpitg[[thread_position_in_threadgroup]],
  4291. uint3 ntg[[threads_per_threadgroup]]);
  4292. template <typename T>
  4293. kernel void kernel_im2col_ext(
  4294. device const float * x,
  4295. device char * dst,
  4296. constant int32_t & ofs0,
  4297. constant int32_t & ofs1,
  4298. constant int32_t & IW,
  4299. constant int32_t & IH,
  4300. constant int32_t & CHW,
  4301. constant int32_t & s0,
  4302. constant int32_t & s1,
  4303. constant int32_t & p0,
  4304. constant int32_t & p1,
  4305. constant int32_t & d0,
  4306. constant int32_t & d1,
  4307. constant int32_t & N,
  4308. constant int32_t & KH,
  4309. constant int32_t & KW,
  4310. uint3 tgpig[[threadgroup_position_in_grid]],
  4311. uint3 tgpg[[threadgroups_per_grid]], // tgpg[0] = D x IC x KH x KW, CHW = IC x KH x KW
  4312. uint3 tpitg[[thread_position_in_threadgroup]],
  4313. uint3 ntg[[threads_per_threadgroup]]) { // [M, 1, 1]
  4314. const int64_t KHW = KH * KW; // KHW == ntg[1] * ntg[2], KW == ntg[2]
  4315. const int64_t d = tgpig[0] / CHW;
  4316. const int64_t chw = tgpig[0] % CHW;
  4317. const int64_t tgpig_0 = chw / KHW; // 0 ~ (IC - 1)
  4318. const int64_t HW = tgpig[0] % KHW;
  4319. const int64_t tpitg_0 = (d * ntg[0]) + tpitg[0];
  4320. if (tpitg_0 >= N) {
  4321. return;
  4322. }
  4323. const int64_t tpitg_1 = HW / KW;
  4324. const int64_t tpitg_2 = HW % KW;
  4325. const int64_t iiw = tgpig[2] * s0 + tpitg_2 * d0 - p0;
  4326. const int64_t iih = tgpig[1] * s1 + tpitg_1 * d1 - p1;
  4327. const int64_t offset_dst =
  4328. (tpitg_0 * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  4329. (tgpig_0 * KHW + tpitg_1 * KW + tpitg_2);
  4330. device T * pdst = (device T *) (dst);
  4331. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4332. pdst[offset_dst] = 0.0f;
  4333. } else {
  4334. const int64_t offset_src = tpitg_0 * ofs0 + tgpig_0 * ofs1;
  4335. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  4336. }
  4337. }
  4338. template [[host_name("kernel_im2col_ext_f32")]] kernel im2col_ext_t kernel_im2col_ext<float>;
  4339. template [[host_name("kernel_im2col_ext_f16")]] kernel im2col_ext_t kernel_im2col_ext<half>;
  4340. typedef void (conv_transpose_1d_t)(
  4341. device const float * src0,
  4342. device const float * src1,
  4343. device char * dst,
  4344. constant int32_t & IC,
  4345. constant int32_t & IL,
  4346. constant int32_t & K,
  4347. constant int32_t & s0,
  4348. constant uint64_t & nb0,
  4349. constant uint64_t & nb1,
  4350. uint3 tgpig[[threadgroup_position_in_grid]],
  4351. uint3 tgpg[[threadgroups_per_grid]]);
  4352. template <typename T>
  4353. kernel void kernel_conv_transpose_1d(
  4354. device const T * src0,
  4355. device const float * src1,
  4356. device char * dst,
  4357. constant int32_t & IC,
  4358. constant int32_t & IL,
  4359. constant int32_t & K,
  4360. constant int32_t & s0,
  4361. constant uint64_t & nb0,
  4362. constant uint64_t & nb1,
  4363. uint3 tgpig[[threadgroup_position_in_grid]],
  4364. uint3 tgpg[[threadgroups_per_grid]]) {
  4365. float v = 0.0f;
  4366. for (int64_t c = 0; c < IC; c++) {
  4367. const int32_t kernel_offset = c * tgpg[1] * K + K * tgpig[1];
  4368. const int32_t input_offset = c * IL;
  4369. for (int64_t i = 0; i < IL; i++) {
  4370. if (tgpig[0] >= i * s0 && tgpig[0] < i * s0 + K) {
  4371. v += src0[kernel_offset + tgpig[0] - i * s0] * src1[input_offset + i];
  4372. }
  4373. }
  4374. }
  4375. device float * dst_ptr = (device float *) (dst + tgpig[0] * nb0 + tgpig[1] * nb1);
  4376. dst_ptr[0] = v;
  4377. }
  4378. template [[host_name("kernel_conv_transpose_1d_f32_f32")]]
  4379. kernel void kernel_conv_transpose_1d<float>(
  4380. device const float * src0,
  4381. device const float * src1,
  4382. device char * dst,
  4383. constant int32_t & IC,
  4384. constant int32_t & IL,
  4385. constant int32_t & K,
  4386. constant int32_t & s0,
  4387. constant uint64_t & nb0,
  4388. constant uint64_t & nb1,
  4389. uint3 tgpig[[threadgroup_position_in_grid]],
  4390. uint3 tgpg[[threadgroups_per_grid]]);
  4391. template [[host_name("kernel_conv_transpose_1d_f16_f32")]]
  4392. kernel void kernel_conv_transpose_1d<half>(
  4393. device const half * src0,
  4394. device const float * src1,
  4395. device char * dst,
  4396. constant int32_t & IC,
  4397. constant int32_t & IL,
  4398. constant int32_t & K,
  4399. constant int32_t & s0,
  4400. constant uint64_t & nb0,
  4401. constant uint64_t & nb1,
  4402. uint3 tgpig[[threadgroup_position_in_grid]],
  4403. uint3 tgpg[[threadgroups_per_grid]]);
  4404. kernel void kernel_upscale_f32(
  4405. device const char * src0,
  4406. device char * dst,
  4407. constant int64_t & ne00,
  4408. constant int64_t & ne01,
  4409. constant int64_t & ne02,
  4410. constant int64_t & ne03,
  4411. constant uint64_t & nb00,
  4412. constant uint64_t & nb01,
  4413. constant uint64_t & nb02,
  4414. constant uint64_t & nb03,
  4415. constant int64_t & ne0,
  4416. constant int64_t & ne1,
  4417. constant int64_t & ne2,
  4418. constant int64_t & ne3,
  4419. constant uint64_t & nb0,
  4420. constant uint64_t & nb1,
  4421. constant uint64_t & nb2,
  4422. constant uint64_t & nb3,
  4423. constant float & sf0,
  4424. constant float & sf1,
  4425. constant float & sf2,
  4426. constant float & sf3,
  4427. uint3 tgpig[[threadgroup_position_in_grid]],
  4428. uint3 tpitg[[thread_position_in_threadgroup]],
  4429. uint3 ntg[[threads_per_threadgroup]]) {
  4430. const int64_t i3 = tgpig.z;
  4431. const int64_t i2 = tgpig.y;
  4432. const int64_t i1 = tgpig.x;
  4433. const int64_t i03 = i3/sf3;
  4434. const int64_t i02 = i2/sf2;
  4435. const int64_t i01 = i1/sf1;
  4436. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4437. const int64_t i00 = i0/sf0;
  4438. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  4439. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  4440. dst_ptr[0] = src0_ptr[0];
  4441. }
  4442. }
  4443. kernel void kernel_pad_f32(
  4444. device const char * src0,
  4445. device char * dst,
  4446. constant int64_t & ne00,
  4447. constant int64_t & ne01,
  4448. constant int64_t & ne02,
  4449. constant int64_t & ne03,
  4450. constant uint64_t & nb00,
  4451. constant uint64_t & nb01,
  4452. constant uint64_t & nb02,
  4453. constant uint64_t & nb03,
  4454. constant int64_t & ne0,
  4455. constant int64_t & ne1,
  4456. constant int64_t & ne2,
  4457. constant int64_t & ne3,
  4458. constant uint64_t & nb0,
  4459. constant uint64_t & nb1,
  4460. constant uint64_t & nb2,
  4461. constant uint64_t & nb3,
  4462. uint3 tgpig[[threadgroup_position_in_grid]],
  4463. uint3 tpitg[[thread_position_in_threadgroup]],
  4464. uint3 ntg[[threads_per_threadgroup]]) {
  4465. const int64_t i3 = tgpig.z;
  4466. const int64_t i2 = tgpig.y;
  4467. const int64_t i1 = tgpig.x;
  4468. const int64_t i03 = i3;
  4469. const int64_t i02 = i2;
  4470. const int64_t i01 = i1;
  4471. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  4472. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  4473. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  4474. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4475. if (i0 < ne00) {
  4476. dst_ptr[i0] = src0_ptr[i0];
  4477. } else {
  4478. dst_ptr[i0] = 0.0f;
  4479. }
  4480. }
  4481. return;
  4482. }
  4483. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4484. dst_ptr[i0] = 0.0f;
  4485. }
  4486. }
  4487. kernel void kernel_pad_reflect_1d_f32(
  4488. device const char * src0,
  4489. device char * dst,
  4490. constant int64_t & ne00,
  4491. constant int64_t & ne01,
  4492. constant int64_t & ne02,
  4493. constant int64_t & ne03,
  4494. constant int64_t & ne0,
  4495. constant uint64_t & nb00,
  4496. constant uint64_t & nb01,
  4497. constant uint64_t & nb02,
  4498. constant uint64_t & nb03,
  4499. constant uint64_t & nb0,
  4500. constant uint64_t & nb1,
  4501. constant uint64_t & nb2,
  4502. constant uint64_t & nb3,
  4503. constant int32_t & p0,
  4504. constant int32_t & p1,
  4505. uint3 tgpig[[threadgroup_position_in_grid]],
  4506. uint3 tgpg[[threadgroups_per_grid]],
  4507. uint3 tpitg[[thread_position_in_threadgroup]],
  4508. uint3 ntg[[threads_per_threadgroup]]) {
  4509. const int64_t i3 = tgpig.z;
  4510. const int64_t i2 = tgpig.y;
  4511. const int64_t i1 = tgpig.x;
  4512. const int64_t i03 = i3;
  4513. const int64_t i02 = i2;
  4514. const int64_t i01 = i1;
  4515. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  4516. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  4517. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  4518. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4519. if (i0 < p0) {
  4520. dst_ptr[i0] = src0_ptr[p0 - i0];
  4521. } else if (i0 < ne0 - p1) {
  4522. dst_ptr[i0] = src0_ptr[i0 - p0];
  4523. } else {
  4524. dst_ptr[i0] = src0_ptr[(ne0 - p1 - p0) - (p1 + 1 - (ne0 - i0)) - 1];
  4525. }
  4526. }
  4527. }
  4528. }
  4529. kernel void kernel_unpad_f32(
  4530. device const char * src0,
  4531. device char * dst,
  4532. constant int64_t & ne00,
  4533. constant int64_t & ne01,
  4534. constant int64_t & ne02,
  4535. constant int64_t & ne03,
  4536. constant uint64_t & nb00,
  4537. constant uint64_t & nb01,
  4538. constant uint64_t & nb02,
  4539. constant uint64_t & nb03,
  4540. constant int64_t & ne0,
  4541. constant int64_t & ne1,
  4542. constant int64_t & ne2,
  4543. constant int64_t & ne3,
  4544. constant uint64_t & nb0,
  4545. constant uint64_t & nb1,
  4546. constant uint64_t & nb2,
  4547. constant uint64_t & nb3,
  4548. uint3 tgpig[[threadgroup_position_in_grid]],
  4549. uint3 tpitg[[thread_position_in_threadgroup]],
  4550. uint3 ntg[[threads_per_threadgroup]]) {
  4551. const int64_t i3 = tgpig.z;
  4552. const int64_t i2 = tgpig.y;
  4553. const int64_t i1 = tgpig.x;
  4554. const int64_t i03 = i3;
  4555. const int64_t i02 = i2;
  4556. const int64_t i01 = i1;
  4557. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  4558. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  4559. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  4560. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4561. if (i0 < ne00) {
  4562. dst_ptr[i0] = src0_ptr[i0];
  4563. }
  4564. }
  4565. return;
  4566. }
  4567. }
  4568. kernel void kernel_arange_f32(
  4569. device char * dst,
  4570. constant int64_t & ne0,
  4571. constant float & start,
  4572. constant float & step,
  4573. uint3 tgpig[[threadgroup_position_in_grid]],
  4574. uint3 tpitg[[thread_position_in_threadgroup]],
  4575. uint3 ntg[[threads_per_threadgroup]]) {
  4576. device float * dst_ptr = (device float *) dst;
  4577. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  4578. dst_ptr[i0] = start + step * i0;
  4579. }
  4580. }
  4581. kernel void kernel_timestep_embedding_f32(
  4582. device const char * src0,
  4583. device char * dst,
  4584. constant uint64_t & nb1,
  4585. constant int & dim,
  4586. constant int & max_period,
  4587. uint3 tgpig[[threadgroup_position_in_grid]],
  4588. uint3 tpitg[[thread_position_in_threadgroup]],
  4589. uint3 ntg[[threads_per_threadgroup]]) {
  4590. int i = tgpig.x;
  4591. device float * embed_data = (device float *)(dst + i*nb1);
  4592. int half_ = dim / 2;
  4593. for (int j = tpitg.x; j < half_; j += ntg.x) {
  4594. float timestep = ((device float *)src0)[i];
  4595. float freq = (float)exp(-log((float)max_period) * j / half_);
  4596. float arg = timestep * freq;
  4597. embed_data[j ] = cos(arg);
  4598. embed_data[j + half_] = sin(arg);
  4599. }
  4600. if (dim % 2 != 0 && tpitg.x == 0) {
  4601. embed_data[dim] = 0.f;
  4602. }
  4603. }
  4604. // bitonic sort implementation following the CUDA kernels as reference
  4605. typedef void (argsort_t)(
  4606. device const float * x,
  4607. device int32_t * dst,
  4608. constant int64_t & ncols,
  4609. constant int64_t & ncols_pad,
  4610. threadgroup int32_t * shared_values [[threadgroup(0)]],
  4611. uint3 tgpig[[threadgroup_position_in_grid]],
  4612. uint3 tpitg[[thread_position_in_threadgroup]]);
  4613. template<ggml_sort_order order>
  4614. kernel void kernel_argsort_f32_i32(
  4615. device const float * x,
  4616. device int32_t * dst,
  4617. constant int64_t & ncols,
  4618. constant int64_t & ncols_pad,
  4619. threadgroup int32_t * shared_values [[threadgroup(0)]],
  4620. uint3 tgpig[[threadgroup_position_in_grid]],
  4621. uint3 tpitg[[thread_position_in_threadgroup]]) {
  4622. // bitonic sort
  4623. int col = tpitg[0];
  4624. int row = tgpig[1];
  4625. if (col >= ncols_pad) return;
  4626. device const float * x_row = x + row * ncols;
  4627. threadgroup int32_t * dst_row = shared_values;
  4628. // initialize indices
  4629. dst_row[col] = col;
  4630. threadgroup_barrier(mem_flags::mem_threadgroup);
  4631. for (int k = 2; k <= ncols_pad; k *= 2) {
  4632. for (int j = k / 2; j > 0; j /= 2) {
  4633. int ixj = col ^ j;
  4634. if (ixj > col) {
  4635. if ((col & k) == 0) {
  4636. if (dst_row[col] >= ncols ||
  4637. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  4638. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  4639. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  4640. ) {
  4641. SWAP(dst_row[col], dst_row[ixj]);
  4642. }
  4643. } else {
  4644. if (dst_row[ixj] >= ncols ||
  4645. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  4646. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  4647. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  4648. ) {
  4649. SWAP(dst_row[col], dst_row[ixj]);
  4650. }
  4651. }
  4652. }
  4653. threadgroup_barrier(mem_flags::mem_threadgroup);
  4654. }
  4655. }
  4656. // copy the result to dst without the padding
  4657. if (col < ncols) {
  4658. dst[row * ncols + col] = dst_row[col];
  4659. }
  4660. }
  4661. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_ASC>;
  4662. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_DESC>;
  4663. kernel void kernel_leaky_relu_f32(
  4664. device const float * src0,
  4665. device float * dst,
  4666. constant float & slope,
  4667. uint tpig[[thread_position_in_grid]]) {
  4668. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  4669. }
  4670. // ref: https://arxiv.org/pdf/2307.08691.pdf
  4671. template<
  4672. typename q_t, // query types in shared memory
  4673. typename q4_t,
  4674. typename q8x8_t,
  4675. typename k_t, // key types in shared memory
  4676. typename k4x4_t,
  4677. typename k8x8_t,
  4678. typename v_t, // value types in shared memory
  4679. typename v4x4_t,
  4680. typename v8x8_t,
  4681. typename qk_t, // Q*K types
  4682. typename qk8x8_t,
  4683. typename s_t, // soft-max types
  4684. typename s8x8_t,
  4685. typename o_t, // attention accumulation types
  4686. typename o4_t,
  4687. typename o8x8_t,
  4688. typename kd4x4_t, // key type in device memory
  4689. short nl_k,
  4690. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  4691. typename vd4x4_t, // key type in device memory
  4692. short nl_v,
  4693. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  4694. short D, // head size
  4695. short Q = 8, // queries per threadgroup
  4696. short KV = 8, // key/value processed per each simdgroup
  4697. short C = 32> // cache items per threadgroup
  4698. kernel void kernel_flash_attn_ext(
  4699. constant ggml_metal_kargs_flash_attn_ext & args,
  4700. device const char * q,
  4701. device const char * k,
  4702. device const char * v,
  4703. device const char * mask,
  4704. device char * dst,
  4705. threadgroup half * shmem_f16 [[threadgroup(0)]],
  4706. uint3 tgpig[[threadgroup_position_in_grid]],
  4707. ushort3 ntg[[threads_per_threadgroup]],
  4708. ushort tiisg[[thread_index_in_simdgroup]],
  4709. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  4710. const short nsg = ntg.y; // number of simdgroups
  4711. const int iq3 = tgpig[2];
  4712. const int iq2 = tgpig[1];
  4713. const int iq1 = tgpig[0]*Q;
  4714. const short D4 = D/4;
  4715. const short D8 = D/8;
  4716. const short D16 = D/16;
  4717. const short NW = N_SIMDWIDTH;
  4718. const short SH = (2*C + Q); // shared memory per simdgroup (s_t == float)
  4719. const short TS = nsg*SH; // shared memory size per query in (s_t == float)
  4720. const short T = D + 2*TS; // shared memory size per query in (half)
  4721. threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  4722. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  4723. threadgroup o_t * so = (threadgroup o_t *) (shmem_f16 + 0*D); // reuse query data for accumulation
  4724. threadgroup o4_t * so4 = (threadgroup o4_t *) (shmem_f16 + 0*D); // same as above but in o4_t
  4725. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + 2*sgitg*SH + Q*D); // scratch buffer for attention, mask and diagonal matrix
  4726. threadgroup k_t * sk = (threadgroup k_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load K in shared memory
  4727. threadgroup k4x4_t * sk4x4 = (threadgroup k4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in k4x4_t
  4728. threadgroup v_t * sv = (threadgroup v_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // scratch buffer to load V in shared memory
  4729. threadgroup v4x4_t * sv4x4 = (threadgroup v4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T); // same as above but in v4x4_t
  4730. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  4731. o8x8_t lo[D8];
  4732. // load heads from Q to shared memory
  4733. for (short j = sgitg; j < Q; j += nsg) {
  4734. device const float4 * q4 = (device const float4 *) ((device const char *) q + ((iq1 + j)*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  4735. for (short i = tiisg; i < D4; i += NW) {
  4736. if (iq1 + j < args.ne01) {
  4737. sq4[j*D4 + i] = (q4_t) q4[i];
  4738. } else {
  4739. sq4[j*D4 + i] = (q4_t) 0.0f;
  4740. }
  4741. }
  4742. }
  4743. // zero out lo
  4744. for (short i = 0; i < D8; ++i) {
  4745. lo[i] = make_filled_simdgroup_matrix<o_t, 8>((o_t) 0.0f);
  4746. }
  4747. // zero out shared memory SH
  4748. for (short j = 0; j < Q; ++j) {
  4749. for (short i = tiisg; i < SH; i += NW) {
  4750. ss[j*TS + i] = 0.0f;
  4751. }
  4752. }
  4753. threadgroup_barrier(mem_flags::mem_threadgroup);
  4754. {
  4755. half S[Q] = { [0 ... Q-1] = 0.0f };
  4756. half M[Q] = { [0 ... Q-1] = -__FLT16_MAX__/2 };
  4757. // thread indices inside the simdgroup
  4758. // TODO: see if we can utilize quad-group functions for better performance
  4759. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (6.9.3)
  4760. const short tx = tiisg%4;
  4761. const short ty = tiisg/4;
  4762. // broadcast kv
  4763. //const short rk2 = args.ne02/args.ne12;
  4764. //const short rk3 = args.ne03/args.ne13;
  4765. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  4766. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  4767. // load the queries from shared memory into local memory
  4768. q8x8_t mq[D8];
  4769. for (short i = 0; i < D8; ++i) {
  4770. simdgroup_load(mq[i], sq + i*8, D);
  4771. }
  4772. const bool has_mask = mask != q;
  4773. half slope = 1.0f;
  4774. // ALiBi
  4775. if (args.max_bias > 0.0f) {
  4776. const short h = iq2;
  4777. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  4778. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  4779. slope = pow(base, exph);
  4780. }
  4781. // loop over the KV cache
  4782. // each simdgroup handles blocks of Q rows and C columns
  4783. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  4784. const int ic = ic0 + C*sgitg;
  4785. if (ic >= args.ne11) {
  4786. break;
  4787. }
  4788. if (has_mask) {
  4789. // used to detect blocks full of -INF
  4790. half smax = -INFINITY;
  4791. // load the mask in shared memory
  4792. #pragma unroll(Q)
  4793. for (short j = 0; j < Q; ++j) {
  4794. device const half * pm = (device const half *) ((device const char *) mask + (iq1 + j)*args.nb31);
  4795. const half m = pm[ic + tiisg];
  4796. ss[j*TS + C + tiisg] = m;
  4797. smax = max(smax, m);
  4798. }
  4799. smax = simd_max(smax);
  4800. if (smax == -INFINITY) {
  4801. continue;
  4802. }
  4803. }
  4804. // Q*K^T
  4805. {
  4806. for (short cc = 0; cc < C/8; ++cc) {
  4807. qk8x8_t mqk = make_filled_simdgroup_matrix<qk_t, 8>((qk_t) 0.0f);
  4808. // this is compile-time check, so it does not have runtime overhead
  4809. if (is_same<kd4x4_t, k4x4_t>::value) {
  4810. // we can read directly from global memory
  4811. 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));
  4812. #pragma unroll(D8)
  4813. for (short i = 0; i < D8; ++i) {
  4814. k8x8_t mk;
  4815. simdgroup_load(mk, pk + i*8, args.nb_12_1/sizeof(k_t), 0, true); // transpose // TODO: use ne10
  4816. simdgroup_multiply_accumulate(mqk, mq[i], mk, mqk);
  4817. }
  4818. } else {
  4819. for (short ii = 0; ii < D16; ii += 4) {
  4820. 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));
  4821. if (D16%4 == 0) {
  4822. // the head is evenly divisible by 4*16 = 64, so no need for bound checks
  4823. {
  4824. k4x4_t tmp;
  4825. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  4826. sk4x4[4*ty + tx] = tmp;
  4827. }
  4828. simdgroup_barrier(mem_flags::mem_threadgroup);
  4829. #pragma unroll(4)
  4830. for (short k = 0; k < 4; ++k) {
  4831. k8x8_t mk;
  4832. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  4833. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  4834. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  4835. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  4836. }
  4837. } else {
  4838. if (ii + tx < D16) {
  4839. k4x4_t tmp;
  4840. deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp);
  4841. sk4x4[4*ty + tx] = tmp;
  4842. }
  4843. simdgroup_barrier(mem_flags::mem_threadgroup);
  4844. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  4845. k8x8_t mk;
  4846. simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose
  4847. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 0], mk, mqk);
  4848. simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose
  4849. simdgroup_multiply_accumulate(mqk, mq[2*(ii + k) + 1], mk, mqk);
  4850. }
  4851. }
  4852. }
  4853. }
  4854. // cast qk_t -> s_t
  4855. //s8x8_t mqks(1.0f);
  4856. //simdgroup_multiply(mqks, mqk, mqks);
  4857. //simdgroup_store(mqks, ss + 8*cc, TS, 0, false);
  4858. simdgroup_store(mqk, ss + 8*cc, TS, 0, false);
  4859. }
  4860. }
  4861. // online softmax
  4862. {
  4863. for (ushort j = 0; j < Q; ++j) {
  4864. const half m = M[j];
  4865. // scale and apply the logitcap / mask
  4866. half s = ss[j*TS + tiisg]*args.scale;
  4867. if (args.logit_softcap != 0.0f) {
  4868. s = args.logit_softcap*precise::tanh(s);
  4869. }
  4870. // mqk = mqk + mask*slope
  4871. s += slope*ss[j*TS + C + tiisg];
  4872. M[j] = simd_max(max(M[j], s));
  4873. const half ms = exp(m - M[j]);
  4874. const half vs = exp(s - M[j]);
  4875. S[j] = S[j]*ms + simd_sum(vs);
  4876. // the P matrix from the paper (Q rows, C columns)
  4877. ss[j*TS + tiisg] = vs;
  4878. // create a QxQ diagonal matrix for rescaling the output
  4879. if (tiisg == j) {
  4880. ss[j*TS + 2*C + j] = ms;
  4881. }
  4882. }
  4883. }
  4884. // O = diag(ms)*O
  4885. {
  4886. s8x8_t mm;
  4887. simdgroup_load(mm, ss + 2*C, TS, 0, false);
  4888. #pragma unroll(D8)
  4889. for (short i = 0; i < D8; ++i) {
  4890. simdgroup_multiply(lo[i], mm, lo[i]);
  4891. }
  4892. }
  4893. // O = O + (Q*K^T)*V
  4894. {
  4895. for (short cc = 0; cc < C/8; ++cc) {
  4896. s8x8_t ms;
  4897. simdgroup_load(ms, ss + 8*cc, TS, 0, false);
  4898. if (is_same<vd4x4_t, v4x4_t>::value) {
  4899. // we can read directly from global memory
  4900. 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));
  4901. #pragma unroll(D8)
  4902. for (short i = 0; i < D8; ++i) {
  4903. v8x8_t mv;
  4904. simdgroup_load(mv, pv + i*8, args.nb_12_1/sizeof(v_t), 0, false); // TODO: use ne20
  4905. simdgroup_multiply_accumulate(lo[i], ms, mv, lo[i]);
  4906. }
  4907. } else {
  4908. for (short ii = 0; ii < D16; ii += 4) {
  4909. 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));
  4910. if (D16%4 == 0) {
  4911. // no need for bound checks
  4912. {
  4913. v4x4_t tmp;
  4914. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  4915. sv4x4[4*ty + tx] = tmp;
  4916. }
  4917. simdgroup_barrier(mem_flags::mem_threadgroup);
  4918. #pragma unroll(4)
  4919. for (short k = 0; k < 4; ++k) {
  4920. v8x8_t mv;
  4921. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  4922. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  4923. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  4924. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  4925. }
  4926. } else {
  4927. if (ii + tx < D16) {
  4928. v4x4_t tmp;
  4929. deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp);
  4930. sv4x4[4*ty + tx] = tmp;
  4931. }
  4932. simdgroup_barrier(mem_flags::mem_threadgroup);
  4933. for (short k = 0; k < 4 && ii + k < D16; ++k) {
  4934. v8x8_t mv;
  4935. simdgroup_load(mv, sv + 16*k + 0*8, 4*16, 0, false);
  4936. simdgroup_multiply_accumulate(lo[2*(ii + k) + 0], ms, mv, lo[2*(ii + k) + 0]);
  4937. simdgroup_load(mv, sv + 16*k + 1*8, 4*16, 0, false);
  4938. simdgroup_multiply_accumulate(lo[2*(ii + k) + 1], ms, mv, lo[2*(ii + k) + 1]);
  4939. }
  4940. }
  4941. }
  4942. }
  4943. }
  4944. }
  4945. }
  4946. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  4947. for (short j = 0; j < Q; ++j) {
  4948. if (tiisg == 0) {
  4949. ss[j*TS + 0] = S[j];
  4950. ss[j*TS + 1] = M[j];
  4951. }
  4952. }
  4953. }
  4954. // reduce the warps sequentially
  4955. for (ushort sg = 1; sg < nsg; ++sg) {
  4956. half S = { 0.0f };
  4957. half M = { -__FLT16_MAX__/2 };
  4958. threadgroup_barrier(mem_flags::mem_threadgroup);
  4959. // each simdgroup stores its output to shared memory, reusing sq
  4960. if (sgitg == sg) {
  4961. for (short i = 0; i < D8; ++i) {
  4962. simdgroup_store(lo[i], so + i*8, D, 0, false);
  4963. }
  4964. }
  4965. threadgroup_barrier(mem_flags::mem_threadgroup);
  4966. // the first simdgroup accumulates the results from the other simdgroups
  4967. if (sgitg == 0) {
  4968. for (short j = 0; j < Q; ++j) {
  4969. const half S0 = ss[j*TS + 0];
  4970. const half S1 = ss[j*TS + sg*SH + 0];
  4971. const half M0 = ss[j*TS + 1];
  4972. const half M1 = ss[j*TS + sg*SH + 1];
  4973. M = max(M0, M1);
  4974. const half ms0 = exp(M0 - M);
  4975. const half ms1 = exp(M1 - M);
  4976. S = S0*ms0 + S1*ms1;
  4977. if (tiisg == 0) {
  4978. ss[j*TS + 0] = S;
  4979. ss[j*TS + 1] = M;
  4980. ss[j*TS + 2*C + j ] = ms0;
  4981. ss[j*TS + 2*C + j + sg*SH] = ms1;
  4982. }
  4983. }
  4984. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  4985. {
  4986. s8x8_t ms0;
  4987. s8x8_t ms1;
  4988. simdgroup_load(ms0, ss + 2*C, TS, 0, false);
  4989. simdgroup_load(ms1, ss + 2*C + sg*SH, TS, 0, false);
  4990. #pragma unroll(D8)
  4991. for (short i = 0; i < D8; ++i) {
  4992. o8x8_t t;
  4993. simdgroup_load (t, so + i*8, D, 0, false);
  4994. simdgroup_multiply(t, ms1, t);
  4995. simdgroup_multiply_accumulate(lo[i], ms0, lo[i], t);
  4996. }
  4997. }
  4998. }
  4999. }
  5000. // store result to shared memory (reuse sq)
  5001. if (sgitg == 0) {
  5002. for (short i = 0; i < D8; ++i) {
  5003. simdgroup_store(lo[i], so + i*8, D, 0, false);
  5004. }
  5005. }
  5006. device float4 * dst4 = (device float4 *) dst;
  5007. // final rescale with 1/S and store to global memory
  5008. if (sgitg == 0) {
  5009. for (short j = 0; j < Q && iq1 + j < args.ne01; ++j) {
  5010. const float S = ss[j*TS + 0];
  5011. for (short i = tiisg; i < D4; i += NW) {
  5012. dst4[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)(iq1 + j)*args.ne1)*D4 + i] = (float4) so4[j*D4 + i]/S;
  5013. }
  5014. }
  5015. }
  5016. }
  5017. // TODO: this is quite ugly. in the future these types will be hardcoded in the kernel, but for now keep them as
  5018. // template to be able to explore different combinations
  5019. //
  5020. #define FA_TYPES \
  5021. half, half4, simdgroup_half8x8, \
  5022. half, half4x4, simdgroup_half8x8, \
  5023. half, half4x4, simdgroup_half8x8, \
  5024. float, simdgroup_float8x8, \
  5025. float, simdgroup_float8x8, \
  5026. half, half4, simdgroup_half8x8
  5027. typedef decltype(kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 64>) flash_attn_ext_t;
  5028. 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>;
  5029. 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>;
  5030. 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>;
  5031. 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>;
  5032. 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>;
  5033. 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>;
  5034. #if defined(GGML_METAL_USE_BF16)
  5035. 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>;
  5036. 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>;
  5037. 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>;
  5038. 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>;
  5039. 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>;
  5040. 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>;
  5041. #endif
  5042. 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>;
  5043. 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>;
  5044. 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>;
  5045. 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>;
  5046. 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>;
  5047. 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>;
  5048. 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>;
  5049. 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>;
  5050. 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>;
  5051. 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>;
  5052. 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>;
  5053. 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>;
  5054. 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>;
  5055. 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>;
  5056. 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>;
  5057. 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>;
  5058. 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>;
  5059. 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>;
  5060. 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>;
  5061. 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>;
  5062. 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>;
  5063. 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>;
  5064. 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>;
  5065. 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>;
  5066. 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>;
  5067. 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>;
  5068. 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>;
  5069. 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>;
  5070. 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>;
  5071. 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>;
  5072. #undef FA_TYPES
  5073. template<
  5074. typename q4_t, // query types in shared memory
  5075. typename q4x4_t,
  5076. typename k4x4_t, // key types in shared memory
  5077. typename v4x4_t, // value types in shared memory
  5078. typename qk_t, // Q*K types
  5079. typename s_t, // soft-max types
  5080. typename s4_t,
  5081. typename s4x4_t,
  5082. typename o4x4_t, // attention accumulation types
  5083. typename kd4x4_t, // key type in device memory
  5084. short nl_k,
  5085. void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &),
  5086. typename vd4x4_t, // key type in device memory
  5087. short nl_v,
  5088. void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &),
  5089. short D, // head size
  5090. short Q = 1, // queries per threadgroup
  5091. short C = 32> // cache items per threadgroup
  5092. kernel void kernel_flash_attn_ext_vec(
  5093. constant ggml_metal_kargs_flash_attn_ext & args,
  5094. device const char * q,
  5095. device const char * k,
  5096. device const char * v,
  5097. device const char * mask,
  5098. device char * dst,
  5099. threadgroup half * shmem_f16 [[threadgroup(0)]],
  5100. uint3 tgpig[[threadgroup_position_in_grid]],
  5101. ushort3 ntg[[threads_per_threadgroup]],
  5102. ushort tiisg[[thread_index_in_simdgroup]],
  5103. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5104. const short nsg = ntg.y; // number of simdgroups
  5105. const int iq3 = tgpig[2];
  5106. const int iq2 = tgpig[1];
  5107. const int iq1 = tgpig[0];
  5108. const short D4 = D/4;
  5109. const short D16 = D/16;
  5110. const short NW = N_SIMDWIDTH;
  5111. const short NL = NW/4; // note: this can be adjusted to support D%64 == 0 and D%32 == 0
  5112. const short SH = 2*C; // shared memory per simdgroup
  5113. const short T = D + nsg*SH; // shared memory size per query in (half)
  5114. //threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*D); // holds the query data
  5115. threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*D); // same as above but in q4_t
  5116. threadgroup q4x4_t * sq4x4 = (threadgroup q4x4_t *) (shmem_f16 + 0*D); // same as above but in q4x4_t
  5117. threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + sgitg*SH + Q*D); // scratch buffer for attention
  5118. threadgroup s4_t * ss4 = (threadgroup s4_t *) (shmem_f16 + sgitg*SH + Q*D); // same as above but in s4_t
  5119. threadgroup half * sm = (threadgroup half *) (shmem_f16 + sgitg*SH + C + Q*D); // scratch buffer for mask
  5120. threadgroup o4x4_t * sr4x4 = (threadgroup o4x4_t *) (shmem_f16 + sgitg*D + Q*T); // scratch buffer for the results
  5121. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  5122. o4x4_t lo[D16/NL];
  5123. // load heads from Q to shared memory
  5124. device const float4 * q4 = (device const float4 *) ((device const char *) q + (iq1*args.nb01 + iq2*args.nb02 + iq3*args.nb03));
  5125. for (short i = tiisg; i < D4; i += NW) {
  5126. if (iq1 < args.ne01) {
  5127. sq4[i] = (q4_t) q4[i];
  5128. } else {
  5129. sq4[i] = (q4_t) 0.0f;
  5130. }
  5131. }
  5132. // zero out lo
  5133. for (short i = 0; i < D16/NL; ++i) {
  5134. lo[i] = (o4x4_t) 0.0f;
  5135. }
  5136. // zero out shared memory SH
  5137. for (short i = tiisg; i < SH/4; i += NW) {
  5138. ss4[i] = (s4_t) 0.0f;
  5139. }
  5140. threadgroup_barrier(mem_flags::mem_threadgroup);
  5141. {
  5142. half S = 0.0f;
  5143. half M = -__FLT16_MAX__/2;
  5144. // thread indices inside the simdgroup
  5145. const short tx = tiisg%NL;
  5146. const short ty = tiisg/NL;
  5147. // broadcast kv
  5148. //const short rk2 = args.ne02/args.ne12;
  5149. //const short rk3 = args.ne03/args.ne13;
  5150. const short ikv2 = iq2/(args.ne02/args.ne_12_2);
  5151. const short ikv3 = iq3/(args.ne03/args.ne_12_3);
  5152. // load the queries from shared memory into local memory
  5153. q4x4_t mq[D16/NL];
  5154. #pragma unroll(D16/NL)
  5155. for (short ii = 0; ii < D16; ii += NL) {
  5156. mq[ii/NL] = sq4x4[ii + tx];
  5157. }
  5158. const bool has_mask = mask != q;
  5159. // pointer to the mask
  5160. device const half * pm = (device const half *) (mask + iq1*args.nb31);
  5161. half slope = 1.0f;
  5162. // ALiBi
  5163. if (args.max_bias > 0.0f) {
  5164. const short h = iq2;
  5165. const half base = h < args.n_head_log2 ? args.m0 : args.m1;
  5166. const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
  5167. slope = pow(base, exph);
  5168. }
  5169. // loop over the KV cache
  5170. // each simdgroup handles blocks of Q rows and C columns
  5171. for (int ic0 = 0; ic0 < args.ne11; ic0 += C*nsg) {
  5172. const int ic = ic0 + C*sgitg;
  5173. if (ic >= args.ne11) {
  5174. break;
  5175. }
  5176. if (has_mask) {
  5177. sm[tiisg] = pm[ic + tiisg];
  5178. }
  5179. // Q*K^T
  5180. {
  5181. // each simdgroup processes 1 query and 4 (NW/NL) keys
  5182. for (short cc = 0; cc < C/4; ++cc) {
  5183. qk_t mqka[4] = { 0.0, 0.0, 0.0, 0.0 };
  5184. 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));
  5185. #pragma unroll(D16/NL)
  5186. for (short ii = 0; ii < D16; ii += NL) {
  5187. const short i = ii + tx;
  5188. k4x4_t mk;
  5189. deq_k(pk + i/nl_k, i%nl_k, mk);
  5190. // note: this is less precise than the version below
  5191. //mqka[0] += dot(mq[ii/NL][0], mk[0]);
  5192. //mqka[1] += dot(mq[ii/NL][1], mk[1]);
  5193. //mqka[2] += dot(mq[ii/NL][2], mk[2]);
  5194. //mqka[3] += dot(mq[ii/NL][3], mk[3]);
  5195. mqka[0] += dot((float4) mq[ii/NL][0], (float4) mk[0]);
  5196. mqka[1] += dot((float4) mq[ii/NL][1], (float4) mk[1]);
  5197. mqka[2] += dot((float4) mq[ii/NL][2], (float4) mk[2]);
  5198. mqka[3] += dot((float4) mq[ii/NL][3], (float4) mk[3]);
  5199. }
  5200. qk_t mqk = mqka[0] + mqka[1] + mqka[2] + mqka[3];
  5201. // simdgroup reduce
  5202. // [ 0 .. 7] -> [ 0]
  5203. // [ 8 .. 15] -> [ 8]
  5204. // [16 .. 23] -> [16]
  5205. // [24 .. 31] -> [24]
  5206. //mqk += simd_shuffle_down(mqk, 16);
  5207. //mqk += simd_shuffle_down(mqk, 8);
  5208. mqk += simd_shuffle_down(mqk, 4);
  5209. mqk += simd_shuffle_down(mqk, 2);
  5210. mqk += simd_shuffle_down(mqk, 1);
  5211. // mqk = mqk*scale + mask*slope
  5212. if (tx == 0) {
  5213. mqk *= args.scale;
  5214. if (args.logit_softcap != 0.0f) {
  5215. mqk = args.logit_softcap*precise::tanh(mqk);
  5216. }
  5217. mqk += sm[4*cc + ty]*slope;
  5218. ss[4*cc + ty] = mqk;
  5219. }
  5220. }
  5221. }
  5222. simdgroup_barrier(mem_flags::mem_threadgroup);
  5223. // online softmax
  5224. {
  5225. const half m = M;
  5226. const half s = ss[tiisg];
  5227. M = simd_max(max(M, s));
  5228. const half ms = exp(m - M);
  5229. const half vs = exp(s - M);
  5230. S = S*ms + simd_sum(vs);
  5231. // the P matrix from the paper (Q rows, C columns)
  5232. ss[tiisg] = vs;
  5233. // O = diag(ms)*O
  5234. #pragma unroll(D16/NL)
  5235. for (short ii = 0; ii < D16; ii += NL) {
  5236. lo[ii/NL] *= ms;
  5237. }
  5238. }
  5239. simdgroup_barrier(mem_flags::mem_threadgroup);
  5240. // O = O + (Q*K^T)*V
  5241. {
  5242. for (short cc = 0; cc < C/4; ++cc) {
  5243. 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));
  5244. const s4x4_t ms(ss[4*cc + ty]);
  5245. #pragma unroll(D16/NL)
  5246. for (short ii = 0; ii < D16; ii += NL) {
  5247. const short i = ii + tx;
  5248. v4x4_t mv;
  5249. deq_v(pv4 + i/nl_v, i%nl_v, mv);
  5250. lo[ii/NL] += mv*ms;
  5251. }
  5252. }
  5253. }
  5254. }
  5255. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  5256. if (tiisg == 0) {
  5257. ss[0] = (s_t) S;
  5258. ss[1] = (s_t) M;
  5259. }
  5260. }
  5261. // simdgroup reduce
  5262. // [ 0, 8, 16, 24] -> [ 0]
  5263. // [ 1, 9, 17, 25] -> [ 1]
  5264. // [ 2, 10, 18, 26] -> [ 2]
  5265. // [ 3, 11, 19, 27] -> [ 3]
  5266. // [ 4, 12, 20, 28] -> [ 4]
  5267. // [ 5, 13, 21, 29] -> [ 5]
  5268. // [ 6, 14, 22, 30] -> [ 6]
  5269. // [ 7, 15, 23, 31] -> [ 7]
  5270. for (short ii = 0; ii < D16; ii += NL) {
  5271. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 16);
  5272. lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 8);
  5273. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 4);
  5274. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 2);
  5275. //lo[ii/NL][0] += simd_shuffle_down(lo[ii/NL][0], 1);
  5276. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 16);
  5277. lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 8);
  5278. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 4);
  5279. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 2);
  5280. //lo[ii/NL][1] += simd_shuffle_down(lo[ii/NL][1], 1);
  5281. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 16);
  5282. lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 8);
  5283. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 4);
  5284. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 2);
  5285. //lo[ii/NL][2] += simd_shuffle_down(lo[ii/NL][2], 1);
  5286. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 16);
  5287. lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 8);
  5288. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 4);
  5289. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 2);
  5290. //lo[ii/NL][3] += simd_shuffle_down(lo[ii/NL][3], 1);
  5291. }
  5292. threadgroup_barrier(mem_flags::mem_threadgroup);
  5293. // store results to shared memory
  5294. for (short i = tiisg; i < D16; i += NL) {
  5295. sr4x4[i] = lo[i/NL];
  5296. }
  5297. threadgroup_barrier(mem_flags::mem_threadgroup);
  5298. // parallel reduce
  5299. for (short r = nsg/2; r > 0; r >>= 1) {
  5300. if (sgitg < r) {
  5301. const half S0 = ss[ 0];
  5302. const half S1 = ss[r*SH + 0];
  5303. const half M0 = ss[ 1];
  5304. const half M1 = ss[r*SH + 1];
  5305. const half M = max(M0, M1);
  5306. const half ms0 = exp(M0 - M);
  5307. const half ms1 = exp(M1 - M);
  5308. const half S = S0*ms0 + S1*ms1;
  5309. if (tiisg == 0) {
  5310. ss[0] = S;
  5311. ss[1] = M;
  5312. }
  5313. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  5314. for (short i = tiisg; i < D16; i += NW) {
  5315. sr4x4[i] = sr4x4[i]*ms0 + sr4x4[i + r*D16]*ms1;
  5316. }
  5317. }
  5318. threadgroup_barrier(mem_flags::mem_threadgroup);
  5319. }
  5320. device float4x4 * dst44 = (device float4x4 *) dst;
  5321. // final rescale with 1/S and store to global memory
  5322. if (sgitg == 0) {
  5323. const float S = ss[0];
  5324. for (short i = tiisg; i < D16; i += NW) {
  5325. dst44[((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)iq1*args.ne1)*D16 + i] = (float4x4) sr4x4[i]/S;
  5326. }
  5327. }
  5328. }
  5329. // note: I think the s_t can be half instead of float, because the Q*K scaling is done before storing to shared mem
  5330. // in the other (non-vec) kernel, we need s_t to also be float because we scale during the soft_max
  5331. //
  5332. #define FA_TYPES \
  5333. half4, half4x4, \
  5334. half4x4, \
  5335. half4x4, \
  5336. float, \
  5337. half, half4, half4x4, \
  5338. half4x4
  5339. typedef decltype(kernel_flash_attn_ext_vec<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 128>) flash_attn_ext_vec_t;
  5340. 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>;
  5341. #if defined(GGML_METAL_USE_BF16)
  5342. 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>;
  5343. #endif
  5344. 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>;
  5345. 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>;
  5346. 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>;
  5347. 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>;
  5348. 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>;
  5349. 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>;
  5350. #if defined(GGML_METAL_USE_BF16)
  5351. 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>;
  5352. #endif
  5353. 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>;
  5354. 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>;
  5355. 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>;
  5356. 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>;
  5357. 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>;
  5358. #undef FA_TYPES
  5359. template<typename T>
  5360. kernel void kernel_set(
  5361. constant ggml_metal_kargs_set & args,
  5362. device const char * src0,
  5363. device const char * src1,
  5364. device char * dst,
  5365. uint3 tgpig[[threadgroup_position_in_grid]],
  5366. ushort3 tpitg[[thread_position_in_threadgroup]],
  5367. ushort3 ntg[[threads_per_threadgroup]]) {
  5368. const int i13 = tgpig[2];
  5369. const int i12 = tgpig[1];
  5370. const int i11 = tgpig[0];
  5371. const int64_t n = i13*args.ne12*args.ne11*args.ne10 + i12*args.ne11*args.ne10 + i11*args.ne10;
  5372. const int64_t i3 = n / (args.ne12*args.ne11*args.ne10);
  5373. const int64_t i2 = (n - i3*args.ne12*args.ne11*args.ne10) / (args.ne11*args.ne10);
  5374. const int64_t i1 = (n - i3*args.ne12*args.ne11*args.ne10 - i2*args.ne11*args.ne10) / args.ne10;
  5375. device T * dst_data = (device T *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + args.offs);
  5376. for (int64_t i10 = tpitg.x; i10 < args.ne10; i10 += ntg.x) {
  5377. device const T * src = (device T *) (src1 + i13*args.nb13 + i12*args.nb12 + i11*args.nb11 + i10*args.nb10);
  5378. dst_data[i10] = (T) src[0];
  5379. }
  5380. }
  5381. typedef decltype(kernel_set<float>) kernel_set_t;
  5382. template [[host_name("kernel_set_f32")]] kernel kernel_set_t kernel_set<float>;
  5383. template [[host_name("kernel_set_i32")]] kernel kernel_set_t kernel_set<int32_t>;
  5384. template<typename T0, typename T1>
  5385. kernel void kernel_cpy(
  5386. constant ggml_metal_kargs_cpy & args,
  5387. device const char * src0,
  5388. device char * dst,
  5389. uint3 tgpig[[threadgroup_position_in_grid]],
  5390. ushort3 tpitg[[thread_position_in_threadgroup]],
  5391. ushort3 ntg[[threads_per_threadgroup]]) {
  5392. const int i03 = tgpig[2];
  5393. const int i02 = tgpig[1];
  5394. const int i01 = tgpig[0];
  5395. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5396. const int64_t i3 = n/(args.ne2*args.ne1*args.ne0);
  5397. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
  5398. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
  5399. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
  5400. device T1 * dst_data = (device T1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5401. for (int64_t i00 = tpitg.x; i00 < args.ne00; i00 += ntg.x) {
  5402. device const T0 * src = (device T0 *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5403. dst_data[i00] = (T1) src[0];
  5404. }
  5405. }
  5406. typedef decltype(kernel_cpy<float, float>) kernel_cpy_t;
  5407. template [[host_name("kernel_cpy_f32_f32")]] kernel kernel_cpy_t kernel_cpy<float, float>;
  5408. template [[host_name("kernel_cpy_f32_f16")]] kernel kernel_cpy_t kernel_cpy<float, half>;
  5409. #if defined(GGML_METAL_USE_BF16)
  5410. template [[host_name("kernel_cpy_f32_bf16")]] kernel kernel_cpy_t kernel_cpy<float, bfloat>;
  5411. #endif
  5412. template [[host_name("kernel_cpy_f16_f32")]] kernel kernel_cpy_t kernel_cpy<half, float>;
  5413. template [[host_name("kernel_cpy_f16_f16")]] kernel kernel_cpy_t kernel_cpy<half, half>;
  5414. #if defined(GGML_METAL_USE_BF16)
  5415. template [[host_name("kernel_cpy_bf16_f32")]] kernel kernel_cpy_t kernel_cpy<bfloat, float>;
  5416. template [[host_name("kernel_cpy_bf16_bf16")]] kernel kernel_cpy_t kernel_cpy<bfloat, bfloat>;
  5417. #endif
  5418. kernel void kernel_cpy_f32_q8_0(
  5419. constant ggml_metal_kargs_cpy & args,
  5420. device const char * src0,
  5421. device char * dst,
  5422. uint3 tgpig[[threadgroup_position_in_grid]],
  5423. ushort3 tpitg[[thread_position_in_threadgroup]],
  5424. ushort3 ntg[[threads_per_threadgroup]]) {
  5425. const int i03 = tgpig[2];
  5426. const int i02 = tgpig[1];
  5427. const int i01 = tgpig[0];
  5428. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5429. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5430. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5431. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5432. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK8_0;
  5433. device block_q8_0 * dst_data = (device block_q8_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5434. for (int64_t i00 = tpitg.x*QK8_0; i00 < args.ne00; i00 += ntg.x*QK8_0) {
  5435. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5436. float amax = 0.0f; // absolute max
  5437. for (int j = 0; j < QK8_0; j++) {
  5438. const float v = src[j];
  5439. amax = MAX(amax, fabs(v));
  5440. }
  5441. const float d = amax / ((1 << 7) - 1);
  5442. const float id = d ? 1.0f/d : 0.0f;
  5443. dst_data[i00/QK8_0].d = d;
  5444. for (int j = 0; j < QK8_0; ++j) {
  5445. const float x0 = src[j]*id;
  5446. dst_data[i00/QK8_0].qs[j] = round(x0);
  5447. }
  5448. }
  5449. }
  5450. kernel void kernel_cpy_f32_q4_0(
  5451. constant ggml_metal_kargs_cpy & args,
  5452. device const char * src0,
  5453. device char * dst,
  5454. uint3 tgpig[[threadgroup_position_in_grid]],
  5455. ushort3 tpitg[[thread_position_in_threadgroup]],
  5456. ushort3 ntg[[threads_per_threadgroup]]) {
  5457. const int i03 = tgpig[2];
  5458. const int i02 = tgpig[1];
  5459. const int i01 = tgpig[0];
  5460. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5461. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5462. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5463. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5464. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_0;
  5465. device block_q4_0 * dst_data = (device block_q4_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5466. for (int64_t i00 = tpitg.x*QK4_0; i00 < args.ne00; i00 += ntg.x*QK4_0) {
  5467. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5468. float amax = 0.0f; // absolute max
  5469. float max = 0.0f;
  5470. for (int j = 0; j < QK4_0; j++) {
  5471. const float v = src[j];
  5472. if (amax < fabs(v)) {
  5473. amax = fabs(v);
  5474. max = v;
  5475. }
  5476. }
  5477. const float d = max / -8;
  5478. const float id = d ? 1.0f/d : 0.0f;
  5479. dst_data[i00/QK4_0].d = d;
  5480. for (int j = 0; j < QK4_0/2; ++j) {
  5481. const float x0 = src[0 + j]*id;
  5482. const float x1 = src[QK4_0/2 + j]*id;
  5483. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  5484. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  5485. dst_data[i00/QK4_0].qs[j] = xi0;
  5486. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  5487. }
  5488. }
  5489. }
  5490. kernel void kernel_cpy_f32_q4_1(
  5491. constant ggml_metal_kargs_cpy & args,
  5492. device const char * src0,
  5493. device char * dst,
  5494. uint3 tgpig[[threadgroup_position_in_grid]],
  5495. ushort3 tpitg[[thread_position_in_threadgroup]],
  5496. ushort3 ntg[[threads_per_threadgroup]]) {
  5497. const int i03 = tgpig[2];
  5498. const int i02 = tgpig[1];
  5499. const int i01 = tgpig[0];
  5500. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5501. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5502. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5503. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5504. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_1;
  5505. device block_q4_1 * dst_data = (device block_q4_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5506. for (int64_t i00 = tpitg.x*QK4_1; i00 < args.ne00; i00 += ntg.x*QK4_1) {
  5507. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5508. float min = FLT_MAX;
  5509. float max = -FLT_MAX;
  5510. for (int j = 0; j < QK4_1; j++) {
  5511. const float v = src[j];
  5512. if (min > v) min = v;
  5513. if (max < v) max = v;
  5514. }
  5515. const float d = (max - min) / ((1 << 4) - 1);
  5516. const float id = d ? 1.0f/d : 0.0f;
  5517. dst_data[i00/QK4_1].d = d;
  5518. dst_data[i00/QK4_1].m = min;
  5519. for (int j = 0; j < QK4_1/2; ++j) {
  5520. const float x0 = (src[0 + j] - min)*id;
  5521. const float x1 = (src[QK4_1/2 + j] - min)*id;
  5522. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  5523. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  5524. dst_data[i00/QK4_1].qs[j] = xi0;
  5525. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  5526. }
  5527. }
  5528. }
  5529. kernel void kernel_cpy_f32_q5_0(
  5530. constant ggml_metal_kargs_cpy & args,
  5531. device const char * src0,
  5532. device char * dst,
  5533. uint3 tgpig[[threadgroup_position_in_grid]],
  5534. ushort3 tpitg[[thread_position_in_threadgroup]],
  5535. ushort3 ntg[[threads_per_threadgroup]]) {
  5536. const int i03 = tgpig[2];
  5537. const int i02 = tgpig[1];
  5538. const int i01 = tgpig[0];
  5539. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5540. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5541. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5542. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5543. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_0;
  5544. device block_q5_0 * dst_data = (device block_q5_0 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5545. for (int64_t i00 = tpitg.x*QK5_0; i00 < args.ne00; i00 += ntg.x*QK5_0) {
  5546. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5547. float amax = 0.0f; // absolute max
  5548. float max = 0.0f;
  5549. for (int j = 0; j < QK5_0; j++) {
  5550. const float v = src[j];
  5551. if (amax < fabs(v)) {
  5552. amax = fabs(v);
  5553. max = v;
  5554. }
  5555. }
  5556. const float d = max / -16;
  5557. const float id = d ? 1.0f/d : 0.0f;
  5558. dst_data[i00/QK5_0].d = d;
  5559. uint32_t qh = 0;
  5560. for (int j = 0; j < QK5_0/2; ++j) {
  5561. const float x0 = src[0 + j]*id;
  5562. const float x1 = src[QK5_0/2 + j]*id;
  5563. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  5564. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  5565. dst_data[i00/QK5_0].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5566. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5567. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  5568. }
  5569. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  5570. for (int j = 0; j < 4; ++j) {
  5571. dst_data[i00/QK5_0].qh[j] = qh8[j];
  5572. }
  5573. }
  5574. }
  5575. kernel void kernel_cpy_f32_q5_1(
  5576. constant ggml_metal_kargs_cpy & args,
  5577. device const char * src0,
  5578. device char * dst,
  5579. uint3 tgpig[[threadgroup_position_in_grid]],
  5580. ushort3 tpitg[[thread_position_in_threadgroup]],
  5581. ushort3 ntg[[threads_per_threadgroup]]) {
  5582. const int i03 = tgpig[2];
  5583. const int i02 = tgpig[1];
  5584. const int i01 = tgpig[0];
  5585. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5586. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5587. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5588. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5589. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK5_1;
  5590. device block_q5_1 * dst_data = (device block_q5_1 *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5591. for (int64_t i00 = tpitg.x*QK5_1; i00 < args.ne00; i00 += ntg.x*QK5_1) {
  5592. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5593. float max = src[0];
  5594. float min = src[0];
  5595. for (int j = 1; j < QK5_1; j++) {
  5596. const float v = src[j];
  5597. min = v < min ? v : min;
  5598. max = v > max ? v : max;
  5599. }
  5600. const float d = (max - min) / 31;
  5601. const float id = d ? 1.0f/d : 0.0f;
  5602. dst_data[i00/QK5_1].d = d;
  5603. dst_data[i00/QK5_1].m = min;
  5604. uint32_t qh = 0;
  5605. for (int j = 0; j < QK5_1/2; ++j) {
  5606. const float x0 = (src[0 + j] - min)*id;
  5607. const float x1 = (src[QK5_1/2 + j] - min)*id;
  5608. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  5609. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  5610. dst_data[i00/QK5_1].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  5611. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  5612. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  5613. }
  5614. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  5615. for (int j = 0; j < 4; ++j) {
  5616. dst_data[i00/QK5_1].qh[j] = qh8[j];
  5617. }
  5618. }
  5619. }
  5620. static inline int best_index_int8(int n, constant float * val, float x) {
  5621. if (x <= val[0]) return 0;
  5622. if (x >= val[n-1]) return n-1;
  5623. int ml = 0, mu = n-1;
  5624. while (mu-ml > 1) {
  5625. int mav = (ml+mu)/2;
  5626. if (x < val[mav]) mu = mav; else ml = mav;
  5627. }
  5628. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  5629. }
  5630. kernel void kernel_cpy_f32_iq4_nl(
  5631. constant ggml_metal_kargs_cpy & args,
  5632. device const char * src0,
  5633. device char * dst,
  5634. uint3 tgpig[[threadgroup_position_in_grid]],
  5635. ushort3 tpitg[[thread_position_in_threadgroup]],
  5636. ushort3 ntg[[threads_per_threadgroup]]) {
  5637. const int i03 = tgpig[2];
  5638. const int i02 = tgpig[1];
  5639. const int i01 = tgpig[0];
  5640. const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
  5641. const int64_t i3 = n / (args.ne2*args.ne1*args.ne0);
  5642. const int64_t i2 = (n - i3*args.ne2*args.ne1*args.ne0) / (args.ne1*args.ne0);
  5643. const int64_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0) / args.ne0;
  5644. const int64_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0)/QK4_NL;
  5645. device block_iq4_nl * dst_data = (device block_iq4_nl *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5646. for (int64_t i00 = tpitg.x*QK4_NL; i00 < args.ne00; i00 += ntg.x*QK4_NL) {
  5647. device const float * src = (device float *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
  5648. float amax = 0.0f; // absolute max
  5649. float max = 0.0f;
  5650. for (int j = 0; j < QK4_0; j++) {
  5651. const float v = src[j];
  5652. if (amax < fabs(v)) {
  5653. amax = fabs(v);
  5654. max = v;
  5655. }
  5656. }
  5657. const float d = max / kvalues_iq4nl_f[0];
  5658. const float id = d ? 1.0f/d : 0.0f;
  5659. float sumqx = 0, sumq2 = 0;
  5660. for (int j = 0; j < QK4_NL/2; ++j) {
  5661. const float x0 = src[0 + j]*id;
  5662. const float x1 = src[QK4_NL/2 + j]*id;
  5663. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl_f, x0);
  5664. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl_f, x1);
  5665. dst_data[i00/QK4_NL].qs[j] = xi0 | (xi1 << 4);
  5666. const float v0 = kvalues_iq4nl_f[xi0];
  5667. const float v1 = kvalues_iq4nl_f[xi1];
  5668. const float w0 = src[0 + j]*src[0 + j];
  5669. const float w1 = src[QK4_NL/2 + j]*src[QK4_NL/2 + j];
  5670. sumqx += w0*v0*src[j] + w1*v1*src[QK4_NL/2 + j];
  5671. sumq2 += w0*v0*v0 + w1*v1*v1;
  5672. }
  5673. dst_data[i00/QK4_NL].d = sumq2 > 0 ? sumqx/sumq2 : d;
  5674. }
  5675. }
  5676. kernel void kernel_concat(
  5677. constant ggml_metal_kargs_concat & args,
  5678. device const char * src0,
  5679. device const char * src1,
  5680. device char * dst,
  5681. uint3 tgpig[[threadgroup_position_in_grid]],
  5682. ushort3 tpitg[[thread_position_in_threadgroup]],
  5683. ushort3 ntg[[threads_per_threadgroup]]) {
  5684. const int i3 = tgpig.z;
  5685. const int i2 = tgpig.y;
  5686. const int i1 = tgpig.x;
  5687. int o[4] = {0, 0, 0, 0};
  5688. o[args.dim] = args.dim == 0 ? args.ne00 : (args.dim == 1 ? args.ne01 : (args.dim == 2 ? args.ne02 : args.ne03));
  5689. device const float * x;
  5690. for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
  5691. if (i0 < args.ne00 && i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
  5692. x = (device const float *)(src0 + (i3 )*args.nb03 + (i2 )*args.nb02 + (i1 )*args.nb01 + (i0 )*args.nb00);
  5693. } else {
  5694. 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);
  5695. }
  5696. device float * y = (device float *)(dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
  5697. *y = *x;
  5698. }
  5699. }
  5700. template<typename args_t>
  5701. void kernel_mul_mv_q2_K_f32_impl(
  5702. args_t args,
  5703. device const char * src0,
  5704. device const char * src1,
  5705. device char * dst,
  5706. threadgroup char * shmem,
  5707. uint3 tgpig,
  5708. ushort tiisg,
  5709. ushort sgitg) {
  5710. const int nb = args.ne00/QK_K;
  5711. const int r0 = tgpig.x;
  5712. const int r1 = tgpig.y;
  5713. const int im = tgpig.z;
  5714. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  5715. const uint i12 = im%args.ne12;
  5716. const uint i13 = im/args.ne12;
  5717. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5718. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5719. device const block_q2_K * x = (device const block_q2_K *) (src0 + offset0);
  5720. device const float * y = (device const float *) (src1 + offset1);
  5721. float yl[32];
  5722. float sumf[N_DST]={0.f}, all_sum;
  5723. const int ix = tiisg/8; // 0...3
  5724. const int it = tiisg%8; // 0...7
  5725. const int iq = it/4; // 0 or 1
  5726. const int ir = it%4; // 0...3
  5727. const int is = (8*ir)/16;// 0 or 1
  5728. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  5729. for (int ib = ix; ib < nb; ib += 4) {
  5730. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  5731. for (int i = 0; i < 8; ++i) {
  5732. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  5733. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  5734. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  5735. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  5736. }
  5737. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  5738. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  5739. device const half * dh = &x[ib].d;
  5740. for (int row = 0; row < N_DST; row++) {
  5741. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  5742. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  5743. for (int i = 0; i < 8; i += 2) {
  5744. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  5745. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  5746. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  5747. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  5748. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  5749. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  5750. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  5751. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  5752. }
  5753. float dall = dh[0];
  5754. float dmin = dh[1] * 1.f/16.f;
  5755. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  5756. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  5757. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  5758. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  5759. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  5760. qs += args.nb01/2;
  5761. sc += args.nb01;
  5762. dh += args.nb01/2;
  5763. }
  5764. y4 += 4 * QK_K;
  5765. }
  5766. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  5767. for (int row = 0; row < N_DST; ++row) {
  5768. all_sum = simd_sum(sumf[row]);
  5769. if (tiisg == 0) {
  5770. dst_f32[first_row + row] = all_sum;
  5771. }
  5772. }
  5773. }
  5774. [[host_name("kernel_mul_mv_q2_K_f32")]]
  5775. kernel void kernel_mul_mv_q2_K_f32(
  5776. constant ggml_metal_kargs_mul_mv & args,
  5777. device const char * src0,
  5778. device const char * src1,
  5779. device char * dst,
  5780. uint3 tgpig[[threadgroup_position_in_grid]],
  5781. ushort tiisg[[thread_index_in_simdgroup]],
  5782. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5783. kernel_mul_mv_q2_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  5784. }
  5785. template<typename args_t>
  5786. void kernel_mul_mv_q3_K_f32_impl(
  5787. args_t args,
  5788. device const char * src0,
  5789. device const char * src1,
  5790. device char * dst,
  5791. threadgroup char * shmem,
  5792. uint3 tgpig,
  5793. ushort tiisg,
  5794. ushort sgitg) {
  5795. const int nb = args.ne00/QK_K;
  5796. const int r0 = tgpig.x;
  5797. const int r1 = tgpig.y;
  5798. const int im = tgpig.z;
  5799. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  5800. const uint i12 = im%args.ne12;
  5801. const uint i13 = im/args.ne12;
  5802. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5803. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5804. device const block_q3_K * x = (device const block_q3_K *) (src0 + offset0);
  5805. device const float * yy = (device const float *) (src1 + offset1);
  5806. float yl[32];
  5807. //const uint16_t kmask1 = 0x3030;
  5808. //const uint16_t kmask2 = 0x0f0f;
  5809. const int tid = tiisg/4;
  5810. const int ix = tiisg%4;
  5811. const int ip = tid/4; // 0 or 1
  5812. const int il = 2*((tid%4)/2); // 0 or 2
  5813. const int ir = tid%2;
  5814. const int n = 8;
  5815. const int l0 = n*ir;
  5816. // One would think that the Metal compiler would figure out that ip and il can only have
  5817. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  5818. // with these two tales.
  5819. //
  5820. // Possible masks for the high bit
  5821. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  5822. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  5823. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  5824. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  5825. // Possible masks for the low 2 bits
  5826. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  5827. const ushort4 hm = mm[2*ip + il/2];
  5828. const short shift = 2*il;
  5829. const float v1 = il == 0 ? 4.f : 64.f;
  5830. const float v2 = 4.f * v1;
  5831. const uint16_t s_shift1 = 4*ip;
  5832. const uint16_t s_shift2 = s_shift1 + il;
  5833. const int q_offset = 32*ip + l0;
  5834. const int y_offset = 128*ip + 32*il + l0;
  5835. device const float * y1 = yy + ix*QK_K + y_offset;
  5836. uint32_t scales32, aux32;
  5837. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  5838. thread const int8_t * scales = (thread const int8_t *)&scales32;
  5839. float sumf1[2] = {0.f};
  5840. float sumf2[2] = {0.f};
  5841. for (int i = ix; i < nb; i += 4) {
  5842. for (int l = 0; l < 8; ++l) {
  5843. yl[l+ 0] = y1[l+ 0];
  5844. yl[l+ 8] = y1[l+16];
  5845. yl[l+16] = y1[l+32];
  5846. yl[l+24] = y1[l+48];
  5847. }
  5848. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  5849. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  5850. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  5851. device const half * dh = &x[i].d;
  5852. for (int row = 0; row < 2; ++row) {
  5853. const float d_all = (float)dh[0];
  5854. scales16[0] = a[4];
  5855. scales16[1] = a[5];
  5856. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  5857. scales16[0] = a[il+0];
  5858. scales16[1] = a[il+1];
  5859. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  5860. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  5861. for (int l = 0; l < n; l += 2) {
  5862. const int32_t qs = q[l/2];
  5863. s1 += yl[l+0] * (qs & qm[il/2][0]);
  5864. s2 += yl[l+1] * (qs & qm[il/2][1]);
  5865. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  5866. s4 += yl[l+16] * (qs & qm[il/2][2]);
  5867. s5 += yl[l+17] * (qs & qm[il/2][3]);
  5868. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  5869. }
  5870. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  5871. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  5872. sumf1[row] += d1 * (scales[0] - 32);
  5873. sumf2[row] += d2 * (scales[2] - 32);
  5874. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  5875. for (int l = 0; l < n; l += 2) {
  5876. const int32_t qs = q[l/2+8];
  5877. s1 += yl[l+8] * (qs & qm[il/2][0]);
  5878. s2 += yl[l+9] * (qs & qm[il/2][1]);
  5879. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  5880. s4 += yl[l+24] * (qs & qm[il/2][2]);
  5881. s5 += yl[l+25] * (qs & qm[il/2][3]);
  5882. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  5883. }
  5884. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  5885. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  5886. sumf1[row] += d1 * (scales[1] - 32);
  5887. sumf2[row] += d2 * (scales[3] - 32);
  5888. q += args.nb01/2;
  5889. h += args.nb01/2;
  5890. a += args.nb01/2;
  5891. dh += args.nb01/2;
  5892. }
  5893. y1 += 4 * QK_K;
  5894. }
  5895. for (int row = 0; row < 2; ++row) {
  5896. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  5897. sumf1[row] = simd_sum(sumf);
  5898. }
  5899. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  5900. if (tiisg == 0) {
  5901. for (int row = 0; row < 2; ++row) {
  5902. dst_f32[first_row + row] = sumf1[row];
  5903. }
  5904. }
  5905. }
  5906. [[host_name("kernel_mul_mv_q3_K_f32")]]
  5907. kernel void kernel_mul_mv_q3_K_f32(
  5908. constant ggml_metal_kargs_mul_mv & args,
  5909. device const char * src0,
  5910. device const char * src1,
  5911. device char * dst,
  5912. uint3 tgpig[[threadgroup_position_in_grid]],
  5913. ushort tiisg[[thread_index_in_simdgroup]],
  5914. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  5915. kernel_mul_mv_q3_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  5916. }
  5917. template<typename args_t>
  5918. void kernel_mul_mv_q4_K_f32_impl(
  5919. args_t args,
  5920. device const char * src0,
  5921. device const char * src1,
  5922. device char * dst,
  5923. threadgroup char * shmem,
  5924. uint3 tgpig,
  5925. ushort tiisg,
  5926. ushort sgitg) {
  5927. const uint16_t kmask1 = 0x3f3f;
  5928. const uint16_t kmask2 = 0x0f0f;
  5929. const uint16_t kmask3 = 0xc0c0;
  5930. const int ix = tiisg/8; // 0...3
  5931. const int it = tiisg%8; // 0...7
  5932. const int iq = it/4; // 0 or 1
  5933. const int ir = it%4; // 0...3
  5934. const int nb = args.ne00/QK_K;
  5935. const int r0 = tgpig.x;
  5936. const int r1 = tgpig.y;
  5937. const int im = tgpig.z;
  5938. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  5939. const int first_row = r0 * N_DST;
  5940. const uint i12 = im%args.ne12;
  5941. const uint i13 = im/args.ne12;
  5942. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  5943. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  5944. device const block_q4_K * x = (device const block_q4_K *) (src0 + offset0);
  5945. device const float * y = (device const float *) (src1 + offset1);
  5946. float yl[16];
  5947. float yh[16];
  5948. float sumf[N_DST]={0.f}, all_sum;
  5949. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  5950. uint16_t sc16[4];
  5951. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  5952. for (int ib = ix; ib < nb; ib += 4) {
  5953. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  5954. for (int i = 0; i < 8; ++i) {
  5955. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  5956. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  5957. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  5958. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  5959. }
  5960. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  5961. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  5962. device const half * dh = &x[ib].d;
  5963. for (int row = 0; row < N_DST; row++) {
  5964. sc16[0] = sc[0] & kmask1;
  5965. sc16[1] = sc[2] & kmask1;
  5966. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  5967. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  5968. device const uint16_t * q2 = q1 + 32;
  5969. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  5970. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  5971. for (int i = 0; i < 8; i += 2) {
  5972. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  5973. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  5974. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  5975. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  5976. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  5977. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  5978. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  5979. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  5980. }
  5981. float dall = dh[0];
  5982. float dmin = dh[1];
  5983. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  5984. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  5985. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  5986. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  5987. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  5988. q1 += args.nb01/2;
  5989. sc += args.nb01/2;
  5990. dh += args.nb01/2;
  5991. }
  5992. y4 += 4 * QK_K;
  5993. }
  5994. device float * dst_f32 = (device float *) dst + (int64_t)im*args.ne0*args.ne1 + (int64_t)r1*args.ne0;
  5995. for (int row = 0; row < N_DST; ++row) {
  5996. all_sum = simd_sum(sumf[row]);
  5997. if (tiisg == 0) {
  5998. dst_f32[first_row + row] = all_sum;
  5999. }
  6000. }
  6001. }
  6002. [[host_name("kernel_mul_mv_q4_K_f32")]]
  6003. kernel void kernel_mul_mv_q4_K_f32(
  6004. constant ggml_metal_kargs_mul_mv & args,
  6005. device const char * src0,
  6006. device const char * src1,
  6007. device char * dst,
  6008. uint3 tgpig[[threadgroup_position_in_grid]],
  6009. ushort tiisg[[thread_index_in_simdgroup]],
  6010. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6011. kernel_mul_mv_q4_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6012. }
  6013. template<typename args_t>
  6014. void kernel_mul_mv_q5_K_f32_impl(
  6015. args_t args,
  6016. device const char * src0,
  6017. device const char * src1,
  6018. device char * dst,
  6019. threadgroup char * shmem,
  6020. uint3 tgpig,
  6021. ushort tiisg,
  6022. ushort sgitg) {
  6023. const int nb = args.ne00/QK_K;
  6024. const int r0 = tgpig.x;
  6025. const int r1 = tgpig.y;
  6026. const int im = tgpig.z;
  6027. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  6028. const uint i12 = im%args.ne12;
  6029. const uint i13 = im/args.ne12;
  6030. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6031. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6032. device const block_q5_K * x = (device const block_q5_K *) (src0 + offset0);
  6033. device const float * yy = (device const float *) (src1 + offset1);
  6034. float sumf[2]={0.f};
  6035. float yl[16], yh[16];
  6036. const uint16_t kmask1 = 0x3f3f;
  6037. const uint16_t kmask2 = 0x0f0f;
  6038. const uint16_t kmask3 = 0xc0c0;
  6039. const int tid = tiisg/4;
  6040. const int ix = tiisg%4;
  6041. const int iq = tid/4;
  6042. const int ir = tid%4;
  6043. const int n = 8;
  6044. const int l0 = n*ir;
  6045. const int q_offset = 32*iq + l0;
  6046. const int y_offset = 64*iq + l0;
  6047. const uint8_t hm1 = 1u << (2*iq);
  6048. const uint8_t hm2 = hm1 << 1;
  6049. const uint8_t hm3 = hm1 << 4;
  6050. const uint8_t hm4 = hm2 << 4;
  6051. uint16_t sc16[4];
  6052. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  6053. device const float * y1 = yy + ix*QK_K + y_offset;
  6054. for (int i = ix; i < nb; i += 4) {
  6055. device const uint8_t * q1 = x[i].qs + q_offset;
  6056. device const uint8_t * qh = x[i].qh + l0;
  6057. device const half * dh = &x[i].d;
  6058. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  6059. device const float * y2 = y1 + 128;
  6060. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  6061. for (int l = 0; l < 8; ++l) {
  6062. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  6063. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  6064. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  6065. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  6066. }
  6067. for (int row = 0; row < 2; ++row) {
  6068. device const uint8_t * q2 = q1 + 64;
  6069. sc16[0] = a[0] & kmask1;
  6070. sc16[1] = a[2] & kmask1;
  6071. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  6072. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  6073. float4 acc1 = {0.f};
  6074. float4 acc2 = {0.f};
  6075. for (int l = 0; l < n; ++l) {
  6076. uint8_t h = qh[l];
  6077. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  6078. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  6079. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  6080. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  6081. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  6082. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  6083. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  6084. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  6085. }
  6086. const float dall = dh[0];
  6087. const float dmin = dh[1];
  6088. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  6089. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  6090. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  6091. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  6092. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  6093. q1 += args.nb01;
  6094. qh += args.nb01;
  6095. dh += args.nb01/2;
  6096. a += args.nb01/2;
  6097. }
  6098. y1 += 4 * QK_K;
  6099. }
  6100. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6101. for (int row = 0; row < 2; ++row) {
  6102. const float tot = simd_sum(sumf[row]);
  6103. if (tiisg == 0) {
  6104. dst_f32[first_row + row] = tot;
  6105. }
  6106. }
  6107. }
  6108. [[host_name("kernel_mul_mv_q5_K_f32")]]
  6109. kernel void kernel_mul_mv_q5_K_f32(
  6110. constant ggml_metal_kargs_mul_mv & args,
  6111. device const char * src0,
  6112. device const char * src1,
  6113. device char * dst,
  6114. uint3 tgpig[[threadgroup_position_in_grid]],
  6115. ushort tiisg[[thread_index_in_simdgroup]],
  6116. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6117. kernel_mul_mv_q5_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6118. }
  6119. template <typename args_t>
  6120. void kernel_mul_mv_q6_K_f32_impl(
  6121. args_t args,
  6122. device const char * src0,
  6123. device const char * src1,
  6124. device char * dst,
  6125. threadgroup char * shmem,
  6126. uint3 tgpig,
  6127. ushort tiisg,
  6128. ushort sgitg) {
  6129. const uint8_t kmask1 = 0x03;
  6130. const uint8_t kmask2 = 0x0C;
  6131. const uint8_t kmask3 = 0x30;
  6132. const uint8_t kmask4 = 0xC0;
  6133. const int nb = args.ne00/QK_K;
  6134. const int r0 = tgpig.x;
  6135. const int r1 = tgpig.y;
  6136. const int im = tgpig.z;
  6137. const int row = 2*r0 + sgitg;
  6138. const uint i12 = im%args.ne12;
  6139. const uint i13 = im/args.ne12;
  6140. const uint64_t offset0 = row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6141. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6142. device const block_q6_K * x = (device const block_q6_K *) (src0 + offset0);
  6143. device const float * yy = (device const float *) (src1 + offset1);
  6144. float sumf = 0;
  6145. const int tid = tiisg/2;
  6146. const int ix = tiisg%2;
  6147. const int ip = tid/8; // 0 or 1
  6148. const int il = tid%8;
  6149. const int n = 4;
  6150. const int l0 = n*il;
  6151. const int is = 8*ip + l0/16;
  6152. const int y_offset = 128*ip + l0;
  6153. const int q_offset_l = 64*ip + l0;
  6154. const int q_offset_h = 32*ip + l0;
  6155. for (int i = ix; i < nb; i += 2) {
  6156. device const uint8_t * q1 = x[i].ql + q_offset_l;
  6157. device const uint8_t * q2 = q1 + 32;
  6158. device const uint8_t * qh = x[i].qh + q_offset_h;
  6159. device const int8_t * sc = x[i].scales + is;
  6160. device const float * y = yy + i * QK_K + y_offset;
  6161. const float dall = x[i].d;
  6162. float4 sums = {0.f, 0.f, 0.f, 0.f};
  6163. for (int l = 0; l < n; ++l) {
  6164. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  6165. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  6166. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  6167. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  6168. }
  6169. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  6170. }
  6171. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6172. const float tot = simd_sum(sumf);
  6173. if (tiisg == 0) {
  6174. dst_f32[row] = tot;
  6175. }
  6176. }
  6177. [[host_name("kernel_mul_mv_q6_K_f32")]]
  6178. kernel void kernel_mul_mv_q6_K_f32(
  6179. constant ggml_metal_kargs_mul_mv & args,
  6180. device const char * src0,
  6181. device const char * src1,
  6182. device char * dst,
  6183. uint3 tgpig[[threadgroup_position_in_grid]],
  6184. ushort tiisg[[thread_index_in_simdgroup]],
  6185. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6186. kernel_mul_mv_q6_K_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6187. }
  6188. // ======================= "True" 2-bit
  6189. template<typename args_t>
  6190. void kernel_mul_mv_iq2_xxs_f32_impl(
  6191. args_t args,
  6192. device const char * src0,
  6193. device const char * src1,
  6194. device char * dst,
  6195. threadgroup char * shmem,
  6196. uint3 tgpig,
  6197. ushort tiisg,
  6198. ushort sgitg) {
  6199. const int nb = args.ne00/QK_K;
  6200. const int r0 = tgpig.x;
  6201. const int r1 = tgpig.y;
  6202. const int im = tgpig.z;
  6203. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6204. const uint i12 = im%args.ne12;
  6205. const uint i13 = im/args.ne12;
  6206. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6207. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6208. device const block_iq2_xxs * x = (device const block_iq2_xxs *) (src0 + offset0);
  6209. device const float * y = (device const float *) (src1 + offset1);
  6210. float yl[32];
  6211. float sumf[N_DST]={0.f}, all_sum;
  6212. const int nb32 = nb * (QK_K / 32);
  6213. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  6214. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  6215. {
  6216. int nval = 4;
  6217. int pos = (32*sgitg + tiisg)*nval;
  6218. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xxs_grid[pos + i];
  6219. nval = 2;
  6220. pos = (32*sgitg + tiisg)*nval;
  6221. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6222. threadgroup_barrier(mem_flags::mem_threadgroup);
  6223. }
  6224. const int ix = tiisg;
  6225. device const float * y4 = y + 32 * ix;
  6226. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6227. for (int i = 0; i < 32; ++i) {
  6228. yl[i] = y4[i];
  6229. }
  6230. const int ibl = ib32 / (QK_K / 32);
  6231. const int ib = ib32 % (QK_K / 32);
  6232. device const block_iq2_xxs * xr = x + ibl;
  6233. device const uint16_t * q2 = xr->qs + 4 * ib;
  6234. device const half * dh = &xr->d;
  6235. for (int row = 0; row < N_DST; row++) {
  6236. const float db = dh[0];
  6237. device const uint8_t * aux8 = (device const uint8_t *)q2;
  6238. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6239. const float d = db * (0.5f + (aux32 >> 28));
  6240. float sum = 0;
  6241. for (int l = 0; l < 4; ++l) {
  6242. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + aux8[l]);
  6243. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  6244. for (int j = 0; j < 8; ++j) {
  6245. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6246. }
  6247. }
  6248. sumf[row] += d * sum;
  6249. dh += args.nb01/2;
  6250. q2 += args.nb01/2;
  6251. }
  6252. y4 += 32 * 32;
  6253. }
  6254. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6255. for (int row = 0; row < N_DST; ++row) {
  6256. all_sum = simd_sum(sumf[row]);
  6257. if (tiisg == 0) {
  6258. dst_f32[first_row + row] = all_sum * 0.25f;
  6259. }
  6260. }
  6261. }
  6262. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  6263. kernel void kernel_mul_mv_iq2_xxs_f32(
  6264. constant ggml_metal_kargs_mul_mv & args,
  6265. device const char * src0,
  6266. device const char * src1,
  6267. device char * dst,
  6268. threadgroup char * shmem [[threadgroup(0)]],
  6269. uint3 tgpig[[threadgroup_position_in_grid]],
  6270. ushort tiisg[[thread_index_in_simdgroup]],
  6271. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6272. kernel_mul_mv_iq2_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6273. }
  6274. template<typename args_t>
  6275. void kernel_mul_mv_iq2_xs_f32_impl(
  6276. args_t args,
  6277. device const char * src0,
  6278. device const char * src1,
  6279. device char * dst,
  6280. threadgroup char * shmem,
  6281. uint3 tgpig,
  6282. ushort tiisg,
  6283. ushort sgitg) {
  6284. const int nb = args.ne00/QK_K;
  6285. const int r0 = tgpig.x;
  6286. const int r1 = tgpig.y;
  6287. const int im = tgpig.z;
  6288. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6289. const uint i12 = im%args.ne12;
  6290. const uint i13 = im/args.ne12;
  6291. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6292. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6293. device const block_iq2_xs * x = (device const block_iq2_xs *) (src0 + offset0);
  6294. device const float * y = (device const float *) (src1 + offset1);
  6295. float yl[32];
  6296. float sumf[N_DST]={0.f}, all_sum;
  6297. const int nb32 = nb * (QK_K / 32);
  6298. threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem);
  6299. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 512);
  6300. {
  6301. int nval = 8;
  6302. int pos = (32*sgitg + tiisg)*nval;
  6303. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xs_grid[pos + i];
  6304. nval = 2;
  6305. pos = (32*sgitg + tiisg)*nval;
  6306. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6307. threadgroup_barrier(mem_flags::mem_threadgroup);
  6308. }
  6309. const int ix = tiisg;
  6310. device const float * y4 = y + 32 * ix;
  6311. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6312. for (int i = 0; i < 32; ++i) {
  6313. yl[i] = y4[i];
  6314. }
  6315. const int ibl = ib32 / (QK_K / 32);
  6316. const int ib = ib32 % (QK_K / 32);
  6317. device const block_iq2_xs * xr = x + ibl;
  6318. device const uint16_t * q2 = xr->qs + 4 * ib;
  6319. device const uint8_t * sc = xr->scales + ib;
  6320. device const half * dh = &xr->d;
  6321. for (int row = 0; row < N_DST; row++) {
  6322. const float db = dh[0];
  6323. const uint8_t ls1 = sc[0] & 0xf;
  6324. const uint8_t ls2 = sc[0] >> 4;
  6325. const float d1 = db * (0.5f + ls1);
  6326. const float d2 = db * (0.5f + ls2);
  6327. float sum1 = 0, sum2 = 0;
  6328. for (int l = 0; l < 2; ++l) {
  6329. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  6330. const uint8_t signs = ssigns[(q2[l] >> 9)];
  6331. for (int j = 0; j < 8; ++j) {
  6332. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6333. }
  6334. }
  6335. for (int l = 2; l < 4; ++l) {
  6336. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511));
  6337. const uint8_t signs = ssigns[(q2[l] >> 9)];
  6338. for (int j = 0; j < 8; ++j) {
  6339. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  6340. }
  6341. }
  6342. sumf[row] += d1 * sum1 + d2 * sum2;
  6343. dh += args.nb01/2;
  6344. q2 += args.nb01/2;
  6345. sc += args.nb01;
  6346. }
  6347. y4 += 32 * 32;
  6348. }
  6349. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6350. for (int row = 0; row < N_DST; ++row) {
  6351. all_sum = simd_sum(sumf[row]);
  6352. if (tiisg == 0) {
  6353. dst_f32[first_row + row] = all_sum * 0.25f;
  6354. }
  6355. }
  6356. }
  6357. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  6358. kernel void kernel_mul_mv_iq2_xs_f32(
  6359. constant ggml_metal_kargs_mul_mv & args,
  6360. device const char * src0,
  6361. device const char * src1,
  6362. device char * dst,
  6363. threadgroup char * shmem [[threadgroup(0)]],
  6364. uint3 tgpig[[threadgroup_position_in_grid]],
  6365. ushort tiisg[[thread_index_in_simdgroup]],
  6366. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6367. kernel_mul_mv_iq2_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6368. }
  6369. template <typename args_t>
  6370. void kernel_mul_mv_iq3_xxs_f32_impl(
  6371. args_t args,
  6372. device const char * src0,
  6373. device const char * src1,
  6374. device char * dst,
  6375. threadgroup char * shmem,
  6376. uint3 tgpig,
  6377. ushort tiisg,
  6378. ushort sgitg) {
  6379. const int nb = args.ne00/QK_K;
  6380. const int r0 = tgpig.x;
  6381. const int r1 = tgpig.y;
  6382. const int im = tgpig.z;
  6383. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6384. const uint i12 = im%args.ne12;
  6385. const uint i13 = im/args.ne12;
  6386. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6387. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6388. device const block_iq3_xxs * x = (device const block_iq3_xxs *) (src0 + offset0);
  6389. device const float * y = (device const float *) (src1 + offset1);
  6390. float yl[32];
  6391. float sumf[N_DST]={0.f}, all_sum;
  6392. const int nb32 = nb * (QK_K / 32);
  6393. threadgroup uint32_t * svalues = (threadgroup uint32_t *)(shmem);
  6394. threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256);
  6395. {
  6396. int nval = 4;
  6397. int pos = (32*sgitg + tiisg)*nval;
  6398. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3xxs_grid[pos + i];
  6399. nval = 2;
  6400. pos = (32*sgitg + tiisg)*nval;
  6401. for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i];
  6402. threadgroup_barrier(mem_flags::mem_threadgroup);
  6403. }
  6404. const int ix = tiisg;
  6405. device const float * y4 = y + 32 * ix;
  6406. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6407. for (int i = 0; i < 32; ++i) {
  6408. yl[i] = y4[i];
  6409. }
  6410. const int ibl = ib32 / (QK_K / 32);
  6411. const int ib = ib32 % (QK_K / 32);
  6412. device const block_iq3_xxs * xr = x + ibl;
  6413. device const uint8_t * q3 = xr->qs + 8 * ib;
  6414. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  6415. device const half * dh = &xr->d;
  6416. for (int row = 0; row < N_DST; row++) {
  6417. const float db = dh[0];
  6418. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  6419. const float d = db * (0.5f + (aux32 >> 28));
  6420. float2 sum = {0};
  6421. for (int l = 0; l < 4; ++l) {
  6422. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + q3[2*l+0]);
  6423. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + q3[2*l+1]);
  6424. const uint8_t signs = ssigns[(aux32 >> 7*l) & 127];
  6425. for (int j = 0; j < 4; ++j) {
  6426. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  6427. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  6428. }
  6429. }
  6430. sumf[row] += d * (sum[0] + sum[1]);
  6431. dh += args.nb01/2;
  6432. q3 += args.nb01;
  6433. gas += args.nb01/2;
  6434. }
  6435. y4 += 32 * 32;
  6436. }
  6437. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6438. for (int row = 0; row < N_DST; ++row) {
  6439. all_sum = simd_sum(sumf[row]);
  6440. if (tiisg == 0) {
  6441. dst_f32[first_row + row] = all_sum * 0.5f;
  6442. }
  6443. }
  6444. }
  6445. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  6446. kernel void kernel_mul_mv_iq3_xxs_f32(
  6447. constant ggml_metal_kargs_mul_mv & args,
  6448. device const char * src0,
  6449. device const char * src1,
  6450. device char * dst,
  6451. threadgroup char * shmem [[threadgroup(0)]],
  6452. uint3 tgpig[[threadgroup_position_in_grid]],
  6453. ushort tiisg[[thread_index_in_simdgroup]],
  6454. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6455. kernel_mul_mv_iq3_xxs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6456. }
  6457. template<typename args_t>
  6458. void kernel_mul_mv_iq3_s_f32_impl(
  6459. args_t args,
  6460. device const char * src0,
  6461. device const char * src1,
  6462. device char * dst,
  6463. threadgroup char * shmem,
  6464. uint3 tgpig,
  6465. ushort tiisg,
  6466. ushort sgitg) {
  6467. const int nb = args.ne00/QK_K;
  6468. const int r0 = tgpig.x;
  6469. const int r1 = tgpig.y;
  6470. const int im = tgpig.z;
  6471. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6472. const uint i12 = im%args.ne12;
  6473. const uint i13 = im/args.ne12;
  6474. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6475. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6476. device const block_iq3_s * x = (device const block_iq3_s *) (src0 + offset0);
  6477. device const float * y = (device const float *) (src1 + offset1);
  6478. float yl[32];
  6479. float sumf[N_DST]={0.f}, all_sum;
  6480. const int nb32 = nb * (QK_K / 32);
  6481. threadgroup uint32_t * svalues = (threadgroup uint32_t *) shmem;
  6482. {
  6483. int nval = 8;
  6484. int pos = (32*sgitg + tiisg)*nval;
  6485. for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3s_grid[pos + i];
  6486. threadgroup_barrier(mem_flags::mem_threadgroup);
  6487. }
  6488. const int ix = tiisg;
  6489. device const float * y4 = y + 32 * ix;
  6490. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6491. for (int i = 0; i < 32; ++i) {
  6492. yl[i] = y4[i];
  6493. }
  6494. const int ibl = ib32 / (QK_K / 32);
  6495. const int ib = ib32 % (QK_K / 32);
  6496. device const block_iq3_s * xr = x + ibl;
  6497. device const uint8_t * qs = xr->qs + 8 * ib;
  6498. device const uint8_t * qh = xr->qh + ib;
  6499. device const uint8_t * sc = xr->scales + (ib/2);
  6500. device const uint8_t * signs = xr->signs + 4 * ib;
  6501. device const half * dh = &xr->d;
  6502. for (int row = 0; row < N_DST; row++) {
  6503. const float db = dh[0];
  6504. const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf));
  6505. float2 sum = {0};
  6506. for (int l = 0; l < 4; ++l) {
  6507. const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? svalues + 256 : svalues;
  6508. const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? svalues + 256 : svalues;
  6509. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]);
  6510. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]);
  6511. for (int j = 0; j < 4; ++j) {
  6512. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  6513. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  6514. }
  6515. }
  6516. sumf[row] += d * (sum[0] + sum[1]);
  6517. dh += args.nb01/2;
  6518. qs += args.nb01;
  6519. qh += args.nb01;
  6520. sc += args.nb01;
  6521. signs += args.nb01;
  6522. }
  6523. y4 += 32 * 32;
  6524. }
  6525. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6526. for (int row = 0; row < N_DST; ++row) {
  6527. all_sum = simd_sum(sumf[row]);
  6528. if (tiisg == 0) {
  6529. dst_f32[first_row + row] = all_sum;
  6530. }
  6531. }
  6532. }
  6533. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  6534. kernel void kernel_mul_mv_iq3_s_f32(
  6535. constant ggml_metal_kargs_mul_mv & args,
  6536. device const char * src0,
  6537. device const char * src1,
  6538. device char * dst,
  6539. threadgroup char * shmem [[threadgroup(0)]],
  6540. uint3 tgpig[[threadgroup_position_in_grid]],
  6541. ushort tiisg[[thread_index_in_simdgroup]],
  6542. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6543. kernel_mul_mv_iq3_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6544. }
  6545. template <typename args_t>
  6546. void kernel_mul_mv_iq2_s_f32_impl(
  6547. args_t args,
  6548. device const char * src0,
  6549. device const char * src1,
  6550. device char * dst,
  6551. threadgroup char * shmem,
  6552. uint3 tgpig,
  6553. ushort tiisg,
  6554. ushort sgitg) {
  6555. const int nb = args.ne00/QK_K;
  6556. const int r0 = tgpig.x;
  6557. const int r1 = tgpig.y;
  6558. const int im = tgpig.z;
  6559. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6560. const uint i12 = im%args.ne12;
  6561. const uint i13 = im/args.ne12;
  6562. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6563. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6564. device const block_iq2_s * x = (device const block_iq2_s *) (src0 + offset0);
  6565. device const float * y = (device const float *) (src1 + offset1);
  6566. float yl[32];
  6567. float sumf[N_DST]={0.f}, all_sum;
  6568. const int nb32 = nb * (QK_K / 32);
  6569. //threadgroup uint64_t * svalues = (threadgroup uint64_t *) shmem;
  6570. //{
  6571. // int nval = 32;
  6572. // int pos = (32*sgitg + tiisg)*nval;
  6573. // for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2s_grid[pos + i];
  6574. // threadgroup_barrier(mem_flags::mem_threadgroup);
  6575. //}
  6576. const int ix = tiisg;
  6577. device const float * y4 = y + 32 * ix;
  6578. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6579. for (int i = 0; i < 32; ++i) {
  6580. yl[i] = y4[i];
  6581. }
  6582. const int ibl = ib32 / (QK_K / 32);
  6583. const int ib = ib32 % (QK_K / 32);
  6584. device const block_iq2_s * xr = x + ibl;
  6585. device const uint8_t * qs = xr->qs + 4 * ib;
  6586. device const uint8_t * qh = xr->qh + ib;
  6587. device const uint8_t * sc = xr->scales + ib;
  6588. device const uint8_t * signs = qs + QK_K/8;
  6589. device const half * dh = &xr->d;
  6590. for (int row = 0; row < N_DST; row++) {
  6591. const float db = dh[0];
  6592. const float d1 = db * (0.5f + (sc[0] & 0xf));
  6593. const float d2 = db * (0.5f + (sc[0] >> 4));
  6594. float2 sum = {0};
  6595. for (int l = 0; l < 2; ++l) {
  6596. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  6597. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  6598. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  6599. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  6600. for (int j = 0; j < 8; ++j) {
  6601. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  6602. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  6603. }
  6604. }
  6605. sumf[row] += d1 * sum[0] + d2 * sum[1];
  6606. dh += args.nb01/2;
  6607. qs += args.nb01;
  6608. qh += args.nb01;
  6609. sc += args.nb01;
  6610. signs += args.nb01;
  6611. }
  6612. y4 += 32 * 32;
  6613. }
  6614. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6615. for (int row = 0; row < N_DST; ++row) {
  6616. all_sum = simd_sum(sumf[row]);
  6617. if (tiisg == 0) {
  6618. dst_f32[first_row + row] = all_sum * 0.25f;
  6619. }
  6620. }
  6621. }
  6622. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  6623. kernel void kernel_mul_mv_iq2_s_f32(
  6624. constant ggml_metal_kargs_mul_mv & args,
  6625. device const char * src0,
  6626. device const char * src1,
  6627. device char * dst,
  6628. threadgroup char * shmem [[threadgroup(0)]],
  6629. uint3 tgpig[[threadgroup_position_in_grid]],
  6630. ushort tiisg[[thread_index_in_simdgroup]],
  6631. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6632. kernel_mul_mv_iq2_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6633. }
  6634. template<typename args_t>
  6635. void kernel_mul_mv_iq1_s_f32_impl(
  6636. args_t args,
  6637. device const char * src0,
  6638. device const char * src1,
  6639. device char * dst,
  6640. threadgroup char * shmem,
  6641. uint3 tgpig,
  6642. ushort tiisg,
  6643. ushort sgitg) {
  6644. const int nb = args.ne00/QK_K;
  6645. const int r0 = tgpig.x;
  6646. const int r1 = tgpig.y;
  6647. const int im = tgpig.z;
  6648. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6649. const uint i12 = im%args.ne12;
  6650. const uint i13 = im/args.ne12;
  6651. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6652. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6653. device const block_iq1_s * x = (device const block_iq1_s *) (src0 + offset0);
  6654. device const float * y = (device const float *) (src1 + offset1);
  6655. float yl[32];
  6656. float sumf[N_DST]={0.f}, all_sum;
  6657. const int nb32 = nb * (QK_K / 32);
  6658. const int ix = tiisg;
  6659. device const float * y4 = y + 32 * ix;
  6660. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6661. float sumy = 0;
  6662. for (int i = 0; i < 32; ++i) {
  6663. yl[i] = y4[i];
  6664. sumy += yl[i];
  6665. }
  6666. const int ibl = ib32 / (QK_K / 32);
  6667. const int ib = ib32 % (QK_K / 32);
  6668. device const block_iq1_s * xr = x + ibl;
  6669. device const uint8_t * qs = xr->qs + 4 * ib;
  6670. device const uint16_t * qh = xr->qh + ib;
  6671. device const half * dh = &xr->d;
  6672. for (int row = 0; row < N_DST; row++) {
  6673. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  6674. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 5) & 0x700)));
  6675. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[0] << 2) & 0x700)));
  6676. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[0] >> 1) & 0x700)));
  6677. float sum = 0;
  6678. for (int j = 0; j < 4; ++j) {
  6679. sum += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  6680. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4)
  6681. + yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  6682. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  6683. }
  6684. sumf[row] += (float)dh[0] * (sum + sumy * (qh[0] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA)) * (2*((qh[0] >> 12) & 7) + 1);
  6685. dh += args.nb01/2;
  6686. qs += args.nb01;
  6687. qh += args.nb01/2;
  6688. }
  6689. y4 += 32 * 32;
  6690. }
  6691. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6692. for (int row = 0; row < N_DST; ++row) {
  6693. all_sum = simd_sum(sumf[row]);
  6694. if (tiisg == 0) {
  6695. dst_f32[first_row + row] = all_sum;
  6696. }
  6697. }
  6698. }
  6699. template <typename args_t>
  6700. void kernel_mul_mv_iq1_m_f32_impl(
  6701. args_t args,
  6702. device const char * src0,
  6703. device const char * src1,
  6704. device char * dst,
  6705. threadgroup char * shmem,
  6706. uint3 tgpig,
  6707. ushort tiisg,
  6708. ushort sgitg) {
  6709. const int nb = args.ne00/QK_K;
  6710. const int r0 = tgpig.x;
  6711. const int r1 = tgpig.y;
  6712. const int im = tgpig.z;
  6713. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  6714. const uint i12 = im%args.ne12;
  6715. const uint i13 = im/args.ne12;
  6716. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6717. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6718. device const block_iq1_m * x = (device const block_iq1_m *) (src0 + offset0);
  6719. device const float * y = (device const float *) (src1 + offset1);
  6720. float yl[32];
  6721. float sumf[N_DST]={0.f}, all_sum;
  6722. const int nb32 = nb * (QK_K / 32);
  6723. const int ix = tiisg;
  6724. device const float * y4 = y + 32 * ix;
  6725. iq1m_scale_t scale;
  6726. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  6727. float4 sumy = {0.f};
  6728. for (int i = 0; i < 8; ++i) {
  6729. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  6730. yl[i+ 8] = y4[i+ 8]; sumy[1] += yl[i+ 8];
  6731. yl[i+16] = y4[i+16]; sumy[2] += yl[i+16];
  6732. yl[i+24] = y4[i+24]; sumy[3] += yl[i+24];
  6733. }
  6734. const int ibl = ib32 / (QK_K / 32);
  6735. const int ib = ib32 % (QK_K / 32);
  6736. device const block_iq1_m * xr = x + ibl;
  6737. device const uint8_t * qs = xr->qs + 4 * ib;
  6738. device const uint8_t * qh = xr->qh + 2 * ib;
  6739. device const uint16_t * sc = (device const uint16_t *)xr->scales;
  6740. for (int row = 0; row < N_DST; row++) {
  6741. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6742. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  6743. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  6744. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[1] << 8) & 0x700)));
  6745. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[1] << 4) & 0x700)));
  6746. float2 sum = {0.f};
  6747. for (int j = 0; j < 4; ++j) {
  6748. sum[0] += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  6749. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4);
  6750. sum[1] += yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  6751. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  6752. }
  6753. 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);
  6754. 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);
  6755. sumf[row] += (float)scale.f16 * ((sum[0] + delta1) * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 7) + 1) +
  6756. (sum[1] + delta2) * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 7) + 1));
  6757. sc += args.nb01/2;
  6758. qs += args.nb01;
  6759. qh += args.nb01;
  6760. }
  6761. y4 += 32 * 32;
  6762. }
  6763. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6764. for (int row = 0; row < N_DST; ++row) {
  6765. all_sum = simd_sum(sumf[row]);
  6766. if (tiisg == 0) {
  6767. dst_f32[first_row + row] = all_sum;
  6768. }
  6769. }
  6770. }
  6771. template<typename args_t>
  6772. void kernel_mul_mv_iq4_nl_f32_impl(
  6773. args_t args,
  6774. device const char * src0,
  6775. device const char * src1,
  6776. device char * dst,
  6777. threadgroup char * shmem,
  6778. uint3 tgpig,
  6779. ushort tiisg,
  6780. ushort sgitg) {
  6781. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  6782. const int nb = args.ne00/QK4_NL;
  6783. const int r0 = tgpig.x;
  6784. const int r1 = tgpig.y;
  6785. const int im = tgpig.z;
  6786. const int first_row = (r0 * 2 + sgitg) * 2;
  6787. const uint i12 = im%args.ne12;
  6788. const uint i13 = im/args.ne12;
  6789. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6790. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6791. device const block_iq4_nl * x = (device const block_iq4_nl *) (src0 + offset0);
  6792. device const float * y = (device const float *) (src1 + offset1);
  6793. const int ix = tiisg/2; // 0...15
  6794. const int it = tiisg%2; // 0 or 1
  6795. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  6796. threadgroup_barrier(mem_flags::mem_threadgroup);
  6797. float4 yl[4];
  6798. float sumf[2]={0.f}, all_sum;
  6799. device const float * yb = y + ix * QK4_NL + it * 8;
  6800. uint32_t aux32[2];
  6801. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  6802. float4 qf1, qf2;
  6803. for (int ib = ix; ib < nb; ib += 16) {
  6804. device const float4 * y4 = (device const float4 *)yb;
  6805. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  6806. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  6807. device const block_iq4_nl & xb = x[row*nb + ib];
  6808. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  6809. float4 acc1 = {0.f}, acc2 = {0.f};
  6810. aux32[0] = q4[0] | (q4[1] << 16);
  6811. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  6812. aux32[0] &= 0x0f0f0f0f;
  6813. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6814. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6815. acc1 += yl[0] * qf1;
  6816. acc2 += yl[1] * qf2;
  6817. aux32[0] = q4[2] | (q4[3] << 16);
  6818. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  6819. aux32[0] &= 0x0f0f0f0f;
  6820. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6821. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6822. acc1 += yl[2] * qf1;
  6823. acc2 += yl[3] * qf2;
  6824. acc1 += acc2;
  6825. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  6826. }
  6827. yb += 16 * QK4_NL;
  6828. }
  6829. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6830. for (int row = 0; row < 2 && first_row + row < args.ne01; ++row) {
  6831. all_sum = simd_sum(sumf[row]);
  6832. if (tiisg == 0) {
  6833. dst_f32[first_row + row] = all_sum;
  6834. }
  6835. }
  6836. }
  6837. template<typename args_t>
  6838. void kernel_mul_mv_iq4_xs_f32_impl(
  6839. args_t args,
  6840. device const char * src0,
  6841. device const char * src1,
  6842. device char * dst,
  6843. threadgroup char * shmem,
  6844. uint3 tgpig,
  6845. ushort tiisg,
  6846. ushort sgitg) {
  6847. threadgroup float * shmem_f32 = (threadgroup float *) shmem;
  6848. const int nb = args.ne00/QK_K;
  6849. const int r0 = tgpig.x;
  6850. const int r1 = tgpig.y;
  6851. const int im = tgpig.z;
  6852. const int first_row = (r0 * 2 + sgitg) * 2;
  6853. const uint i12 = im%args.ne12;
  6854. const uint i13 = im/args.ne12;
  6855. const uint64_t offset0 = first_row*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  6856. const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13;
  6857. device const block_iq4_xs * x = (device const block_iq4_xs *) (src0 + offset0);
  6858. device const float * y = (device const float *) (src1 + offset1);
  6859. const int ix = tiisg/16; // 0 or 1
  6860. const int it = tiisg%16; // 0...15
  6861. const int ib = it/2;
  6862. const int il = it%2;
  6863. shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
  6864. threadgroup_barrier(mem_flags::mem_threadgroup);
  6865. float4 yl[4];
  6866. float sumf[2]={0.f}, all_sum;
  6867. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  6868. uint32_t aux32[2];
  6869. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  6870. float4 qf1, qf2;
  6871. for (int ibl = ix; ibl < nb; ibl += 2) {
  6872. device const float4 * y4 = (device const float4 *)yb;
  6873. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  6874. for (int row = 0; row < 2; ++row) {
  6875. device const block_iq4_xs & xb = x[row*nb + ibl];
  6876. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  6877. float4 acc1 = {0.f}, acc2 = {0.f};
  6878. aux32[0] = (q4[0] ) & 0x0f0f0f0f;
  6879. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  6880. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6881. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6882. acc1 += yl[0] * qf1;
  6883. acc2 += yl[1] * qf2;
  6884. aux32[0] = (q4[1] ) & 0x0f0f0f0f;
  6885. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  6886. qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]};
  6887. qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]};
  6888. acc1 += yl[2] * qf1;
  6889. acc2 += yl[3] * qf2;
  6890. acc1 += acc2;
  6891. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  6892. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  6893. }
  6894. yb += 2 * QK_K;
  6895. }
  6896. device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
  6897. for (int row = 0; row < 2; ++row) {
  6898. all_sum = simd_sum(sumf[row]);
  6899. if (tiisg == 0) {
  6900. dst_f32[first_row + row] = all_sum;
  6901. }
  6902. }
  6903. }
  6904. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  6905. kernel void kernel_mul_mv_iq1_s_f32(
  6906. constant ggml_metal_kargs_mul_mv & args,
  6907. device const char * src0,
  6908. device const char * src1,
  6909. device char * dst,
  6910. uint3 tgpig[[threadgroup_position_in_grid]],
  6911. ushort tiisg[[thread_index_in_simdgroup]],
  6912. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6913. kernel_mul_mv_iq1_s_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6914. }
  6915. [[host_name("kernel_mul_mv_iq1_m_f32")]]
  6916. kernel void kernel_mul_mv_iq1_m_f32(
  6917. constant ggml_metal_kargs_mul_mv & args,
  6918. device const char * src0,
  6919. device const char * src1,
  6920. device char * dst,
  6921. uint3 tgpig[[threadgroup_position_in_grid]],
  6922. ushort tiisg[[thread_index_in_simdgroup]],
  6923. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6924. kernel_mul_mv_iq1_m_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg);
  6925. }
  6926. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  6927. kernel void kernel_mul_mv_iq4_nl_f32(
  6928. constant ggml_metal_kargs_mul_mv & args,
  6929. device const char * src0,
  6930. device const char * src1,
  6931. device char * dst,
  6932. threadgroup char * shmem [[threadgroup(0)]],
  6933. uint3 tgpig[[threadgroup_position_in_grid]],
  6934. ushort tiisg[[thread_index_in_simdgroup]],
  6935. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6936. kernel_mul_mv_iq4_nl_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6937. }
  6938. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  6939. kernel void kernel_mul_mv_iq4_xs_f32(
  6940. constant ggml_metal_kargs_mul_mv & args,
  6941. device const char * src0,
  6942. device const char * src1,
  6943. device char * dst,
  6944. threadgroup char * shmem [[threadgroup(0)]],
  6945. uint3 tgpig[[threadgroup_position_in_grid]],
  6946. ushort tiisg[[thread_index_in_simdgroup]],
  6947. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  6948. kernel_mul_mv_iq4_xs_f32_impl<constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  6949. }
  6950. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  6951. kernel void kernel_get_rows_q(
  6952. device const void * src0,
  6953. device const void * src1,
  6954. device float * dst,
  6955. constant int64_t & ne00,
  6956. constant uint64_t & nb01,
  6957. constant uint64_t & nb02,
  6958. constant int64_t & ne10,
  6959. constant uint64_t & nb10,
  6960. constant uint64_t & nb11,
  6961. constant uint64_t & nb1,
  6962. constant uint64_t & nb2,
  6963. uint3 tgpig[[threadgroup_position_in_grid]],
  6964. uint tiitg[[thread_index_in_threadgroup]],
  6965. uint3 tptg [[threads_per_threadgroup]]) {
  6966. const int64_t i10 = tgpig.x;
  6967. const int64_t i11 = tgpig.y;
  6968. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  6969. const int64_t i02 = i11;
  6970. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  6971. float4x4 temp;
  6972. dequantize_func(((device const block_q *) ((const device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  6973. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  6974. }
  6975. }
  6976. template<typename T>
  6977. kernel void kernel_get_rows_f(
  6978. device const void * src0,
  6979. device const void * src1,
  6980. device float * dst,
  6981. constant int64_t & ne00,
  6982. constant uint64_t & nb01,
  6983. constant uint64_t & nb02,
  6984. constant int64_t & ne10,
  6985. constant uint64_t & nb10,
  6986. constant uint64_t & nb11,
  6987. constant uint64_t & nb1,
  6988. constant uint64_t & nb2,
  6989. uint3 tgpig[[threadgroup_position_in_grid]],
  6990. uint tiitg[[thread_index_in_threadgroup]],
  6991. uint3 tptg [[threads_per_threadgroup]]) {
  6992. const int64_t i10 = tgpig.x;
  6993. const int64_t i11 = tgpig.y;
  6994. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  6995. const int64_t i02 = i11;
  6996. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  6997. (( device float *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  6998. ((const device T *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  6999. }
  7000. }
  7001. kernel void kernel_get_rows_i32(
  7002. device const void * src0,
  7003. device const void * src1,
  7004. device int32_t * dst,
  7005. constant int64_t & ne00,
  7006. constant uint64_t & nb01,
  7007. constant uint64_t & nb02,
  7008. constant int64_t & ne10,
  7009. constant uint64_t & nb10,
  7010. constant uint64_t & nb11,
  7011. constant uint64_t & nb1,
  7012. constant uint64_t & nb2,
  7013. uint3 tgpig[[threadgroup_position_in_grid]],
  7014. uint tiitg[[thread_index_in_threadgroup]],
  7015. uint3 tptg [[threads_per_threadgroup]]) {
  7016. const int64_t i10 = tgpig.x;
  7017. const int64_t i11 = tgpig.y;
  7018. const int64_t r = ((const device int32_t *) ((const device char *) src1 + i11*nb11 + i10*nb10))[0];
  7019. const int64_t i02 = i11;
  7020. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  7021. (( device int32_t *) (( device char *) dst + i11*nb2 + i10*nb1))[ind] =
  7022. ((const device int32_t *) ((const device char *) src0 + i02*nb02 + r*nb01))[ind];
  7023. }
  7024. }
  7025. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  7026. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  7027. #define BLOCK_SIZE_K 32
  7028. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  7029. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  7030. #define THREAD_PER_BLOCK 128
  7031. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  7032. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  7033. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  7034. #define SG_MAT_ROW 8
  7035. // each block_q contains 16*nl weights
  7036. template<typename T, typename T4x4, typename simdgroup_T8x8, typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread T4x4 &)>
  7037. kernel void kernel_mul_mm(
  7038. constant ggml_metal_kargs_mul_mm & args,
  7039. device const char * src0,
  7040. device const char * src1,
  7041. device char * dst,
  7042. threadgroup char * shmem [[threadgroup(0)]],
  7043. uint3 tgpig[[threadgroup_position_in_grid]],
  7044. ushort tiitg[[thread_index_in_threadgroup]],
  7045. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7046. threadgroup T * sa = (threadgroup T *)(shmem);
  7047. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  7048. const int r0 = tgpig.y;
  7049. const int r1 = tgpig.x;
  7050. const int im = tgpig.z;
  7051. // if this block is of 64x32 shape or smaller
  7052. const short n_rows = (args.ne0 - r0*BLOCK_SIZE_M < BLOCK_SIZE_M) ? (args.ne0 - r0*BLOCK_SIZE_M) : BLOCK_SIZE_M;
  7053. const short n_cols = (args.ne1 - r1*BLOCK_SIZE_N < BLOCK_SIZE_N) ? (args.ne1 - r1*BLOCK_SIZE_N) : BLOCK_SIZE_N;
  7054. // a thread shouldn't load data outside of the matrix
  7055. const short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  7056. const short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  7057. simdgroup_T8x8 ma[4];
  7058. simdgroup_float8x8 mb[2];
  7059. simdgroup_float8x8 mc[8];
  7060. for (short i = 0; i < 8; i++){
  7061. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  7062. }
  7063. short il = (tiitg % THREAD_PER_ROW);
  7064. const int i12 = im%args.ne12;
  7065. const int i13 = im/args.ne12;
  7066. const uint64_t offset0 = (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
  7067. const short offset1 = il/nl;
  7068. device const block_q * x = (device const block_q *)(src0
  7069. + args.nb01*(r0*BLOCK_SIZE_M + thread_row) + offset0) + offset1;
  7070. device const float * y = (device const float *)(src1
  7071. + args.nb13*i13
  7072. + args.nb12*i12
  7073. + args.nb11*(r1*BLOCK_SIZE_N + thread_col)
  7074. + args.nb10*(BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  7075. for (int loop_k = 0; loop_k < args.ne00; loop_k += BLOCK_SIZE_K) {
  7076. // load data and store to threadgroup memory
  7077. T4x4 temp_a;
  7078. dequantize_func(x, il, temp_a);
  7079. threadgroup_barrier(mem_flags::mem_threadgroup);
  7080. #pragma unroll(16)
  7081. for (short i = 0; i < 16; i++) {
  7082. *(sa + SG_MAT_SIZE * ((tiitg/THREAD_PER_ROW/8) \
  7083. + (tiitg%THREAD_PER_ROW)*16 + (i/8)*8) \
  7084. + (tiitg/THREAD_PER_ROW)%8 + (i&7)*8) = temp_a[i/4][i%4];
  7085. }
  7086. *(threadgroup float2x4 *)(sb + 32*8*(tiitg%THREAD_PER_COL) + 8*(tiitg/THREAD_PER_COL)) = *((device float2x4 *) y);
  7087. il = (il + 2 < nl) ? il + 2 : il % 2;
  7088. x = (il < 2) ? x + (2 + nl - 1)/nl : x;
  7089. y += BLOCK_SIZE_K;
  7090. threadgroup_barrier(mem_flags::mem_threadgroup);
  7091. // load matrices from threadgroup memory and conduct outer products
  7092. threadgroup const T * lsma = (sa + THREAD_MAT_M*SG_MAT_SIZE*(sgitg%2));
  7093. threadgroup const float * lsmb = (sb + THREAD_MAT_N*SG_MAT_SIZE*(sgitg/2));
  7094. #pragma unroll(4)
  7095. for (short ik = 0; ik < BLOCK_SIZE_K/8; ik++) {
  7096. #pragma unroll(4)
  7097. for (short i = 0; i < 4; i++) {
  7098. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  7099. }
  7100. simdgroup_barrier(mem_flags::mem_none);
  7101. #pragma unroll(2)
  7102. for (short i = 0; i < 2; i++) {
  7103. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  7104. }
  7105. #pragma unroll(8)
  7106. for (short i = 0; i < 8; i++){
  7107. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  7108. }
  7109. lsma += (BLOCK_SIZE_M/SG_MAT_ROW)*SG_MAT_SIZE;
  7110. lsmb += (BLOCK_SIZE_N/SG_MAT_ROW)*SG_MAT_SIZE;
  7111. }
  7112. }
  7113. if ((r0 + 1) * BLOCK_SIZE_M <= args.ne0 && (r1 + 1) * BLOCK_SIZE_N <= args.ne1) {
  7114. device float * C = (device float *) dst +
  7115. (BLOCK_SIZE_M * r0 + 32*(sgitg & 1)) + \
  7116. (BLOCK_SIZE_N * r1 + 16*(sgitg >> 1)) * args.ne0 + im*args.ne1*args.ne0;
  7117. for (short i = 0; i < 8; i++) {
  7118. simdgroup_store(mc[i], C + 8 * (i%4) + 8 * args.ne0 * (i/4), args.ne0);
  7119. }
  7120. } else {
  7121. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  7122. threadgroup_barrier(mem_flags::mem_threadgroup);
  7123. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  7124. + 32*(sgitg&1) + (16*(sgitg >> 1))*BLOCK_SIZE_M;
  7125. for (short i = 0; i < 8; i++) {
  7126. simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*BLOCK_SIZE_M*(i/4), BLOCK_SIZE_M);
  7127. }
  7128. threadgroup_barrier(mem_flags::mem_threadgroup);
  7129. if (sgitg == 0) {
  7130. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  7131. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + (r1*BLOCK_SIZE_N + j)*args.ne0 + im*args.ne1*args.ne0;
  7132. device float4 * D4 = (device float4 *) D;
  7133. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  7134. threadgroup float4 * C4 = (threadgroup float4 *) C;
  7135. int i = 0;
  7136. for (; i < n_rows/4; i++) {
  7137. *(D4 + i) = *(C4 + i);
  7138. }
  7139. i *= 4;
  7140. for (; i < n_rows; i++) {
  7141. *(D + i) = *(C + i);
  7142. }
  7143. }
  7144. }
  7145. }
  7146. }
  7147. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in rowids
  7148. // TODO: this kernel needs to be reimplemented from scratch for better performance
  7149. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  7150. void kernel_mul_mm_id_impl(
  7151. int32_t ne00,
  7152. int32_t ne02,
  7153. uint64_t nb01,
  7154. uint64_t nb02,
  7155. int32_t ne11,
  7156. int32_t ne12,
  7157. uint64_t nb10,
  7158. uint64_t nb11,
  7159. uint64_t nb12,
  7160. int32_t ne0,
  7161. int32_t ne1,
  7162. int64_t ne0ne1,
  7163. device const char * src0,
  7164. device const char * src1,
  7165. threadgroup ushort2 * rowids,
  7166. device char * dst,
  7167. threadgroup char * shmem,
  7168. uint3 tgpig[[threadgroup_position_in_grid]],
  7169. ushort tiitg[[thread_index_in_threadgroup]],
  7170. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7171. threadgroup half * sa = (threadgroup half *)(shmem);
  7172. threadgroup float * sb = (threadgroup float *)(shmem + 4096);
  7173. const int r0 = tgpig.y;
  7174. const int r1 = tgpig.x;
  7175. if (r1*BLOCK_SIZE_N >= ne1) return;
  7176. // if this block is of 64x32 shape or smaller
  7177. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  7178. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  7179. // a thread shouldn't load data outside of the matrix
  7180. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  7181. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  7182. simdgroup_half8x8 ma[4];
  7183. simdgroup_float8x8 mb[2];
  7184. simdgroup_float8x8 mc[8];
  7185. for (int i = 0; i < 8; i++){
  7186. mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  7187. }
  7188. short il = (tiitg % THREAD_PER_ROW);
  7189. ushort offset1 = il/nl;
  7190. threadgroup const auto & id = rowids[r1 * BLOCK_SIZE_N + thread_col];
  7191. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01) + offset1;
  7192. device const float * y = (device const float *)(src1
  7193. + nb12 * id[1]
  7194. + nb11 * (id[0] % ne11)
  7195. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  7196. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  7197. // load data and store to threadgroup memory
  7198. half4x4 temp_a;
  7199. dequantize_func(x, il, temp_a);
  7200. threadgroup_barrier(mem_flags::mem_threadgroup);
  7201. for (int i = 0; i < 16; i++) {
  7202. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  7203. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  7204. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  7205. }
  7206. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  7207. il = (il + 2 < nl) ? il + 2 : il % 2;
  7208. x = (il < 2) ? x + (2+nl-1)/nl : x;
  7209. y += BLOCK_SIZE_K;
  7210. threadgroup_barrier(mem_flags::mem_threadgroup);
  7211. // load matrices from threadgroup memory and conduct outer products
  7212. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  7213. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  7214. #pragma unroll(BLOCK_SIZE_K/8)
  7215. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  7216. #pragma unroll(4)
  7217. for (int i = 0; i < 4; i++) {
  7218. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  7219. }
  7220. simdgroup_barrier(mem_flags::mem_none);
  7221. #pragma unroll(2)
  7222. for (int i = 0; i < 2; i++) {
  7223. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  7224. }
  7225. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  7226. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  7227. #pragma unroll(8)
  7228. for (int i = 0; i < 8; i++){
  7229. simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
  7230. }
  7231. }
  7232. }
  7233. {
  7234. threadgroup_barrier(mem_flags::mem_threadgroup);
  7235. threadgroup float * temp_str = ((threadgroup float *) shmem) \
  7236. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  7237. for (int i = 0; i < 8; i++) {
  7238. simdgroup_store(mc[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  7239. }
  7240. threadgroup_barrier(mem_flags::mem_threadgroup);
  7241. if (sgitg == 0) {
  7242. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  7243. threadgroup const auto & jid = rowids[r1 * BLOCK_SIZE_N + j];
  7244. int64_t joff = jid[0]*ne0 + jid[1]*ne0ne1;
  7245. device float * D = (device float *) dst + (r0*BLOCK_SIZE_M) + joff;
  7246. device float4 * D4 = (device float4 *) D;
  7247. threadgroup float * C = temp_str + (j*BLOCK_SIZE_M);
  7248. threadgroup float4 * C4 = (threadgroup float4 *) C;
  7249. int i = 0;
  7250. for (; i < n_rows/4; i++) {
  7251. *(D4 + i) = *(C4 + i);
  7252. }
  7253. i *= 4;
  7254. for (; i < n_rows; i++) {
  7255. *(D + i) = *(C + i);
  7256. }
  7257. }
  7258. }
  7259. }
  7260. }
  7261. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  7262. kernel void kernel_mul_mm_id(
  7263. constant ggml_metal_kargs_mul_mm_id & args,
  7264. device const char * src0s,
  7265. device const char * src1,
  7266. device char * dst,
  7267. device const char * ids,
  7268. threadgroup char * shmem [[threadgroup(0)]],
  7269. uint3 tgpig[[threadgroup_position_in_grid]],
  7270. ushort tiitg[[thread_index_in_threadgroup]],
  7271. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7272. const int32_t i02 = tgpig.z;
  7273. tgpig.z = 0;
  7274. device const char * src0 = src0s + i02*args.nb02;
  7275. // row indices
  7276. threadgroup ushort2 * rowids = (threadgroup ushort2 *)(shmem + 8192);
  7277. // TODO: parallelize this loop
  7278. int32_t _ne1 = 0;
  7279. for (ushort ii1 = 0; ii1 < args.nei1; ii1++) {
  7280. for (ushort ii0 = 0; ii0 < args.nei0; ii0++) {
  7281. int32_t id = ((device int32_t *) (ids + ii1*args.nbi1))[ii0];
  7282. if (id == i02) {
  7283. if (tiitg == 0) {
  7284. rowids[_ne1] = ushort2(ii0, ii1);
  7285. }
  7286. _ne1++;
  7287. }
  7288. }
  7289. }
  7290. threadgroup_barrier(mem_flags::mem_threadgroup);
  7291. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  7292. args.ne00,
  7293. args.ne02,
  7294. args.nb01,
  7295. args.nb02,
  7296. args.ne11,
  7297. args.ne12,
  7298. args.nb10,
  7299. args.nb11,
  7300. args.nb12,
  7301. args.ne0,
  7302. _ne1,
  7303. (int64_t)args.ne0*args.ne1,
  7304. src0,
  7305. src1,
  7306. rowids,
  7307. dst,
  7308. shmem,
  7309. tgpig,
  7310. tiitg,
  7311. sgitg);
  7312. }
  7313. #define QK_NL 16
  7314. //
  7315. // get rows
  7316. //
  7317. typedef decltype(kernel_get_rows_f<float>) get_rows_f_t;
  7318. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_f_t kernel_get_rows_f<float>;
  7319. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_f_t kernel_get_rows_f<half>;
  7320. #if defined(GGML_METAL_USE_BF16)
  7321. template [[host_name("kernel_get_rows_bf16")]] kernel get_rows_f_t kernel_get_rows_f<bfloat>;
  7322. #endif
  7323. typedef decltype(kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>) get_rows_q_t;
  7324. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>;
  7325. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q4_1, 2, dequantize_q4_1>;
  7326. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_0, 2, dequantize_q5_0>;
  7327. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_q_t kernel_get_rows_q<block_q5_1, 2, dequantize_q5_1>;
  7328. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q8_0, 2, dequantize_q8_0>;
  7329. 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>;
  7330. 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>;
  7331. 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>;
  7332. 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>;
  7333. 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>;
  7334. 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>;
  7335. 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>;
  7336. 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>;
  7337. 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>;
  7338. 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>;
  7339. 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>;
  7340. 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>;
  7341. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_q_t kernel_get_rows_q<block_iq4_nl, 2, dequantize_iq4_nl>;
  7342. 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>;
  7343. //
  7344. // matrix-matrix multiplication
  7345. //
  7346. typedef decltype(kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>) mat_mm_t;
  7347. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, float4x4, 1, dequantize_f32>;
  7348. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half4x4, 1, dequantize_f16>;
  7349. #if defined(GGML_METAL_USE_BF16)
  7350. template [[host_name("kernel_mul_mm_bf16_f32")]] kernel mat_mm_t kernel_mul_mm<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16>;
  7351. #endif
  7352. 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>;
  7353. 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>;
  7354. 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>;
  7355. 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>;
  7356. 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>;
  7357. 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>;
  7358. 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>;
  7359. 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>;
  7360. 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>;
  7361. 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>;
  7362. 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>;
  7363. 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>;
  7364. 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>;
  7365. 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>;
  7366. 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>;
  7367. 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>;
  7368. 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>;
  7369. 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>;
  7370. 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>;
  7371. //
  7372. // indirect matrix-matrix multiplication
  7373. //
  7374. typedef decltype(kernel_mul_mm_id<float4x4, 1, dequantize_f32>) mat_mm_id_t;
  7375. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  7376. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  7377. #if defined(GGML_METAL_USE_BF16)
  7378. template [[host_name("kernel_mul_mm_id_bf16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<bfloat4x4, 1, dequantize_bf16>;
  7379. #endif
  7380. 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>;
  7381. 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>;
  7382. 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>;
  7383. 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>;
  7384. 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>;
  7385. 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>;
  7386. 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>;
  7387. 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>;
  7388. 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>;
  7389. 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>;
  7390. 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>;
  7391. 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>;
  7392. 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>;
  7393. 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>;
  7394. 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>;
  7395. 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>;
  7396. 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>;
  7397. 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>;
  7398. 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>;
  7399. //
  7400. // matrix-vector multiplication
  7401. //
  7402. typedef void (kernel_mul_mv_impl_t)(
  7403. ggml_metal_kargs_mul_mv args,
  7404. device const char * src0,
  7405. device const char * src1,
  7406. device char * dst,
  7407. uint3 tgpig,
  7408. ushort tiisg);
  7409. typedef void (kernel_mul_mv2_impl_t)(
  7410. ggml_metal_kargs_mul_mv args,
  7411. device const char * src0,
  7412. device const char * src1,
  7413. device char * dst,
  7414. threadgroup char * shmem,
  7415. uint3 tgpig,
  7416. ushort tiisg,
  7417. ushort sgitg);
  7418. template<kernel_mul_mv_impl_t impl_fn>
  7419. void mmv_fn(
  7420. ggml_metal_kargs_mul_mv args,
  7421. device const char * src0,
  7422. device const char * src1,
  7423. device char * dst,
  7424. threadgroup char * shmem,
  7425. uint3 tgpig,
  7426. ushort tiitg,
  7427. ushort tiisg,
  7428. ushort sgitg) {
  7429. impl_fn(args, src0, src1, dst, tgpig, tiisg);
  7430. }
  7431. template<kernel_mul_mv2_impl_t impl_fn>
  7432. void mmv_fn(
  7433. ggml_metal_kargs_mul_mv args,
  7434. device const char * src0,
  7435. device const char * src1,
  7436. device char * dst,
  7437. threadgroup char * shmem,
  7438. uint3 tgpig,
  7439. ushort tiitg,
  7440. ushort tiisg,
  7441. ushort sgitg) {
  7442. impl_fn(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
  7443. }
  7444. typedef decltype(mmv_fn<kernel_mul_mv_impl<half, half4, half, half4, ggml_metal_kargs_mul_mv>>) mul_mv_impl_fn_t;
  7445. template<mul_mv_impl_fn_t impl_fn>
  7446. kernel void kernel_mul_mv_id(
  7447. constant ggml_metal_kargs_mul_mv_id & args,
  7448. device const char * src0s,
  7449. device const char * src1,
  7450. device char * dst,
  7451. device const char * ids,
  7452. threadgroup char * shmem [[threadgroup(0)]],
  7453. uint3 tgpig[[threadgroup_position_in_grid]],
  7454. ushort tiitg[[thread_index_in_threadgroup]],
  7455. ushort tiisg[[thread_index_in_simdgroup]],
  7456. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  7457. const int iid1 = tgpig.z/args.nei0;
  7458. const int idx = tgpig.z%args.nei0;
  7459. tgpig.z = 0;
  7460. const int32_t i02 = ((device const int32_t *) (ids + iid1*args.nbi1))[idx];
  7461. const int64_t i11 = idx % args.ne11;
  7462. const int64_t i12 = iid1;
  7463. const int64_t i1 = idx;
  7464. const int64_t i2 = i12;
  7465. device const char * src0_cur = src0s + i02*args.nb02;
  7466. device const char * src1_cur = src1 + i11*args.nb11 + i12*args.nb12;
  7467. device char * dst_cur = dst + (i1*args.ne0 + i2*args.ne1*args.ne0)*sizeof(float);
  7468. ggml_metal_kargs_mul_mv args0 = {
  7469. /*.ne00 =*/ args.ne00,
  7470. /*.ne01 =*/ args.ne01,
  7471. /*.ne02 =*/ 1, // args.ne02,
  7472. /*.nb00 =*/ args.nb00,
  7473. /*.nb01 =*/ args.nb01,
  7474. /*.nb02 =*/ args.nb02,
  7475. /*.nb03 =*/ args.nb02, // args.ne02 == 1
  7476. /*.ne10 =*/ args.ne10,
  7477. /*.ne11 =*/ 1, // args.ne11,
  7478. /*.ne12 =*/ 1, // args.ne12,
  7479. /*.nb10 =*/ args.nb10,
  7480. /*.nb11 =*/ args.nb11,
  7481. /*.nb12 =*/ args.nb12,
  7482. /*.nb13 =*/ args.nb12, // ne12 == 1
  7483. /*.ne0 =*/ args.ne0,
  7484. /*.ne1 =*/ 1, // args.ne1,
  7485. /*.r2 =*/ 1,
  7486. /*.r3 =*/ 1,
  7487. };
  7488. impl_fn(
  7489. args0,
  7490. /* src0 */ src0_cur,
  7491. /* src1 */ src1_cur,
  7492. /* dst */ dst_cur,
  7493. shmem,
  7494. tgpig,
  7495. tiitg,
  7496. tiisg,
  7497. sgitg);
  7498. }
  7499. typedef decltype(kernel_mul_mv_id<mmv_fn<kernel_mul_mv_impl<float, float4, float, float4>>>) kernel_mul_mv_id_t;
  7500. 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>>>;
  7501. 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>>>;
  7502. #if defined(GGML_METAL_USE_BF16)
  7503. 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>>>;
  7504. #endif
  7505. 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>>;
  7506. 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>>>;
  7507. 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>>>;
  7508. 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>>>;
  7509. 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>>>;
  7510. 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>>;
  7511. 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>>;
  7512. 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>>;
  7513. 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>>;
  7514. 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>>;
  7515. 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>>;
  7516. 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>>;
  7517. 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>>;
  7518. 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>>;
  7519. 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>>;
  7520. 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>>;
  7521. 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>>;
  7522. 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>>;
  7523. 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>>;
  7524. kernel void kernel_pool_2d_max_f32(
  7525. device const float * src0,
  7526. device float * dst,
  7527. constant int32_t & k0,
  7528. constant int32_t & k1,
  7529. constant int32_t & s0,
  7530. constant int32_t & s1,
  7531. constant int32_t & p0,
  7532. constant int32_t & p1,
  7533. constant int64_t & IH,
  7534. constant int64_t & IW,
  7535. constant int64_t & OH,
  7536. constant int64_t & OW,
  7537. constant int64_t & parallel_elements,
  7538. uint gid[[thread_position_in_grid]]) {
  7539. if (gid >= parallel_elements) {
  7540. return;
  7541. }
  7542. const int idx = gid;
  7543. const int I_HW = IH * IW;
  7544. const int O_HW = OH * OW;
  7545. const int nc = idx / O_HW;
  7546. const int cur_oh = idx % O_HW / OW;
  7547. const int cur_ow = idx % O_HW % OW;
  7548. device const float * i_ptr = src0 + nc * I_HW;
  7549. device float * o_ptr = dst + nc * O_HW;
  7550. const int start_h = cur_oh * s1 - p1;
  7551. const int bh = MAX(0, start_h);
  7552. const int eh = MIN(IH, start_h + k1);
  7553. const int start_w = cur_ow * s0 - p0;
  7554. const int bw = MAX(0, start_w);
  7555. const int ew = MIN(IW, start_w + k0);
  7556. float res = -INFINITY;
  7557. for (int i = bh; i < eh; i += 1) {
  7558. for (int j = bw; j < ew; j += 1) {
  7559. res = MAX(res, i_ptr[i * IW + j]);
  7560. }
  7561. }
  7562. o_ptr[cur_oh * OW + cur_ow] = res;
  7563. }
  7564. kernel void kernel_pool_2d_avg_f32(
  7565. device const float * src0,
  7566. device float * dst,
  7567. constant int32_t & k0,
  7568. constant int32_t & k1,
  7569. constant int32_t & s0,
  7570. constant int32_t & s1,
  7571. constant int32_t & p0,
  7572. constant int32_t & p1,
  7573. constant int64_t & IH,
  7574. constant int64_t & IW,
  7575. constant int64_t & OH,
  7576. constant int64_t & OW,
  7577. constant int64_t & parallel_elements,
  7578. uint gid[[thread_position_in_grid]]) {
  7579. if (gid >= parallel_elements) {
  7580. return;
  7581. }
  7582. const int idx = gid;
  7583. const int I_HW = IH * IW;
  7584. const int O_HW = OH * OW;
  7585. const int nc = idx / O_HW;
  7586. const int cur_oh = idx % O_HW / OW;
  7587. const int cur_ow = idx % O_HW % OW;
  7588. device const float * i_ptr = src0 + nc * I_HW;
  7589. device float * o_ptr = dst + nc * O_HW;
  7590. const int start_h = cur_oh * s1 - p1;
  7591. const int bh = MAX(0, start_h);
  7592. const int eh = MIN(IH, start_h + k1);
  7593. const int start_w = cur_ow * s0 - p0;
  7594. const int bw = MAX(0, start_w);
  7595. const int ew = MIN(IW, start_w + k0);
  7596. // const float scale = 1. / ((eh - bh) * (ew - bw));
  7597. const float scale = 1. / (k0 * k1);
  7598. float res = 0;
  7599. for (int i = bh; i < eh; i += 1) {
  7600. for (int j = bw; j < ew; j += 1) {
  7601. float cur = i_ptr[i * IW + j];
  7602. res += cur * scale;
  7603. }
  7604. }
  7605. o_ptr[cur_oh * OW + cur_ow] = res;
  7606. }