ggml-metal.metal 300 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  5. #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; }
  6. #define QK4_0 32
  7. #define QR4_0 2
  8. typedef struct {
  9. half d; // delta
  10. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  11. } block_q4_0;
  12. #define QK4_1 32
  13. typedef struct {
  14. half d; // delta
  15. half m; // min
  16. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  17. } block_q4_1;
  18. #define QK5_0 32
  19. typedef struct {
  20. half d; // delta
  21. uint8_t qh[4]; // 5-th bit of quants
  22. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  23. } block_q5_0;
  24. #define QK5_1 32
  25. typedef struct {
  26. half d; // delta
  27. half m; // min
  28. uint8_t qh[4]; // 5-th bit of quants
  29. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  30. } block_q5_1;
  31. #define QK8_0 32
  32. typedef struct {
  33. half d; // delta
  34. int8_t qs[QK8_0]; // quants
  35. } block_q8_0;
  36. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  37. enum ggml_sort_order {
  38. GGML_SORT_ASC,
  39. GGML_SORT_DESC,
  40. };
  41. // general-purpose kernel for addition, multiplication and division of two tensors
  42. // pros: works for non-contiguous tensors, supports broadcast across all dims
  43. // cons: not very efficient
  44. kernel void kernel_add(
  45. device const char * src0,
  46. device const char * src1,
  47. device char * dst,
  48. constant int64_t & ne00,
  49. constant int64_t & ne01,
  50. constant int64_t & ne02,
  51. constant int64_t & ne03,
  52. constant uint64_t & nb00,
  53. constant uint64_t & nb01,
  54. constant uint64_t & nb02,
  55. constant uint64_t & nb03,
  56. constant int64_t & ne10,
  57. constant int64_t & ne11,
  58. constant int64_t & ne12,
  59. constant int64_t & ne13,
  60. constant uint64_t & nb10,
  61. constant uint64_t & nb11,
  62. constant uint64_t & nb12,
  63. constant uint64_t & nb13,
  64. constant int64_t & ne0,
  65. constant int64_t & ne1,
  66. constant int64_t & ne2,
  67. constant int64_t & ne3,
  68. constant uint64_t & nb0,
  69. constant uint64_t & nb1,
  70. constant uint64_t & nb2,
  71. constant uint64_t & nb3,
  72. constant int64_t & offs,
  73. uint3 tgpig[[threadgroup_position_in_grid]],
  74. uint3 tpitg[[thread_position_in_threadgroup]],
  75. uint3 ntg[[threads_per_threadgroup]]) {
  76. const int64_t i03 = tgpig.z;
  77. const int64_t i02 = tgpig.y;
  78. const int64_t i01 = tgpig.x;
  79. const int64_t i13 = i03 % ne13;
  80. const int64_t i12 = i02 % ne12;
  81. const int64_t i11 = i01 % ne11;
  82. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01 + offs;
  83. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  84. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + offs;
  85. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  86. const int i10 = i0 % ne10;
  87. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) + *((device float *)(src1_ptr + i10*nb10));
  88. }
  89. }
  90. kernel void kernel_mul(
  91. device const char * src0,
  92. device const char * src1,
  93. device char * dst,
  94. constant int64_t & ne00,
  95. constant int64_t & ne01,
  96. constant int64_t & ne02,
  97. constant int64_t & ne03,
  98. constant uint64_t & nb00,
  99. constant uint64_t & nb01,
  100. constant uint64_t & nb02,
  101. constant uint64_t & nb03,
  102. constant int64_t & ne10,
  103. constant int64_t & ne11,
  104. constant int64_t & ne12,
  105. constant int64_t & ne13,
  106. constant uint64_t & nb10,
  107. constant uint64_t & nb11,
  108. constant uint64_t & nb12,
  109. constant uint64_t & nb13,
  110. constant int64_t & ne0,
  111. constant int64_t & ne1,
  112. constant int64_t & ne2,
  113. constant int64_t & ne3,
  114. constant uint64_t & nb0,
  115. constant uint64_t & nb1,
  116. constant uint64_t & nb2,
  117. constant uint64_t & nb3,
  118. uint3 tgpig[[threadgroup_position_in_grid]],
  119. uint3 tpitg[[thread_position_in_threadgroup]],
  120. uint3 ntg[[threads_per_threadgroup]]) {
  121. const int64_t i03 = tgpig.z;
  122. const int64_t i02 = tgpig.y;
  123. const int64_t i01 = tgpig.x;
  124. const int64_t i13 = i03 % ne13;
  125. const int64_t i12 = i02 % ne12;
  126. const int64_t i11 = i01 % ne11;
  127. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01;
  128. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  129. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1;
  130. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  131. const int i10 = i0 % ne10;
  132. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) * *((device float *)(src1_ptr + i10*nb10));
  133. }
  134. }
  135. kernel void kernel_div(
  136. device const char * src0,
  137. device const char * src1,
  138. device char * dst,
  139. constant int64_t & ne00,
  140. constant int64_t & ne01,
  141. constant int64_t & ne02,
  142. constant int64_t & ne03,
  143. constant uint64_t & nb00,
  144. constant uint64_t & nb01,
  145. constant uint64_t & nb02,
  146. constant uint64_t & nb03,
  147. constant int64_t & ne10,
  148. constant int64_t & ne11,
  149. constant int64_t & ne12,
  150. constant int64_t & ne13,
  151. constant uint64_t & nb10,
  152. constant uint64_t & nb11,
  153. constant uint64_t & nb12,
  154. constant uint64_t & nb13,
  155. constant int64_t & ne0,
  156. constant int64_t & ne1,
  157. constant int64_t & ne2,
  158. constant int64_t & ne3,
  159. constant uint64_t & nb0,
  160. constant uint64_t & nb1,
  161. constant uint64_t & nb2,
  162. constant uint64_t & nb3,
  163. uint3 tgpig[[threadgroup_position_in_grid]],
  164. uint3 tpitg[[thread_position_in_threadgroup]],
  165. uint3 ntg[[threads_per_threadgroup]]) {
  166. const int64_t i03 = tgpig.z;
  167. const int64_t i02 = tgpig.y;
  168. const int64_t i01 = tgpig.x;
  169. const int64_t i13 = i03 % ne13;
  170. const int64_t i12 = i02 % ne12;
  171. const int64_t i11 = i01 % ne11;
  172. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01;
  173. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  174. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1;
  175. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  176. const int i10 = i0 % ne10;
  177. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) / *((device float *)(src1_ptr + i10*nb10));
  178. }
  179. }
  180. // assumption: src1 is a row
  181. // broadcast src1 into src0
  182. kernel void kernel_add_row(
  183. device const float4 * src0,
  184. device const float4 * src1,
  185. device float4 * dst,
  186. constant uint64_t & nb [[buffer(28)]],
  187. uint tpig[[thread_position_in_grid]]) {
  188. dst[tpig] = src0[tpig] + src1[tpig % nb];
  189. }
  190. kernel void kernel_mul_row(
  191. device const float4 * src0,
  192. device const float4 * src1,
  193. device float4 * dst,
  194. constant uint64_t & nb [[buffer(28)]],
  195. uint tpig[[thread_position_in_grid]]) {
  196. dst[tpig] = src0[tpig] * src1[tpig % nb];
  197. }
  198. kernel void kernel_div_row(
  199. device const float4 * src0,
  200. device const float4 * src1,
  201. device float4 * dst,
  202. constant uint64_t & nb [[buffer(28)]],
  203. uint tpig[[thread_position_in_grid]]) {
  204. dst[tpig] = src0[tpig] / src1[tpig % nb];
  205. }
  206. kernel void kernel_scale(
  207. device const float * src0,
  208. device float * dst,
  209. constant float & scale,
  210. uint tpig[[thread_position_in_grid]]) {
  211. dst[tpig] = src0[tpig] * scale;
  212. }
  213. kernel void kernel_scale_4(
  214. device const float4 * src0,
  215. device float4 * dst,
  216. constant float & scale,
  217. uint tpig[[thread_position_in_grid]]) {
  218. dst[tpig] = src0[tpig] * scale;
  219. }
  220. kernel void kernel_relu(
  221. device const float * src0,
  222. device float * dst,
  223. uint tpig[[thread_position_in_grid]]) {
  224. dst[tpig] = max(0.0f, src0[tpig]);
  225. }
  226. kernel void kernel_tanh(
  227. device const float * src0,
  228. device float * dst,
  229. uint tpig[[thread_position_in_grid]]) {
  230. device const float & x = src0[tpig];
  231. dst[tpig] = precise::tanh(x);
  232. }
  233. constant float GELU_COEF_A = 0.044715f;
  234. constant float GELU_QUICK_COEF = -1.702f;
  235. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  236. kernel void kernel_gelu(
  237. device const float4 * src0,
  238. device float4 * dst,
  239. uint tpig[[thread_position_in_grid]]) {
  240. device const float4 & x = src0[tpig];
  241. // BEWARE !!!
  242. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  243. // This was observed with Falcon 7B and 40B models
  244. //
  245. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  246. }
  247. kernel void kernel_gelu_quick(
  248. device const float4 * src0,
  249. device float4 * dst,
  250. uint tpig[[thread_position_in_grid]]) {
  251. device const float4 & x = src0[tpig];
  252. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  253. }
  254. kernel void kernel_silu(
  255. device const float4 * src0,
  256. device float4 * dst,
  257. uint tpig[[thread_position_in_grid]]) {
  258. device const float4 & x = src0[tpig];
  259. dst[tpig] = x / (1.0f + exp(-x));
  260. }
  261. kernel void kernel_sqr(
  262. device const float * src0,
  263. device float * dst,
  264. uint tpig[[thread_position_in_grid]]) {
  265. dst[tpig] = src0[tpig] * src0[tpig];
  266. }
  267. kernel void kernel_sum_rows(
  268. device const float * src0,
  269. device float * dst,
  270. constant int64_t & ne00,
  271. constant int64_t & ne01,
  272. constant int64_t & ne02,
  273. constant int64_t & ne03,
  274. constant uint64_t & nb00,
  275. constant uint64_t & nb01,
  276. constant uint64_t & nb02,
  277. constant uint64_t & nb03,
  278. constant int64_t & ne10,
  279. constant int64_t & ne11,
  280. constant int64_t & ne12,
  281. constant int64_t & ne13,
  282. constant uint64_t & nb10,
  283. constant uint64_t & nb11,
  284. constant uint64_t & nb12,
  285. constant uint64_t & nb13,
  286. constant int64_t & ne0,
  287. constant int64_t & ne1,
  288. constant int64_t & ne2,
  289. constant int64_t & ne3,
  290. constant uint64_t & nb0,
  291. constant uint64_t & nb1,
  292. constant uint64_t & nb2,
  293. constant uint64_t & nb3,
  294. uint3 tpig[[thread_position_in_grid]]) {
  295. int64_t i3 = tpig.z;
  296. int64_t i2 = tpig.y;
  297. int64_t i1 = tpig.x;
  298. if (i3 >= ne03 || i2 >= ne02 || i1 >= ne01) {
  299. return;
  300. }
  301. device const float * src_row = (device const float *) ((device const char *) src0 + i1*nb01 + i2*nb02 + i3*nb03);
  302. device float * dst_row = (device float *) ((device char *) dst + i1*nb1 + i2*nb2 + i3*nb3);
  303. float row_sum = 0;
  304. for (int64_t i0 = 0; i0 < ne00; i0++) {
  305. row_sum += src_row[i0];
  306. }
  307. dst_row[0] = row_sum;
  308. }
  309. kernel void kernel_soft_max(
  310. device const float * src0,
  311. device const float * src1,
  312. device const float * src2,
  313. device float * dst,
  314. constant int64_t & ne00,
  315. constant int64_t & ne01,
  316. constant int64_t & ne02,
  317. constant float & scale,
  318. constant float & max_bias,
  319. constant float & m0,
  320. constant float & m1,
  321. constant uint32_t & n_head_log2,
  322. threadgroup float * buf [[threadgroup(0)]],
  323. uint tgpig[[threadgroup_position_in_grid]],
  324. uint tpitg[[thread_position_in_threadgroup]],
  325. uint sgitg[[simdgroup_index_in_threadgroup]],
  326. uint tiisg[[thread_index_in_simdgroup]],
  327. uint ntg[[threads_per_threadgroup]]) {
  328. const int64_t i03 = (tgpig) / (ne02*ne01);
  329. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  330. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  331. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  332. device const float * pmask = src1 != src0 ? src1 + i01*ne00 : nullptr;
  333. device const float * ppos = src2 != src0 ? src2 : nullptr;
  334. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  335. float slope = 0.0f;
  336. // ALiBi
  337. if (max_bias > 0.0f) {
  338. const int64_t h = i02;
  339. const float base = h < n_head_log2 ? m0 : m1;
  340. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  341. slope = pow(base, exp);
  342. }
  343. // parallel max
  344. float lmax = -INFINITY;
  345. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  346. lmax = MAX(lmax, psrc0[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f));
  347. }
  348. // find the max value in the block
  349. float max_val = simd_max(lmax);
  350. if (ntg > N_SIMDWIDTH) {
  351. if (sgitg == 0) {
  352. buf[tiisg] = -INFINITY;
  353. }
  354. threadgroup_barrier(mem_flags::mem_threadgroup);
  355. if (tiisg == 0) {
  356. buf[sgitg] = max_val;
  357. }
  358. threadgroup_barrier(mem_flags::mem_threadgroup);
  359. max_val = buf[tiisg];
  360. max_val = simd_max(max_val);
  361. }
  362. // parallel sum
  363. float lsum = 0.0f;
  364. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  365. const float exp_psrc0 = exp((psrc0[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f)) - max_val);
  366. lsum += exp_psrc0;
  367. pdst[i00] = exp_psrc0;
  368. }
  369. // This barrier fixes a failing test
  370. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  371. threadgroup_barrier(mem_flags::mem_none);
  372. float sum = simd_sum(lsum);
  373. if (ntg > N_SIMDWIDTH) {
  374. if (sgitg == 0) {
  375. buf[tiisg] = 0.0f;
  376. }
  377. threadgroup_barrier(mem_flags::mem_threadgroup);
  378. if (tiisg == 0) {
  379. buf[sgitg] = sum;
  380. }
  381. threadgroup_barrier(mem_flags::mem_threadgroup);
  382. sum = buf[tiisg];
  383. sum = simd_sum(sum);
  384. }
  385. const float inv_sum = 1.0f/sum;
  386. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  387. pdst[i00] *= inv_sum;
  388. }
  389. }
  390. kernel void kernel_soft_max_4(
  391. device const float * src0,
  392. device const float * src1,
  393. device const float * src2,
  394. device float * dst,
  395. constant int64_t & ne00,
  396. constant int64_t & ne01,
  397. constant int64_t & ne02,
  398. constant float & scale,
  399. constant float & max_bias,
  400. constant float & m0,
  401. constant float & m1,
  402. constant uint32_t & n_head_log2,
  403. threadgroup float * buf [[threadgroup(0)]],
  404. uint tgpig[[threadgroup_position_in_grid]],
  405. uint tpitg[[thread_position_in_threadgroup]],
  406. uint sgitg[[simdgroup_index_in_threadgroup]],
  407. uint tiisg[[thread_index_in_simdgroup]],
  408. uint ntg[[threads_per_threadgroup]]) {
  409. const int64_t i03 = (tgpig) / (ne02*ne01);
  410. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  411. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  412. device const float4 * psrc4 = (device const float4 *)(src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  413. device const float4 * pmask = src1 != src0 ? (device const float4 *)(src1 + i01*ne00) : nullptr;
  414. device const float4 * ppos = src2 != src0 ? (device const float4 *)(src2) : nullptr;
  415. device float4 * pdst4 = (device float4 *)(dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  416. float slope = 0.0f;
  417. if (max_bias > 0.0f) {
  418. const int64_t h = i02;
  419. const float base = h < n_head_log2 ? m0 : m1;
  420. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  421. slope = pow(base, exp);
  422. }
  423. // parallel max
  424. float4 lmax4 = -INFINITY;
  425. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  426. lmax4 = fmax(lmax4, psrc4[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f));
  427. }
  428. const float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  429. float max_val = simd_max(lmax);
  430. if (ntg > N_SIMDWIDTH) {
  431. if (sgitg == 0) {
  432. buf[tiisg] = -INFINITY;
  433. }
  434. threadgroup_barrier(mem_flags::mem_threadgroup);
  435. if (tiisg == 0) {
  436. buf[sgitg] = max_val;
  437. }
  438. threadgroup_barrier(mem_flags::mem_threadgroup);
  439. max_val = buf[tiisg];
  440. max_val = simd_max(max_val);
  441. }
  442. // parallel sum
  443. float4 lsum4 = 0.0f;
  444. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  445. const float4 exp_psrc4 = exp((psrc4[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f)) - max_val);
  446. lsum4 += exp_psrc4;
  447. pdst4[i00] = exp_psrc4;
  448. }
  449. const float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  450. // This barrier fixes a failing test
  451. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  452. threadgroup_barrier(mem_flags::mem_none);
  453. float sum = simd_sum(lsum);
  454. if (ntg > N_SIMDWIDTH) {
  455. if (sgitg == 0) {
  456. buf[tiisg] = 0.0f;
  457. }
  458. threadgroup_barrier(mem_flags::mem_threadgroup);
  459. if (tiisg == 0) {
  460. buf[sgitg] = sum;
  461. }
  462. threadgroup_barrier(mem_flags::mem_threadgroup);
  463. sum = buf[tiisg];
  464. sum = simd_sum(sum);
  465. }
  466. const float inv_sum = 1.0f/sum;
  467. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  468. pdst4[i00] *= inv_sum;
  469. }
  470. }
  471. kernel void kernel_diag_mask_inf(
  472. device const float * src0,
  473. device float * dst,
  474. constant int64_t & ne00,
  475. constant int64_t & ne01,
  476. constant int & n_past,
  477. uint3 tpig[[thread_position_in_grid]]) {
  478. const int64_t i02 = tpig[2];
  479. const int64_t i01 = tpig[1];
  480. const int64_t i00 = tpig[0];
  481. if (i00 > n_past + i01) {
  482. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  483. } else {
  484. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  485. }
  486. }
  487. kernel void kernel_diag_mask_inf_8(
  488. device const float4 * src0,
  489. device float4 * dst,
  490. constant int64_t & ne00,
  491. constant int64_t & ne01,
  492. constant int & n_past,
  493. uint3 tpig[[thread_position_in_grid]]) {
  494. const int64_t i = 2*tpig[0];
  495. dst[i+0] = src0[i+0];
  496. dst[i+1] = src0[i+1];
  497. int64_t i4 = 4*i;
  498. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  499. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  500. const int64_t i00 = i4;
  501. for (int k = 3; k >= 0; --k) {
  502. if (i00 + 4 + k <= n_past + i01) {
  503. break;
  504. }
  505. dst[i+1][k] = -INFINITY;
  506. if (i00 + k > n_past + i01) {
  507. dst[i][k] = -INFINITY;
  508. }
  509. }
  510. }
  511. kernel void kernel_norm(
  512. device const void * src0,
  513. device float * dst,
  514. constant int64_t & ne00,
  515. constant uint64_t & nb01,
  516. constant float & eps,
  517. threadgroup float * sum [[threadgroup(0)]],
  518. uint tgpig[[threadgroup_position_in_grid]],
  519. uint tpitg[[thread_position_in_threadgroup]],
  520. uint ntg[[threads_per_threadgroup]]) {
  521. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  522. // MEAN
  523. // parallel sum
  524. sum[tpitg] = 0.0f;
  525. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  526. sum[tpitg] += x[i00];
  527. }
  528. // reduce
  529. threadgroup_barrier(mem_flags::mem_threadgroup);
  530. for (uint i = ntg/2; i > 0; i /= 2) {
  531. if (tpitg < i) {
  532. sum[tpitg] += sum[tpitg + i];
  533. }
  534. threadgroup_barrier(mem_flags::mem_threadgroup);
  535. }
  536. const float mean = sum[0] / ne00;
  537. // recenter and VARIANCE
  538. threadgroup_barrier(mem_flags::mem_threadgroup);
  539. device float * y = dst + tgpig*ne00;
  540. sum[tpitg] = 0.0f;
  541. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  542. y[i00] = x[i00] - mean;
  543. sum[tpitg] += y[i00] * y[i00];
  544. }
  545. // reduce
  546. threadgroup_barrier(mem_flags::mem_threadgroup);
  547. for (uint i = ntg/2; i > 0; i /= 2) {
  548. if (tpitg < i) {
  549. sum[tpitg] += sum[tpitg + i];
  550. }
  551. threadgroup_barrier(mem_flags::mem_threadgroup);
  552. }
  553. const float variance = sum[0] / ne00;
  554. const float scale = 1.0f/sqrt(variance + eps);
  555. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  556. y[i00] = y[i00] * scale;
  557. }
  558. }
  559. kernel void kernel_rms_norm(
  560. device const void * src0,
  561. device float * dst,
  562. constant int64_t & ne00,
  563. constant uint64_t & nb01,
  564. constant float & eps,
  565. threadgroup float * buf [[threadgroup(0)]],
  566. uint tgpig[[threadgroup_position_in_grid]],
  567. uint tpitg[[thread_position_in_threadgroup]],
  568. uint sgitg[[simdgroup_index_in_threadgroup]],
  569. uint tiisg[[thread_index_in_simdgroup]],
  570. uint ntg[[threads_per_threadgroup]]) {
  571. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  572. float4 sumf = 0;
  573. float all_sum = 0;
  574. // parallel sum
  575. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  576. sumf += x[i00] * x[i00];
  577. }
  578. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  579. all_sum = simd_sum(all_sum);
  580. if (ntg > N_SIMDWIDTH) {
  581. if (sgitg == 0) {
  582. buf[tiisg] = 0.0f;
  583. }
  584. threadgroup_barrier(mem_flags::mem_threadgroup);
  585. if (tiisg == 0) {
  586. buf[sgitg] = all_sum;
  587. }
  588. threadgroup_barrier(mem_flags::mem_threadgroup);
  589. all_sum = buf[tiisg];
  590. all_sum = simd_sum(all_sum);
  591. }
  592. const float mean = all_sum/ne00;
  593. const float scale = 1.0f/sqrt(mean + eps);
  594. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  595. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  596. y[i00] = x[i00] * scale;
  597. }
  598. }
  599. kernel void kernel_group_norm(
  600. device const float * src0,
  601. device float * dst,
  602. constant int64_t & ne00,
  603. constant int64_t & ne01,
  604. constant int64_t & ne02,
  605. constant uint64_t & nb00,
  606. constant uint64_t & nb01,
  607. constant uint64_t & nb02,
  608. constant int32_t & n_groups,
  609. constant float & eps,
  610. threadgroup float * buf [[threadgroup(0)]],
  611. uint tgpig[[threadgroup_position_in_grid]],
  612. uint tpitg[[thread_position_in_threadgroup]],
  613. uint sgitg[[simdgroup_index_in_threadgroup]],
  614. uint tiisg[[thread_index_in_simdgroup]],
  615. uint ntg[[threads_per_threadgroup]]) {
  616. const int64_t ne = ne00*ne01*ne02;
  617. const int64_t gs = ne00*ne01*((ne02 + n_groups - 1) / n_groups);
  618. int start = tgpig * gs;
  619. int end = start + gs;
  620. start += tpitg;
  621. if (end >= ne) {
  622. end = ne;
  623. }
  624. float tmp = 0.0f; // partial sum for thread in warp
  625. for (int j = start; j < end; j += ntg) {
  626. tmp += src0[j];
  627. }
  628. threadgroup_barrier(mem_flags::mem_threadgroup);
  629. tmp = simd_sum(tmp);
  630. if (ntg > N_SIMDWIDTH) {
  631. if (sgitg == 0) {
  632. buf[tiisg] = 0.0f;
  633. }
  634. threadgroup_barrier(mem_flags::mem_threadgroup);
  635. if (tiisg == 0) {
  636. buf[sgitg] = tmp;
  637. }
  638. threadgroup_barrier(mem_flags::mem_threadgroup);
  639. tmp = buf[tiisg];
  640. tmp = simd_sum(tmp);
  641. }
  642. const float mean = tmp / gs;
  643. tmp = 0.0f;
  644. for (int j = start; j < end; j += ntg) {
  645. float xi = src0[j] - mean;
  646. dst[j] = xi;
  647. tmp += xi * xi;
  648. }
  649. tmp = simd_sum(tmp);
  650. if (ntg > N_SIMDWIDTH) {
  651. if (sgitg == 0) {
  652. buf[tiisg] = 0.0f;
  653. }
  654. threadgroup_barrier(mem_flags::mem_threadgroup);
  655. if (tiisg == 0) {
  656. buf[sgitg] = tmp;
  657. }
  658. threadgroup_barrier(mem_flags::mem_threadgroup);
  659. tmp = buf[tiisg];
  660. tmp = simd_sum(tmp);
  661. }
  662. const float variance = tmp / gs;
  663. const float scale = 1.0f/sqrt(variance + eps);
  664. for (int j = start; j < end; j += ntg) {
  665. dst[j] *= scale;
  666. }
  667. }
  668. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  669. // il indicates where the q4 quants begin (0 or QK4_0/4)
  670. // we assume that the yl's have been multiplied with the appropriate scale factor
  671. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  672. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  673. float d = qb_curr->d;
  674. float2 acc = 0.f;
  675. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 1 + il/2);
  676. for (int i = 0; i < 8; i+=2) {
  677. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  678. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  679. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  680. + yl[i + 9] * (qs[i / 2] & 0xF000);
  681. }
  682. return d * (sumy * -8.f + acc[0] + acc[1]);
  683. }
  684. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  685. // il indicates where the q4 quants begin (0 or QK4_0/4)
  686. // we assume that the yl's have been multiplied with the appropriate scale factor
  687. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  688. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  689. float d = qb_curr->d;
  690. float m = qb_curr->m;
  691. float2 acc = 0.f;
  692. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  693. for (int i = 0; i < 8; i+=2) {
  694. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  695. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  696. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  697. + yl[i + 9] * (qs[i / 2] & 0xF000);
  698. }
  699. return d * (acc[0] + acc[1]) + sumy * m;
  700. }
  701. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  702. // il indicates where the q5 quants begin (0 or QK5_0/4)
  703. // we assume that the yl's have been multiplied with the appropriate scale factor
  704. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  705. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  706. float d = qb_curr->d;
  707. float2 acc = 0.f;
  708. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  709. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  710. for (int i = 0; i < 8; i+=2) {
  711. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  712. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  713. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  714. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  715. }
  716. return d * (sumy * -16.f + acc[0] + acc[1]);
  717. }
  718. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  719. // il indicates where the q5 quants begin (0 or QK5_1/4)
  720. // we assume that the yl's have been multiplied with the appropriate scale factor
  721. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  722. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  723. float d = qb_curr->d;
  724. float m = qb_curr->m;
  725. float2 acc = 0.f;
  726. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  727. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  728. for (int i = 0; i < 8; i+=2) {
  729. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  730. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  731. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  732. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  733. }
  734. return d * (acc[0] + acc[1]) + sumy * m;
  735. }
  736. // putting them in the kernel cause a significant performance penalty
  737. #define N_DST 4 // each SIMD group works on 4 rows
  738. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  739. //Note: This is a template, but strictly speaking it only applies to
  740. // quantizations where the block size is 32. It also does not
  741. // guard against the number of rows not being divisible by
  742. // N_DST, so this is another explicit assumption of the implementation.
  743. template<typename block_q_type, int nr, int nsg, int nw>
  744. void mul_vec_q_n_f32_impl(
  745. device const void * src0,
  746. device const float * src1,
  747. device float * dst,
  748. int64_t ne00,
  749. int64_t ne01,
  750. int64_t ne02,
  751. int64_t ne10,
  752. int64_t ne12,
  753. int64_t ne0,
  754. int64_t ne1,
  755. uint r2,
  756. uint r3,
  757. uint3 tgpig, uint tiisg, uint sgitg) {
  758. const int nb = ne00/QK4_0;
  759. const int r0 = tgpig.x;
  760. const int r1 = tgpig.y;
  761. const int im = tgpig.z;
  762. const int first_row = (r0 * nsg + sgitg) * nr;
  763. const uint i12 = im%ne12;
  764. const uint i13 = im/ne12;
  765. const uint offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  766. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  767. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  768. float yl[16]; // src1 vector cache
  769. float sumf[nr] = {0.f};
  770. const int ix = (tiisg/2);
  771. const int il = (tiisg%2)*8;
  772. device const float * yb = y + ix * QK4_0 + il;
  773. // each thread in a SIMD group deals with half a block.
  774. for (int ib = ix; ib < nb; ib += nw/2) {
  775. float sumy = 0;
  776. for (int i = 0; i < 8; i += 2) {
  777. sumy += yb[i] + yb[i+1];
  778. yl[i+0] = yb[i+ 0];
  779. yl[i+1] = yb[i+ 1]/256.f;
  780. sumy += yb[i+16] + yb[i+17];
  781. yl[i+8] = yb[i+16]/16.f;
  782. yl[i+9] = yb[i+17]/4096.f;
  783. }
  784. for (int row = 0; row < nr; row++) {
  785. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  786. }
  787. yb += QK4_0 * 16;
  788. }
  789. for (int row = 0; row < nr; ++row) {
  790. const float tot = simd_sum(sumf[row]);
  791. if (tiisg == 0 && first_row + row < ne01) {
  792. dst[im*ne0*ne1 + r1*ne0 + first_row + row] = tot;
  793. }
  794. }
  795. }
  796. kernel void kernel_mul_mv_q4_0_f32(
  797. device const void * src0,
  798. device const float * src1,
  799. device float * dst,
  800. constant int64_t & ne00,
  801. constant int64_t & ne01,
  802. constant int64_t & ne02,
  803. constant uint64_t & nb00,
  804. constant uint64_t & nb01,
  805. constant uint64_t & nb02,
  806. constant int64_t & ne10,
  807. constant int64_t & ne11,
  808. constant int64_t & ne12,
  809. constant uint64_t & nb10,
  810. constant uint64_t & nb11,
  811. constant uint64_t & nb12,
  812. constant int64_t & ne0,
  813. constant int64_t & ne1,
  814. constant uint & r2,
  815. constant uint & r3,
  816. uint3 tgpig[[threadgroup_position_in_grid]],
  817. uint tiisg[[thread_index_in_simdgroup]],
  818. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  819. mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  820. }
  821. kernel void kernel_mul_mv_q4_1_f32(
  822. device const void * src0,
  823. device const float * src1,
  824. device float * dst,
  825. constant int64_t & ne00,
  826. constant int64_t & ne01,
  827. constant int64_t & ne02,
  828. constant uint64_t & nb00,
  829. constant uint64_t & nb01,
  830. constant uint64_t & nb02,
  831. constant int64_t & ne10,
  832. constant int64_t & ne11,
  833. constant int64_t & ne12,
  834. constant uint64_t & nb10,
  835. constant uint64_t & nb11,
  836. constant uint64_t & nb12,
  837. constant int64_t & ne0,
  838. constant int64_t & ne1,
  839. constant uint & r2,
  840. constant uint & r3,
  841. uint3 tgpig[[threadgroup_position_in_grid]],
  842. uint tiisg[[thread_index_in_simdgroup]],
  843. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  844. mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  845. }
  846. kernel void kernel_mul_mv_q5_0_f32(
  847. device const void * src0,
  848. device const float * src1,
  849. device float * dst,
  850. constant int64_t & ne00,
  851. constant int64_t & ne01,
  852. constant int64_t & ne02,
  853. constant uint64_t & nb00,
  854. constant uint64_t & nb01,
  855. constant uint64_t & nb02,
  856. constant int64_t & ne10,
  857. constant int64_t & ne11,
  858. constant int64_t & ne12,
  859. constant uint64_t & nb10,
  860. constant uint64_t & nb11,
  861. constant uint64_t & nb12,
  862. constant int64_t & ne0,
  863. constant int64_t & ne1,
  864. constant uint & r2,
  865. constant uint & r3,
  866. uint3 tgpig[[threadgroup_position_in_grid]],
  867. uint tiisg[[thread_index_in_simdgroup]],
  868. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  869. mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  870. }
  871. kernel void kernel_mul_mv_q5_1_f32(
  872. device const void * src0,
  873. device const float * src1,
  874. device float * dst,
  875. constant int64_t & ne00,
  876. constant int64_t & ne01,
  877. constant int64_t & ne02,
  878. constant uint64_t & nb00,
  879. constant uint64_t & nb01,
  880. constant uint64_t & nb02,
  881. constant int64_t & ne10,
  882. constant int64_t & ne11,
  883. constant int64_t & ne12,
  884. constant uint64_t & nb10,
  885. constant uint64_t & nb11,
  886. constant uint64_t & nb12,
  887. constant int64_t & ne0,
  888. constant int64_t & ne1,
  889. constant uint & r2,
  890. constant uint & r3,
  891. uint3 tgpig[[threadgroup_position_in_grid]],
  892. uint tiisg[[thread_index_in_simdgroup]],
  893. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  894. mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  895. }
  896. #define NB_Q8_0 8
  897. void kernel_mul_mv_q8_0_f32_impl(
  898. device const void * src0,
  899. device const float * src1,
  900. device float * dst,
  901. constant int64_t & ne00,
  902. constant int64_t & ne01,
  903. constant int64_t & ne02,
  904. constant int64_t & ne10,
  905. constant int64_t & ne12,
  906. constant int64_t & ne0,
  907. constant int64_t & ne1,
  908. constant uint & r2,
  909. constant uint & r3,
  910. uint3 tgpig[[threadgroup_position_in_grid]],
  911. uint tiisg[[thread_index_in_simdgroup]],
  912. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  913. const int nr = N_DST;
  914. const int nsg = N_SIMDGROUP;
  915. const int nw = N_SIMDWIDTH;
  916. const int nb = ne00/QK8_0;
  917. const int r0 = tgpig.x;
  918. const int r1 = tgpig.y;
  919. const int im = tgpig.z;
  920. const int first_row = (r0 * nsg + sgitg) * nr;
  921. const uint i12 = im%ne12;
  922. const uint i13 = im/ne12;
  923. const uint offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  924. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  925. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  926. float yl[NB_Q8_0];
  927. float sumf[nr]={0.f};
  928. const int ix = tiisg/4;
  929. const int il = tiisg%4;
  930. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  931. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  932. for (int ib = ix; ib < nb; ib += nw/4) {
  933. for (int i = 0; i < NB_Q8_0; ++i) {
  934. yl[i] = yb[i];
  935. }
  936. for (int row = 0; row < nr; row++) {
  937. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  938. float sumq = 0.f;
  939. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  940. sumq += qs[iq] * yl[iq];
  941. }
  942. sumf[row] += sumq*x[ib+row*nb].d;
  943. }
  944. yb += NB_Q8_0 * nw;
  945. }
  946. for (int row = 0; row < nr; ++row) {
  947. const float tot = simd_sum(sumf[row]);
  948. if (tiisg == 0 && first_row + row < ne01) {
  949. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  950. }
  951. }
  952. }
  953. [[host_name("kernel_mul_mv_q8_0_f32")]]
  954. kernel void kernel_mul_mv_q8_0_f32(
  955. device const void * src0,
  956. device const float * src1,
  957. device float * dst,
  958. constant int64_t & ne00,
  959. constant int64_t & ne01,
  960. constant int64_t & ne02,
  961. constant uint64_t & nb00,
  962. constant uint64_t & nb01,
  963. constant uint64_t & nb02,
  964. constant int64_t & ne10,
  965. constant int64_t & ne11,
  966. constant int64_t & ne12,
  967. constant uint64_t & nb10,
  968. constant uint64_t & nb11,
  969. constant uint64_t & nb12,
  970. constant int64_t & ne0,
  971. constant int64_t & ne1,
  972. constant uint & r2,
  973. constant uint & r3,
  974. uint3 tgpig[[threadgroup_position_in_grid]],
  975. uint tiisg[[thread_index_in_simdgroup]],
  976. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  977. kernel_mul_mv_q8_0_f32_impl(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  978. }
  979. #define N_F32_F32 4
  980. void kernel_mul_mv_f32_f32_impl(
  981. device const char * src0,
  982. device const char * src1,
  983. device float * dst,
  984. constant int64_t & ne00,
  985. constant int64_t & ne01,
  986. constant int64_t & ne02,
  987. constant uint64_t & nb00,
  988. constant uint64_t & nb01,
  989. constant uint64_t & nb02,
  990. constant int64_t & ne10,
  991. constant int64_t & ne11,
  992. constant int64_t & ne12,
  993. constant uint64_t & nb10,
  994. constant uint64_t & nb11,
  995. constant uint64_t & nb12,
  996. constant int64_t & ne0,
  997. constant int64_t & ne1,
  998. constant uint & r2,
  999. constant uint & r3,
  1000. uint3 tgpig[[threadgroup_position_in_grid]],
  1001. uint tiisg[[thread_index_in_simdgroup]]) {
  1002. const int64_t r0 = tgpig.x;
  1003. const int64_t rb = tgpig.y*N_F32_F32;
  1004. const int64_t im = tgpig.z;
  1005. const uint i12 = im%ne12;
  1006. const uint i13 = im/ne12;
  1007. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1008. device const float * x = (device const float *) (src0 + offset0);
  1009. if (ne00 < 128) {
  1010. for (int row = 0; row < N_F32_F32; ++row) {
  1011. int r1 = rb + row;
  1012. if (r1 >= ne11) {
  1013. break;
  1014. }
  1015. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1016. float sumf = 0;
  1017. for (int i = tiisg; i < ne00; i += 32) {
  1018. sumf += (float) x[i] * (float) y[i];
  1019. }
  1020. float all_sum = simd_sum(sumf);
  1021. if (tiisg == 0) {
  1022. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1023. }
  1024. }
  1025. } else {
  1026. device const float4 * x4 = (device const float4 *)x;
  1027. for (int row = 0; row < N_F32_F32; ++row) {
  1028. int r1 = rb + row;
  1029. if (r1 >= ne11) {
  1030. break;
  1031. }
  1032. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1033. device const float4 * y4 = (device const float4 *) y;
  1034. float sumf = 0;
  1035. for (int i = tiisg; i < ne00/4; i += 32) {
  1036. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1037. }
  1038. float all_sum = simd_sum(sumf);
  1039. if (tiisg == 0) {
  1040. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1041. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. [[host_name("kernel_mul_mv_f32_f32")]]
  1047. kernel void kernel_mul_mv_f32_f32(
  1048. device const char * src0,
  1049. device const char * src1,
  1050. device float * dst,
  1051. constant int64_t & ne00,
  1052. constant int64_t & ne01,
  1053. constant int64_t & ne02,
  1054. constant uint64_t & nb00,
  1055. constant uint64_t & nb01,
  1056. constant uint64_t & nb02,
  1057. constant int64_t & ne10,
  1058. constant int64_t & ne11,
  1059. constant int64_t & ne12,
  1060. constant uint64_t & nb10,
  1061. constant uint64_t & nb11,
  1062. constant uint64_t & nb12,
  1063. constant int64_t & ne0,
  1064. constant int64_t & ne1,
  1065. constant uint & r2,
  1066. constant uint & r3,
  1067. uint3 tgpig[[threadgroup_position_in_grid]],
  1068. uint tiisg[[thread_index_in_simdgroup]]) {
  1069. kernel_mul_mv_f32_f32_impl(src0, src1, dst, ne00, ne01, ne02, nb00, nb01, nb02, ne10, ne11, ne12, nb10, nb11, nb12, ne0, ne1, r2, r3, tgpig, tiisg);
  1070. }
  1071. #define N_F16_F16 4
  1072. kernel void kernel_mul_mv_f16_f16(
  1073. device const char * src0,
  1074. device const char * src1,
  1075. device float * dst,
  1076. constant int64_t & ne00,
  1077. constant int64_t & ne01,
  1078. constant int64_t & ne02,
  1079. constant uint64_t & nb00,
  1080. constant uint64_t & nb01,
  1081. constant uint64_t & nb02,
  1082. constant int64_t & ne10,
  1083. constant int64_t & ne11,
  1084. constant int64_t & ne12,
  1085. constant uint64_t & nb10,
  1086. constant uint64_t & nb11,
  1087. constant uint64_t & nb12,
  1088. constant int64_t & ne0,
  1089. constant int64_t & ne1,
  1090. constant uint & r2,
  1091. constant uint & r3,
  1092. uint3 tgpig[[threadgroup_position_in_grid]],
  1093. uint tiisg[[thread_index_in_simdgroup]]) {
  1094. const int64_t r0 = tgpig.x;
  1095. const int64_t rb = tgpig.y*N_F16_F16;
  1096. const int64_t im = tgpig.z;
  1097. const uint i12 = im%ne12;
  1098. const uint i13 = im/ne12;
  1099. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1100. device const half * x = (device const half *) (src0 + offset0);
  1101. if (ne00 < 128) {
  1102. for (int row = 0; row < N_F16_F16; ++row) {
  1103. int r1 = rb + row;
  1104. if (r1 >= ne11) {
  1105. break;
  1106. }
  1107. device const half * y = (device const half *) (src1 + r1*nb11 + im*nb12);
  1108. float sumf = 0;
  1109. for (int i = tiisg; i < ne00; i += 32) {
  1110. sumf += (half) x[i] * (half) y[i];
  1111. }
  1112. float all_sum = simd_sum(sumf);
  1113. if (tiisg == 0) {
  1114. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1115. }
  1116. }
  1117. } else {
  1118. device const half4 * x4 = (device const half4 *)x;
  1119. for (int row = 0; row < N_F16_F16; ++row) {
  1120. int r1 = rb + row;
  1121. if (r1 >= ne11) {
  1122. break;
  1123. }
  1124. device const half * y = (device const half *) (src1 + r1*nb11 + im*nb12);
  1125. device const half4 * y4 = (device const half4 *) y;
  1126. float sumf = 0;
  1127. for (int i = tiisg; i < ne00/4; i += 32) {
  1128. for (int k = 0; k < 4; ++k) sumf += (half) x4[i][k] * y4[i][k];
  1129. }
  1130. float all_sum = simd_sum(sumf);
  1131. if (tiisg == 0) {
  1132. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (half) x[i] * y[i];
  1133. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. void kernel_mul_mv_f16_f32_1row_impl(
  1139. device const char * src0,
  1140. device const char * src1,
  1141. device float * dst,
  1142. constant int64_t & ne00,
  1143. constant int64_t & ne01,
  1144. constant int64_t & ne02,
  1145. constant uint64_t & nb00,
  1146. constant uint64_t & nb01,
  1147. constant uint64_t & nb02,
  1148. constant int64_t & ne10,
  1149. constant int64_t & ne11,
  1150. constant int64_t & ne12,
  1151. constant uint64_t & nb10,
  1152. constant uint64_t & nb11,
  1153. constant uint64_t & nb12,
  1154. constant int64_t & ne0,
  1155. constant int64_t & ne1,
  1156. constant uint & r2,
  1157. constant uint & r3,
  1158. uint3 tgpig[[threadgroup_position_in_grid]],
  1159. uint tiisg[[thread_index_in_simdgroup]]) {
  1160. const int64_t r0 = tgpig.x;
  1161. const int64_t r1 = tgpig.y;
  1162. const int64_t im = tgpig.z;
  1163. const uint i12 = im%ne12;
  1164. const uint i13 = im/ne12;
  1165. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1166. device const half * x = (device const half *) (src0 + offset0);
  1167. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1168. float sumf = 0;
  1169. if (ne00 < 128) {
  1170. for (int i = tiisg; i < ne00; i += 32) {
  1171. sumf += (float) x[i] * (float) y[i];
  1172. }
  1173. float all_sum = simd_sum(sumf);
  1174. if (tiisg == 0) {
  1175. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1176. }
  1177. } else {
  1178. device const half4 * x4 = (device const half4 *) x;
  1179. device const float4 * y4 = (device const float4 *) y;
  1180. for (int i = tiisg; i < ne00/4; i += 32) {
  1181. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  1182. }
  1183. float all_sum = simd_sum(sumf);
  1184. if (tiisg == 0) {
  1185. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1186. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1187. }
  1188. }
  1189. }
  1190. [[host_name("kernel_mul_mv_f16_f32_1row")]]
  1191. kernel void kernel_mul_mv_f16_f32_1row(
  1192. device const char * src0,
  1193. device const char * src1,
  1194. device float * dst,
  1195. constant int64_t & ne00,
  1196. constant int64_t & ne01,
  1197. constant int64_t & ne02,
  1198. constant uint64_t & nb00,
  1199. constant uint64_t & nb01,
  1200. constant uint64_t & nb02,
  1201. constant int64_t & ne10,
  1202. constant int64_t & ne11,
  1203. constant int64_t & ne12,
  1204. constant uint64_t & nb10,
  1205. constant uint64_t & nb11,
  1206. constant uint64_t & nb12,
  1207. constant int64_t & ne0,
  1208. constant int64_t & ne1,
  1209. constant uint & r2,
  1210. constant uint & r3,
  1211. uint3 tgpig[[threadgroup_position_in_grid]],
  1212. uint tiisg[[thread_index_in_simdgroup]]) {
  1213. kernel_mul_mv_f16_f32_1row_impl(src0, src1, dst, ne00, ne01, ne02, nb00, nb01, nb02, ne10, ne11, ne12, nb10, nb11, nb12, ne0, ne1, r2, r3, tgpig, tiisg);
  1214. }
  1215. #define N_F16_F32 4
  1216. void kernel_mul_mv_f16_f32_impl(
  1217. device const char * src0,
  1218. device const char * src1,
  1219. device float * dst,
  1220. constant int64_t & ne00,
  1221. constant int64_t & ne01,
  1222. constant int64_t & ne02,
  1223. constant uint64_t & nb00,
  1224. constant uint64_t & nb01,
  1225. constant uint64_t & nb02,
  1226. constant int64_t & ne10,
  1227. constant int64_t & ne11,
  1228. constant int64_t & ne12,
  1229. constant uint64_t & nb10,
  1230. constant uint64_t & nb11,
  1231. constant uint64_t & nb12,
  1232. constant int64_t & ne0,
  1233. constant int64_t & ne1,
  1234. constant uint & r2,
  1235. constant uint & r3,
  1236. uint3 tgpig[[threadgroup_position_in_grid]],
  1237. uint tiisg[[thread_index_in_simdgroup]]) {
  1238. const int64_t r0 = tgpig.x;
  1239. const int64_t rb = tgpig.y*N_F16_F32;
  1240. const int64_t im = tgpig.z;
  1241. const uint i12 = im%ne12;
  1242. const uint i13 = im/ne12;
  1243. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1244. device const half * x = (device const half *) (src0 + offset0);
  1245. if (ne00 < 128) {
  1246. for (int row = 0; row < N_F16_F32; ++row) {
  1247. int r1 = rb + row;
  1248. if (r1 >= ne11) {
  1249. break;
  1250. }
  1251. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1252. float sumf = 0;
  1253. for (int i = tiisg; i < ne00; i += 32) {
  1254. sumf += (float) x[i] * (float) y[i];
  1255. }
  1256. float all_sum = simd_sum(sumf);
  1257. if (tiisg == 0) {
  1258. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1259. }
  1260. }
  1261. } else {
  1262. device const half4 * x4 = (device const half4 *)x;
  1263. for (int row = 0; row < N_F16_F32; ++row) {
  1264. int r1 = rb + row;
  1265. if (r1 >= ne11) {
  1266. break;
  1267. }
  1268. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1269. device const float4 * y4 = (device const float4 *) y;
  1270. float sumf = 0;
  1271. for (int i = tiisg; i < ne00/4; i += 32) {
  1272. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1273. }
  1274. float all_sum = simd_sum(sumf);
  1275. if (tiisg == 0) {
  1276. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1277. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1278. }
  1279. }
  1280. }
  1281. }
  1282. [[host_name("kernel_mul_mv_f16_f32")]]
  1283. kernel void kernel_mul_mv_f16_f32(
  1284. device const char * src0,
  1285. device const char * src1,
  1286. device float * dst,
  1287. constant int64_t & ne00,
  1288. constant int64_t & ne01,
  1289. constant int64_t & ne02,
  1290. constant uint64_t & nb00,
  1291. constant uint64_t & nb01,
  1292. constant uint64_t & nb02,
  1293. constant int64_t & ne10,
  1294. constant int64_t & ne11,
  1295. constant int64_t & ne12,
  1296. constant uint64_t & nb10,
  1297. constant uint64_t & nb11,
  1298. constant uint64_t & nb12,
  1299. constant int64_t & ne0,
  1300. constant int64_t & ne1,
  1301. constant uint & r2,
  1302. constant uint & r3,
  1303. uint3 tgpig[[threadgroup_position_in_grid]],
  1304. uint tiisg[[thread_index_in_simdgroup]]) {
  1305. kernel_mul_mv_f16_f32_impl(src0, src1, dst, ne00, ne01, ne02, nb00, nb01, nb02, ne10, ne11, ne12, nb10, nb11, nb12, ne0, ne1, r2, r3, tgpig, tiisg);
  1306. }
  1307. // Assumes row size (ne00) is a multiple of 4
  1308. kernel void kernel_mul_mv_f16_f32_l4(
  1309. device const char * src0,
  1310. device const char * src1,
  1311. device float * dst,
  1312. constant int64_t & ne00,
  1313. constant int64_t & ne01,
  1314. constant int64_t & ne02,
  1315. constant uint64_t & nb00,
  1316. constant uint64_t & nb01,
  1317. constant uint64_t & nb02,
  1318. constant int64_t & ne10,
  1319. constant int64_t & ne11,
  1320. constant int64_t & ne12,
  1321. constant uint64_t & nb10,
  1322. constant uint64_t & nb11,
  1323. constant uint64_t & nb12,
  1324. constant int64_t & ne0,
  1325. constant int64_t & ne1,
  1326. constant uint & r2,
  1327. constant uint & r3,
  1328. uint3 tgpig[[threadgroup_position_in_grid]],
  1329. uint tiisg[[thread_index_in_simdgroup]]) {
  1330. const int nrows = ne11;
  1331. const int64_t r0 = tgpig.x;
  1332. const int64_t im = tgpig.z;
  1333. const uint i12 = im%ne12;
  1334. const uint i13 = im/ne12;
  1335. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1336. device const half4 * x4 = (device const half4 *) (src0 + offset0);
  1337. for (int r1 = 0; r1 < nrows; ++r1) {
  1338. device const float4 * y4 = (device const float4 *) (src1 + r1*nb11 + im*nb12);
  1339. float sumf = 0;
  1340. for (int i = tiisg; i < ne00/4; i += 32) {
  1341. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1342. }
  1343. float all_sum = simd_sum(sumf);
  1344. if (tiisg == 0) {
  1345. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1346. }
  1347. }
  1348. }
  1349. kernel void kernel_alibi_f32(
  1350. device const float * src0,
  1351. device float * dst,
  1352. constant int64_t & ne00,
  1353. constant int64_t & ne01,
  1354. constant int64_t & ne02,
  1355. constant int64_t & ne03,
  1356. constant uint64_t & nb00,
  1357. constant uint64_t & nb01,
  1358. constant uint64_t & nb02,
  1359. constant uint64_t & nb03,
  1360. constant int64_t & ne0,
  1361. constant int64_t & ne1,
  1362. constant int64_t & ne2,
  1363. constant int64_t & ne3,
  1364. constant uint64_t & nb0,
  1365. constant uint64_t & nb1,
  1366. constant uint64_t & nb2,
  1367. constant uint64_t & nb3,
  1368. constant float & m0,
  1369. constant float & m1,
  1370. constant int & n_heads_log2_floor,
  1371. uint3 tgpig[[threadgroup_position_in_grid]],
  1372. uint3 tpitg[[thread_position_in_threadgroup]],
  1373. uint3 ntg[[threads_per_threadgroup]]) {
  1374. const int64_t i03 = tgpig[2];
  1375. const int64_t i02 = tgpig[1];
  1376. const int64_t i01 = tgpig[0];
  1377. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1378. const int64_t i3 = n / (ne2*ne1*ne0);
  1379. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1380. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1381. //const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1382. const int64_t k = i3*ne3 + i2;
  1383. float m_k;
  1384. if (k < n_heads_log2_floor) {
  1385. m_k = pow(m0, k + 1);
  1386. } else {
  1387. m_k = pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  1388. }
  1389. device char * dst_row = (device char *) dst + i3*nb3 + i2*nb2 + i1*nb1;
  1390. device const char * src_row = (device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01;
  1391. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1392. const float src_v = *(device float *)(src_row + i00*nb00);
  1393. device float * dst_v = (device float *)(dst_row + i00*nb0);
  1394. *dst_v = i00 * m_k + src_v;
  1395. }
  1396. }
  1397. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  1398. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  1399. return 1.0f - min(1.0f, max(0.0f, y));
  1400. }
  1401. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  1402. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  1403. static void rope_yarn(
  1404. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  1405. thread float * cos_theta, thread float * sin_theta
  1406. ) {
  1407. // Get n-d rotational scaling corrected for extrapolation
  1408. float theta_interp = freq_scale * theta_extrap;
  1409. float theta = theta_interp;
  1410. if (ext_factor != 0.0f) {
  1411. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  1412. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1413. // Get n-d magnitude scaling corrected for interpolation
  1414. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  1415. }
  1416. *cos_theta = cos(theta) * mscale;
  1417. *sin_theta = sin(theta) * mscale;
  1418. }
  1419. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  1420. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  1421. static float rope_yarn_corr_factor(int n_dims, int n_orig_ctx, float n_rot, float base) {
  1422. return n_dims * log(n_orig_ctx / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  1423. }
  1424. static void rope_yarn_corr_dims(
  1425. int n_dims, int n_orig_ctx, float freq_base, float beta_fast, float beta_slow, float dims[2]
  1426. ) {
  1427. // start and end correction dims
  1428. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_orig_ctx, beta_fast, freq_base)));
  1429. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_orig_ctx, beta_slow, freq_base)));
  1430. }
  1431. typedef void (rope_t)(
  1432. device const void * src0,
  1433. device const int32_t * src1,
  1434. device float * dst,
  1435. constant int64_t & ne00,
  1436. constant int64_t & ne01,
  1437. constant int64_t & ne02,
  1438. constant int64_t & ne03,
  1439. constant uint64_t & nb00,
  1440. constant uint64_t & nb01,
  1441. constant uint64_t & nb02,
  1442. constant uint64_t & nb03,
  1443. constant int64_t & ne0,
  1444. constant int64_t & ne1,
  1445. constant int64_t & ne2,
  1446. constant int64_t & ne3,
  1447. constant uint64_t & nb0,
  1448. constant uint64_t & nb1,
  1449. constant uint64_t & nb2,
  1450. constant uint64_t & nb3,
  1451. constant int & n_past,
  1452. constant int & n_dims,
  1453. constant int & mode,
  1454. constant int & n_orig_ctx,
  1455. constant float & freq_base,
  1456. constant float & freq_scale,
  1457. constant float & ext_factor,
  1458. constant float & attn_factor,
  1459. constant float & beta_fast,
  1460. constant float & beta_slow,
  1461. uint tiitg[[thread_index_in_threadgroup]],
  1462. uint3 tptg[[threads_per_threadgroup]],
  1463. uint3 tgpig[[threadgroup_position_in_grid]]);
  1464. template<typename T>
  1465. kernel void kernel_rope(
  1466. device const void * src0,
  1467. device const int32_t * src1,
  1468. device float * dst,
  1469. constant int64_t & ne00,
  1470. constant int64_t & ne01,
  1471. constant int64_t & ne02,
  1472. constant int64_t & ne03,
  1473. constant uint64_t & nb00,
  1474. constant uint64_t & nb01,
  1475. constant uint64_t & nb02,
  1476. constant uint64_t & nb03,
  1477. constant int64_t & ne0,
  1478. constant int64_t & ne1,
  1479. constant int64_t & ne2,
  1480. constant int64_t & ne3,
  1481. constant uint64_t & nb0,
  1482. constant uint64_t & nb1,
  1483. constant uint64_t & nb2,
  1484. constant uint64_t & nb3,
  1485. constant int & n_past,
  1486. constant int & n_dims,
  1487. constant int & mode,
  1488. constant int & n_orig_ctx,
  1489. constant float & freq_base,
  1490. constant float & freq_scale,
  1491. constant float & ext_factor,
  1492. constant float & attn_factor,
  1493. constant float & beta_fast,
  1494. constant float & beta_slow,
  1495. uint tiitg[[thread_index_in_threadgroup]],
  1496. uint3 tptg[[threads_per_threadgroup]],
  1497. uint3 tgpig[[threadgroup_position_in_grid]]) {
  1498. const int64_t i3 = tgpig[2];
  1499. const int64_t i2 = tgpig[1];
  1500. const int64_t i1 = tgpig[0];
  1501. const bool is_neox = mode & 2;
  1502. float corr_dims[2];
  1503. rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims);
  1504. device const int32_t * pos = src1;
  1505. const int64_t p = pos[i2];
  1506. const float theta_0 = (float)p;
  1507. const float inv_ndims = -1.f/n_dims;
  1508. if (!is_neox) {
  1509. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  1510. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  1511. float cos_theta, sin_theta;
  1512. rope_yarn(theta, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1513. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1514. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1515. const T x0 = src[0];
  1516. const T x1 = src[1];
  1517. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1518. dst_data[1] = x0*sin_theta + x1*cos_theta;
  1519. }
  1520. } else {
  1521. for (int64_t ic = 2*tiitg; ic < ne0; ic += 2*tptg.x) {
  1522. if (ic < n_dims) {
  1523. const int64_t ib = 0;
  1524. // simplified from `(ib * n_dims + ic) * inv_ndims`
  1525. const float cur_rot = inv_ndims*ic - ib;
  1526. const float theta = theta_0 * pow(freq_base, cur_rot);
  1527. float cos_theta, sin_theta;
  1528. rope_yarn(theta, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1529. const int64_t i0 = ib*n_dims + ic/2;
  1530. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1531. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1532. const float x0 = src[0];
  1533. const float x1 = src[n_dims/2];
  1534. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1535. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  1536. } else {
  1537. const int64_t i0 = ic;
  1538. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1539. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1540. dst_data[0] = src[0];
  1541. dst_data[1] = src[1];
  1542. }
  1543. }
  1544. }
  1545. }
  1546. template [[host_name("kernel_rope_f32")]] kernel rope_t kernel_rope<float>;
  1547. template [[host_name("kernel_rope_f16")]] kernel rope_t kernel_rope<half>;
  1548. typedef void (im2col_t)(
  1549. device const float * x,
  1550. device char * dst,
  1551. constant int32_t & ofs0,
  1552. constant int32_t & ofs1,
  1553. constant int32_t & IW,
  1554. constant int32_t & IH,
  1555. constant int32_t & CHW,
  1556. constant int32_t & s0,
  1557. constant int32_t & s1,
  1558. constant int32_t & p0,
  1559. constant int32_t & p1,
  1560. constant int32_t & d0,
  1561. constant int32_t & d1,
  1562. uint3 tgpig[[threadgroup_position_in_grid]],
  1563. uint3 tgpg[[threadgroups_per_grid]],
  1564. uint3 tpitg[[thread_position_in_threadgroup]],
  1565. uint3 ntg[[threads_per_threadgroup]]);
  1566. template <typename T>
  1567. kernel void kernel_im2col(
  1568. device const float * x,
  1569. device char * dst,
  1570. constant int32_t & ofs0,
  1571. constant int32_t & ofs1,
  1572. constant int32_t & IW,
  1573. constant int32_t & IH,
  1574. constant int32_t & CHW,
  1575. constant int32_t & s0,
  1576. constant int32_t & s1,
  1577. constant int32_t & p0,
  1578. constant int32_t & p1,
  1579. constant int32_t & d0,
  1580. constant int32_t & d1,
  1581. uint3 tgpig[[threadgroup_position_in_grid]],
  1582. uint3 tgpg[[threadgroups_per_grid]],
  1583. uint3 tpitg[[thread_position_in_threadgroup]],
  1584. uint3 ntg[[threads_per_threadgroup]]) {
  1585. const int32_t iiw = tgpig[2] * s0 + tpitg[2] * d0 - p0;
  1586. const int32_t iih = tgpig[1] * s1 + tpitg[1] * d1 - p1;
  1587. const int32_t offset_dst =
  1588. (tpitg[0] * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  1589. (tgpig[0] * (ntg[1] * ntg[2]) + tpitg[1] * ntg[2] + tpitg[2]);
  1590. device T * pdst = (device T *) (dst);
  1591. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  1592. pdst[offset_dst] = 0.0f;
  1593. } else {
  1594. const int32_t offset_src = tpitg[0] * ofs0 + tgpig[0] * ofs1;
  1595. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  1596. }
  1597. }
  1598. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  1599. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  1600. kernel void kernel_upscale_f32(
  1601. device const char * src0,
  1602. device char * dst,
  1603. constant int64_t & ne00,
  1604. constant int64_t & ne01,
  1605. constant int64_t & ne02,
  1606. constant int64_t & ne03,
  1607. constant uint64_t & nb00,
  1608. constant uint64_t & nb01,
  1609. constant uint64_t & nb02,
  1610. constant uint64_t & nb03,
  1611. constant int64_t & ne0,
  1612. constant int64_t & ne1,
  1613. constant int64_t & ne2,
  1614. constant int64_t & ne3,
  1615. constant uint64_t & nb0,
  1616. constant uint64_t & nb1,
  1617. constant uint64_t & nb2,
  1618. constant uint64_t & nb3,
  1619. constant int32_t & sf,
  1620. uint3 tgpig[[threadgroup_position_in_grid]],
  1621. uint3 tpitg[[thread_position_in_threadgroup]],
  1622. uint3 ntg[[threads_per_threadgroup]]) {
  1623. const int64_t i3 = tgpig.z;
  1624. const int64_t i2 = tgpig.y;
  1625. const int64_t i1 = tgpig.x;
  1626. const int64_t i03 = i3;
  1627. const int64_t i02 = i2;
  1628. const int64_t i01 = i1/sf;
  1629. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  1630. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  1631. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1632. dst_ptr[i0] = src0_ptr[i0/sf];
  1633. }
  1634. }
  1635. kernel void kernel_pad_f32(
  1636. device const char * src0,
  1637. device char * dst,
  1638. constant int64_t & ne00,
  1639. constant int64_t & ne01,
  1640. constant int64_t & ne02,
  1641. constant int64_t & ne03,
  1642. constant uint64_t & nb00,
  1643. constant uint64_t & nb01,
  1644. constant uint64_t & nb02,
  1645. constant uint64_t & nb03,
  1646. constant int64_t & ne0,
  1647. constant int64_t & ne1,
  1648. constant int64_t & ne2,
  1649. constant int64_t & ne3,
  1650. constant uint64_t & nb0,
  1651. constant uint64_t & nb1,
  1652. constant uint64_t & nb2,
  1653. constant uint64_t & nb3,
  1654. uint3 tgpig[[threadgroup_position_in_grid]],
  1655. uint3 tpitg[[thread_position_in_threadgroup]],
  1656. uint3 ntg[[threads_per_threadgroup]]) {
  1657. const int64_t i3 = tgpig.z;
  1658. const int64_t i2 = tgpig.y;
  1659. const int64_t i1 = tgpig.x;
  1660. const int64_t i03 = i3;
  1661. const int64_t i02 = i2;
  1662. const int64_t i01 = i1;
  1663. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  1664. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  1665. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  1666. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1667. if (i0 < ne00) {
  1668. dst_ptr[i0] = src0_ptr[i0];
  1669. } else {
  1670. dst_ptr[i0] = 0.0f;
  1671. }
  1672. }
  1673. return;
  1674. }
  1675. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1676. dst_ptr[i0] = 0.0f;
  1677. }
  1678. }
  1679. // bitonic sort implementation following the CUDA kernels as reference
  1680. typedef void (argsort_t)(
  1681. device const float * x,
  1682. device int32_t * dst,
  1683. constant int64_t & ncols,
  1684. uint3 tgpig[[threadgroup_position_in_grid]],
  1685. uint3 tpitg[[thread_position_in_threadgroup]]);
  1686. template<ggml_sort_order order>
  1687. kernel void kernel_argsort_f32_i32(
  1688. device const float * x,
  1689. device int32_t * dst,
  1690. constant int64_t & ncols,
  1691. uint3 tgpig[[threadgroup_position_in_grid]],
  1692. uint3 tpitg[[thread_position_in_threadgroup]]) {
  1693. // bitonic sort
  1694. int col = tpitg[0];
  1695. int row = tgpig[1];
  1696. if (col >= ncols) return;
  1697. device const float * x_row = x + row * ncols;
  1698. device int32_t * dst_row = dst + row * ncols;
  1699. // initialize indices
  1700. if (col < ncols) {
  1701. dst_row[col] = col;
  1702. }
  1703. threadgroup_barrier(mem_flags::mem_threadgroup);
  1704. for (int k = 2; k <= ncols; k *= 2) {
  1705. for (int j = k / 2; j > 0; j /= 2) {
  1706. int ixj = col ^ j;
  1707. if (ixj > col) {
  1708. if ((col & k) == 0) {
  1709. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  1710. SWAP(dst_row[col], dst_row[ixj]);
  1711. }
  1712. } else {
  1713. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  1714. SWAP(dst_row[col], dst_row[ixj]);
  1715. }
  1716. }
  1717. }
  1718. threadgroup_barrier(mem_flags::mem_threadgroup);
  1719. }
  1720. }
  1721. }
  1722. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ASC>;
  1723. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_DESC>;
  1724. kernel void kernel_leaky_relu_f32(
  1725. device const float * src0,
  1726. device float * dst,
  1727. constant float & slope,
  1728. uint tpig[[thread_position_in_grid]]) {
  1729. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  1730. }
  1731. kernel void kernel_cpy_f16_f16(
  1732. device const half * src0,
  1733. device half * dst,
  1734. constant int64_t & ne00,
  1735. constant int64_t & ne01,
  1736. constant int64_t & ne02,
  1737. constant int64_t & ne03,
  1738. constant uint64_t & nb00,
  1739. constant uint64_t & nb01,
  1740. constant uint64_t & nb02,
  1741. constant uint64_t & nb03,
  1742. constant int64_t & ne0,
  1743. constant int64_t & ne1,
  1744. constant int64_t & ne2,
  1745. constant int64_t & ne3,
  1746. constant uint64_t & nb0,
  1747. constant uint64_t & nb1,
  1748. constant uint64_t & nb2,
  1749. constant uint64_t & nb3,
  1750. uint3 tgpig[[threadgroup_position_in_grid]],
  1751. uint3 tpitg[[thread_position_in_threadgroup]],
  1752. uint3 ntg[[threads_per_threadgroup]]) {
  1753. const int64_t i03 = tgpig[2];
  1754. const int64_t i02 = tgpig[1];
  1755. const int64_t i01 = tgpig[0];
  1756. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1757. const int64_t i3 = n / (ne2*ne1*ne0);
  1758. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1759. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1760. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1761. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1762. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1763. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1764. dst_data[i00] = src[0];
  1765. }
  1766. }
  1767. kernel void kernel_cpy_f16_f32(
  1768. device const half * src0,
  1769. device float * dst,
  1770. constant int64_t & ne00,
  1771. constant int64_t & ne01,
  1772. constant int64_t & ne02,
  1773. constant int64_t & ne03,
  1774. constant uint64_t & nb00,
  1775. constant uint64_t & nb01,
  1776. constant uint64_t & nb02,
  1777. constant uint64_t & nb03,
  1778. constant int64_t & ne0,
  1779. constant int64_t & ne1,
  1780. constant int64_t & ne2,
  1781. constant int64_t & ne3,
  1782. constant uint64_t & nb0,
  1783. constant uint64_t & nb1,
  1784. constant uint64_t & nb2,
  1785. constant uint64_t & nb3,
  1786. uint3 tgpig[[threadgroup_position_in_grid]],
  1787. uint3 tpitg[[thread_position_in_threadgroup]],
  1788. uint3 ntg[[threads_per_threadgroup]]) {
  1789. const int64_t i03 = tgpig[2];
  1790. const int64_t i02 = tgpig[1];
  1791. const int64_t i01 = tgpig[0];
  1792. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1793. const int64_t i3 = n / (ne2*ne1*ne0);
  1794. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1795. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1796. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1797. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1798. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1799. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1800. dst_data[i00] = src[0];
  1801. }
  1802. }
  1803. kernel void kernel_cpy_f32_f16(
  1804. device const float * src0,
  1805. device half * dst,
  1806. constant int64_t & ne00,
  1807. constant int64_t & ne01,
  1808. constant int64_t & ne02,
  1809. constant int64_t & ne03,
  1810. constant uint64_t & nb00,
  1811. constant uint64_t & nb01,
  1812. constant uint64_t & nb02,
  1813. constant uint64_t & nb03,
  1814. constant int64_t & ne0,
  1815. constant int64_t & ne1,
  1816. constant int64_t & ne2,
  1817. constant int64_t & ne3,
  1818. constant uint64_t & nb0,
  1819. constant uint64_t & nb1,
  1820. constant uint64_t & nb2,
  1821. constant uint64_t & nb3,
  1822. uint3 tgpig[[threadgroup_position_in_grid]],
  1823. uint3 tpitg[[thread_position_in_threadgroup]],
  1824. uint3 ntg[[threads_per_threadgroup]]) {
  1825. const int64_t i03 = tgpig[2];
  1826. const int64_t i02 = tgpig[1];
  1827. const int64_t i01 = tgpig[0];
  1828. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1829. const int64_t i3 = n / (ne2*ne1*ne0);
  1830. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1831. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1832. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1833. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1834. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1835. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1836. dst_data[i00] = src[0];
  1837. }
  1838. }
  1839. kernel void kernel_cpy_f32_f32(
  1840. device const float * src0,
  1841. device float * dst,
  1842. constant int64_t & ne00,
  1843. constant int64_t & ne01,
  1844. constant int64_t & ne02,
  1845. constant int64_t & ne03,
  1846. constant uint64_t & nb00,
  1847. constant uint64_t & nb01,
  1848. constant uint64_t & nb02,
  1849. constant uint64_t & nb03,
  1850. constant int64_t & ne0,
  1851. constant int64_t & ne1,
  1852. constant int64_t & ne2,
  1853. constant int64_t & ne3,
  1854. constant uint64_t & nb0,
  1855. constant uint64_t & nb1,
  1856. constant uint64_t & nb2,
  1857. constant uint64_t & nb3,
  1858. uint3 tgpig[[threadgroup_position_in_grid]],
  1859. uint3 tpitg[[thread_position_in_threadgroup]],
  1860. uint3 ntg[[threads_per_threadgroup]]) {
  1861. const int64_t i03 = tgpig[2];
  1862. const int64_t i02 = tgpig[1];
  1863. const int64_t i01 = tgpig[0];
  1864. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1865. const int64_t i3 = n / (ne2*ne1*ne0);
  1866. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1867. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1868. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1869. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1870. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1871. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1872. dst_data[i00] = src[0];
  1873. }
  1874. }
  1875. kernel void kernel_cpy_f32_q8_0(
  1876. device const float * src0,
  1877. device void * dst,
  1878. constant int64_t & ne00,
  1879. constant int64_t & ne01,
  1880. constant int64_t & ne02,
  1881. constant int64_t & ne03,
  1882. constant uint64_t & nb00,
  1883. constant uint64_t & nb01,
  1884. constant uint64_t & nb02,
  1885. constant uint64_t & nb03,
  1886. constant int64_t & ne0,
  1887. constant int64_t & ne1,
  1888. constant int64_t & ne2,
  1889. constant int64_t & ne3,
  1890. constant uint64_t & nb0,
  1891. constant uint64_t & nb1,
  1892. constant uint64_t & nb2,
  1893. constant uint64_t & nb3,
  1894. uint3 tgpig[[threadgroup_position_in_grid]],
  1895. uint3 tpitg[[thread_position_in_threadgroup]],
  1896. uint3 ntg[[threads_per_threadgroup]]) {
  1897. const int64_t i03 = tgpig[2];
  1898. const int64_t i02 = tgpig[1];
  1899. const int64_t i01 = tgpig[0];
  1900. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1901. const int64_t i3 = n / (ne2*ne1*ne0);
  1902. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1903. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1904. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK8_0;
  1905. device block_q8_0 * dst_data = (device block_q8_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1906. for (int64_t i00 = tpitg.x*QK8_0; i00 < ne00; i00 += ntg.x*QK8_0) {
  1907. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1908. float amax = 0.0f; // absolute max
  1909. for (int j = 0; j < QK8_0; j++) {
  1910. const float v = src[j];
  1911. amax = MAX(amax, fabs(v));
  1912. }
  1913. const float d = amax / ((1 << 7) - 1);
  1914. const float id = d ? 1.0f/d : 0.0f;
  1915. dst_data[i00/QK8_0].d = d;
  1916. for (int j = 0; j < QK8_0; ++j) {
  1917. const float x0 = src[j]*id;
  1918. dst_data[i00/QK8_0].qs[j] = round(x0);
  1919. }
  1920. }
  1921. }
  1922. kernel void kernel_cpy_f32_q4_0(
  1923. device const float * src0,
  1924. device void * dst,
  1925. constant int64_t & ne00,
  1926. constant int64_t & ne01,
  1927. constant int64_t & ne02,
  1928. constant int64_t & ne03,
  1929. constant uint64_t & nb00,
  1930. constant uint64_t & nb01,
  1931. constant uint64_t & nb02,
  1932. constant uint64_t & nb03,
  1933. constant int64_t & ne0,
  1934. constant int64_t & ne1,
  1935. constant int64_t & ne2,
  1936. constant int64_t & ne3,
  1937. constant uint64_t & nb0,
  1938. constant uint64_t & nb1,
  1939. constant uint64_t & nb2,
  1940. constant uint64_t & nb3,
  1941. uint3 tgpig[[threadgroup_position_in_grid]],
  1942. uint3 tpitg[[thread_position_in_threadgroup]],
  1943. uint3 ntg[[threads_per_threadgroup]]) {
  1944. const int64_t i03 = tgpig[2];
  1945. const int64_t i02 = tgpig[1];
  1946. const int64_t i01 = tgpig[0];
  1947. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1948. const int64_t i3 = n / (ne2*ne1*ne0);
  1949. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1950. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1951. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_0;
  1952. device block_q4_0 * dst_data = (device block_q4_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1953. for (int64_t i00 = tpitg.x*QK4_0; i00 < ne00; i00 += ntg.x*QK4_0) {
  1954. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1955. float amax = 0.0f; // absolute max
  1956. float max = 0.0f;
  1957. for (int j = 0; j < QK4_0; j++) {
  1958. const float v = src[j];
  1959. if (amax < fabs(v)) {
  1960. amax = fabs(v);
  1961. max = v;
  1962. }
  1963. }
  1964. const float d = max / -8;
  1965. const float id = d ? 1.0f/d : 0.0f;
  1966. dst_data[i00/QK4_0].d = d;
  1967. for (int j = 0; j < QK4_0/2; ++j) {
  1968. const float x0 = src[0 + j]*id;
  1969. const float x1 = src[QK4_0/2 + j]*id;
  1970. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  1971. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  1972. dst_data[i00/QK4_0].qs[j] = xi0;
  1973. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  1974. }
  1975. }
  1976. }
  1977. kernel void kernel_cpy_f32_q4_1(
  1978. device const float * src0,
  1979. device void * dst,
  1980. constant int64_t & ne00,
  1981. constant int64_t & ne01,
  1982. constant int64_t & ne02,
  1983. constant int64_t & ne03,
  1984. constant uint64_t & nb00,
  1985. constant uint64_t & nb01,
  1986. constant uint64_t & nb02,
  1987. constant uint64_t & nb03,
  1988. constant int64_t & ne0,
  1989. constant int64_t & ne1,
  1990. constant int64_t & ne2,
  1991. constant int64_t & ne3,
  1992. constant uint64_t & nb0,
  1993. constant uint64_t & nb1,
  1994. constant uint64_t & nb2,
  1995. constant uint64_t & nb3,
  1996. uint3 tgpig[[threadgroup_position_in_grid]],
  1997. uint3 tpitg[[thread_position_in_threadgroup]],
  1998. uint3 ntg[[threads_per_threadgroup]]) {
  1999. const int64_t i03 = tgpig[2];
  2000. const int64_t i02 = tgpig[1];
  2001. const int64_t i01 = tgpig[0];
  2002. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2003. const int64_t i3 = n / (ne2*ne1*ne0);
  2004. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2005. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2006. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_1;
  2007. device block_q4_1 * dst_data = (device block_q4_1 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2008. for (int64_t i00 = tpitg.x*QK4_1; i00 < ne00; i00 += ntg.x*QK4_1) {
  2009. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2010. float min = FLT_MAX;
  2011. float max = -FLT_MAX;
  2012. for (int j = 0; j < QK4_1; j++) {
  2013. const float v = src[j];
  2014. if (min > v) min = v;
  2015. if (max < v) max = v;
  2016. }
  2017. const float d = (max - min) / ((1 << 4) - 1);
  2018. const float id = d ? 1.0f/d : 0.0f;
  2019. dst_data[i00/QK4_1].d = d;
  2020. dst_data[i00/QK4_1].m = min;
  2021. for (int j = 0; j < QK4_1/2; ++j) {
  2022. const float x0 = (src[0 + j] - min)*id;
  2023. const float x1 = (src[QK4_1/2 + j] - min)*id;
  2024. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  2025. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  2026. dst_data[i00/QK4_1].qs[j] = xi0;
  2027. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  2028. }
  2029. }
  2030. }
  2031. kernel void kernel_concat(
  2032. device const char * src0,
  2033. device const char * src1,
  2034. device char * dst,
  2035. constant int64_t & ne00,
  2036. constant int64_t & ne01,
  2037. constant int64_t & ne02,
  2038. constant int64_t & ne03,
  2039. constant uint64_t & nb00,
  2040. constant uint64_t & nb01,
  2041. constant uint64_t & nb02,
  2042. constant uint64_t & nb03,
  2043. constant int64_t & ne10,
  2044. constant int64_t & ne11,
  2045. constant int64_t & ne12,
  2046. constant int64_t & ne13,
  2047. constant uint64_t & nb10,
  2048. constant uint64_t & nb11,
  2049. constant uint64_t & nb12,
  2050. constant uint64_t & nb13,
  2051. constant int64_t & ne0,
  2052. constant int64_t & ne1,
  2053. constant int64_t & ne2,
  2054. constant int64_t & ne3,
  2055. constant uint64_t & nb0,
  2056. constant uint64_t & nb1,
  2057. constant uint64_t & nb2,
  2058. constant uint64_t & nb3,
  2059. uint3 tgpig[[threadgroup_position_in_grid]],
  2060. uint3 tpitg[[thread_position_in_threadgroup]],
  2061. uint3 ntg[[threads_per_threadgroup]]) {
  2062. const int64_t i03 = tgpig.z;
  2063. const int64_t i02 = tgpig.y;
  2064. const int64_t i01 = tgpig.x;
  2065. const int64_t i13 = i03 % ne13;
  2066. const int64_t i12 = i02 % ne12;
  2067. const int64_t i11 = i01 % ne11;
  2068. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01 + tpitg.x*nb00;
  2069. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11 + tpitg.x*nb10;
  2070. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + tpitg.x*nb0;
  2071. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2072. if (i02 < ne02) {
  2073. ((device float *)dst_ptr)[0] = ((device float *)src0_ptr)[0];
  2074. src0_ptr += ntg.x*nb00;
  2075. } else {
  2076. ((device float *)dst_ptr)[0] = ((device float *)src1_ptr)[0];
  2077. src1_ptr += ntg.x*nb10;
  2078. }
  2079. dst_ptr += ntg.x*nb0;
  2080. }
  2081. }
  2082. //============================================ k-quants ======================================================
  2083. #ifndef QK_K
  2084. #define QK_K 256
  2085. #else
  2086. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  2087. #endif
  2088. #if QK_K == 256
  2089. #define K_SCALE_SIZE 12
  2090. #else
  2091. #define K_SCALE_SIZE 4
  2092. #endif
  2093. typedef struct {
  2094. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  2095. uint8_t qs[QK_K/4]; // quants
  2096. half d; // super-block scale for quantized scales
  2097. half dmin; // super-block scale for quantized mins
  2098. } block_q2_K;
  2099. // 84 bytes / block
  2100. typedef struct {
  2101. uint8_t hmask[QK_K/8]; // quants - high bit
  2102. uint8_t qs[QK_K/4]; // quants - low 2 bits
  2103. #if QK_K == 64
  2104. uint8_t scales[2];
  2105. #else
  2106. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  2107. #endif
  2108. half d; // super-block scale
  2109. } block_q3_K;
  2110. #if QK_K == 64
  2111. typedef struct {
  2112. half d[2]; // super-block scales/mins
  2113. uint8_t scales[2];
  2114. uint8_t qs[QK_K/2]; // 4-bit quants
  2115. } block_q4_K;
  2116. #else
  2117. typedef struct {
  2118. half d; // super-block scale for quantized scales
  2119. half dmin; // super-block scale for quantized mins
  2120. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  2121. uint8_t qs[QK_K/2]; // 4--bit quants
  2122. } block_q4_K;
  2123. #endif
  2124. #if QK_K == 64
  2125. typedef struct {
  2126. half d; // super-block scales/mins
  2127. int8_t scales[QK_K/16]; // 8-bit block scales
  2128. uint8_t qh[QK_K/8]; // quants, high bit
  2129. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2130. } block_q5_K;
  2131. #else
  2132. typedef struct {
  2133. half d; // super-block scale for quantized scales
  2134. half dmin; // super-block scale for quantized mins
  2135. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  2136. uint8_t qh[QK_K/8]; // quants, high bit
  2137. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2138. } block_q5_K;
  2139. // 176 bytes / block
  2140. #endif
  2141. typedef struct {
  2142. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  2143. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  2144. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  2145. half d; // super-block scale
  2146. } block_q6_K;
  2147. // 210 bytes / block
  2148. typedef struct {
  2149. half d;
  2150. uint16_t qs[QK_K/8];
  2151. } block_iq2_xxs;
  2152. // 66 bytes / block for QK_K = 256, so 2.0625 bpw
  2153. typedef struct {
  2154. half d;
  2155. uint16_t qs[QK_K/8];
  2156. uint8_t scales[QK_K/32];
  2157. } block_iq2_xs;
  2158. // 74 bytes / block for QK_K = 256, so 2.3125 bpw
  2159. // 2.5625 bpw quants
  2160. typedef struct {
  2161. half d;
  2162. uint8_t qs[QK_K/4];
  2163. uint8_t qh[QK_K/32];
  2164. uint8_t scales[QK_K/32];
  2165. } block_iq2_s;
  2166. typedef struct {
  2167. half d;
  2168. uint8_t qs[3*QK_K/8];
  2169. } block_iq3_xxs;
  2170. // 98 bytes / block for QK_K = 256, so 3.0625 bpw
  2171. // 3.4375 bpw
  2172. #if QK_K == 64
  2173. #define IQ3S_N_SCALE 2
  2174. #else
  2175. #define IQ3S_N_SCALE QK_K/64
  2176. #endif
  2177. typedef struct {
  2178. half d;
  2179. uint8_t qs[QK_K/4];
  2180. uint8_t qh[QK_K/32];
  2181. uint8_t signs[QK_K/8];
  2182. uint8_t scales[IQ3S_N_SCALE];
  2183. } block_iq3_s;
  2184. typedef struct {
  2185. half d;
  2186. uint8_t qs[QK_K/8];
  2187. uint8_t scales[QK_K/16];
  2188. } block_iq1_s;
  2189. // Non-linear quants
  2190. #define QK4_NL 32
  2191. typedef struct {
  2192. half d;
  2193. uint8_t qs[QK4_NL/2];
  2194. } block_iq4_nl;
  2195. #if QK_K == 64
  2196. #define block_iq4_xs block_iq4_nl
  2197. #else
  2198. typedef struct {
  2199. half d;
  2200. uint16_t scales_h;
  2201. uint8_t scales_l[QK_K/64];
  2202. uint8_t qs[QK_K/2];
  2203. } block_iq4_xs;
  2204. #endif
  2205. //====================================== dot products =========================
  2206. void kernel_mul_mv_q2_K_f32_impl(
  2207. device const void * src0,
  2208. device const float * src1,
  2209. device float * dst,
  2210. constant int64_t & ne00,
  2211. constant int64_t & ne01,
  2212. constant int64_t & ne02,
  2213. constant int64_t & ne10,
  2214. constant int64_t & ne12,
  2215. constant int64_t & ne0,
  2216. constant int64_t & ne1,
  2217. constant uint & r2,
  2218. constant uint & r3,
  2219. uint3 tgpig[[threadgroup_position_in_grid]],
  2220. uint tiisg[[thread_index_in_simdgroup]],
  2221. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2222. const int nb = ne00/QK_K;
  2223. const int r0 = tgpig.x;
  2224. const int r1 = tgpig.y;
  2225. const int im = tgpig.z;
  2226. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2227. const int ib_row = first_row * nb;
  2228. const uint i12 = im%ne12;
  2229. const uint i13 = im/ne12;
  2230. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2231. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  2232. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2233. float yl[32];
  2234. float sumf[N_DST]={0.f}, all_sum;
  2235. const int step = sizeof(block_q2_K) * nb;
  2236. #if QK_K == 256
  2237. const int ix = tiisg/8; // 0...3
  2238. const int it = tiisg%8; // 0...7
  2239. const int iq = it/4; // 0 or 1
  2240. const int ir = it%4; // 0...3
  2241. const int is = (8*ir)/16;// 0 or 1
  2242. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  2243. for (int ib = ix; ib < nb; ib += 4) {
  2244. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2245. for (int i = 0; i < 8; ++i) {
  2246. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2247. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  2248. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  2249. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  2250. }
  2251. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  2252. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2253. device const half * dh = &x[ib].d;
  2254. for (int row = 0; row < N_DST; row++) {
  2255. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2256. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2257. for (int i = 0; i < 8; i += 2) {
  2258. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2259. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2260. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2261. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2262. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2263. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2264. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2265. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2266. }
  2267. float dall = dh[0];
  2268. float dmin = dh[1] * 1.f/16.f;
  2269. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2270. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  2271. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  2272. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  2273. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  2274. qs += step/2;
  2275. sc += step;
  2276. dh += step/2;
  2277. }
  2278. y4 += 4 * QK_K;
  2279. }
  2280. #else
  2281. const int ix = tiisg/2; // 0...15
  2282. const int it = tiisg%2; // 0...1
  2283. device const float * y4 = y + ix * QK_K + 8 * it;
  2284. for (int ib = ix; ib < nb; ib += 16) {
  2285. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2286. for (int i = 0; i < 8; ++i) {
  2287. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2288. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  2289. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  2290. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  2291. }
  2292. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  2293. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2294. device const half * dh = &x[ib].d;
  2295. for (int row = 0; row < N_DST; row++) {
  2296. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2297. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2298. for (int i = 0; i < 8; i += 2) {
  2299. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2300. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2301. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2302. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2303. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2304. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2305. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2306. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2307. }
  2308. float dall = dh[0];
  2309. float dmin = dh[1];
  2310. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2311. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  2312. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  2313. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  2314. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  2315. qs += step/2;
  2316. sc += step;
  2317. dh += step/2;
  2318. }
  2319. y4 += 16 * QK_K;
  2320. }
  2321. #endif
  2322. for (int row = 0; row < N_DST; ++row) {
  2323. all_sum = simd_sum(sumf[row]);
  2324. if (tiisg == 0) {
  2325. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2326. }
  2327. }
  2328. }
  2329. [[host_name("kernel_mul_mv_q2_K_f32")]]
  2330. kernel void kernel_mul_mv_q2_K_f32(
  2331. device const void * src0,
  2332. device const float * src1,
  2333. device float * dst,
  2334. constant int64_t & ne00,
  2335. constant int64_t & ne01,
  2336. constant int64_t & ne02,
  2337. constant uint64_t & nb00,
  2338. constant uint64_t & nb01,
  2339. constant uint64_t & nb02,
  2340. constant int64_t & ne10,
  2341. constant int64_t & ne11,
  2342. constant int64_t & ne12,
  2343. constant uint64_t & nb10,
  2344. constant uint64_t & nb11,
  2345. constant uint64_t & nb12,
  2346. constant int64_t & ne0,
  2347. constant int64_t & ne1,
  2348. constant uint & r2,
  2349. constant uint & r3,
  2350. uint3 tgpig[[threadgroup_position_in_grid]],
  2351. uint tiisg[[thread_index_in_simdgroup]],
  2352. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2353. kernel_mul_mv_q2_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2354. }
  2355. #if QK_K == 256
  2356. void kernel_mul_mv_q3_K_f32_impl(
  2357. device const void * src0,
  2358. device const float * src1,
  2359. device float * dst,
  2360. constant int64_t & ne00,
  2361. constant int64_t & ne01,
  2362. constant int64_t & ne02,
  2363. constant int64_t & ne10,
  2364. constant int64_t & ne12,
  2365. constant int64_t & ne0,
  2366. constant int64_t & ne1,
  2367. constant uint & r2,
  2368. constant uint & r3,
  2369. uint3 tgpig[[threadgroup_position_in_grid]],
  2370. uint tiisg[[thread_index_in_simdgroup]],
  2371. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2372. const int nb = ne00/QK_K;
  2373. const int64_t r0 = tgpig.x;
  2374. const int64_t r1 = tgpig.y;
  2375. const int64_t im = tgpig.z;
  2376. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2377. const uint i12 = im%ne12;
  2378. const uint i13 = im/ne12;
  2379. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2380. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  2381. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2382. float yl[32];
  2383. //const uint16_t kmask1 = 0x3030;
  2384. //const uint16_t kmask2 = 0x0f0f;
  2385. const int tid = tiisg/4;
  2386. const int ix = tiisg%4;
  2387. const int ip = tid/4; // 0 or 1
  2388. const int il = 2*((tid%4)/2); // 0 or 2
  2389. const int ir = tid%2;
  2390. const int n = 8;
  2391. const int l0 = n*ir;
  2392. // One would think that the Metal compiler would figure out that ip and il can only have
  2393. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  2394. // with these two tales.
  2395. //
  2396. // Possible masks for the high bit
  2397. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  2398. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  2399. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  2400. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  2401. // Possible masks for the low 2 bits
  2402. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  2403. const ushort4 hm = mm[2*ip + il/2];
  2404. const int shift = 2*il;
  2405. const float v1 = il == 0 ? 4.f : 64.f;
  2406. const float v2 = 4.f * v1;
  2407. const uint16_t s_shift1 = 4*ip;
  2408. const uint16_t s_shift2 = s_shift1 + il;
  2409. const int q_offset = 32*ip + l0;
  2410. const int y_offset = 128*ip + 32*il + l0;
  2411. const int step = sizeof(block_q3_K) * nb / 2;
  2412. device const float * y1 = yy + ix*QK_K + y_offset;
  2413. uint32_t scales32, aux32;
  2414. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  2415. thread const int8_t * scales = (thread const int8_t *)&scales32;
  2416. float sumf1[2] = {0.f};
  2417. float sumf2[2] = {0.f};
  2418. for (int i = ix; i < nb; i += 4) {
  2419. for (int l = 0; l < 8; ++l) {
  2420. yl[l+ 0] = y1[l+ 0];
  2421. yl[l+ 8] = y1[l+16];
  2422. yl[l+16] = y1[l+32];
  2423. yl[l+24] = y1[l+48];
  2424. }
  2425. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  2426. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  2427. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  2428. device const half * dh = &x[i].d;
  2429. for (int row = 0; row < 2; ++row) {
  2430. const float d_all = (float)dh[0];
  2431. scales16[0] = a[4];
  2432. scales16[1] = a[5];
  2433. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  2434. scales16[0] = a[il+0];
  2435. scales16[1] = a[il+1];
  2436. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  2437. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  2438. for (int l = 0; l < n; l += 2) {
  2439. const int32_t qs = q[l/2];
  2440. s1 += yl[l+0] * (qs & qm[il/2][0]);
  2441. s2 += yl[l+1] * (qs & qm[il/2][1]);
  2442. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  2443. s4 += yl[l+16] * (qs & qm[il/2][2]);
  2444. s5 += yl[l+17] * (qs & qm[il/2][3]);
  2445. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  2446. }
  2447. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2448. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2449. sumf1[row] += d1 * (scales[0] - 32);
  2450. sumf2[row] += d2 * (scales[2] - 32);
  2451. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  2452. for (int l = 0; l < n; l += 2) {
  2453. const int32_t qs = q[l/2+8];
  2454. s1 += yl[l+8] * (qs & qm[il/2][0]);
  2455. s2 += yl[l+9] * (qs & qm[il/2][1]);
  2456. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  2457. s4 += yl[l+24] * (qs & qm[il/2][2]);
  2458. s5 += yl[l+25] * (qs & qm[il/2][3]);
  2459. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  2460. }
  2461. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2462. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2463. sumf1[row] += d1 * (scales[1] - 32);
  2464. sumf2[row] += d2 * (scales[3] - 32);
  2465. q += step;
  2466. h += step;
  2467. a += step;
  2468. dh += step;
  2469. }
  2470. y1 += 4 * QK_K;
  2471. }
  2472. for (int row = 0; row < 2; ++row) {
  2473. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  2474. sumf1[row] = simd_sum(sumf);
  2475. }
  2476. if (tiisg == 0) {
  2477. for (int row = 0; row < 2; ++row) {
  2478. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = sumf1[row];
  2479. }
  2480. }
  2481. }
  2482. #else
  2483. void kernel_mul_mv_q3_K_f32_impl(
  2484. device const void * src0,
  2485. device const float * src1,
  2486. device float * dst,
  2487. constant int64_t & ne00,
  2488. constant int64_t & ne01,
  2489. constant int64_t & ne02,
  2490. constant int64_t & ne10,
  2491. constant int64_t & ne12,
  2492. constant int64_t & ne0,
  2493. constant int64_t & ne1,
  2494. constant uint & r2,
  2495. constant uint & r3,
  2496. uint3 tgpig[[threadgroup_position_in_grid]],
  2497. uint tiisg[[thread_index_in_simdgroup]],
  2498. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2499. const int nb = ne00/QK_K;
  2500. const int64_t r0 = tgpig.x;
  2501. const int64_t r1 = tgpig.y;
  2502. const int64_t im = tgpig.z;
  2503. const int row = 2 * r0 + sgitg;
  2504. const uint i12 = im%ne12;
  2505. const uint i13 = im/ne12;
  2506. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2507. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  2508. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2509. const int ix = tiisg/4;
  2510. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  2511. const int iq = il/8; // 0, 0, 1, 1
  2512. const int in = il%8; // 0, 4, 0, 4
  2513. float2 sum = {0.f, 0.f};
  2514. for (int i = ix; i < nb; i += 8) {
  2515. const float d_all = (float)(x[i].d);
  2516. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  2517. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  2518. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  2519. device const float * y = yy + i * QK_K + il;
  2520. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  2521. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  2522. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  2523. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  2524. for (int l = 0; l < 4; l += 2) {
  2525. const uint16_t hm = h[l/2] >> iq;
  2526. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  2527. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  2528. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  2529. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  2530. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  2531. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  2532. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  2533. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  2534. }
  2535. }
  2536. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  2537. const float tot = simd_sum(sumf);
  2538. if (tiisg == 0) {
  2539. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  2540. }
  2541. }
  2542. #endif
  2543. [[host_name("kernel_mul_mv_q3_K_f32")]]
  2544. kernel void kernel_mul_mv_q3_K_f32(
  2545. device const void * src0,
  2546. device const float * src1,
  2547. device float * dst,
  2548. constant int64_t & ne00,
  2549. constant int64_t & ne01,
  2550. constant int64_t & ne02,
  2551. constant uint64_t & nb00,
  2552. constant uint64_t & nb01,
  2553. constant uint64_t & nb02,
  2554. constant int64_t & ne10,
  2555. constant int64_t & ne11,
  2556. constant int64_t & ne12,
  2557. constant uint64_t & nb10,
  2558. constant uint64_t & nb11,
  2559. constant uint64_t & nb12,
  2560. constant int64_t & ne0,
  2561. constant int64_t & ne1,
  2562. constant uint & r2,
  2563. constant uint & r3,
  2564. uint3 tgpig[[threadgroup_position_in_grid]],
  2565. uint tiisg[[thread_index_in_simdgroup]],
  2566. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2567. kernel_mul_mv_q3_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2568. }
  2569. #if QK_K == 256
  2570. void kernel_mul_mv_q4_K_f32_impl(
  2571. device const void * src0,
  2572. device const float * src1,
  2573. device float * dst,
  2574. constant int64_t & ne00,
  2575. constant int64_t & ne01,
  2576. constant int64_t & ne02,
  2577. constant int64_t & ne10,
  2578. constant int64_t & ne12,
  2579. constant int64_t & ne0,
  2580. constant int64_t & ne1,
  2581. constant uint & r2,
  2582. constant uint & r3,
  2583. uint3 tgpig[[threadgroup_position_in_grid]],
  2584. uint tiisg[[thread_index_in_simdgroup]],
  2585. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2586. const uint16_t kmask1 = 0x3f3f;
  2587. const uint16_t kmask2 = 0x0f0f;
  2588. const uint16_t kmask3 = 0xc0c0;
  2589. const int ix = tiisg/8; // 0...3
  2590. const int it = tiisg%8; // 0...7
  2591. const int iq = it/4; // 0 or 1
  2592. const int ir = it%4; // 0...3
  2593. const int nb = ne00/QK_K;
  2594. const int r0 = tgpig.x;
  2595. const int r1 = tgpig.y;
  2596. const int im = tgpig.z;
  2597. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2598. const int first_row = r0 * N_DST;
  2599. const int ib_row = first_row * nb;
  2600. const uint i12 = im%ne12;
  2601. const uint i13 = im/ne12;
  2602. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2603. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2604. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2605. float yl[16];
  2606. float yh[16];
  2607. float sumf[N_DST]={0.f}, all_sum;
  2608. const int step = sizeof(block_q4_K) * nb / 2;
  2609. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  2610. uint16_t sc16[4];
  2611. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2612. for (int ib = ix; ib < nb; ib += 4) {
  2613. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2614. for (int i = 0; i < 8; ++i) {
  2615. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  2616. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  2617. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  2618. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  2619. }
  2620. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  2621. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2622. device const half * dh = &x[ib].d;
  2623. for (int row = 0; row < N_DST; row++) {
  2624. sc16[0] = sc[0] & kmask1;
  2625. sc16[1] = sc[2] & kmask1;
  2626. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  2627. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  2628. device const uint16_t * q2 = q1 + 32;
  2629. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2630. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2631. for (int i = 0; i < 8; i += 2) {
  2632. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  2633. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  2634. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  2635. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  2636. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  2637. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  2638. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  2639. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  2640. }
  2641. float dall = dh[0];
  2642. float dmin = dh[1];
  2643. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  2644. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  2645. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  2646. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  2647. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2648. q1 += step;
  2649. sc += step;
  2650. dh += step;
  2651. }
  2652. y4 += 4 * QK_K;
  2653. }
  2654. for (int row = 0; row < N_DST; ++row) {
  2655. all_sum = simd_sum(sumf[row]);
  2656. if (tiisg == 0) {
  2657. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2658. }
  2659. }
  2660. }
  2661. #else
  2662. void kernel_mul_mv_q4_K_f32_impl(
  2663. device const void * src0,
  2664. device const float * src1,
  2665. device float * dst,
  2666. constant int64_t & ne00,
  2667. constant int64_t & ne01,
  2668. constant int64_t & ne02,
  2669. constant int64_t & ne10,
  2670. constant int64_t & ne12,
  2671. constant int64_t & ne0,
  2672. constant int64_t & ne1,
  2673. constant uint & r2,
  2674. constant uint & r3,
  2675. uint3 tgpig[[threadgroup_position_in_grid]],
  2676. uint tiisg[[thread_index_in_simdgroup]],
  2677. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2678. const int ix = tiisg/4; // 0...7
  2679. const int it = tiisg%4; // 0...3
  2680. const int nb = ne00/QK_K;
  2681. const int r0 = tgpig.x;
  2682. const int r1 = tgpig.y;
  2683. const int im = tgpig.z;
  2684. const int first_row = r0 * N_DST;
  2685. const int ib_row = first_row * nb;
  2686. const uint i12 = im%ne12;
  2687. const uint i13 = im/ne12;
  2688. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2689. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2690. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2691. float yl[8];
  2692. float yh[8];
  2693. float sumf[N_DST]={0.f}, all_sum;
  2694. const int step = sizeof(block_q4_K) * nb / 2;
  2695. device const float * y4 = y + ix * QK_K + 8 * it;
  2696. uint16_t sc16[4];
  2697. for (int ib = ix; ib < nb; ib += 8) {
  2698. float2 sumy = {0.f, 0.f};
  2699. for (int i = 0; i < 8; ++i) {
  2700. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  2701. yh[i] = y4[i+32]; sumy[1] += yh[i];
  2702. }
  2703. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  2704. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2705. device const half * dh = x[ib].d;
  2706. for (int row = 0; row < N_DST; row++) {
  2707. sc16[0] = sc[0] & 0x000f;
  2708. sc16[1] = sc[0] & 0x0f00;
  2709. sc16[2] = sc[0] & 0x00f0;
  2710. sc16[3] = sc[0] & 0xf000;
  2711. float2 acc1 = {0.f, 0.f};
  2712. float2 acc2 = {0.f, 0.f};
  2713. for (int i = 0; i < 8; i += 2) {
  2714. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  2715. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  2716. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  2717. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  2718. }
  2719. float dall = dh[0];
  2720. float dmin = dh[1];
  2721. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  2722. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  2723. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  2724. qs += step;
  2725. sc += step;
  2726. dh += step;
  2727. }
  2728. y4 += 8 * QK_K;
  2729. }
  2730. for (int row = 0; row < N_DST; ++row) {
  2731. all_sum = simd_sum(sumf[row]);
  2732. if (tiisg == 0) {
  2733. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2734. }
  2735. }
  2736. }
  2737. #endif
  2738. [[host_name("kernel_mul_mv_q4_K_f32")]]
  2739. kernel void kernel_mul_mv_q4_K_f32(
  2740. device const void * src0,
  2741. device const float * src1,
  2742. device float * dst,
  2743. constant int64_t & ne00,
  2744. constant int64_t & ne01,
  2745. constant int64_t & ne02,
  2746. constant uint64_t & nb00,
  2747. constant uint64_t & nb01,
  2748. constant uint64_t & nb02,
  2749. constant int64_t & ne10,
  2750. constant int64_t & ne11,
  2751. constant int64_t & ne12,
  2752. constant uint64_t & nb10,
  2753. constant uint64_t & nb11,
  2754. constant uint64_t & nb12,
  2755. constant int64_t & ne0,
  2756. constant int64_t & ne1,
  2757. constant uint & r2,
  2758. constant uint & r3,
  2759. uint3 tgpig[[threadgroup_position_in_grid]],
  2760. uint tiisg[[thread_index_in_simdgroup]],
  2761. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2762. kernel_mul_mv_q4_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2763. }
  2764. void kernel_mul_mv_q5_K_f32_impl(
  2765. device const void * src0,
  2766. device const float * src1,
  2767. device float * dst,
  2768. constant int64_t & ne00,
  2769. constant int64_t & ne01,
  2770. constant int64_t & ne02,
  2771. constant int64_t & ne10,
  2772. constant int64_t & ne12,
  2773. constant int64_t & ne0,
  2774. constant int64_t & ne1,
  2775. constant uint & r2,
  2776. constant uint & r3,
  2777. uint3 tgpig[[threadgroup_position_in_grid]],
  2778. uint tiisg[[thread_index_in_simdgroup]],
  2779. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2780. const int nb = ne00/QK_K;
  2781. const int64_t r0 = tgpig.x;
  2782. const int64_t r1 = tgpig.y;
  2783. const int im = tgpig.z;
  2784. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2785. const uint i12 = im%ne12;
  2786. const uint i13 = im/ne12;
  2787. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2788. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  2789. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2790. float sumf[2]={0.f};
  2791. const int step = sizeof(block_q5_K) * nb;
  2792. #if QK_K == 256
  2793. #
  2794. float yl[16], yh[16];
  2795. const uint16_t kmask1 = 0x3f3f;
  2796. const uint16_t kmask2 = 0x0f0f;
  2797. const uint16_t kmask3 = 0xc0c0;
  2798. const int tid = tiisg/4;
  2799. const int ix = tiisg%4;
  2800. const int iq = tid/4;
  2801. const int ir = tid%4;
  2802. const int n = 8;
  2803. const int l0 = n*ir;
  2804. const int q_offset = 32*iq + l0;
  2805. const int y_offset = 64*iq + l0;
  2806. const uint8_t hm1 = 1u << (2*iq);
  2807. const uint8_t hm2 = hm1 << 1;
  2808. const uint8_t hm3 = hm1 << 4;
  2809. const uint8_t hm4 = hm2 << 4;
  2810. uint16_t sc16[4];
  2811. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2812. device const float * y1 = yy + ix*QK_K + y_offset;
  2813. for (int i = ix; i < nb; i += 4) {
  2814. device const uint8_t * q1 = x[i].qs + q_offset;
  2815. device const uint8_t * qh = x[i].qh + l0;
  2816. device const half * dh = &x[i].d;
  2817. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  2818. device const float * y2 = y1 + 128;
  2819. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2820. for (int l = 0; l < 8; ++l) {
  2821. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  2822. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  2823. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  2824. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  2825. }
  2826. for (int row = 0; row < 2; ++row) {
  2827. device const uint8_t * q2 = q1 + 64;
  2828. sc16[0] = a[0] & kmask1;
  2829. sc16[1] = a[2] & kmask1;
  2830. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  2831. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  2832. float4 acc1 = {0.f};
  2833. float4 acc2 = {0.f};
  2834. for (int l = 0; l < n; ++l) {
  2835. uint8_t h = qh[l];
  2836. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  2837. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  2838. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  2839. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  2840. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  2841. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  2842. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  2843. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  2844. }
  2845. const float dall = dh[0];
  2846. const float dmin = dh[1];
  2847. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  2848. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  2849. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  2850. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  2851. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2852. q1 += step;
  2853. qh += step;
  2854. dh += step/2;
  2855. a += step/2;
  2856. }
  2857. y1 += 4 * QK_K;
  2858. }
  2859. #else
  2860. float yl[8], yh[8];
  2861. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  2862. const int ix = tiisg%8;
  2863. const int iq = il/8; // 0, 0, 1, 1
  2864. const int in = il%8; // 0, 4, 0, 4
  2865. device const float * y = yy + ix*QK_K + il;
  2866. for (int i = ix; i < nb; i += 8) {
  2867. for (int l = 0; l < 4; ++l) {
  2868. yl[l+0] = y[l+ 0];
  2869. yl[l+4] = y[l+16];
  2870. yh[l+0] = y[l+32];
  2871. yh[l+4] = y[l+48];
  2872. }
  2873. device const half * dh = &x[i].d;
  2874. device const uint8_t * q = x[i].qs + il;
  2875. device const uint8_t * h = x[i].qh + in;
  2876. device const int8_t * s = x[i].scales;
  2877. for (int row = 0; row < 2; ++row) {
  2878. const float d = dh[0];
  2879. float2 acc = {0.f, 0.f};
  2880. for (int l = 0; l < 4; ++l) {
  2881. const uint8_t hl = h[l] >> iq;
  2882. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  2883. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  2884. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  2885. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  2886. }
  2887. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  2888. q += step;
  2889. h += step;
  2890. s += step;
  2891. dh += step/2;
  2892. }
  2893. y += 8 * QK_K;
  2894. }
  2895. #endif
  2896. for (int row = 0; row < 2; ++row) {
  2897. const float tot = simd_sum(sumf[row]);
  2898. if (tiisg == 0) {
  2899. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  2900. }
  2901. }
  2902. }
  2903. [[host_name("kernel_mul_mv_q5_K_f32")]]
  2904. kernel void kernel_mul_mv_q5_K_f32(
  2905. device const void * src0,
  2906. device const float * src1,
  2907. device float * dst,
  2908. constant int64_t & ne00,
  2909. constant int64_t & ne01,
  2910. constant int64_t & ne02,
  2911. constant uint64_t & nb00,
  2912. constant uint64_t & nb01,
  2913. constant uint64_t & nb02,
  2914. constant int64_t & ne10,
  2915. constant int64_t & ne11,
  2916. constant int64_t & ne12,
  2917. constant uint64_t & nb10,
  2918. constant uint64_t & nb11,
  2919. constant uint64_t & nb12,
  2920. constant int64_t & ne0,
  2921. constant int64_t & ne1,
  2922. constant uint & r2,
  2923. constant uint & r3,
  2924. uint3 tgpig[[threadgroup_position_in_grid]],
  2925. uint tiisg[[thread_index_in_simdgroup]],
  2926. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2927. kernel_mul_mv_q5_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2928. }
  2929. void kernel_mul_mv_q6_K_f32_impl(
  2930. device const void * src0,
  2931. device const float * src1,
  2932. device float * dst,
  2933. constant int64_t & ne00,
  2934. constant int64_t & ne01,
  2935. constant int64_t & ne02,
  2936. constant int64_t & ne10,
  2937. constant int64_t & ne12,
  2938. constant int64_t & ne0,
  2939. constant int64_t & ne1,
  2940. constant uint & r2,
  2941. constant uint & r3,
  2942. uint3 tgpig[[threadgroup_position_in_grid]],
  2943. uint tiisg[[thread_index_in_simdgroup]],
  2944. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2945. const uint8_t kmask1 = 0x03;
  2946. const uint8_t kmask2 = 0x0C;
  2947. const uint8_t kmask3 = 0x30;
  2948. const uint8_t kmask4 = 0xC0;
  2949. const int nb = ne00/QK_K;
  2950. const int64_t r0 = tgpig.x;
  2951. const int64_t r1 = tgpig.y;
  2952. const int im = tgpig.z;
  2953. const int row = 2 * r0 + sgitg;
  2954. const uint i12 = im%ne12;
  2955. const uint i13 = im/ne12;
  2956. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2957. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  2958. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2959. float sumf = 0;
  2960. #if QK_K == 256
  2961. const int tid = tiisg/2;
  2962. const int ix = tiisg%2;
  2963. const int ip = tid/8; // 0 or 1
  2964. const int il = tid%8;
  2965. const int n = 4;
  2966. const int l0 = n*il;
  2967. const int is = 8*ip + l0/16;
  2968. const int y_offset = 128*ip + l0;
  2969. const int q_offset_l = 64*ip + l0;
  2970. const int q_offset_h = 32*ip + l0;
  2971. for (int i = ix; i < nb; i += 2) {
  2972. device const uint8_t * q1 = x[i].ql + q_offset_l;
  2973. device const uint8_t * q2 = q1 + 32;
  2974. device const uint8_t * qh = x[i].qh + q_offset_h;
  2975. device const int8_t * sc = x[i].scales + is;
  2976. device const float * y = yy + i * QK_K + y_offset;
  2977. const float dall = x[i].d;
  2978. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2979. for (int l = 0; l < n; ++l) {
  2980. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2981. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  2982. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  2983. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  2984. }
  2985. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  2986. }
  2987. #else
  2988. const int ix = tiisg/4;
  2989. const int il = 4*(tiisg%4);
  2990. for (int i = ix; i < nb; i += 8) {
  2991. device const float * y = yy + i * QK_K + il;
  2992. device const uint8_t * ql = x[i].ql + il;
  2993. device const uint8_t * qh = x[i].qh + il;
  2994. device const int8_t * s = x[i].scales;
  2995. const float d = x[i].d;
  2996. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2997. for (int l = 0; l < 4; ++l) {
  2998. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2999. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  3000. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  3001. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  3002. }
  3003. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  3004. }
  3005. #endif
  3006. const float tot = simd_sum(sumf);
  3007. if (tiisg == 0) {
  3008. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  3009. }
  3010. }
  3011. [[host_name("kernel_mul_mv_q6_K_f32")]]
  3012. kernel void kernel_mul_mv_q6_K_f32(
  3013. device const void * src0,
  3014. device const float * src1,
  3015. device float * dst,
  3016. constant int64_t & ne00,
  3017. constant int64_t & ne01,
  3018. constant int64_t & ne02,
  3019. constant uint64_t & nb00,
  3020. constant uint64_t & nb01,
  3021. constant uint64_t & nb02,
  3022. constant int64_t & ne10,
  3023. constant int64_t & ne11,
  3024. constant int64_t & ne12,
  3025. constant uint64_t & nb10,
  3026. constant uint64_t & nb11,
  3027. constant uint64_t & nb12,
  3028. constant int64_t & ne0,
  3029. constant int64_t & ne1,
  3030. constant uint & r2,
  3031. constant uint & r3,
  3032. uint3 tgpig[[threadgroup_position_in_grid]],
  3033. uint tiisg[[thread_index_in_simdgroup]],
  3034. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3035. kernel_mul_mv_q6_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  3036. }
  3037. // ======================= "True" 2-bit
  3038. constexpr constant static uint64_t iq2xxs_grid[256] = {
  3039. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3040. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  3041. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  3042. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  3043. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  3044. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  3045. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  3046. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  3047. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  3048. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  3049. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  3050. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  3051. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  3052. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  3053. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  3054. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  3055. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  3056. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  3057. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  3058. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  3059. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  3060. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  3061. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  3062. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  3063. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  3064. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  3065. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  3066. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  3067. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  3068. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  3069. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  3070. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  3071. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  3072. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  3073. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  3074. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  3075. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  3076. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  3077. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  3078. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  3079. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  3080. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  3081. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  3082. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  3083. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  3084. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  3085. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  3086. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  3087. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  3088. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  3089. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  3090. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  3091. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  3092. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  3093. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  3094. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  3095. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  3096. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  3097. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  3098. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  3099. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  3100. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  3101. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  3102. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  3103. };
  3104. constexpr constant static uint64_t iq2xs_grid[512] = {
  3105. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3106. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3107. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3108. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3109. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3110. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  3111. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  3112. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  3113. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  3114. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  3115. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  3116. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  3117. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  3118. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  3119. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  3120. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  3121. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  3122. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  3123. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  3124. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  3125. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  3126. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  3127. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  3128. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  3129. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  3130. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  3131. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  3132. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  3133. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  3134. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  3135. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  3136. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  3137. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  3138. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  3139. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  3140. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  3141. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  3142. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  3143. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  3144. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  3145. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  3146. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  3147. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  3148. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  3149. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  3150. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  3151. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  3152. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  3153. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  3154. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  3155. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  3156. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  3157. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  3158. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  3159. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  3160. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  3161. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  3162. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  3163. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  3164. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  3165. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  3166. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  3167. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  3168. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  3169. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  3170. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  3171. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  3172. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  3173. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  3174. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  3175. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  3176. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  3177. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  3178. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  3179. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  3180. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  3181. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  3182. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  3183. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  3184. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  3185. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  3186. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  3187. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  3188. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  3189. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  3190. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  3191. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  3192. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  3193. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  3194. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  3195. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  3196. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  3197. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  3198. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  3199. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  3200. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  3201. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  3202. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  3203. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  3204. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  3205. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  3206. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  3207. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  3208. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  3209. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  3210. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  3211. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  3212. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  3213. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  3214. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  3215. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  3216. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  3217. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  3218. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  3219. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  3220. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  3221. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  3222. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  3223. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  3224. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  3225. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  3226. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  3227. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  3228. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  3229. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  3230. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  3231. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  3232. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  3233. };
  3234. constexpr constant static uint64_t iq2s_grid[1024] = {
  3235. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3236. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3237. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3238. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3239. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3240. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  3241. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  3242. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  3243. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  3244. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  3245. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  3246. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  3247. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  3248. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  3249. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  3250. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  3251. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  3252. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  3253. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  3254. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  3255. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  3256. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  3257. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  3258. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  3259. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  3260. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  3261. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  3262. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  3263. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  3264. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  3265. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  3266. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  3267. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  3268. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  3269. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  3270. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  3271. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  3272. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  3273. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  3274. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  3275. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  3276. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  3277. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  3278. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  3279. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  3280. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  3281. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  3282. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  3283. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  3284. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  3285. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  3286. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  3287. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  3288. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  3289. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  3290. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  3291. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  3292. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  3293. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  3294. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  3295. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  3296. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  3297. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  3298. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  3299. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  3300. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  3301. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  3302. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  3303. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  3304. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  3305. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  3306. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  3307. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  3308. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  3309. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  3310. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  3311. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  3312. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  3313. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  3314. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  3315. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  3316. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  3317. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  3318. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  3319. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  3320. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  3321. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  3322. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  3323. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  3324. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  3325. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  3326. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  3327. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  3328. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  3329. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  3330. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  3331. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  3332. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  3333. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  3334. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  3335. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  3336. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  3337. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  3338. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  3339. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  3340. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  3341. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  3342. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  3343. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  3344. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  3345. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  3346. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  3347. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  3348. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  3349. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  3350. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  3351. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  3352. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  3353. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  3354. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  3355. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  3356. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  3357. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  3358. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  3359. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  3360. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  3361. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  3362. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  3363. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  3364. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  3365. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  3366. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  3367. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  3368. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  3369. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  3370. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  3371. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  3372. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  3373. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  3374. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  3375. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  3376. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  3377. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  3378. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  3379. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  3380. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  3381. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  3382. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  3383. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  3384. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  3385. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  3386. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  3387. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  3388. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  3389. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  3390. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  3391. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  3392. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  3393. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  3394. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  3395. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  3396. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  3397. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  3398. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  3399. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  3400. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  3401. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  3402. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  3403. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  3404. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  3405. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  3406. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  3407. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  3408. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  3409. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  3410. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  3411. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  3412. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  3413. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  3414. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  3415. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  3416. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  3417. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  3418. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  3419. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  3420. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  3421. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  3422. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  3423. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  3424. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  3425. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  3426. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  3427. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  3428. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  3429. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  3430. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  3431. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  3432. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  3433. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  3434. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  3435. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  3436. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  3437. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  3438. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  3439. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  3440. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  3441. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  3442. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  3443. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  3444. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  3445. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  3446. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  3447. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  3448. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  3449. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  3450. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  3451. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  3452. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  3453. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  3454. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  3455. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  3456. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  3457. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  3458. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  3459. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  3460. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  3461. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  3462. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  3463. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  3464. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  3465. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  3466. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  3467. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  3468. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  3469. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  3470. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  3471. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  3472. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  3473. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  3474. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  3475. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  3476. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  3477. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  3478. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  3479. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  3480. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  3481. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  3482. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  3483. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  3484. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  3485. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  3486. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  3487. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  3488. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  3489. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  3490. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  3491. };
  3492. constexpr constant static uint32_t iq3xxs_grid[256] = {
  3493. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  3494. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  3495. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  3496. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  3497. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  3498. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  3499. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  3500. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  3501. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  3502. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  3503. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  3504. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  3505. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  3506. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  3507. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  3508. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  3509. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  3510. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  3511. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  3512. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  3513. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  3514. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  3515. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  3516. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  3517. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  3518. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  3519. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  3520. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  3521. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  3522. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  3523. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  3524. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  3525. };
  3526. constexpr constant static uint32_t iq3s_grid[512] = {
  3527. 0x01010101, 0x01010103, 0x01010105, 0x0101010b, 0x0101010f, 0x01010301, 0x01010303, 0x01010305,
  3528. 0x01010309, 0x0101030d, 0x01010501, 0x01010503, 0x0101050b, 0x01010707, 0x01010901, 0x01010905,
  3529. 0x0101090b, 0x0101090f, 0x01010b03, 0x01010b07, 0x01010d01, 0x01010d05, 0x01010f03, 0x01010f09,
  3530. 0x01010f0f, 0x01030101, 0x01030103, 0x01030105, 0x01030109, 0x01030301, 0x01030303, 0x0103030b,
  3531. 0x01030501, 0x01030507, 0x0103050f, 0x01030703, 0x0103070b, 0x01030909, 0x01030d03, 0x01030d0b,
  3532. 0x01030f05, 0x01050101, 0x01050103, 0x0105010b, 0x0105010f, 0x01050301, 0x01050307, 0x0105030d,
  3533. 0x01050503, 0x0105050b, 0x01050701, 0x01050709, 0x01050905, 0x0105090b, 0x0105090f, 0x01050b03,
  3534. 0x01050b07, 0x01050f01, 0x01050f07, 0x01070107, 0x01070303, 0x0107030b, 0x01070501, 0x01070505,
  3535. 0x01070703, 0x01070707, 0x0107070d, 0x01070909, 0x01070b01, 0x01070b05, 0x01070d0f, 0x01070f03,
  3536. 0x01070f0b, 0x01090101, 0x01090307, 0x0109030f, 0x01090503, 0x01090509, 0x01090705, 0x01090901,
  3537. 0x01090907, 0x01090b03, 0x01090f01, 0x010b0105, 0x010b0109, 0x010b0501, 0x010b0505, 0x010b050d,
  3538. 0x010b0707, 0x010b0903, 0x010b090b, 0x010b090f, 0x010b0d0d, 0x010b0f07, 0x010d010d, 0x010d0303,
  3539. 0x010d0307, 0x010d0703, 0x010d0b05, 0x010d0f03, 0x010f0101, 0x010f0105, 0x010f0109, 0x010f0501,
  3540. 0x010f0505, 0x010f050d, 0x010f0707, 0x010f0b01, 0x010f0b09, 0x03010101, 0x03010103, 0x03010105,
  3541. 0x03010109, 0x03010301, 0x03010303, 0x03010307, 0x0301030b, 0x0301030f, 0x03010501, 0x03010505,
  3542. 0x03010703, 0x03010709, 0x0301070d, 0x03010b09, 0x03010b0d, 0x03010d03, 0x03010f05, 0x03030101,
  3543. 0x03030103, 0x03030107, 0x0303010d, 0x03030301, 0x03030309, 0x03030503, 0x03030701, 0x03030707,
  3544. 0x03030903, 0x03030b01, 0x03030b05, 0x03030f01, 0x03030f0d, 0x03050101, 0x03050305, 0x0305030b,
  3545. 0x0305030f, 0x03050501, 0x03050509, 0x03050705, 0x03050901, 0x03050907, 0x03050b0b, 0x03050d01,
  3546. 0x03050f05, 0x03070103, 0x03070109, 0x0307010f, 0x03070301, 0x03070307, 0x03070503, 0x0307050f,
  3547. 0x03070701, 0x03070709, 0x03070903, 0x03070d05, 0x03070f01, 0x03090107, 0x0309010b, 0x03090305,
  3548. 0x03090309, 0x03090703, 0x03090707, 0x03090905, 0x0309090d, 0x03090b01, 0x03090b09, 0x030b0103,
  3549. 0x030b0301, 0x030b0307, 0x030b0503, 0x030b0701, 0x030b0705, 0x030b0b03, 0x030d0501, 0x030d0509,
  3550. 0x030d050f, 0x030d0909, 0x030d090d, 0x030f0103, 0x030f0107, 0x030f0301, 0x030f0305, 0x030f0503,
  3551. 0x030f070b, 0x030f0903, 0x030f0d05, 0x030f0f01, 0x05010101, 0x05010103, 0x05010107, 0x0501010b,
  3552. 0x0501010f, 0x05010301, 0x05010305, 0x05010309, 0x0501030d, 0x05010503, 0x05010507, 0x0501050f,
  3553. 0x05010701, 0x05010705, 0x05010903, 0x05010907, 0x0501090b, 0x05010b01, 0x05010b05, 0x05010d0f,
  3554. 0x05010f01, 0x05010f07, 0x05010f0b, 0x05030101, 0x05030105, 0x05030301, 0x05030307, 0x0503030f,
  3555. 0x05030505, 0x0503050b, 0x05030703, 0x05030709, 0x05030905, 0x05030b03, 0x05050103, 0x05050109,
  3556. 0x0505010f, 0x05050503, 0x05050507, 0x05050701, 0x0505070f, 0x05050903, 0x05050b07, 0x05050b0f,
  3557. 0x05050f03, 0x05050f09, 0x05070101, 0x05070105, 0x0507010b, 0x05070303, 0x05070505, 0x05070509,
  3558. 0x05070703, 0x05070707, 0x05070905, 0x05070b01, 0x05070d0d, 0x05090103, 0x0509010f, 0x05090501,
  3559. 0x05090507, 0x05090705, 0x0509070b, 0x05090903, 0x05090f05, 0x05090f0b, 0x050b0109, 0x050b0303,
  3560. 0x050b0505, 0x050b070f, 0x050b0901, 0x050b0b07, 0x050b0f01, 0x050d0101, 0x050d0105, 0x050d010f,
  3561. 0x050d0503, 0x050d0b0b, 0x050d0d03, 0x050f010b, 0x050f0303, 0x050f050d, 0x050f0701, 0x050f0907,
  3562. 0x050f0b01, 0x07010105, 0x07010303, 0x07010307, 0x0701030b, 0x0701030f, 0x07010505, 0x07010703,
  3563. 0x07010707, 0x0701070b, 0x07010905, 0x07010909, 0x0701090f, 0x07010b03, 0x07010d07, 0x07010f03,
  3564. 0x07030103, 0x07030107, 0x0703010b, 0x07030309, 0x07030503, 0x07030507, 0x07030901, 0x07030d01,
  3565. 0x07030f05, 0x07030f0d, 0x07050101, 0x07050305, 0x07050501, 0x07050705, 0x07050709, 0x07050b01,
  3566. 0x07070103, 0x07070301, 0x07070309, 0x07070503, 0x07070507, 0x0707050f, 0x07070701, 0x07070903,
  3567. 0x07070907, 0x0707090f, 0x07070b0b, 0x07070f07, 0x07090107, 0x07090303, 0x0709030d, 0x07090505,
  3568. 0x07090703, 0x07090b05, 0x07090d01, 0x07090d09, 0x070b0103, 0x070b0301, 0x070b0305, 0x070b050b,
  3569. 0x070b0705, 0x070b0909, 0x070b0b0d, 0x070b0f07, 0x070d030d, 0x070d0903, 0x070f0103, 0x070f0107,
  3570. 0x070f0501, 0x070f0505, 0x070f070b, 0x09010101, 0x09010109, 0x09010305, 0x09010501, 0x09010509,
  3571. 0x0901050f, 0x09010705, 0x09010903, 0x09010b01, 0x09010f01, 0x09030105, 0x0903010f, 0x09030303,
  3572. 0x09030307, 0x09030505, 0x09030701, 0x0903070b, 0x09030907, 0x09030b03, 0x09030b0b, 0x09050103,
  3573. 0x09050107, 0x09050301, 0x0905030b, 0x09050503, 0x09050707, 0x09050901, 0x09050b0f, 0x09050d05,
  3574. 0x09050f01, 0x09070109, 0x09070303, 0x09070307, 0x09070501, 0x09070505, 0x09070703, 0x0907070b,
  3575. 0x09090101, 0x09090105, 0x09090509, 0x0909070f, 0x09090901, 0x09090f03, 0x090b010b, 0x090b010f,
  3576. 0x090b0503, 0x090b0d05, 0x090d0307, 0x090d0709, 0x090d0d01, 0x090f0301, 0x090f030b, 0x090f0701,
  3577. 0x090f0907, 0x090f0b03, 0x0b010105, 0x0b010301, 0x0b010309, 0x0b010505, 0x0b010901, 0x0b010909,
  3578. 0x0b01090f, 0x0b010b05, 0x0b010d0d, 0x0b010f09, 0x0b030103, 0x0b030107, 0x0b03010b, 0x0b030305,
  3579. 0x0b030503, 0x0b030705, 0x0b030f05, 0x0b050101, 0x0b050303, 0x0b050507, 0x0b050701, 0x0b05070d,
  3580. 0x0b050b07, 0x0b070105, 0x0b07010f, 0x0b070301, 0x0b07050f, 0x0b070909, 0x0b070b03, 0x0b070d0b,
  3581. 0x0b070f07, 0x0b090103, 0x0b090109, 0x0b090501, 0x0b090705, 0x0b09090d, 0x0b0b0305, 0x0b0b050d,
  3582. 0x0b0b0b03, 0x0b0b0b07, 0x0b0d0905, 0x0b0f0105, 0x0b0f0109, 0x0b0f0505, 0x0d010303, 0x0d010307,
  3583. 0x0d01030b, 0x0d010703, 0x0d010707, 0x0d010d01, 0x0d030101, 0x0d030501, 0x0d03050f, 0x0d030d09,
  3584. 0x0d050305, 0x0d050709, 0x0d050905, 0x0d050b0b, 0x0d050d05, 0x0d050f01, 0x0d070101, 0x0d070309,
  3585. 0x0d070503, 0x0d070901, 0x0d09050b, 0x0d090907, 0x0d090d05, 0x0d0b0101, 0x0d0b0107, 0x0d0b0709,
  3586. 0x0d0b0d01, 0x0d0d010b, 0x0d0d0901, 0x0d0f0303, 0x0d0f0307, 0x0f010101, 0x0f010109, 0x0f01010f,
  3587. 0x0f010501, 0x0f010505, 0x0f01070d, 0x0f010901, 0x0f010b09, 0x0f010d05, 0x0f030105, 0x0f030303,
  3588. 0x0f030509, 0x0f030907, 0x0f03090b, 0x0f050103, 0x0f050109, 0x0f050301, 0x0f05030d, 0x0f050503,
  3589. 0x0f050701, 0x0f050b03, 0x0f070105, 0x0f070705, 0x0f07070b, 0x0f070b07, 0x0f090103, 0x0f09010b,
  3590. 0x0f090307, 0x0f090501, 0x0f090b01, 0x0f0b0505, 0x0f0b0905, 0x0f0d0105, 0x0f0d0703, 0x0f0f0101,
  3591. };
  3592. #define NGRID_IQ1S 512
  3593. constexpr constant static uint64_t iq1s_grid[NGRID_IQ1S] = {
  3594. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  3595. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  3596. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  3597. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  3598. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  3599. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  3600. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  3601. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  3602. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  3603. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  3604. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  3605. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  3606. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  3607. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  3608. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  3609. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  3610. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  3611. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  3612. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  3613. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  3614. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  3615. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  3616. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  3617. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  3618. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  3619. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  3620. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  3621. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  3622. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  3623. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  3624. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  3625. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  3626. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  3627. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  3628. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  3629. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  3630. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  3631. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  3632. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  3633. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  3634. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  3635. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  3636. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  3637. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  3638. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  3639. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  3640. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  3641. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  3642. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  3643. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  3644. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  3645. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  3646. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  3647. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  3648. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  3649. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  3650. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  3651. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  3652. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  3653. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  3654. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  3655. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  3656. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  3657. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  3658. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  3659. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  3660. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  3661. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  3662. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  3663. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  3664. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  3665. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  3666. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  3667. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  3668. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  3669. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  3670. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  3671. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  3672. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  3673. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  3674. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  3675. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  3676. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  3677. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  3678. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  3679. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  3680. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  3681. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  3682. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  3683. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  3684. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  3685. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  3686. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  3687. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  3688. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  3689. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  3690. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  3691. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  3692. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  3693. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  3694. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  3695. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  3696. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  3697. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  3698. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  3699. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  3700. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  3701. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  3702. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  3703. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  3704. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  3705. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  3706. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  3707. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  3708. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  3709. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  3710. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  3711. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  3712. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  3713. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  3714. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  3715. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  3716. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  3717. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  3718. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  3719. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  3720. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  3721. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  3722. };
  3723. constexpr constant static uint8_t ksigns_iq2xs[128] = {
  3724. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  3725. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  3726. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  3727. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  3728. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  3729. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  3730. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  3731. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  3732. };
  3733. constexpr constant static uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  3734. void kernel_mul_mv_iq2_xxs_f32_impl(
  3735. device const void * src0,
  3736. device const float * src1,
  3737. device float * dst,
  3738. constant int64_t & ne00,
  3739. constant int64_t & ne01,
  3740. constant int64_t & ne02,
  3741. constant int64_t & ne10,
  3742. constant int64_t & ne12,
  3743. constant int64_t & ne0,
  3744. constant int64_t & ne1,
  3745. constant uint & r2,
  3746. constant uint & r3,
  3747. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3748. uint3 tgpig[[threadgroup_position_in_grid]],
  3749. uint tiisg[[thread_index_in_simdgroup]],
  3750. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3751. const int nb = ne00/QK_K;
  3752. const int r0 = tgpig.x;
  3753. const int r1 = tgpig.y;
  3754. const int im = tgpig.z;
  3755. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3756. const int ib_row = first_row * nb;
  3757. const uint i12 = im%ne12;
  3758. const uint i13 = im/ne12;
  3759. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3760. device const block_iq2_xxs * x = (device const block_iq2_xxs *) src0 + ib_row + offset0;
  3761. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3762. float yl[32];
  3763. float sumf[N_DST]={0.f}, all_sum;
  3764. const int nb32 = nb * (QK_K / 32);
  3765. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3766. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3767. {
  3768. int nval = 4;
  3769. int pos = (32*sgitg + tiisg)*nval;
  3770. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xxs_grid[pos + i];
  3771. nval = 2;
  3772. pos = (32*sgitg + tiisg)*nval;
  3773. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3774. threadgroup_barrier(mem_flags::mem_threadgroup);
  3775. }
  3776. const int ix = tiisg;
  3777. device const float * y4 = y + 32 * ix;
  3778. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3779. for (int i = 0; i < 32; ++i) {
  3780. yl[i] = y4[i];
  3781. }
  3782. const int ibl = ib32 / (QK_K / 32);
  3783. const int ib = ib32 % (QK_K / 32);
  3784. device const block_iq2_xxs * xr = x + ibl;
  3785. device const uint16_t * q2 = xr->qs + 4 * ib;
  3786. device const half * dh = &xr->d;
  3787. for (int row = 0; row < N_DST; row++) {
  3788. const float db = dh[0];
  3789. device const uint8_t * aux8 = (device const uint8_t *)q2;
  3790. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3791. const float d = db * (0.5f + (aux32 >> 28));
  3792. float sum = 0;
  3793. for (int l = 0; l < 4; ++l) {
  3794. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + aux8[l]);
  3795. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  3796. for (int j = 0; j < 8; ++j) {
  3797. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3798. }
  3799. }
  3800. sumf[row] += d * sum;
  3801. dh += nb*sizeof(block_iq2_xxs)/2;
  3802. q2 += nb*sizeof(block_iq2_xxs)/2;
  3803. }
  3804. y4 += 32 * 32;
  3805. }
  3806. for (int row = 0; row < N_DST; ++row) {
  3807. all_sum = simd_sum(sumf[row]);
  3808. if (tiisg == 0) {
  3809. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3810. }
  3811. }
  3812. }
  3813. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  3814. kernel void kernel_mul_mv_iq2_xxs_f32(
  3815. device const void * src0,
  3816. device const float * src1,
  3817. device float * dst,
  3818. constant int64_t & ne00,
  3819. constant int64_t & ne01,
  3820. constant int64_t & ne02,
  3821. constant uint64_t & nb00,
  3822. constant uint64_t & nb01,
  3823. constant uint64_t & nb02,
  3824. constant int64_t & ne10,
  3825. constant int64_t & ne11,
  3826. constant int64_t & ne12,
  3827. constant uint64_t & nb10,
  3828. constant uint64_t & nb11,
  3829. constant uint64_t & nb12,
  3830. constant int64_t & ne0,
  3831. constant int64_t & ne1,
  3832. constant uint & r2,
  3833. constant uint & r3,
  3834. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3835. uint3 tgpig[[threadgroup_position_in_grid]],
  3836. uint tiisg[[thread_index_in_simdgroup]],
  3837. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3838. kernel_mul_mv_iq2_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3839. }
  3840. void kernel_mul_mv_iq2_xs_f32_impl(
  3841. device const void * src0,
  3842. device const float * src1,
  3843. device float * dst,
  3844. constant int64_t & ne00,
  3845. constant int64_t & ne01,
  3846. constant int64_t & ne02,
  3847. constant int64_t & ne10,
  3848. constant int64_t & ne12,
  3849. constant int64_t & ne0,
  3850. constant int64_t & ne1,
  3851. constant uint & r2,
  3852. constant uint & r3,
  3853. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3854. uint3 tgpig[[threadgroup_position_in_grid]],
  3855. uint tiisg[[thread_index_in_simdgroup]],
  3856. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3857. const int nb = ne00/QK_K;
  3858. const int r0 = tgpig.x;
  3859. const int r1 = tgpig.y;
  3860. const int im = tgpig.z;
  3861. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3862. const int ib_row = first_row * nb;
  3863. const uint i12 = im%ne12;
  3864. const uint i13 = im/ne12;
  3865. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3866. device const block_iq2_xs * x = (device const block_iq2_xs *) src0 + ib_row + offset0;
  3867. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3868. float yl[32];
  3869. float sumf[N_DST]={0.f}, all_sum;
  3870. const int nb32 = nb * (QK_K / 32);
  3871. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3872. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 512);
  3873. {
  3874. int nval = 8;
  3875. int pos = (32*sgitg + tiisg)*nval;
  3876. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xs_grid[pos + i];
  3877. nval = 2;
  3878. pos = (32*sgitg + tiisg)*nval;
  3879. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3880. threadgroup_barrier(mem_flags::mem_threadgroup);
  3881. }
  3882. const int ix = tiisg;
  3883. device const float * y4 = y + 32 * ix;
  3884. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3885. for (int i = 0; i < 32; ++i) {
  3886. yl[i] = y4[i];
  3887. }
  3888. const int ibl = ib32 / (QK_K / 32);
  3889. const int ib = ib32 % (QK_K / 32);
  3890. device const block_iq2_xs * xr = x + ibl;
  3891. device const uint16_t * q2 = xr->qs + 4 * ib;
  3892. device const uint8_t * sc = xr->scales + ib;
  3893. device const half * dh = &xr->d;
  3894. for (int row = 0; row < N_DST; row++) {
  3895. const float db = dh[0];
  3896. const uint8_t ls1 = sc[0] & 0xf;
  3897. const uint8_t ls2 = sc[0] >> 4;
  3898. const float d1 = db * (0.5f + ls1);
  3899. const float d2 = db * (0.5f + ls2);
  3900. float sum1 = 0, sum2 = 0;
  3901. for (int l = 0; l < 2; ++l) {
  3902. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3903. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3904. for (int j = 0; j < 8; ++j) {
  3905. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3906. }
  3907. }
  3908. for (int l = 2; l < 4; ++l) {
  3909. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3910. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3911. for (int j = 0; j < 8; ++j) {
  3912. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3913. }
  3914. }
  3915. sumf[row] += d1 * sum1 + d2 * sum2;
  3916. dh += nb*sizeof(block_iq2_xs)/2;
  3917. q2 += nb*sizeof(block_iq2_xs)/2;
  3918. sc += nb*sizeof(block_iq2_xs);
  3919. }
  3920. y4 += 32 * 32;
  3921. }
  3922. for (int row = 0; row < N_DST; ++row) {
  3923. all_sum = simd_sum(sumf[row]);
  3924. if (tiisg == 0) {
  3925. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3926. }
  3927. }
  3928. }
  3929. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  3930. kernel void kernel_mul_mv_iq2_xs_f32(
  3931. device const void * src0,
  3932. device const float * src1,
  3933. device float * dst,
  3934. constant int64_t & ne00,
  3935. constant int64_t & ne01,
  3936. constant int64_t & ne02,
  3937. constant uint64_t & nb00,
  3938. constant uint64_t & nb01,
  3939. constant uint64_t & nb02,
  3940. constant int64_t & ne10,
  3941. constant int64_t & ne11,
  3942. constant int64_t & ne12,
  3943. constant uint64_t & nb10,
  3944. constant uint64_t & nb11,
  3945. constant uint64_t & nb12,
  3946. constant int64_t & ne0,
  3947. constant int64_t & ne1,
  3948. constant uint & r2,
  3949. constant uint & r3,
  3950. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3951. uint3 tgpig[[threadgroup_position_in_grid]],
  3952. uint tiisg[[thread_index_in_simdgroup]],
  3953. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3954. kernel_mul_mv_iq2_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3955. }
  3956. void kernel_mul_mv_iq3_xxs_f32_impl(
  3957. device const void * src0,
  3958. device const float * src1,
  3959. device float * dst,
  3960. constant int64_t & ne00,
  3961. constant int64_t & ne01,
  3962. constant int64_t & ne02,
  3963. constant int64_t & ne10,
  3964. constant int64_t & ne12,
  3965. constant int64_t & ne0,
  3966. constant int64_t & ne1,
  3967. constant uint & r2,
  3968. constant uint & r3,
  3969. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3970. uint3 tgpig[[threadgroup_position_in_grid]],
  3971. uint tiisg[[thread_index_in_simdgroup]],
  3972. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3973. const int nb = ne00/QK_K;
  3974. const int r0 = tgpig.x;
  3975. const int r1 = tgpig.y;
  3976. const int im = tgpig.z;
  3977. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3978. const int ib_row = first_row * nb;
  3979. const uint i12 = im%ne12;
  3980. const uint i13 = im/ne12;
  3981. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3982. device const block_iq3_xxs * x = (device const block_iq3_xxs *) src0 + ib_row + offset0;
  3983. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3984. float yl[32];
  3985. float sumf[N_DST]={0.f}, all_sum;
  3986. const int nb32 = nb * (QK_K / 32);
  3987. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  3988. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3989. {
  3990. int nval = 4;
  3991. int pos = (32*sgitg + tiisg)*nval;
  3992. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xxs_grid[pos + i];
  3993. nval = 2;
  3994. pos = (32*sgitg + tiisg)*nval;
  3995. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3996. threadgroup_barrier(mem_flags::mem_threadgroup);
  3997. }
  3998. const int ix = tiisg;
  3999. device const float * y4 = y + 32 * ix;
  4000. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4001. for (int i = 0; i < 32; ++i) {
  4002. yl[i] = y4[i];
  4003. }
  4004. const int ibl = ib32 / (QK_K / 32);
  4005. const int ib = ib32 % (QK_K / 32);
  4006. device const block_iq3_xxs * xr = x + ibl;
  4007. device const uint8_t * q3 = xr->qs + 8 * ib;
  4008. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  4009. device const half * dh = &xr->d;
  4010. for (int row = 0; row < N_DST; row++) {
  4011. const float db = dh[0];
  4012. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4013. const float d = db * (0.5f + (aux32 >> 28));
  4014. float2 sum = {0};
  4015. for (int l = 0; l < 4; ++l) {
  4016. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + q3[2*l+0]);
  4017. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + q3[2*l+1]);
  4018. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  4019. for (int j = 0; j < 4; ++j) {
  4020. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4021. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4022. }
  4023. }
  4024. sumf[row] += d * (sum[0] + sum[1]);
  4025. dh += nb*sizeof(block_iq3_xxs)/2;
  4026. q3 += nb*sizeof(block_iq3_xxs);
  4027. gas += nb*sizeof(block_iq3_xxs)/2;
  4028. }
  4029. y4 += 32 * 32;
  4030. }
  4031. for (int row = 0; row < N_DST; ++row) {
  4032. all_sum = simd_sum(sumf[row]);
  4033. if (tiisg == 0) {
  4034. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  4035. }
  4036. }
  4037. }
  4038. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  4039. kernel void kernel_mul_mv_iq3_xxs_f32(
  4040. device const void * src0,
  4041. device const float * src1,
  4042. device float * dst,
  4043. constant int64_t & ne00,
  4044. constant int64_t & ne01,
  4045. constant int64_t & ne02,
  4046. constant uint64_t & nb00,
  4047. constant uint64_t & nb01,
  4048. constant uint64_t & nb02,
  4049. constant int64_t & ne10,
  4050. constant int64_t & ne11,
  4051. constant int64_t & ne12,
  4052. constant uint64_t & nb10,
  4053. constant uint64_t & nb11,
  4054. constant uint64_t & nb12,
  4055. constant int64_t & ne0,
  4056. constant int64_t & ne1,
  4057. constant uint & r2,
  4058. constant uint & r3,
  4059. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4060. uint3 tgpig[[threadgroup_position_in_grid]],
  4061. uint tiisg[[thread_index_in_simdgroup]],
  4062. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4063. kernel_mul_mv_iq3_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4064. }
  4065. void kernel_mul_mv_iq3_s_f32_impl(
  4066. device const void * src0,
  4067. device const float * src1,
  4068. device float * dst,
  4069. constant int64_t & ne00,
  4070. constant int64_t & ne01,
  4071. constant int64_t & ne02,
  4072. constant int64_t & ne10,
  4073. constant int64_t & ne12,
  4074. constant int64_t & ne0,
  4075. constant int64_t & ne1,
  4076. constant uint & r2,
  4077. constant uint & r3,
  4078. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4079. uint3 tgpig[[threadgroup_position_in_grid]],
  4080. uint tiisg[[thread_index_in_simdgroup]],
  4081. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4082. const int nb = ne00/QK_K;
  4083. const int r0 = tgpig.x;
  4084. const int r1 = tgpig.y;
  4085. const int im = tgpig.z;
  4086. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4087. const int ib_row = first_row * nb;
  4088. const uint i12 = im%ne12;
  4089. const uint i13 = im/ne12;
  4090. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4091. device const block_iq3_s * x = (device const block_iq3_s *) src0 + ib_row + offset0;
  4092. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4093. float yl[32];
  4094. float sumf[N_DST]={0.f}, all_sum;
  4095. const int nb32 = nb * (QK_K / 32);
  4096. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  4097. {
  4098. int nval = 8;
  4099. int pos = (32*sgitg + tiisg)*nval;
  4100. for (int i = 0; i < nval; ++i) values[pos + i] = iq3s_grid[pos + i];
  4101. threadgroup_barrier(mem_flags::mem_threadgroup);
  4102. }
  4103. const int ix = tiisg;
  4104. device const float * y4 = y + 32 * ix;
  4105. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4106. for (int i = 0; i < 32; ++i) {
  4107. yl[i] = y4[i];
  4108. }
  4109. const int ibl = ib32 / (QK_K / 32);
  4110. const int ib = ib32 % (QK_K / 32);
  4111. device const block_iq3_s * xr = x + ibl;
  4112. device const uint8_t * qs = xr->qs + 8 * ib;
  4113. device const uint8_t * qh = xr->qh + ib;
  4114. device const uint8_t * sc = xr->scales + (ib/2);
  4115. device const uint8_t * signs = xr->signs + 4 * ib;
  4116. device const half * dh = &xr->d;
  4117. for (int row = 0; row < N_DST; row++) {
  4118. const float db = dh[0];
  4119. const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf));
  4120. float2 sum = {0};
  4121. for (int l = 0; l < 4; ++l) {
  4122. const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? values + 256 : values;
  4123. const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? values + 256 : values;
  4124. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]);
  4125. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]);
  4126. for (int j = 0; j < 4; ++j) {
  4127. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  4128. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  4129. }
  4130. }
  4131. sumf[row] += d * (sum[0] + sum[1]);
  4132. dh += nb*sizeof(block_iq3_s)/2;
  4133. qs += nb*sizeof(block_iq3_s);
  4134. qh += nb*sizeof(block_iq3_s);
  4135. sc += nb*sizeof(block_iq3_s);
  4136. signs += nb*sizeof(block_iq3_s);
  4137. }
  4138. y4 += 32 * 32;
  4139. }
  4140. for (int row = 0; row < N_DST; ++row) {
  4141. all_sum = simd_sum(sumf[row]);
  4142. if (tiisg == 0) {
  4143. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4144. }
  4145. }
  4146. }
  4147. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  4148. kernel void kernel_mul_mv_iq3_s_f32(
  4149. device const void * src0,
  4150. device const float * src1,
  4151. device float * dst,
  4152. constant int64_t & ne00,
  4153. constant int64_t & ne01,
  4154. constant int64_t & ne02,
  4155. constant uint64_t & nb00,
  4156. constant uint64_t & nb01,
  4157. constant uint64_t & nb02,
  4158. constant int64_t & ne10,
  4159. constant int64_t & ne11,
  4160. constant int64_t & ne12,
  4161. constant uint64_t & nb10,
  4162. constant uint64_t & nb11,
  4163. constant uint64_t & nb12,
  4164. constant int64_t & ne0,
  4165. constant int64_t & ne1,
  4166. constant uint & r2,
  4167. constant uint & r3,
  4168. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4169. uint3 tgpig[[threadgroup_position_in_grid]],
  4170. uint tiisg[[thread_index_in_simdgroup]],
  4171. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4172. kernel_mul_mv_iq3_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4173. }
  4174. void kernel_mul_mv_iq2_s_f32_impl(
  4175. device const void * src0,
  4176. device const float * src1,
  4177. device float * dst,
  4178. constant int64_t & ne00,
  4179. constant int64_t & ne01,
  4180. constant int64_t & ne02,
  4181. constant int64_t & ne10,
  4182. constant int64_t & ne12,
  4183. constant int64_t & ne0,
  4184. constant int64_t & ne1,
  4185. constant uint & r2,
  4186. constant uint & r3,
  4187. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4188. uint3 tgpig[[threadgroup_position_in_grid]],
  4189. uint tiisg[[thread_index_in_simdgroup]],
  4190. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4191. const int nb = ne00/QK_K;
  4192. const int r0 = tgpig.x;
  4193. const int r1 = tgpig.y;
  4194. const int im = tgpig.z;
  4195. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4196. const int ib_row = first_row * nb;
  4197. const uint i12 = im%ne12;
  4198. const uint i13 = im/ne12;
  4199. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4200. device const block_iq2_s * x = (device const block_iq2_s *) src0 + ib_row + offset0;
  4201. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4202. float yl[32];
  4203. float sumf[N_DST]={0.f}, all_sum;
  4204. const int nb32 = nb * (QK_K / 32);
  4205. //threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  4206. //{
  4207. // int nval = 32;
  4208. // int pos = (32*sgitg + tiisg)*nval;
  4209. // for (int i = 0; i < nval; ++i) values[pos + i] = iq2s_grid[pos + i];
  4210. // threadgroup_barrier(mem_flags::mem_threadgroup);
  4211. //}
  4212. const int ix = tiisg;
  4213. device const float * y4 = y + 32 * ix;
  4214. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4215. for (int i = 0; i < 32; ++i) {
  4216. yl[i] = y4[i];
  4217. }
  4218. const int ibl = ib32 / (QK_K / 32);
  4219. const int ib = ib32 % (QK_K / 32);
  4220. device const block_iq2_s * xr = x + ibl;
  4221. device const uint8_t * qs = xr->qs + 4 * ib;
  4222. device const uint8_t * qh = xr->qh + ib;
  4223. device const uint8_t * sc = xr->scales + ib;
  4224. device const uint8_t * signs = qs + QK_K/8;
  4225. device const half * dh = &xr->d;
  4226. for (int row = 0; row < N_DST; row++) {
  4227. const float db = dh[0];
  4228. const float d1 = db * (0.5f + (sc[0] & 0xf));
  4229. const float d2 = db * (0.5f + (sc[0] >> 4));
  4230. float2 sum = {0};
  4231. for (int l = 0; l < 2; ++l) {
  4232. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4233. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4234. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4235. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4236. for (int j = 0; j < 8; ++j) {
  4237. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  4238. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  4239. }
  4240. }
  4241. sumf[row] += d1 * sum[0] + d2 * sum[1];
  4242. dh += nb*sizeof(block_iq2_s)/2;
  4243. qs += nb*sizeof(block_iq2_s);
  4244. qh += nb*sizeof(block_iq2_s);
  4245. sc += nb*sizeof(block_iq2_s);
  4246. signs += nb*sizeof(block_iq2_s);
  4247. }
  4248. y4 += 32 * 32;
  4249. }
  4250. for (int row = 0; row < N_DST; ++row) {
  4251. all_sum = simd_sum(sumf[row]);
  4252. if (tiisg == 0) {
  4253. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  4254. }
  4255. }
  4256. }
  4257. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  4258. kernel void kernel_mul_mv_iq2_s_f32(
  4259. device const void * src0,
  4260. device const float * src1,
  4261. device float * dst,
  4262. constant int64_t & ne00,
  4263. constant int64_t & ne01,
  4264. constant int64_t & ne02,
  4265. constant uint64_t & nb00,
  4266. constant uint64_t & nb01,
  4267. constant uint64_t & nb02,
  4268. constant int64_t & ne10,
  4269. constant int64_t & ne11,
  4270. constant int64_t & ne12,
  4271. constant uint64_t & nb10,
  4272. constant uint64_t & nb11,
  4273. constant uint64_t & nb12,
  4274. constant int64_t & ne0,
  4275. constant int64_t & ne1,
  4276. constant uint & r2,
  4277. constant uint & r3,
  4278. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4279. uint3 tgpig[[threadgroup_position_in_grid]],
  4280. uint tiisg[[thread_index_in_simdgroup]],
  4281. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4282. kernel_mul_mv_iq2_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4283. }
  4284. void kernel_mul_mv_iq1_s_f32_impl(
  4285. device const void * src0,
  4286. device const float * src1,
  4287. device float * dst,
  4288. constant int64_t & ne00,
  4289. constant int64_t & ne01,
  4290. constant int64_t & ne02,
  4291. constant int64_t & ne10,
  4292. constant int64_t & ne12,
  4293. constant int64_t & ne0,
  4294. constant int64_t & ne1,
  4295. constant uint & r2,
  4296. constant uint & r3,
  4297. uint3 tgpig[[threadgroup_position_in_grid]],
  4298. uint tiisg[[thread_index_in_simdgroup]],
  4299. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4300. const int nb = ne00/QK_K;
  4301. const int r0 = tgpig.x;
  4302. const int r1 = tgpig.y;
  4303. const int im = tgpig.z;
  4304. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4305. const int ib_row = first_row * nb;
  4306. const uint i12 = im%ne12;
  4307. const uint i13 = im/ne12;
  4308. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4309. device const block_iq1_s * x = (device const block_iq1_s *) src0 + ib_row + offset0;
  4310. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4311. float yl[16];
  4312. float sumf[N_DST]={0.f}, all_sum;
  4313. const int nb32 = nb * (QK_K / 32);
  4314. const int ix = tiisg/2;
  4315. const int il = tiisg%2;
  4316. device const float * y4 = y + 32 * ix + 16 * il;
  4317. for (int ib32 = ix; ib32 < nb32; ib32 += 16) {
  4318. for (int i = 0; i < 16; ++i) {
  4319. yl[i] = y4[i];
  4320. }
  4321. const int ibl = ib32 / (QK_K / 32);
  4322. const int ib = ib32 % (QK_K / 32);
  4323. device const block_iq1_s * xr = x + ibl;
  4324. device const uint8_t * qs = xr->qs + 4 * ib + 2 * il;
  4325. device const uint8_t * sc = xr->scales + 2 * ib + il;
  4326. device const half * dh = &xr->d;
  4327. for (int row = 0; row < N_DST; row++) {
  4328. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4329. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4330. float2 sum = {0};
  4331. for (int j = 0; j < 8; ++j) {
  4332. sum[0] += yl[j+ 0] * grid1[j];
  4333. sum[1] += yl[j+ 8] * grid2[j];
  4334. }
  4335. sumf[row] += (float)dh[0] * (sum[0] * (2*(sc[0] & 7) + 1) + sum[1] * (2*((sc[0] >> 4) & 7) + 1));
  4336. dh += nb*sizeof(block_iq1_s)/2;
  4337. qs += nb*sizeof(block_iq1_s);
  4338. sc += nb*sizeof(block_iq1_s);
  4339. }
  4340. y4 += 16 * 32;
  4341. }
  4342. for (int row = 0; row < N_DST; ++row) {
  4343. all_sum = simd_sum(sumf[row]);
  4344. if (tiisg == 0) {
  4345. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4346. }
  4347. }
  4348. }
  4349. constexpr constant static float kvalues_iq4nl_f[16] = {
  4350. -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
  4351. };
  4352. void kernel_mul_mv_iq4_nl_f32_impl(
  4353. device const void * src0,
  4354. device const float * src1,
  4355. device float * dst,
  4356. constant int64_t & ne00,
  4357. constant int64_t & ne01,
  4358. constant int64_t & ne02,
  4359. constant int64_t & ne10,
  4360. constant int64_t & ne12,
  4361. constant int64_t & ne0,
  4362. constant int64_t & ne1,
  4363. constant uint & r2,
  4364. constant uint & r3,
  4365. threadgroup float * shared_values [[threadgroup(0)]],
  4366. uint3 tgpig[[threadgroup_position_in_grid]],
  4367. uint tiisg[[thread_index_in_simdgroup]],
  4368. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4369. const int nb = ne00/QK4_NL;
  4370. const int r0 = tgpig.x;
  4371. const int r1 = tgpig.y;
  4372. const int im = tgpig.z;
  4373. const int first_row = (r0 * 2 + sgitg) * 2;
  4374. const int ib_row = first_row * nb;
  4375. const uint i12 = im%ne12;
  4376. const uint i13 = im/ne12;
  4377. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4378. device const block_iq4_nl * x = (device const block_iq4_nl *) src0 + ib_row + offset0;
  4379. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4380. const int ix = tiisg/2; // 0...15
  4381. const int it = tiisg%2; // 0 or 1
  4382. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4383. threadgroup_barrier(mem_flags::mem_threadgroup);
  4384. float4 yl[4];
  4385. float sumf[2]={0.f}, all_sum;
  4386. device const float * yb = y + ix * QK4_NL + it * 8;
  4387. uint32_t aux32[2];
  4388. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4389. float4 qf1, qf2;
  4390. for (int ib = ix; ib < nb; ib += 16) {
  4391. device const float4 * y4 = (device const float4 *)yb;
  4392. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4393. for (int row = 0; row < 2; ++row) {
  4394. device const block_iq4_nl & xb = x[row*nb + ib];
  4395. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  4396. float4 acc1 = {0.f}, acc2 = {0.f};
  4397. aux32[0] = q4[0] | (q4[1] << 16);
  4398. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4399. aux32[0] &= 0x0f0f0f0f;
  4400. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4401. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4402. acc1 += yl[0] * qf1;
  4403. acc2 += yl[1] * qf2;
  4404. aux32[0] = q4[2] | (q4[3] << 16);
  4405. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4406. aux32[0] &= 0x0f0f0f0f;
  4407. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4408. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4409. acc1 += yl[2] * qf1;
  4410. acc2 += yl[3] * qf2;
  4411. acc1 += acc2;
  4412. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4413. }
  4414. yb += 16 * QK4_NL;
  4415. }
  4416. for (int row = 0; row < 2; ++row) {
  4417. all_sum = simd_sum(sumf[row]);
  4418. if (tiisg == 0) {
  4419. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4420. }
  4421. }
  4422. }
  4423. #if QK_K != 64
  4424. void kernel_mul_mv_iq4_xs_f32_impl(
  4425. device const void * src0,
  4426. device const float * src1,
  4427. device float * dst,
  4428. constant int64_t & ne00,
  4429. constant int64_t & ne01,
  4430. constant int64_t & ne02,
  4431. constant int64_t & ne10,
  4432. constant int64_t & ne12,
  4433. constant int64_t & ne0,
  4434. constant int64_t & ne1,
  4435. constant uint & r2,
  4436. constant uint & r3,
  4437. threadgroup float * shared_values [[threadgroup(0)]],
  4438. uint3 tgpig[[threadgroup_position_in_grid]],
  4439. uint tiisg[[thread_index_in_simdgroup]],
  4440. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4441. const int nb = ne00/QK_K;
  4442. const int r0 = tgpig.x;
  4443. const int r1 = tgpig.y;
  4444. const int im = tgpig.z;
  4445. const int first_row = (r0 * 2 + sgitg) * 2;
  4446. const int ib_row = first_row * nb;
  4447. const uint i12 = im%ne12;
  4448. const uint i13 = im/ne12;
  4449. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4450. device const block_iq4_xs * x = (device const block_iq4_xs *) src0 + ib_row + offset0;
  4451. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4452. const int ix = tiisg/16; // 0 or 1
  4453. const int it = tiisg%16; // 0...15
  4454. const int ib = it/2;
  4455. const int il = it%2;
  4456. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4457. threadgroup_barrier(mem_flags::mem_threadgroup);
  4458. float4 yl[4];
  4459. float sumf[2]={0.f}, all_sum;
  4460. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  4461. uint32_t aux32[2];
  4462. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4463. float4 qf1, qf2;
  4464. for (int ibl = ix; ibl < nb; ibl += 2) {
  4465. device const float4 * y4 = (device const float4 *)yb;
  4466. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4467. for (int row = 0; row < 2; ++row) {
  4468. device const block_iq4_xs & xb = x[row*nb + ibl];
  4469. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  4470. float4 acc1 = {0.f}, acc2 = {0.f};
  4471. aux32[0] = q4[0] & 0x0f0f0f0f;
  4472. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  4473. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4474. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4475. acc1 += yl[0] * qf1;
  4476. acc2 += yl[1] * qf2;
  4477. aux32[0] = q4[1] & 0x0f0f0f0f;
  4478. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  4479. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4480. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4481. acc1 += yl[2] * qf1;
  4482. acc2 += yl[3] * qf2;
  4483. acc1 += acc2;
  4484. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  4485. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4486. }
  4487. yb += 2 * QK_K;
  4488. }
  4489. for (int row = 0; row < 2; ++row) {
  4490. all_sum = simd_sum(sumf[row]);
  4491. if (tiisg == 0) {
  4492. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4493. }
  4494. }
  4495. }
  4496. #endif
  4497. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  4498. kernel void kernel_mul_mv_iq1_s_f32(
  4499. device const void * src0,
  4500. device const float * src1,
  4501. device float * dst,
  4502. constant int64_t & ne00,
  4503. constant int64_t & ne01,
  4504. constant int64_t & ne02,
  4505. constant uint64_t & nb00,
  4506. constant uint64_t & nb01,
  4507. constant uint64_t & nb02,
  4508. constant int64_t & ne10,
  4509. constant int64_t & ne11,
  4510. constant int64_t & ne12,
  4511. constant uint64_t & nb10,
  4512. constant uint64_t & nb11,
  4513. constant uint64_t & nb12,
  4514. constant int64_t & ne0,
  4515. constant int64_t & ne1,
  4516. constant uint & r2,
  4517. constant uint & r3,
  4518. uint3 tgpig[[threadgroup_position_in_grid]],
  4519. uint tiisg[[thread_index_in_simdgroup]],
  4520. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4521. kernel_mul_mv_iq1_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  4522. }
  4523. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  4524. kernel void kernel_mul_mv_iq4_nl_f32(
  4525. device const void * src0,
  4526. device const float * src1,
  4527. device float * dst,
  4528. constant int64_t & ne00,
  4529. constant int64_t & ne01,
  4530. constant int64_t & ne02,
  4531. constant uint64_t & nb00,
  4532. constant uint64_t & nb01,
  4533. constant uint64_t & nb02,
  4534. constant int64_t & ne10,
  4535. constant int64_t & ne11,
  4536. constant int64_t & ne12,
  4537. constant uint64_t & nb10,
  4538. constant uint64_t & nb11,
  4539. constant uint64_t & nb12,
  4540. constant int64_t & ne0,
  4541. constant int64_t & ne1,
  4542. constant uint & r2,
  4543. constant uint & r3,
  4544. threadgroup float * shared_values [[threadgroup(0)]],
  4545. uint3 tgpig[[threadgroup_position_in_grid]],
  4546. uint tiisg[[thread_index_in_simdgroup]],
  4547. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4548. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4549. }
  4550. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  4551. kernel void kernel_mul_mv_iq4_xs_f32(
  4552. device const void * src0,
  4553. device const float * src1,
  4554. device float * dst,
  4555. constant int64_t & ne00,
  4556. constant int64_t & ne01,
  4557. constant int64_t & ne02,
  4558. constant uint64_t & nb00,
  4559. constant uint64_t & nb01,
  4560. constant uint64_t & nb02,
  4561. constant int64_t & ne10,
  4562. constant int64_t & ne11,
  4563. constant int64_t & ne12,
  4564. constant uint64_t & nb10,
  4565. constant uint64_t & nb11,
  4566. constant uint64_t & nb12,
  4567. constant int64_t & ne0,
  4568. constant int64_t & ne1,
  4569. constant uint & r2,
  4570. constant uint & r3,
  4571. threadgroup float * shared_values [[threadgroup(0)]],
  4572. uint3 tgpig[[threadgroup_position_in_grid]],
  4573. uint tiisg[[thread_index_in_simdgroup]],
  4574. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4575. #if QK_K == 64
  4576. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4577. #else
  4578. kernel_mul_mv_iq4_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4579. #endif
  4580. }
  4581. //============================= templates and their specializations =============================
  4582. // NOTE: this is not dequantizing - we are simply fitting the template
  4583. template <typename type4x4>
  4584. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  4585. float4x4 temp = *(((device float4x4 *)src));
  4586. for (int i = 0; i < 16; i++){
  4587. reg[i/4][i%4] = temp[i/4][i%4];
  4588. }
  4589. }
  4590. template <typename type4x4>
  4591. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  4592. half4x4 temp = *(((device half4x4 *)src));
  4593. for (int i = 0; i < 16; i++){
  4594. reg[i/4][i%4] = temp[i/4][i%4];
  4595. }
  4596. }
  4597. template <typename type4x4>
  4598. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  4599. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  4600. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4601. const float d2 = d1 / 256.f;
  4602. const float md = -8.h * xb->d;
  4603. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4604. const ushort mask1 = mask0 << 8;
  4605. for (int i=0;i<8;i++) {
  4606. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  4607. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  4608. }
  4609. }
  4610. template <typename type4x4>
  4611. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  4612. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  4613. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4614. const float d2 = d1 / 256.f;
  4615. const float m = xb->m;
  4616. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4617. const ushort mask1 = mask0 << 8;
  4618. for (int i=0;i<8;i++) {
  4619. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  4620. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  4621. }
  4622. }
  4623. template <typename type4x4>
  4624. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  4625. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  4626. const float d = xb->d;
  4627. const float md = -16.h * xb->d;
  4628. const ushort mask = il ? 0x00F0 : 0x000F;
  4629. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4630. const int x_mv = il ? 4 : 0;
  4631. const int gh_mv = il ? 12 : 0;
  4632. const int gh_bk = il ? 0 : 4;
  4633. for (int i = 0; i < 8; i++) {
  4634. // extract the 5-th bits for x0 and x1
  4635. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4636. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4637. // combine the 4-bits from qs with the 5th bit
  4638. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4639. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4640. reg[i/2][2*(i%2)+0] = d * x0 + md;
  4641. reg[i/2][2*(i%2)+1] = d * x1 + md;
  4642. }
  4643. }
  4644. template <typename type4x4>
  4645. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  4646. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  4647. const float d = xb->d;
  4648. const float m = xb->m;
  4649. const ushort mask = il ? 0x00F0 : 0x000F;
  4650. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4651. const int x_mv = il ? 4 : 0;
  4652. const int gh_mv = il ? 12 : 0;
  4653. const int gh_bk = il ? 0 : 4;
  4654. for (int i = 0; i < 8; i++) {
  4655. // extract the 5-th bits for x0 and x1
  4656. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4657. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4658. // combine the 4-bits from qs with the 5th bit
  4659. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4660. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4661. reg[i/2][2*(i%2)+0] = d * x0 + m;
  4662. reg[i/2][2*(i%2)+1] = d * x1 + m;
  4663. }
  4664. }
  4665. template <typename type4x4>
  4666. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  4667. device const int8_t * qs = ((device const int8_t *)xb->qs);
  4668. const half d = xb->d;
  4669. for (int i = 0; i < 16; i++) {
  4670. reg[i/4][i%4] = (qs[i + 16*il] * d);
  4671. }
  4672. }
  4673. template <typename type4x4>
  4674. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  4675. const float d = xb->d;
  4676. const float min = xb->dmin;
  4677. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4678. float dl, ml;
  4679. uint8_t sc = xb->scales[il];
  4680. #if QK_K == 256
  4681. q = q + 32*(il/8) + 16*(il&1);
  4682. il = (il/2)%4;
  4683. #endif
  4684. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4685. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4686. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  4687. for (int i = 0; i < 16; ++i) {
  4688. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4689. }
  4690. }
  4691. template <typename type4x4>
  4692. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  4693. const half d_all = xb->d;
  4694. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4695. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  4696. device const int8_t * scales = (device const int8_t *)xb->scales;
  4697. #if QK_K == 256
  4698. q = q + 32 * (il/8) + 16 * (il&1);
  4699. h = h + 16 * (il&1);
  4700. uint8_t m = 1 << (il/2);
  4701. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  4702. ((il/4)>0 ? 12 : 3);
  4703. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  4704. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  4705. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  4706. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  4707. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  4708. const float ml = 4.f * dl;
  4709. il = (il/2) & 3;
  4710. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4711. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4712. dl *= coef;
  4713. for (int i = 0; i < 16; ++i) {
  4714. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  4715. }
  4716. #else
  4717. float kcoef = il&1 ? 1.f/16.f : 1.f;
  4718. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  4719. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  4720. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4721. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4722. uint8_t m = 1<<(il*2);
  4723. for (int i = 0; i < 16; ++i) {
  4724. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  4725. }
  4726. #endif
  4727. }
  4728. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  4729. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  4730. : 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))};
  4731. }
  4732. template <typename type4x4>
  4733. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  4734. device const uchar * q = xb->qs;
  4735. #if QK_K == 256
  4736. short is = (il/4) * 2;
  4737. q = q + (il/4) * 32 + 16 * (il&1);
  4738. il = il & 3;
  4739. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4740. const float d = il < 2 ? xb->d : xb->d / 16.h;
  4741. const float min = xb->dmin;
  4742. const float dl = d * sc[0];
  4743. const float ml = min * sc[1];
  4744. #else
  4745. (void) get_scale_min_k4_just2;
  4746. q = q + 16 * (il&1);
  4747. device const uint8_t * s = xb->scales;
  4748. device const half2 * dh = (device const half2 *)xb->d;
  4749. const float2 d = (float2)dh[0];
  4750. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  4751. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  4752. #endif
  4753. const ushort mask = il<2 ? 0x0F : 0xF0;
  4754. for (int i = 0; i < 16; ++i) {
  4755. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4756. }
  4757. }
  4758. template <typename type4x4>
  4759. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  4760. device const uint8_t * q = xb->qs;
  4761. device const uint8_t * qh = xb->qh;
  4762. #if QK_K == 256
  4763. short is = (il/4) * 2;
  4764. q = q + 32 * (il/4) + 16 * (il&1);
  4765. qh = qh + 16 * (il&1);
  4766. uint8_t ul = 1 << (il/2);
  4767. il = il & 3;
  4768. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4769. const float d = il < 2 ? xb->d : xb->d / 16.f;
  4770. const float min = xb->dmin;
  4771. const float dl = d * sc[0];
  4772. const float ml = min * sc[1];
  4773. const ushort mask = il<2 ? 0x0F : 0xF0;
  4774. const float qh_val = il<2 ? 16.f : 256.f;
  4775. for (int i = 0; i < 16; ++i) {
  4776. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  4777. }
  4778. #else
  4779. q = q + 16 * (il&1);
  4780. device const int8_t * s = xb->scales;
  4781. const float dl = xb->d * s[il];
  4782. uint8_t m = 1<<(il*2);
  4783. const float coef = il<2 ? 1.f : 1.f/16.f;
  4784. const ushort mask = il<2 ? 0x0F : 0xF0;
  4785. for (int i = 0; i < 16; ++i) {
  4786. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  4787. }
  4788. #endif
  4789. }
  4790. template <typename type4x4>
  4791. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  4792. const half d_all = xb->d;
  4793. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  4794. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  4795. device const int8_t * scales = (device const int8_t *)xb->scales;
  4796. #if QK_K == 256
  4797. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  4798. qh = qh + 32*(il/8) + 16*(il&1);
  4799. float sc = scales[(il%2) + 2 * ((il/2))];
  4800. il = (il/2) & 3;
  4801. #else
  4802. ql = ql + 16 * (il&1);
  4803. float sc = scales[il];
  4804. #endif
  4805. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4806. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  4807. const float coef = il>1 ? 1.f/16.f : 1.f;
  4808. const float ml = d_all * sc * 32.f;
  4809. const float dl = d_all * sc * coef;
  4810. for (int i = 0; i < 16; ++i) {
  4811. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  4812. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  4813. reg[i/4][i%4] = dl * q - ml;
  4814. }
  4815. }
  4816. template <typename type4x4>
  4817. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  4818. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4819. const float d = xb->d;
  4820. const int ib32 = il/2;
  4821. il = il%2;
  4822. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4823. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  4824. device const uint16_t * q2 = xb->qs + 4*ib32;
  4825. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  4826. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  4827. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  4828. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  4829. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4830. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  4831. for (int i = 0; i < 8; ++i) {
  4832. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4833. }
  4834. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4835. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  4836. for (int i = 0; i < 8; ++i) {
  4837. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4838. }
  4839. }
  4840. template <typename type4x4>
  4841. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  4842. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4843. const float d = xb->d;
  4844. const int ib32 = il/2;
  4845. il = il%2;
  4846. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4847. device const uint16_t * q2 = xb->qs + 4*ib32;
  4848. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4849. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  4850. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  4851. for (int i = 0; i < 8; ++i) {
  4852. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4853. }
  4854. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  4855. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  4856. for (int i = 0; i < 8; ++i) {
  4857. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4858. }
  4859. }
  4860. template <typename type4x4>
  4861. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  4862. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4863. const float d = xb->d;
  4864. const int ib32 = il/2;
  4865. il = il%2;
  4866. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4867. device const uint8_t * q3 = xb->qs + 8*ib32;
  4868. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  4869. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4870. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  4871. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  4872. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  4873. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4874. for (int i = 0; i < 4; ++i) {
  4875. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4876. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4877. }
  4878. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  4879. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  4880. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  4881. for (int i = 0; i < 4; ++i) {
  4882. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4883. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4884. }
  4885. }
  4886. template <typename type4x4>
  4887. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  4888. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4889. const float d = xb->d;
  4890. const int ib32 = il/2;
  4891. il = il%2;
  4892. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4893. device const uint8_t * qs = xb->qs + 8*ib32;
  4894. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  4895. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4896. const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
  4897. constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  4898. constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  4899. for (int i = 0; i < 4; ++i) {
  4900. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  4901. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  4902. }
  4903. grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  4904. grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  4905. for (int i = 0; i < 4; ++i) {
  4906. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  4907. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  4908. }
  4909. }
  4910. template <typename type4x4>
  4911. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  4912. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4913. const float d = xb->d;
  4914. const int ib32 = il/2;
  4915. il = il%2;
  4916. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4917. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4918. device const uint8_t * signs = qs + QK_K/8;
  4919. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4920. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4921. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  4922. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  4923. for (int i = 0; i < 8; ++i) {
  4924. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  4925. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  4926. }
  4927. }
  4928. template <typename type4x4>
  4929. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  4930. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4931. const float d = xb->d;
  4932. device const uint8_t * qs = xb->qs + 2*il;
  4933. device const uint8_t * sc = xb->scales + il;
  4934. const float dl1 = d * (2*(sc[0] & 7) + 1);
  4935. const float dl2 = d * (2*((sc[0] >> 4) & 7) + 1);
  4936. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4937. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4938. for (int i = 0; i < 8; ++i) {
  4939. reg[i/4+0][i%4] = dl1 * grid1[i];
  4940. reg[i/4+2][i%4] = dl2 * grid2[i];
  4941. }
  4942. }
  4943. template <typename type4x4>
  4944. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  4945. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  4946. const float d = xb->d;
  4947. uint32_t aux32;
  4948. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4949. for (int i = 0; i < 4; ++i) {
  4950. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  4951. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4952. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4953. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4954. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4955. }
  4956. }
  4957. template <typename type4x4>
  4958. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  4959. #if QK_K == 64
  4960. dequantize_iq4_nl(xb, il, reg);
  4961. #else
  4962. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4963. const int ib32 = il/2;
  4964. il = il%2;
  4965. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4966. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  4967. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  4968. const float d = (float)xb->d * (ls - 32);
  4969. uint32_t aux32;
  4970. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4971. for (int i = 0; i < 4; ++i) {
  4972. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  4973. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4974. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4975. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4976. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4977. }
  4978. #endif
  4979. }
  4980. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  4981. kernel void kernel_get_rows(
  4982. device const void * src0,
  4983. device const char * src1,
  4984. device float * dst,
  4985. constant int64_t & ne00,
  4986. constant uint64_t & nb01,
  4987. constant uint64_t & nb02,
  4988. constant int64_t & ne10,
  4989. constant uint64_t & nb10,
  4990. constant uint64_t & nb11,
  4991. constant uint64_t & nb1,
  4992. constant uint64_t & nb2,
  4993. uint3 tgpig[[threadgroup_position_in_grid]],
  4994. uint tiitg[[thread_index_in_threadgroup]],
  4995. uint3 tptg [[threads_per_threadgroup]]) {
  4996. //const int64_t i = tgpig;
  4997. //const int64_t r = ((device int32_t *) src1)[i];
  4998. const int64_t i10 = tgpig.x;
  4999. const int64_t i11 = tgpig.y;
  5000. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5001. const int64_t i02 = i11;
  5002. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  5003. float4x4 temp;
  5004. dequantize_func(
  5005. ((device const block_q *) ((device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  5006. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  5007. }
  5008. }
  5009. kernel void kernel_get_rows_f32(
  5010. device const void * src0,
  5011. device const char * src1,
  5012. device float * dst,
  5013. constant int64_t & ne00,
  5014. constant uint64_t & nb01,
  5015. constant uint64_t & nb02,
  5016. constant int64_t & ne10,
  5017. constant uint64_t & nb10,
  5018. constant uint64_t & nb11,
  5019. constant uint64_t & nb1,
  5020. constant uint64_t & nb2,
  5021. uint3 tgpig[[threadgroup_position_in_grid]],
  5022. uint tiitg[[thread_index_in_threadgroup]],
  5023. uint3 tptg [[threads_per_threadgroup]]) {
  5024. const int64_t i10 = tgpig.x;
  5025. const int64_t i11 = tgpig.y;
  5026. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5027. const int64_t i02 = i11;
  5028. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5029. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5030. ((device float *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5031. }
  5032. }
  5033. kernel void kernel_get_rows_f16(
  5034. device const void * src0,
  5035. device const char * src1,
  5036. device float * dst,
  5037. constant int64_t & ne00,
  5038. constant uint64_t & nb01,
  5039. constant uint64_t & nb02,
  5040. constant int64_t & ne10,
  5041. constant uint64_t & nb10,
  5042. constant uint64_t & nb11,
  5043. constant uint64_t & nb1,
  5044. constant uint64_t & nb2,
  5045. uint3 tgpig[[threadgroup_position_in_grid]],
  5046. uint tiitg[[thread_index_in_threadgroup]],
  5047. uint3 tptg [[threads_per_threadgroup]]) {
  5048. const int64_t i10 = tgpig.x;
  5049. const int64_t i11 = tgpig.y;
  5050. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5051. const int64_t i02 = i11;
  5052. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5053. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5054. ((device half *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5055. }
  5056. }
  5057. kernel void kernel_get_rows_i32(
  5058. device const void * src0,
  5059. device const char * src1,
  5060. device int32_t * dst,
  5061. constant int64_t & ne00,
  5062. constant uint64_t & nb01,
  5063. constant uint64_t & nb02,
  5064. constant int64_t & ne10,
  5065. constant uint64_t & nb10,
  5066. constant uint64_t & nb11,
  5067. constant uint64_t & nb1,
  5068. constant uint64_t & nb2,
  5069. uint3 tgpig[[threadgroup_position_in_grid]],
  5070. uint tiitg[[thread_index_in_threadgroup]],
  5071. uint3 tptg [[threads_per_threadgroup]]) {
  5072. const int64_t i10 = tgpig.x;
  5073. const int64_t i11 = tgpig.y;
  5074. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5075. const int64_t i02 = i11;
  5076. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5077. ((device int32_t *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5078. ((device int32_t *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5079. }
  5080. }
  5081. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  5082. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  5083. #define BLOCK_SIZE_K 32
  5084. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  5085. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  5086. #define THREAD_PER_BLOCK 128
  5087. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  5088. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  5089. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  5090. #define SG_MAT_ROW 8
  5091. // each block_q contains 16*nl weights
  5092. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5093. void kernel_mul_mm_impl(device const uchar * src0,
  5094. device const uchar * src1,
  5095. device float * dst,
  5096. constant int64_t & ne00,
  5097. constant int64_t & ne02,
  5098. constant uint64_t & nb01,
  5099. constant uint64_t & nb02,
  5100. constant int64_t & ne12,
  5101. constant uint64_t & nb10,
  5102. constant uint64_t & nb11,
  5103. constant uint64_t & nb12,
  5104. constant int64_t & ne0,
  5105. constant int64_t & ne1,
  5106. constant uint & r2,
  5107. constant uint & r3,
  5108. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5109. uint3 tgpig[[threadgroup_position_in_grid]],
  5110. uint tiitg[[thread_index_in_threadgroup]],
  5111. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5112. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5113. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5114. const uint r0 = tgpig.y;
  5115. const uint r1 = tgpig.x;
  5116. const uint im = tgpig.z;
  5117. // if this block is of 64x32 shape or smaller
  5118. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5119. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5120. // a thread shouldn't load data outside of the matrix
  5121. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5122. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5123. simdgroup_half8x8 ma[4];
  5124. simdgroup_float8x8 mb[2];
  5125. simdgroup_float8x8 c_res[8];
  5126. for (int i = 0; i < 8; i++){
  5127. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5128. }
  5129. short il = (tiitg % THREAD_PER_ROW);
  5130. const uint i12 = im%ne12;
  5131. const uint i13 = im/ne12;
  5132. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5133. ushort offset1 = il/nl;
  5134. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5135. device const float * y = (device const float *)(src1
  5136. + nb12 * im
  5137. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  5138. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5139. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5140. // load data and store to threadgroup memory
  5141. half4x4 temp_a;
  5142. dequantize_func(x, il, temp_a);
  5143. threadgroup_barrier(mem_flags::mem_threadgroup);
  5144. #pragma unroll(16)
  5145. for (int i = 0; i < 16; i++) {
  5146. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5147. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5148. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5149. }
  5150. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5151. il = (il + 2 < nl) ? il + 2 : il % 2;
  5152. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5153. y += BLOCK_SIZE_K;
  5154. threadgroup_barrier(mem_flags::mem_threadgroup);
  5155. // load matrices from threadgroup memory and conduct outer products
  5156. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5157. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5158. #pragma unroll(4)
  5159. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5160. #pragma unroll(4)
  5161. for (int i = 0; i < 4; i++) {
  5162. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5163. }
  5164. simdgroup_barrier(mem_flags::mem_none);
  5165. #pragma unroll(2)
  5166. for (int i = 0; i < 2; i++) {
  5167. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5168. }
  5169. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5170. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5171. #pragma unroll(8)
  5172. for (int i = 0; i < 8; i++){
  5173. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5174. }
  5175. }
  5176. }
  5177. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  5178. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  5179. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  5180. for (int i = 0; i < 8; i++) {
  5181. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  5182. }
  5183. } else {
  5184. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  5185. threadgroup_barrier(mem_flags::mem_threadgroup);
  5186. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5187. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5188. for (int i = 0; i < 8; i++) {
  5189. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5190. }
  5191. threadgroup_barrier(mem_flags::mem_threadgroup);
  5192. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  5193. if (sgitg == 0) {
  5194. for (int i = 0; i < n_rows; i++) {
  5195. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5196. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5197. }
  5198. }
  5199. }
  5200. }
  5201. }
  5202. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in src1ids
  5203. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5204. void kernel_mul_mm_id_impl(
  5205. device const uchar * src0,
  5206. device const uchar * src1,
  5207. thread short * src1ids,
  5208. device float * dst,
  5209. constant int64_t & ne00,
  5210. constant int64_t & ne02,
  5211. constant uint64_t & nb01,
  5212. constant uint64_t & nb02,
  5213. constant int64_t & ne12,
  5214. constant uint64_t & nb10,
  5215. constant uint64_t & nb11,
  5216. constant uint64_t & nb12,
  5217. constant int64_t & ne0,
  5218. int64_t ne1,
  5219. constant uint & r2,
  5220. constant uint & r3,
  5221. threadgroup uchar * shared_memory,
  5222. uint3 tgpig[[threadgroup_position_in_grid]],
  5223. uint tiitg[[thread_index_in_threadgroup]],
  5224. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5225. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5226. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5227. const uint r0 = tgpig.y;
  5228. const uint r1 = tgpig.x;
  5229. const uint im = tgpig.z;
  5230. if (r1 * BLOCK_SIZE_N >= ne1) return;
  5231. // if this block is of 64x32 shape or smaller
  5232. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5233. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5234. // a thread shouldn't load data outside of the matrix
  5235. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5236. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5237. simdgroup_half8x8 ma[4];
  5238. simdgroup_float8x8 mb[2];
  5239. simdgroup_float8x8 c_res[8];
  5240. for (int i = 0; i < 8; i++){
  5241. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5242. }
  5243. short il = (tiitg % THREAD_PER_ROW);
  5244. const uint i12 = im%ne12;
  5245. const uint i13 = im/ne12;
  5246. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5247. ushort offset1 = il/nl;
  5248. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5249. device const float * y = (device const float *)(src1
  5250. + nb12 * im
  5251. + nb11 * src1ids[r1 * BLOCK_SIZE_N + thread_col]
  5252. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5253. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5254. // load data and store to threadgroup memory
  5255. half4x4 temp_a;
  5256. dequantize_func(x, il, temp_a);
  5257. threadgroup_barrier(mem_flags::mem_threadgroup);
  5258. for (int i = 0; i < 16; i++) {
  5259. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5260. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5261. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5262. }
  5263. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5264. il = (il + 2 < nl) ? il + 2 : il % 2;
  5265. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5266. y += BLOCK_SIZE_K;
  5267. threadgroup_barrier(mem_flags::mem_threadgroup);
  5268. // load matrices from threadgroup memory and conduct outer products
  5269. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5270. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5271. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5272. for (int i = 0; i < 4; i++) {
  5273. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5274. }
  5275. simdgroup_barrier(mem_flags::mem_none);
  5276. for (int i = 0; i < 2; i++) {
  5277. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5278. }
  5279. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5280. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5281. for (int i = 0; i < 8; i++){
  5282. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5283. }
  5284. }
  5285. }
  5286. {
  5287. threadgroup_barrier(mem_flags::mem_threadgroup);
  5288. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5289. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5290. for (int i = 0; i < 8; i++) {
  5291. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5292. }
  5293. threadgroup_barrier(mem_flags::mem_threadgroup);
  5294. device float * C = dst + (BLOCK_SIZE_M * r0) + im*ne1*ne0;
  5295. if (sgitg == 0) {
  5296. for (int i = 0; i < n_rows; i++) {
  5297. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5298. *(C + i + src1ids[j + r1*BLOCK_SIZE_N] * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5299. }
  5300. }
  5301. }
  5302. }
  5303. }
  5304. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5305. kernel void kernel_mul_mm(device const uchar * src0,
  5306. device const uchar * src1,
  5307. device float * dst,
  5308. constant int64_t & ne00,
  5309. constant int64_t & ne02,
  5310. constant uint64_t & nb01,
  5311. constant uint64_t & nb02,
  5312. constant int64_t & ne12,
  5313. constant uint64_t & nb10,
  5314. constant uint64_t & nb11,
  5315. constant uint64_t & nb12,
  5316. constant int64_t & ne0,
  5317. constant int64_t & ne1,
  5318. constant uint & r2,
  5319. constant uint & r3,
  5320. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5321. uint3 tgpig[[threadgroup_position_in_grid]],
  5322. uint tiitg[[thread_index_in_threadgroup]],
  5323. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5324. kernel_mul_mm_impl<block_q, nl, dequantize_func>(
  5325. src0,
  5326. src1,
  5327. dst,
  5328. ne00,
  5329. ne02,
  5330. nb01,
  5331. nb02,
  5332. ne12,
  5333. nb10,
  5334. nb11,
  5335. nb12,
  5336. ne0,
  5337. ne1,
  5338. r2,
  5339. r3,
  5340. shared_memory,
  5341. tgpig,
  5342. tiitg,
  5343. sgitg);
  5344. }
  5345. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5346. kernel void kernel_mul_mm_id(
  5347. device const uchar * ids,
  5348. device const uchar * src1,
  5349. device float * dst,
  5350. constant uint64_t & nbi1,
  5351. constant int64_t & ne00,
  5352. constant int64_t & ne02,
  5353. constant uint64_t & nb01,
  5354. constant uint64_t & nb02,
  5355. constant int64_t & ne12,
  5356. constant int64_t & ne13,
  5357. constant uint64_t & nb10,
  5358. constant uint64_t & nb11,
  5359. constant uint64_t & nb12,
  5360. constant int64_t & ne0,
  5361. constant int64_t & ne1,
  5362. constant uint64_t & nb1,
  5363. constant uint & r2,
  5364. constant uint & r3,
  5365. constant int & idx,
  5366. device const uchar * src00,
  5367. device const uchar * src01,
  5368. device const uchar * src02,
  5369. device const uchar * src03,
  5370. device const uchar * src04,
  5371. device const uchar * src05,
  5372. device const uchar * src06,
  5373. device const uchar * src07,
  5374. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5375. uint3 tgpig[[threadgroup_position_in_grid]],
  5376. uint tiitg[[thread_index_in_threadgroup]],
  5377. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5378. device const uchar * src0s[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5379. // expert id
  5380. const int32_t id = tgpig.z/(ne12*ne13);
  5381. tgpig.z = tgpig.z%(ne12*ne13);
  5382. // row indices of src1 for expert id
  5383. int64_t _ne1 = 0;
  5384. short src1ids[512];
  5385. for (int64_t i1 = 0; i1 < ne1; i1++) {
  5386. if (((device int32_t *) (ids + i1*nbi1))[idx] == id) {
  5387. src1ids[_ne1++] = i1;
  5388. }
  5389. }
  5390. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  5391. src0s[id],
  5392. src1,
  5393. src1ids,
  5394. dst,
  5395. ne00,
  5396. ne02,
  5397. nb01,
  5398. nb02,
  5399. ne12,
  5400. nb10,
  5401. nb11,
  5402. nb12,
  5403. ne0,
  5404. _ne1,
  5405. r2,
  5406. r3,
  5407. shared_memory,
  5408. tgpig,
  5409. tiitg,
  5410. sgitg);
  5411. }
  5412. #if QK_K == 256
  5413. #define QK_NL 16
  5414. #else
  5415. #define QK_NL 4
  5416. #endif
  5417. //
  5418. // get rows
  5419. //
  5420. typedef void (get_rows_t)(
  5421. device const void * src0,
  5422. device const char * src1,
  5423. device float * dst,
  5424. constant int64_t & ne00,
  5425. constant uint64_t & nb01,
  5426. constant uint64_t & nb02,
  5427. constant int64_t & ne10,
  5428. constant uint64_t & nb10,
  5429. constant uint64_t & nb11,
  5430. constant uint64_t & nb1,
  5431. constant uint64_t & nb2,
  5432. uint3, uint, uint3);
  5433. //template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  5434. //template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  5435. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  5436. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  5437. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  5438. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  5439. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  5440. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  5441. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  5442. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  5443. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  5444. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  5445. template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_t kernel_get_rows<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5446. template [[host_name("kernel_get_rows_iq2_xs")]] kernel get_rows_t kernel_get_rows<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5447. template [[host_name("kernel_get_rows_iq3_xxs")]] kernel get_rows_t kernel_get_rows<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5448. template [[host_name("kernel_get_rows_iq3_s")]] kernel get_rows_t kernel_get_rows<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5449. template [[host_name("kernel_get_rows_iq2_s")]] kernel get_rows_t kernel_get_rows<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5450. template [[host_name("kernel_get_rows_iq1_s")]] kernel get_rows_t kernel_get_rows<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5451. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_t kernel_get_rows<block_iq4_nl, 2, dequantize_iq4_nl>;
  5452. #if QK_K == 64
  5453. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_t kernel_get_rows<block_iq4_xs, 2, dequantize_iq4_xs>;
  5454. #else
  5455. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_t kernel_get_rows<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5456. #endif
  5457. //
  5458. // matrix-matrix multiplication
  5459. //
  5460. typedef void (mat_mm_t)(
  5461. device const uchar * src0,
  5462. device const uchar * src1,
  5463. device float * dst,
  5464. constant int64_t & ne00,
  5465. constant int64_t & ne02,
  5466. constant uint64_t & nb01,
  5467. constant uint64_t & nb02,
  5468. constant int64_t & ne12,
  5469. constant uint64_t & nb10,
  5470. constant uint64_t & nb11,
  5471. constant uint64_t & nb12,
  5472. constant int64_t & ne0,
  5473. constant int64_t & ne1,
  5474. constant uint & r2,
  5475. constant uint & r3,
  5476. threadgroup uchar *,
  5477. uint3, uint, uint);
  5478. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  5479. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  5480. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  5481. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  5482. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  5483. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  5484. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  5485. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  5486. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  5487. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  5488. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  5489. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;
  5490. template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5491. template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5492. template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5493. template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5494. template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5495. template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5496. template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_nl>;
  5497. #if QK_K == 64
  5498. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_xs>;
  5499. #else
  5500. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5501. #endif
  5502. //
  5503. // indirect matrix-matrix multiplication
  5504. //
  5505. typedef void (mat_mm_id_t)(
  5506. device const uchar * ids,
  5507. device const uchar * src1,
  5508. device float * dst,
  5509. constant uint64_t & nbi1,
  5510. constant int64_t & ne00,
  5511. constant int64_t & ne02,
  5512. constant uint64_t & nb01,
  5513. constant uint64_t & nb02,
  5514. constant int64_t & ne12,
  5515. constant int64_t & ne13,
  5516. constant uint64_t & nb10,
  5517. constant uint64_t & nb11,
  5518. constant uint64_t & nb12,
  5519. constant int64_t & ne0,
  5520. constant int64_t & ne1,
  5521. constant uint64_t & nb1,
  5522. constant uint & r2,
  5523. constant uint & r3,
  5524. constant int & idx,
  5525. device const uchar * src00,
  5526. device const uchar * src01,
  5527. device const uchar * src02,
  5528. device const uchar * src03,
  5529. device const uchar * src04,
  5530. device const uchar * src05,
  5531. device const uchar * src06,
  5532. device const uchar * src07,
  5533. threadgroup uchar *,
  5534. uint3, uint, uint);
  5535. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  5536. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  5537. 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>;
  5538. 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>;
  5539. 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>;
  5540. 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>;
  5541. 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>;
  5542. 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>;
  5543. 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>;
  5544. 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>;
  5545. 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>;
  5546. 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>;
  5547. 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>;
  5548. 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>;
  5549. 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>;
  5550. 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>;
  5551. 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>;
  5552. 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>;
  5553. 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>;
  5554. #if QK_K == 64
  5555. template [[host_name("kernel_mul_mm_id_iq4_xs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq4_xs, 2, dequantize_iq4_xs>;
  5556. #else
  5557. 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>;
  5558. #endif
  5559. //
  5560. // matrix-vector multiplication
  5561. //
  5562. [[host_name("kernel_mul_mv_id_f32_f32")]]
  5563. kernel void kernel_mul_mv_id_f32_f32(
  5564. device const char * ids,
  5565. device const char * src1,
  5566. device float * dst,
  5567. constant uint64_t & nbi1,
  5568. constant int64_t & ne00,
  5569. constant int64_t & ne01,
  5570. constant int64_t & ne02,
  5571. constant uint64_t & nb00,
  5572. constant uint64_t & nb01,
  5573. constant uint64_t & nb02,
  5574. constant int64_t & ne10,
  5575. constant int64_t & ne11,
  5576. constant int64_t & ne12,
  5577. constant int64_t & ne13,
  5578. constant uint64_t & nb10,
  5579. constant uint64_t & nb11,
  5580. constant uint64_t & nb12,
  5581. constant int64_t & ne0,
  5582. constant int64_t & ne1,
  5583. constant uint64_t & nb1,
  5584. constant uint & r2,
  5585. constant uint & r3,
  5586. constant int & idx,
  5587. device const char * src00,
  5588. device const char * src01,
  5589. device const char * src02,
  5590. device const char * src03,
  5591. device const char * src04,
  5592. device const char * src05,
  5593. device const char * src06,
  5594. device const char * src07,
  5595. uint3 tgpig[[threadgroup_position_in_grid]],
  5596. uint tiitg[[thread_index_in_threadgroup]],
  5597. uint tiisg[[thread_index_in_simdgroup]],
  5598. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5599. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5600. const int64_t bid = tgpig.z/(ne12*ne13);
  5601. tgpig.z = tgpig.z%(ne12*ne13);
  5602. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5603. kernel_mul_mv_f32_f32_impl(
  5604. src0[id],
  5605. src1 + bid*nb11,
  5606. dst + bid*ne0,
  5607. ne00,
  5608. ne01,
  5609. ne02,
  5610. nb00,
  5611. nb01,
  5612. nb02,
  5613. ne10,
  5614. ne11,
  5615. ne12,
  5616. nb10,
  5617. nb11,
  5618. nb12,
  5619. ne0,
  5620. ne1,
  5621. r2,
  5622. r3,
  5623. tgpig,
  5624. tiisg);
  5625. }
  5626. [[host_name("kernel_mul_mv_id_f16_f32")]]
  5627. kernel void kernel_mul_mv_id_f16_f32(
  5628. device const char * ids,
  5629. device const char * src1,
  5630. device float * dst,
  5631. constant uint64_t & nbi1,
  5632. constant int64_t & ne00,
  5633. constant int64_t & ne01,
  5634. constant int64_t & ne02,
  5635. constant uint64_t & nb00,
  5636. constant uint64_t & nb01,
  5637. constant uint64_t & nb02,
  5638. constant int64_t & ne10,
  5639. constant int64_t & ne11,
  5640. constant int64_t & ne12,
  5641. constant int64_t & ne13,
  5642. constant uint64_t & nb10,
  5643. constant uint64_t & nb11,
  5644. constant uint64_t & nb12,
  5645. constant int64_t & ne0,
  5646. constant int64_t & ne1,
  5647. constant uint64_t & nb1,
  5648. constant uint & r2,
  5649. constant uint & r3,
  5650. constant int & idx,
  5651. device const char * src00,
  5652. device const char * src01,
  5653. device const char * src02,
  5654. device const char * src03,
  5655. device const char * src04,
  5656. device const char * src05,
  5657. device const char * src06,
  5658. device const char * src07,
  5659. uint3 tgpig[[threadgroup_position_in_grid]],
  5660. uint tiitg[[thread_index_in_threadgroup]],
  5661. uint tiisg[[thread_index_in_simdgroup]],
  5662. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5663. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5664. const int64_t bid = tgpig.z/(ne12*ne13);
  5665. tgpig.z = tgpig.z%(ne12*ne13);
  5666. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5667. kernel_mul_mv_f16_f32_impl(
  5668. src0[id],
  5669. src1 + bid*nb11,
  5670. dst + bid*ne0,
  5671. ne00,
  5672. ne01,
  5673. ne02,
  5674. nb00,
  5675. nb01,
  5676. nb02,
  5677. ne10,
  5678. ne11,
  5679. ne12,
  5680. nb10,
  5681. nb11,
  5682. nb12,
  5683. ne0,
  5684. ne1,
  5685. r2,
  5686. r3,
  5687. tgpig,
  5688. tiisg);
  5689. }
  5690. [[host_name("kernel_mul_mv_id_q8_0_f32")]]
  5691. kernel void kernel_mul_mv_id_q8_0_f32(
  5692. device const char * ids,
  5693. device const char * src1,
  5694. device float * dst,
  5695. constant uint64_t & nbi1,
  5696. constant int64_t & ne00,
  5697. constant int64_t & ne01,
  5698. constant int64_t & ne02,
  5699. constant uint64_t & nb00,
  5700. constant uint64_t & nb01,
  5701. constant uint64_t & nb02,
  5702. constant int64_t & ne10,
  5703. constant int64_t & ne11,
  5704. constant int64_t & ne12,
  5705. constant int64_t & ne13,
  5706. constant uint64_t & nb10,
  5707. constant uint64_t & nb11,
  5708. constant uint64_t & nb12,
  5709. constant int64_t & ne0,
  5710. constant int64_t & ne1,
  5711. constant uint64_t & nb1,
  5712. constant uint & r2,
  5713. constant uint & r3,
  5714. constant int & idx,
  5715. device const char * src00,
  5716. device const char * src01,
  5717. device const char * src02,
  5718. device const char * src03,
  5719. device const char * src04,
  5720. device const char * src05,
  5721. device const char * src06,
  5722. device const char * src07,
  5723. uint3 tgpig[[threadgroup_position_in_grid]],
  5724. uint tiitg[[thread_index_in_threadgroup]],
  5725. uint tiisg[[thread_index_in_simdgroup]],
  5726. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5727. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5728. const int64_t bid = tgpig.z/(ne12*ne13);
  5729. tgpig.z = tgpig.z%(ne12*ne13);
  5730. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5731. kernel_mul_mv_q8_0_f32_impl(
  5732. src0[id],
  5733. (device const float *) (src1 + bid*nb11),
  5734. dst + bid*ne0,
  5735. ne00,
  5736. ne01,
  5737. ne02,
  5738. ne10,
  5739. ne12,
  5740. ne0,
  5741. ne1,
  5742. r2,
  5743. r3,
  5744. tgpig,
  5745. tiisg,
  5746. sgitg);
  5747. }
  5748. [[host_name("kernel_mul_mv_id_q4_0_f32")]]
  5749. kernel void kernel_mul_mv_id_q4_0_f32(
  5750. device const char * ids,
  5751. device const char * src1,
  5752. device float * dst,
  5753. constant uint64_t & nbi1,
  5754. constant int64_t & ne00,
  5755. constant int64_t & ne01,
  5756. constant int64_t & ne02,
  5757. constant uint64_t & nb00,
  5758. constant uint64_t & nb01,
  5759. constant uint64_t & nb02,
  5760. constant int64_t & ne10,
  5761. constant int64_t & ne11,
  5762. constant int64_t & ne12,
  5763. constant int64_t & ne13,
  5764. constant uint64_t & nb10,
  5765. constant uint64_t & nb11,
  5766. constant uint64_t & nb12,
  5767. constant int64_t & ne0,
  5768. constant int64_t & ne1,
  5769. constant uint64_t & nb1,
  5770. constant uint & r2,
  5771. constant uint & r3,
  5772. constant int & idx,
  5773. device const char * src00,
  5774. device const char * src01,
  5775. device const char * src02,
  5776. device const char * src03,
  5777. device const char * src04,
  5778. device const char * src05,
  5779. device const char * src06,
  5780. device const char * src07,
  5781. uint3 tgpig[[threadgroup_position_in_grid]],
  5782. uint tiitg[[thread_index_in_threadgroup]],
  5783. uint tiisg[[thread_index_in_simdgroup]],
  5784. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5785. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5786. const int64_t bid = tgpig.z/(ne12*ne13);
  5787. tgpig.z = tgpig.z%(ne12*ne13);
  5788. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5789. mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5790. src0[id],
  5791. (device const float *) (src1 + bid*nb11),
  5792. dst + bid*ne0,
  5793. ne00,
  5794. ne01,
  5795. ne02,
  5796. ne10,
  5797. ne12,
  5798. ne0,
  5799. ne1,
  5800. r2,
  5801. r3,
  5802. tgpig,
  5803. tiisg,
  5804. sgitg);
  5805. }
  5806. [[host_name("kernel_mul_mv_id_q4_1_f32")]]
  5807. kernel void kernel_mul_mv_id_q4_1_f32(
  5808. device const char * ids,
  5809. device const char * src1,
  5810. device float * dst,
  5811. constant uint64_t & nbi1,
  5812. constant int64_t & ne00,
  5813. constant int64_t & ne01,
  5814. constant int64_t & ne02,
  5815. constant uint64_t & nb00,
  5816. constant uint64_t & nb01,
  5817. constant uint64_t & nb02,
  5818. constant int64_t & ne10,
  5819. constant int64_t & ne11,
  5820. constant int64_t & ne12,
  5821. constant int64_t & ne13,
  5822. constant uint64_t & nb10,
  5823. constant uint64_t & nb11,
  5824. constant uint64_t & nb12,
  5825. constant int64_t & ne0,
  5826. constant int64_t & ne1,
  5827. constant uint64_t & nb1,
  5828. constant uint & r2,
  5829. constant uint & r3,
  5830. constant int & idx,
  5831. device const char * src00,
  5832. device const char * src01,
  5833. device const char * src02,
  5834. device const char * src03,
  5835. device const char * src04,
  5836. device const char * src05,
  5837. device const char * src06,
  5838. device const char * src07,
  5839. uint3 tgpig[[threadgroup_position_in_grid]],
  5840. uint tiitg[[thread_index_in_threadgroup]],
  5841. uint tiisg[[thread_index_in_simdgroup]],
  5842. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5843. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5844. const int64_t bid = tgpig.z/(ne12*ne13);
  5845. tgpig.z = tgpig.z%(ne12*ne13);
  5846. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5847. mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5848. src0[id],
  5849. (device const float *) (src1 + bid*nb11),
  5850. dst + bid*ne0,
  5851. ne00,
  5852. ne01,
  5853. ne02,
  5854. ne10,
  5855. ne12,
  5856. ne0,
  5857. ne1,
  5858. r2,
  5859. r3,
  5860. tgpig,
  5861. tiisg,
  5862. sgitg);
  5863. }
  5864. [[host_name("kernel_mul_mv_id_q5_0_f32")]]
  5865. kernel void kernel_mul_mv_id_q5_0_f32(
  5866. device const char * ids,
  5867. device const char * src1,
  5868. device float * dst,
  5869. constant uint64_t & nbi1,
  5870. constant int64_t & ne00,
  5871. constant int64_t & ne01,
  5872. constant int64_t & ne02,
  5873. constant uint64_t & nb00,
  5874. constant uint64_t & nb01,
  5875. constant uint64_t & nb02,
  5876. constant int64_t & ne10,
  5877. constant int64_t & ne11,
  5878. constant int64_t & ne12,
  5879. constant int64_t & ne13,
  5880. constant uint64_t & nb10,
  5881. constant uint64_t & nb11,
  5882. constant uint64_t & nb12,
  5883. constant int64_t & ne0,
  5884. constant int64_t & ne1,
  5885. constant uint64_t & nb1,
  5886. constant uint & r2,
  5887. constant uint & r3,
  5888. constant int & idx,
  5889. device const char * src00,
  5890. device const char * src01,
  5891. device const char * src02,
  5892. device const char * src03,
  5893. device const char * src04,
  5894. device const char * src05,
  5895. device const char * src06,
  5896. device const char * src07,
  5897. uint3 tgpig[[threadgroup_position_in_grid]],
  5898. uint tiitg[[thread_index_in_threadgroup]],
  5899. uint tiisg[[thread_index_in_simdgroup]],
  5900. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5901. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5902. const int64_t bid = tgpig.z/(ne12*ne13);
  5903. tgpig.z = tgpig.z%(ne12*ne13);
  5904. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5905. mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5906. src0[id],
  5907. (device const float *) (src1 + bid*nb11),
  5908. dst + bid*ne0,
  5909. ne00,
  5910. ne01,
  5911. ne02,
  5912. ne10,
  5913. ne12,
  5914. ne0,
  5915. ne1,
  5916. r2,
  5917. r3,
  5918. tgpig,
  5919. tiisg,
  5920. sgitg);
  5921. }
  5922. [[host_name("kernel_mul_mv_id_q5_1_f32")]]
  5923. kernel void kernel_mul_mv_id_q5_1_f32(
  5924. device const char * ids,
  5925. device const char * src1,
  5926. device float * dst,
  5927. constant uint64_t & nbi1,
  5928. constant int64_t & ne00,
  5929. constant int64_t & ne01,
  5930. constant int64_t & ne02,
  5931. constant uint64_t & nb00,
  5932. constant uint64_t & nb01,
  5933. constant uint64_t & nb02,
  5934. constant int64_t & ne10,
  5935. constant int64_t & ne11,
  5936. constant int64_t & ne12,
  5937. constant int64_t & ne13,
  5938. constant uint64_t & nb10,
  5939. constant uint64_t & nb11,
  5940. constant uint64_t & nb12,
  5941. constant int64_t & ne0,
  5942. constant int64_t & ne1,
  5943. constant uint64_t & nb1,
  5944. constant uint & r2,
  5945. constant uint & r3,
  5946. constant int & idx,
  5947. device const char * src00,
  5948. device const char * src01,
  5949. device const char * src02,
  5950. device const char * src03,
  5951. device const char * src04,
  5952. device const char * src05,
  5953. device const char * src06,
  5954. device const char * src07,
  5955. uint3 tgpig[[threadgroup_position_in_grid]],
  5956. uint tiitg[[thread_index_in_threadgroup]],
  5957. uint tiisg[[thread_index_in_simdgroup]],
  5958. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5959. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5960. const int64_t bid = tgpig.z/(ne12*ne13);
  5961. tgpig.z = tgpig.z%(ne12*ne13);
  5962. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5963. mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5964. src0[id],
  5965. (device const float *) (src1 + bid*nb11),
  5966. dst + bid*ne0,
  5967. ne00,
  5968. ne01,
  5969. ne02,
  5970. ne10,
  5971. ne12,
  5972. ne0,
  5973. ne1,
  5974. r2,
  5975. r3,
  5976. tgpig,
  5977. tiisg,
  5978. sgitg);
  5979. }
  5980. [[host_name("kernel_mul_mv_id_q2_K_f32")]]
  5981. kernel void kernel_mul_mv_id_q2_K_f32(
  5982. device const char * ids,
  5983. device const char * src1,
  5984. device float * dst,
  5985. constant uint64_t & nbi1,
  5986. constant int64_t & ne00,
  5987. constant int64_t & ne01,
  5988. constant int64_t & ne02,
  5989. constant uint64_t & nb00,
  5990. constant uint64_t & nb01,
  5991. constant uint64_t & nb02,
  5992. constant int64_t & ne10,
  5993. constant int64_t & ne11,
  5994. constant int64_t & ne12,
  5995. constant int64_t & ne13,
  5996. constant uint64_t & nb10,
  5997. constant uint64_t & nb11,
  5998. constant uint64_t & nb12,
  5999. constant int64_t & ne0,
  6000. constant int64_t & ne1,
  6001. constant uint64_t & nb1,
  6002. constant uint & r2,
  6003. constant uint & r3,
  6004. constant int & idx,
  6005. device const char * src00,
  6006. device const char * src01,
  6007. device const char * src02,
  6008. device const char * src03,
  6009. device const char * src04,
  6010. device const char * src05,
  6011. device const char * src06,
  6012. device const char * src07,
  6013. uint3 tgpig[[threadgroup_position_in_grid]],
  6014. uint tiitg[[thread_index_in_threadgroup]],
  6015. uint tiisg[[thread_index_in_simdgroup]],
  6016. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6017. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6018. const int64_t bid = tgpig.z/(ne12*ne13);
  6019. tgpig.z = tgpig.z%(ne12*ne13);
  6020. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6021. kernel_mul_mv_q2_K_f32_impl(
  6022. src0[id],
  6023. (device const float *) (src1 + bid*nb11),
  6024. dst + bid*ne0,
  6025. ne00,
  6026. ne01,
  6027. ne02,
  6028. ne10,
  6029. ne12,
  6030. ne0,
  6031. ne1,
  6032. r2,
  6033. r3,
  6034. tgpig,
  6035. tiisg,
  6036. sgitg);
  6037. }
  6038. [[host_name("kernel_mul_mv_id_q3_K_f32")]]
  6039. kernel void kernel_mul_mv_id_q3_K_f32(
  6040. device const char * ids,
  6041. device const char * src1,
  6042. device float * dst,
  6043. constant uint64_t & nbi1,
  6044. constant int64_t & ne00,
  6045. constant int64_t & ne01,
  6046. constant int64_t & ne02,
  6047. constant uint64_t & nb00,
  6048. constant uint64_t & nb01,
  6049. constant uint64_t & nb02,
  6050. constant int64_t & ne10,
  6051. constant int64_t & ne11,
  6052. constant int64_t & ne12,
  6053. constant int64_t & ne13,
  6054. constant uint64_t & nb10,
  6055. constant uint64_t & nb11,
  6056. constant uint64_t & nb12,
  6057. constant int64_t & ne0,
  6058. constant int64_t & ne1,
  6059. constant uint64_t & nb1,
  6060. constant uint & r2,
  6061. constant uint & r3,
  6062. constant int & idx,
  6063. device const char * src00,
  6064. device const char * src01,
  6065. device const char * src02,
  6066. device const char * src03,
  6067. device const char * src04,
  6068. device const char * src05,
  6069. device const char * src06,
  6070. device const char * src07,
  6071. uint3 tgpig[[threadgroup_position_in_grid]],
  6072. uint tiitg[[thread_index_in_threadgroup]],
  6073. uint tiisg[[thread_index_in_simdgroup]],
  6074. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6075. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6076. const int64_t bid = tgpig.z/(ne12*ne13);
  6077. tgpig.z = tgpig.z%(ne12*ne13);
  6078. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6079. kernel_mul_mv_q3_K_f32_impl(
  6080. src0[id],
  6081. (device const float *) (src1 + bid*nb11),
  6082. dst + bid*ne0,
  6083. ne00,
  6084. ne01,
  6085. ne02,
  6086. ne10,
  6087. ne12,
  6088. ne0,
  6089. ne1,
  6090. r2,
  6091. r3,
  6092. tgpig,
  6093. tiisg,
  6094. sgitg);
  6095. }
  6096. [[host_name("kernel_mul_mv_id_q4_K_f32")]]
  6097. kernel void kernel_mul_mv_id_q4_K_f32(
  6098. device const char * ids,
  6099. device const char * src1,
  6100. device float * dst,
  6101. constant uint64_t & nbi1,
  6102. constant int64_t & ne00,
  6103. constant int64_t & ne01,
  6104. constant int64_t & ne02,
  6105. constant uint64_t & nb00,
  6106. constant uint64_t & nb01,
  6107. constant uint64_t & nb02,
  6108. constant int64_t & ne10,
  6109. constant int64_t & ne11,
  6110. constant int64_t & ne12,
  6111. constant int64_t & ne13,
  6112. constant uint64_t & nb10,
  6113. constant uint64_t & nb11,
  6114. constant uint64_t & nb12,
  6115. constant int64_t & ne0,
  6116. constant int64_t & ne1,
  6117. constant uint64_t & nb1,
  6118. constant uint & r2,
  6119. constant uint & r3,
  6120. constant int & idx,
  6121. device const char * src00,
  6122. device const char * src01,
  6123. device const char * src02,
  6124. device const char * src03,
  6125. device const char * src04,
  6126. device const char * src05,
  6127. device const char * src06,
  6128. device const char * src07,
  6129. uint3 tgpig[[threadgroup_position_in_grid]],
  6130. uint tiitg[[thread_index_in_threadgroup]],
  6131. uint tiisg[[thread_index_in_simdgroup]],
  6132. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6133. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6134. const int64_t bid = tgpig.z/(ne12*ne13);
  6135. tgpig.z = tgpig.z%(ne12*ne13);
  6136. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6137. kernel_mul_mv_q4_K_f32_impl(
  6138. src0[id],
  6139. (device const float *) (src1 + bid*nb11),
  6140. dst + bid*ne0,
  6141. ne00,
  6142. ne01,
  6143. ne02,
  6144. ne10,
  6145. ne12,
  6146. ne0,
  6147. ne1,
  6148. r2,
  6149. r3,
  6150. tgpig,
  6151. tiisg,
  6152. sgitg);
  6153. }
  6154. [[host_name("kernel_mul_mv_id_q5_K_f32")]]
  6155. kernel void kernel_mul_mv_id_q5_K_f32(
  6156. device const char * ids,
  6157. device const char * src1,
  6158. device float * dst,
  6159. constant uint64_t & nbi1,
  6160. constant int64_t & ne00,
  6161. constant int64_t & ne01,
  6162. constant int64_t & ne02,
  6163. constant uint64_t & nb00,
  6164. constant uint64_t & nb01,
  6165. constant uint64_t & nb02,
  6166. constant int64_t & ne10,
  6167. constant int64_t & ne11,
  6168. constant int64_t & ne12,
  6169. constant int64_t & ne13,
  6170. constant uint64_t & nb10,
  6171. constant uint64_t & nb11,
  6172. constant uint64_t & nb12,
  6173. constant int64_t & ne0,
  6174. constant int64_t & ne1,
  6175. constant uint64_t & nb1,
  6176. constant uint & r2,
  6177. constant uint & r3,
  6178. constant int & idx,
  6179. device const char * src00,
  6180. device const char * src01,
  6181. device const char * src02,
  6182. device const char * src03,
  6183. device const char * src04,
  6184. device const char * src05,
  6185. device const char * src06,
  6186. device const char * src07,
  6187. uint3 tgpig[[threadgroup_position_in_grid]],
  6188. uint tiitg[[thread_index_in_threadgroup]],
  6189. uint tiisg[[thread_index_in_simdgroup]],
  6190. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6191. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6192. const int64_t bid = tgpig.z/(ne12*ne13);
  6193. tgpig.z = tgpig.z%(ne12*ne13);
  6194. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6195. kernel_mul_mv_q5_K_f32_impl(
  6196. src0[id],
  6197. (device const float *) (src1 + bid*nb11),
  6198. dst + bid*ne0,
  6199. ne00,
  6200. ne01,
  6201. ne02,
  6202. ne10,
  6203. ne12,
  6204. ne0,
  6205. ne1,
  6206. r2,
  6207. r3,
  6208. tgpig,
  6209. tiisg,
  6210. sgitg);
  6211. }
  6212. [[host_name("kernel_mul_mv_id_q6_K_f32")]]
  6213. kernel void kernel_mul_mv_id_q6_K_f32(
  6214. device const char * ids,
  6215. device const char * src1,
  6216. device float * dst,
  6217. constant uint64_t & nbi1,
  6218. constant int64_t & ne00,
  6219. constant int64_t & ne01,
  6220. constant int64_t & ne02,
  6221. constant uint64_t & nb00,
  6222. constant uint64_t & nb01,
  6223. constant uint64_t & nb02,
  6224. constant int64_t & ne10,
  6225. constant int64_t & ne11,
  6226. constant int64_t & ne12,
  6227. constant int64_t & ne13,
  6228. constant uint64_t & nb10,
  6229. constant uint64_t & nb11,
  6230. constant uint64_t & nb12,
  6231. constant int64_t & ne0,
  6232. constant int64_t & ne1,
  6233. constant uint64_t & nb1,
  6234. constant uint & r2,
  6235. constant uint & r3,
  6236. constant int & idx,
  6237. device const char * src00,
  6238. device const char * src01,
  6239. device const char * src02,
  6240. device const char * src03,
  6241. device const char * src04,
  6242. device const char * src05,
  6243. device const char * src06,
  6244. device const char * src07,
  6245. uint3 tgpig[[threadgroup_position_in_grid]],
  6246. uint tiitg[[thread_index_in_threadgroup]],
  6247. uint tiisg[[thread_index_in_simdgroup]],
  6248. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6249. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6250. const int64_t bid = tgpig.z/(ne12*ne13);
  6251. tgpig.z = tgpig.z%(ne12*ne13);
  6252. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6253. kernel_mul_mv_q6_K_f32_impl(
  6254. src0[id],
  6255. (device const float *) (src1 + bid*nb11),
  6256. dst + bid*ne0,
  6257. ne00,
  6258. ne01,
  6259. ne02,
  6260. ne10,
  6261. ne12,
  6262. ne0,
  6263. ne1,
  6264. r2,
  6265. r3,
  6266. tgpig,
  6267. tiisg,
  6268. sgitg);
  6269. }
  6270. [[host_name("kernel_mul_mv_id_iq2_xxs_f32")]]
  6271. kernel void kernel_mul_mv_id_iq2_xxs_f32(
  6272. device const char * ids,
  6273. device const char * src1,
  6274. device float * dst,
  6275. constant uint64_t & nbi1,
  6276. constant int64_t & ne00,
  6277. constant int64_t & ne01,
  6278. constant int64_t & ne02,
  6279. constant uint64_t & nb00,
  6280. constant uint64_t & nb01,
  6281. constant uint64_t & nb02,
  6282. constant int64_t & ne10,
  6283. constant int64_t & ne11,
  6284. constant int64_t & ne12,
  6285. constant int64_t & ne13,
  6286. constant uint64_t & nb10,
  6287. constant uint64_t & nb11,
  6288. constant uint64_t & nb12,
  6289. constant int64_t & ne0,
  6290. constant int64_t & ne1,
  6291. constant uint64_t & nb1,
  6292. constant uint & r2,
  6293. constant uint & r3,
  6294. constant int & idx,
  6295. device const char * src00,
  6296. device const char * src01,
  6297. device const char * src02,
  6298. device const char * src03,
  6299. device const char * src04,
  6300. device const char * src05,
  6301. device const char * src06,
  6302. device const char * src07,
  6303. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6304. uint3 tgpig[[threadgroup_position_in_grid]],
  6305. uint tiitg[[thread_index_in_threadgroup]],
  6306. uint tiisg[[thread_index_in_simdgroup]],
  6307. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6308. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6309. const int64_t bid = tgpig.z/(ne12*ne13);
  6310. tgpig.z = tgpig.z%(ne12*ne13);
  6311. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6312. kernel_mul_mv_iq2_xxs_f32_impl(
  6313. src0[id],
  6314. (device const float *) (src1 + bid*nb11),
  6315. dst + bid*ne0,
  6316. ne00,
  6317. ne01,
  6318. ne02,
  6319. ne10,
  6320. ne12,
  6321. ne0,
  6322. ne1,
  6323. r2,
  6324. r3,
  6325. shared_values,
  6326. tgpig,
  6327. tiisg,
  6328. sgitg);
  6329. }
  6330. [[host_name("kernel_mul_mv_id_iq2_xs_f32")]]
  6331. kernel void kernel_mul_mv_id_iq2_xs_f32(
  6332. device const char * ids,
  6333. device const char * src1,
  6334. device float * dst,
  6335. constant uint64_t & nbi1,
  6336. constant int64_t & ne00,
  6337. constant int64_t & ne01,
  6338. constant int64_t & ne02,
  6339. constant uint64_t & nb00,
  6340. constant uint64_t & nb01,
  6341. constant uint64_t & nb02,
  6342. constant int64_t & ne10,
  6343. constant int64_t & ne11,
  6344. constant int64_t & ne12,
  6345. constant int64_t & ne13,
  6346. constant uint64_t & nb10,
  6347. constant uint64_t & nb11,
  6348. constant uint64_t & nb12,
  6349. constant int64_t & ne0,
  6350. constant int64_t & ne1,
  6351. constant uint64_t & nb1,
  6352. constant uint & r2,
  6353. constant uint & r3,
  6354. constant int & idx,
  6355. device const char * src00,
  6356. device const char * src01,
  6357. device const char * src02,
  6358. device const char * src03,
  6359. device const char * src04,
  6360. device const char * src05,
  6361. device const char * src06,
  6362. device const char * src07,
  6363. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6364. uint3 tgpig[[threadgroup_position_in_grid]],
  6365. uint tiitg[[thread_index_in_threadgroup]],
  6366. uint tiisg[[thread_index_in_simdgroup]],
  6367. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6368. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6369. const int64_t bid = tgpig.z/(ne12*ne13);
  6370. tgpig.z = tgpig.z%(ne12*ne13);
  6371. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6372. kernel_mul_mv_iq2_xs_f32_impl(
  6373. src0[id],
  6374. (device const float *) (src1 + bid*nb11),
  6375. dst + bid*ne0,
  6376. ne00,
  6377. ne01,
  6378. ne02,
  6379. ne10,
  6380. ne12,
  6381. ne0,
  6382. ne1,
  6383. r2,
  6384. r3,
  6385. shared_values,
  6386. tgpig,
  6387. tiisg,
  6388. sgitg);
  6389. }
  6390. [[host_name("kernel_mul_mv_id_iq3_xxs_f32")]]
  6391. kernel void kernel_mul_mv_id_iq3_xxs_f32(
  6392. device const char * ids,
  6393. device const char * src1,
  6394. device float * dst,
  6395. constant uint64_t & nbi1,
  6396. constant int64_t & ne00,
  6397. constant int64_t & ne01,
  6398. constant int64_t & ne02,
  6399. constant uint64_t & nb00,
  6400. constant uint64_t & nb01,
  6401. constant uint64_t & nb02,
  6402. constant int64_t & ne10,
  6403. constant int64_t & ne11,
  6404. constant int64_t & ne12,
  6405. constant int64_t & ne13,
  6406. constant uint64_t & nb10,
  6407. constant uint64_t & nb11,
  6408. constant uint64_t & nb12,
  6409. constant int64_t & ne0,
  6410. constant int64_t & ne1,
  6411. constant uint64_t & nb1,
  6412. constant uint & r2,
  6413. constant uint & r3,
  6414. constant int & idx,
  6415. device const char * src00,
  6416. device const char * src01,
  6417. device const char * src02,
  6418. device const char * src03,
  6419. device const char * src04,
  6420. device const char * src05,
  6421. device const char * src06,
  6422. device const char * src07,
  6423. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6424. uint3 tgpig[[threadgroup_position_in_grid]],
  6425. uint tiitg[[thread_index_in_threadgroup]],
  6426. uint tiisg[[thread_index_in_simdgroup]],
  6427. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6428. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6429. const int64_t bid = tgpig.z/(ne12*ne13);
  6430. tgpig.z = tgpig.z%(ne12*ne13);
  6431. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6432. kernel_mul_mv_iq3_xxs_f32_impl(
  6433. src0[id],
  6434. (device const float *) (src1 + bid*nb11),
  6435. dst + bid*ne0,
  6436. ne00,
  6437. ne01,
  6438. ne02,
  6439. ne10,
  6440. ne12,
  6441. ne0,
  6442. ne1,
  6443. r2,
  6444. r3,
  6445. shared_values,
  6446. tgpig,
  6447. tiisg,
  6448. sgitg);
  6449. }
  6450. [[host_name("kernel_mul_mv_id_iq3_s_f32")]]
  6451. kernel void kernel_mul_mv_id_iq3_s_f32(
  6452. device const char * ids,
  6453. device const char * src1,
  6454. device float * dst,
  6455. constant uint64_t & nbi1,
  6456. constant int64_t & ne00,
  6457. constant int64_t & ne01,
  6458. constant int64_t & ne02,
  6459. constant uint64_t & nb00,
  6460. constant uint64_t & nb01,
  6461. constant uint64_t & nb02,
  6462. constant int64_t & ne10,
  6463. constant int64_t & ne11,
  6464. constant int64_t & ne12,
  6465. constant int64_t & ne13,
  6466. constant uint64_t & nb10,
  6467. constant uint64_t & nb11,
  6468. constant uint64_t & nb12,
  6469. constant int64_t & ne0,
  6470. constant int64_t & ne1,
  6471. constant uint64_t & nb1,
  6472. constant uint & r2,
  6473. constant uint & r3,
  6474. constant int & idx,
  6475. device const char * src00,
  6476. device const char * src01,
  6477. device const char * src02,
  6478. device const char * src03,
  6479. device const char * src04,
  6480. device const char * src05,
  6481. device const char * src06,
  6482. device const char * src07,
  6483. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6484. uint3 tgpig[[threadgroup_position_in_grid]],
  6485. uint tiitg[[thread_index_in_threadgroup]],
  6486. uint tiisg[[thread_index_in_simdgroup]],
  6487. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6488. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6489. const int64_t bid = tgpig.z/(ne12*ne13);
  6490. tgpig.z = tgpig.z%(ne12*ne13);
  6491. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6492. kernel_mul_mv_iq3_s_f32_impl(
  6493. src0[id],
  6494. (device const float *) (src1 + bid*nb11),
  6495. dst + bid*ne0,
  6496. ne00,
  6497. ne01,
  6498. ne02,
  6499. ne10,
  6500. ne12,
  6501. ne0,
  6502. ne1,
  6503. r2,
  6504. r3,
  6505. shared_values,
  6506. tgpig,
  6507. tiisg,
  6508. sgitg);
  6509. }
  6510. [[host_name("kernel_mul_mv_id_iq2_s_f32")]]
  6511. kernel void kernel_mul_mv_id_iq2_s_f32(
  6512. device const char * ids,
  6513. device const char * src1,
  6514. device float * dst,
  6515. constant uint64_t & nbi1,
  6516. constant int64_t & ne00,
  6517. constant int64_t & ne01,
  6518. constant int64_t & ne02,
  6519. constant uint64_t & nb00,
  6520. constant uint64_t & nb01,
  6521. constant uint64_t & nb02,
  6522. constant int64_t & ne10,
  6523. constant int64_t & ne11,
  6524. constant int64_t & ne12,
  6525. constant int64_t & ne13,
  6526. constant uint64_t & nb10,
  6527. constant uint64_t & nb11,
  6528. constant uint64_t & nb12,
  6529. constant int64_t & ne0,
  6530. constant int64_t & ne1,
  6531. constant uint64_t & nb1,
  6532. constant uint & r2,
  6533. constant uint & r3,
  6534. constant int & idx,
  6535. device const char * src00,
  6536. device const char * src01,
  6537. device const char * src02,
  6538. device const char * src03,
  6539. device const char * src04,
  6540. device const char * src05,
  6541. device const char * src06,
  6542. device const char * src07,
  6543. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6544. uint3 tgpig[[threadgroup_position_in_grid]],
  6545. uint tiitg[[thread_index_in_threadgroup]],
  6546. uint tiisg[[thread_index_in_simdgroup]],
  6547. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6548. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6549. const int64_t bid = tgpig.z/(ne12*ne13);
  6550. tgpig.z = tgpig.z%(ne12*ne13);
  6551. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6552. kernel_mul_mv_iq2_s_f32_impl(
  6553. src0[id],
  6554. (device const float *) (src1 + bid*nb11),
  6555. dst + bid*ne0,
  6556. ne00,
  6557. ne01,
  6558. ne02,
  6559. ne10,
  6560. ne12,
  6561. ne0,
  6562. ne1,
  6563. r2,
  6564. r3,
  6565. shared_values,
  6566. tgpig,
  6567. tiisg,
  6568. sgitg);
  6569. }
  6570. [[host_name("kernel_mul_mv_id_iq1_s_f32")]]
  6571. kernel void kernel_mul_mv_id_iq1_s_f32(
  6572. device const char * ids,
  6573. device const char * src1,
  6574. device float * dst,
  6575. constant uint64_t & nbi1,
  6576. constant int64_t & ne00,
  6577. constant int64_t & ne01,
  6578. constant int64_t & ne02,
  6579. constant uint64_t & nb00,
  6580. constant uint64_t & nb01,
  6581. constant uint64_t & nb02,
  6582. constant int64_t & ne10,
  6583. constant int64_t & ne11,
  6584. constant int64_t & ne12,
  6585. constant int64_t & ne13,
  6586. constant uint64_t & nb10,
  6587. constant uint64_t & nb11,
  6588. constant uint64_t & nb12,
  6589. constant int64_t & ne0,
  6590. constant int64_t & ne1,
  6591. constant uint64_t & nb1,
  6592. constant uint & r2,
  6593. constant uint & r3,
  6594. constant int & idx,
  6595. device const char * src00,
  6596. device const char * src01,
  6597. device const char * src02,
  6598. device const char * src03,
  6599. device const char * src04,
  6600. device const char * src05,
  6601. device const char * src06,
  6602. device const char * src07,
  6603. uint3 tgpig[[threadgroup_position_in_grid]],
  6604. uint tiitg[[thread_index_in_threadgroup]],
  6605. uint tiisg[[thread_index_in_simdgroup]],
  6606. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6607. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6608. const int64_t bid = tgpig.z/(ne12*ne13);
  6609. tgpig.z = tgpig.z%(ne12*ne13);
  6610. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6611. kernel_mul_mv_iq1_s_f32_impl(
  6612. src0[id],
  6613. (device const float *) (src1 + bid*nb11),
  6614. dst + bid*ne0,
  6615. ne00,
  6616. ne01,
  6617. ne02,
  6618. ne10,
  6619. ne12,
  6620. ne0,
  6621. ne1,
  6622. r2,
  6623. r3,
  6624. tgpig,
  6625. tiisg,
  6626. sgitg);
  6627. }
  6628. [[host_name("kernel_mul_mv_id_iq4_nl_f32")]]
  6629. kernel void kernel_mul_mv_id_iq4_nl_f32(
  6630. device const char * ids,
  6631. device const char * src1,
  6632. device float * dst,
  6633. constant uint64_t & nbi1,
  6634. constant int64_t & ne00,
  6635. constant int64_t & ne01,
  6636. constant int64_t & ne02,
  6637. constant uint64_t & nb00,
  6638. constant uint64_t & nb01,
  6639. constant uint64_t & nb02,
  6640. constant int64_t & ne10,
  6641. constant int64_t & ne11,
  6642. constant int64_t & ne12,
  6643. constant int64_t & ne13,
  6644. constant uint64_t & nb10,
  6645. constant uint64_t & nb11,
  6646. constant uint64_t & nb12,
  6647. constant int64_t & ne0,
  6648. constant int64_t & ne1,
  6649. constant uint64_t & nb1,
  6650. constant uint & r2,
  6651. constant uint & r3,
  6652. constant int & idx,
  6653. device const char * src00,
  6654. device const char * src01,
  6655. device const char * src02,
  6656. device const char * src03,
  6657. device const char * src04,
  6658. device const char * src05,
  6659. device const char * src06,
  6660. device const char * src07,
  6661. threadgroup float * shared_values [[threadgroup(0)]],
  6662. uint3 tgpig[[threadgroup_position_in_grid]],
  6663. uint tiitg[[thread_index_in_threadgroup]],
  6664. uint tiisg[[thread_index_in_simdgroup]],
  6665. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6666. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6667. const int64_t bid = tgpig.z/(ne12*ne13);
  6668. tgpig.z = tgpig.z%(ne12*ne13);
  6669. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6670. kernel_mul_mv_iq4_nl_f32_impl(
  6671. src0[id],
  6672. (device const float *) (src1 + bid*nb11),
  6673. dst + bid*ne0,
  6674. ne00,
  6675. ne01,
  6676. ne02,
  6677. ne10,
  6678. ne12,
  6679. ne0,
  6680. ne1,
  6681. r2,
  6682. r3,
  6683. shared_values,
  6684. tgpig,
  6685. tiisg,
  6686. sgitg);
  6687. }
  6688. [[host_name("kernel_mul_mv_id_iq4_xs_f32")]]
  6689. kernel void kernel_mul_mv_id_iq4_xs_f32(
  6690. device const char * ids,
  6691. device const char * src1,
  6692. device float * dst,
  6693. constant uint64_t & nbi1,
  6694. constant int64_t & ne00,
  6695. constant int64_t & ne01,
  6696. constant int64_t & ne02,
  6697. constant uint64_t & nb00,
  6698. constant uint64_t & nb01,
  6699. constant uint64_t & nb02,
  6700. constant int64_t & ne10,
  6701. constant int64_t & ne11,
  6702. constant int64_t & ne12,
  6703. constant int64_t & ne13,
  6704. constant uint64_t & nb10,
  6705. constant uint64_t & nb11,
  6706. constant uint64_t & nb12,
  6707. constant int64_t & ne0,
  6708. constant int64_t & ne1,
  6709. constant uint64_t & nb1,
  6710. constant uint & r2,
  6711. constant uint & r3,
  6712. constant int & idx,
  6713. device const char * src00,
  6714. device const char * src01,
  6715. device const char * src02,
  6716. device const char * src03,
  6717. device const char * src04,
  6718. device const char * src05,
  6719. device const char * src06,
  6720. device const char * src07,
  6721. threadgroup float * shared_values [[threadgroup(0)]],
  6722. uint3 tgpig[[threadgroup_position_in_grid]],
  6723. uint tiitg[[thread_index_in_threadgroup]],
  6724. uint tiisg[[thread_index_in_simdgroup]],
  6725. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6726. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6727. const int64_t bid = tgpig.z/(ne12*ne13);
  6728. tgpig.z = tgpig.z%(ne12*ne13);
  6729. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6730. #if QK_K == 64
  6731. kernel_mul_mv_iq4_nl_f32_impl(
  6732. #else
  6733. kernel_mul_mv_iq4_xs_f32_impl(
  6734. #endif
  6735. src0[id],
  6736. (device const float *) (src1 + bid*nb11),
  6737. dst + bid*ne0,
  6738. ne00,
  6739. ne01,
  6740. ne02,
  6741. ne10,
  6742. ne12,
  6743. ne0,
  6744. ne1,
  6745. r2,
  6746. r3,
  6747. shared_values,
  6748. tgpig,
  6749. tiisg,
  6750. sgitg);
  6751. }