1
0

ggml-metal.metal 292 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509
  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. //====================================== dot products =========================
  2196. void kernel_mul_mv_q2_K_f32_impl(
  2197. device const void * src0,
  2198. device const float * src1,
  2199. device float * dst,
  2200. constant int64_t & ne00,
  2201. constant int64_t & ne01,
  2202. constant int64_t & ne02,
  2203. constant int64_t & ne10,
  2204. constant int64_t & ne12,
  2205. constant int64_t & ne0,
  2206. constant int64_t & ne1,
  2207. constant uint & r2,
  2208. constant uint & r3,
  2209. uint3 tgpig[[threadgroup_position_in_grid]],
  2210. uint tiisg[[thread_index_in_simdgroup]],
  2211. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2212. const int nb = ne00/QK_K;
  2213. const int r0 = tgpig.x;
  2214. const int r1 = tgpig.y;
  2215. const int im = tgpig.z;
  2216. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2217. const int ib_row = first_row * nb;
  2218. const uint i12 = im%ne12;
  2219. const uint i13 = im/ne12;
  2220. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2221. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  2222. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2223. float yl[32];
  2224. float sumf[N_DST]={0.f}, all_sum;
  2225. const int step = sizeof(block_q2_K) * nb;
  2226. #if QK_K == 256
  2227. const int ix = tiisg/8; // 0...3
  2228. const int it = tiisg%8; // 0...7
  2229. const int iq = it/4; // 0 or 1
  2230. const int ir = it%4; // 0...3
  2231. const int is = (8*ir)/16;// 0 or 1
  2232. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  2233. for (int ib = ix; ib < nb; ib += 4) {
  2234. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2235. for (int i = 0; i < 8; ++i) {
  2236. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2237. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  2238. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  2239. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  2240. }
  2241. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  2242. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2243. device const half * dh = &x[ib].d;
  2244. for (int row = 0; row < N_DST; row++) {
  2245. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2246. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2247. for (int i = 0; i < 8; i += 2) {
  2248. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2249. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2250. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2251. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2252. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2253. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2254. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2255. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2256. }
  2257. float dall = dh[0];
  2258. float dmin = dh[1] * 1.f/16.f;
  2259. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2260. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  2261. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  2262. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  2263. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  2264. qs += step/2;
  2265. sc += step;
  2266. dh += step/2;
  2267. }
  2268. y4 += 4 * QK_K;
  2269. }
  2270. #else
  2271. const int ix = tiisg/2; // 0...15
  2272. const int it = tiisg%2; // 0...1
  2273. device const float * y4 = y + ix * QK_K + 8 * it;
  2274. for (int ib = ix; ib < nb; ib += 16) {
  2275. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2276. for (int i = 0; i < 8; ++i) {
  2277. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2278. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  2279. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  2280. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  2281. }
  2282. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  2283. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2284. device const half * dh = &x[ib].d;
  2285. for (int row = 0; row < N_DST; row++) {
  2286. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2287. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2288. for (int i = 0; i < 8; i += 2) {
  2289. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2290. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2291. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2292. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2293. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2294. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2295. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2296. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2297. }
  2298. float dall = dh[0];
  2299. float dmin = dh[1];
  2300. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2301. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  2302. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  2303. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  2304. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  2305. qs += step/2;
  2306. sc += step;
  2307. dh += step/2;
  2308. }
  2309. y4 += 16 * QK_K;
  2310. }
  2311. #endif
  2312. for (int row = 0; row < N_DST; ++row) {
  2313. all_sum = simd_sum(sumf[row]);
  2314. if (tiisg == 0) {
  2315. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2316. }
  2317. }
  2318. }
  2319. [[host_name("kernel_mul_mv_q2_K_f32")]]
  2320. kernel void kernel_mul_mv_q2_K_f32(
  2321. device const void * src0,
  2322. device const float * src1,
  2323. device float * dst,
  2324. constant int64_t & ne00,
  2325. constant int64_t & ne01,
  2326. constant int64_t & ne02,
  2327. constant uint64_t & nb00,
  2328. constant uint64_t & nb01,
  2329. constant uint64_t & nb02,
  2330. constant int64_t & ne10,
  2331. constant int64_t & ne11,
  2332. constant int64_t & ne12,
  2333. constant uint64_t & nb10,
  2334. constant uint64_t & nb11,
  2335. constant uint64_t & nb12,
  2336. constant int64_t & ne0,
  2337. constant int64_t & ne1,
  2338. constant uint & r2,
  2339. constant uint & r3,
  2340. uint3 tgpig[[threadgroup_position_in_grid]],
  2341. uint tiisg[[thread_index_in_simdgroup]],
  2342. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2343. kernel_mul_mv_q2_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2344. }
  2345. #if QK_K == 256
  2346. void kernel_mul_mv_q3_K_f32_impl(
  2347. device const void * src0,
  2348. device const float * src1,
  2349. device float * dst,
  2350. constant int64_t & ne00,
  2351. constant int64_t & ne01,
  2352. constant int64_t & ne02,
  2353. constant int64_t & ne10,
  2354. constant int64_t & ne12,
  2355. constant int64_t & ne0,
  2356. constant int64_t & ne1,
  2357. constant uint & r2,
  2358. constant uint & r3,
  2359. uint3 tgpig[[threadgroup_position_in_grid]],
  2360. uint tiisg[[thread_index_in_simdgroup]],
  2361. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2362. const int nb = ne00/QK_K;
  2363. const int64_t r0 = tgpig.x;
  2364. const int64_t r1 = tgpig.y;
  2365. const int64_t im = tgpig.z;
  2366. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2367. const uint i12 = im%ne12;
  2368. const uint i13 = im/ne12;
  2369. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2370. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  2371. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2372. float yl[32];
  2373. //const uint16_t kmask1 = 0x3030;
  2374. //const uint16_t kmask2 = 0x0f0f;
  2375. const int tid = tiisg/4;
  2376. const int ix = tiisg%4;
  2377. const int ip = tid/4; // 0 or 1
  2378. const int il = 2*((tid%4)/2); // 0 or 2
  2379. const int ir = tid%2;
  2380. const int n = 8;
  2381. const int l0 = n*ir;
  2382. // One would think that the Metal compiler would figure out that ip and il can only have
  2383. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  2384. // with these two tales.
  2385. //
  2386. // Possible masks for the high bit
  2387. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  2388. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  2389. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  2390. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  2391. // Possible masks for the low 2 bits
  2392. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  2393. const ushort4 hm = mm[2*ip + il/2];
  2394. const int shift = 2*il;
  2395. const float v1 = il == 0 ? 4.f : 64.f;
  2396. const float v2 = 4.f * v1;
  2397. const uint16_t s_shift1 = 4*ip;
  2398. const uint16_t s_shift2 = s_shift1 + il;
  2399. const int q_offset = 32*ip + l0;
  2400. const int y_offset = 128*ip + 32*il + l0;
  2401. const int step = sizeof(block_q3_K) * nb / 2;
  2402. device const float * y1 = yy + ix*QK_K + y_offset;
  2403. uint32_t scales32, aux32;
  2404. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  2405. thread const int8_t * scales = (thread const int8_t *)&scales32;
  2406. float sumf1[2] = {0.f};
  2407. float sumf2[2] = {0.f};
  2408. for (int i = ix; i < nb; i += 4) {
  2409. for (int l = 0; l < 8; ++l) {
  2410. yl[l+ 0] = y1[l+ 0];
  2411. yl[l+ 8] = y1[l+16];
  2412. yl[l+16] = y1[l+32];
  2413. yl[l+24] = y1[l+48];
  2414. }
  2415. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  2416. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  2417. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  2418. device const half * dh = &x[i].d;
  2419. for (int row = 0; row < 2; ++row) {
  2420. const float d_all = (float)dh[0];
  2421. scales16[0] = a[4];
  2422. scales16[1] = a[5];
  2423. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  2424. scales16[0] = a[il+0];
  2425. scales16[1] = a[il+1];
  2426. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  2427. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  2428. for (int l = 0; l < n; l += 2) {
  2429. const int32_t qs = q[l/2];
  2430. s1 += yl[l+0] * (qs & qm[il/2][0]);
  2431. s2 += yl[l+1] * (qs & qm[il/2][1]);
  2432. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  2433. s4 += yl[l+16] * (qs & qm[il/2][2]);
  2434. s5 += yl[l+17] * (qs & qm[il/2][3]);
  2435. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  2436. }
  2437. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2438. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2439. sumf1[row] += d1 * (scales[0] - 32);
  2440. sumf2[row] += d2 * (scales[2] - 32);
  2441. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  2442. for (int l = 0; l < n; l += 2) {
  2443. const int32_t qs = q[l/2+8];
  2444. s1 += yl[l+8] * (qs & qm[il/2][0]);
  2445. s2 += yl[l+9] * (qs & qm[il/2][1]);
  2446. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  2447. s4 += yl[l+24] * (qs & qm[il/2][2]);
  2448. s5 += yl[l+25] * (qs & qm[il/2][3]);
  2449. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  2450. }
  2451. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2452. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2453. sumf1[row] += d1 * (scales[1] - 32);
  2454. sumf2[row] += d2 * (scales[3] - 32);
  2455. q += step;
  2456. h += step;
  2457. a += step;
  2458. dh += step;
  2459. }
  2460. y1 += 4 * QK_K;
  2461. }
  2462. for (int row = 0; row < 2; ++row) {
  2463. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  2464. sumf1[row] = simd_sum(sumf);
  2465. }
  2466. if (tiisg == 0) {
  2467. for (int row = 0; row < 2; ++row) {
  2468. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = sumf1[row];
  2469. }
  2470. }
  2471. }
  2472. #else
  2473. void kernel_mul_mv_q3_K_f32_impl(
  2474. device const void * src0,
  2475. device const float * src1,
  2476. device float * dst,
  2477. constant int64_t & ne00,
  2478. constant int64_t & ne01,
  2479. constant int64_t & ne02,
  2480. constant int64_t & ne10,
  2481. constant int64_t & ne12,
  2482. constant int64_t & ne0,
  2483. constant int64_t & ne1,
  2484. constant uint & r2,
  2485. constant uint & r3,
  2486. uint3 tgpig[[threadgroup_position_in_grid]],
  2487. uint tiisg[[thread_index_in_simdgroup]],
  2488. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2489. const int nb = ne00/QK_K;
  2490. const int64_t r0 = tgpig.x;
  2491. const int64_t r1 = tgpig.y;
  2492. const int64_t im = tgpig.z;
  2493. const int row = 2 * r0 + sgitg;
  2494. const uint i12 = im%ne12;
  2495. const uint i13 = im/ne12;
  2496. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2497. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  2498. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2499. const int ix = tiisg/4;
  2500. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  2501. const int iq = il/8; // 0, 0, 1, 1
  2502. const int in = il%8; // 0, 4, 0, 4
  2503. float2 sum = {0.f, 0.f};
  2504. for (int i = ix; i < nb; i += 8) {
  2505. const float d_all = (float)(x[i].d);
  2506. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  2507. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  2508. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  2509. device const float * y = yy + i * QK_K + il;
  2510. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  2511. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  2512. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  2513. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  2514. for (int l = 0; l < 4; l += 2) {
  2515. const uint16_t hm = h[l/2] >> iq;
  2516. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  2517. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  2518. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  2519. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  2520. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  2521. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  2522. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  2523. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  2524. }
  2525. }
  2526. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  2527. const float tot = simd_sum(sumf);
  2528. if (tiisg == 0) {
  2529. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  2530. }
  2531. }
  2532. #endif
  2533. [[host_name("kernel_mul_mv_q3_K_f32")]]
  2534. kernel void kernel_mul_mv_q3_K_f32(
  2535. device const void * src0,
  2536. device const float * src1,
  2537. device float * dst,
  2538. constant int64_t & ne00,
  2539. constant int64_t & ne01,
  2540. constant int64_t & ne02,
  2541. constant uint64_t & nb00,
  2542. constant uint64_t & nb01,
  2543. constant uint64_t & nb02,
  2544. constant int64_t & ne10,
  2545. constant int64_t & ne11,
  2546. constant int64_t & ne12,
  2547. constant uint64_t & nb10,
  2548. constant uint64_t & nb11,
  2549. constant uint64_t & nb12,
  2550. constant int64_t & ne0,
  2551. constant int64_t & ne1,
  2552. constant uint & r2,
  2553. constant uint & r3,
  2554. uint3 tgpig[[threadgroup_position_in_grid]],
  2555. uint tiisg[[thread_index_in_simdgroup]],
  2556. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2557. kernel_mul_mv_q3_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2558. }
  2559. #if QK_K == 256
  2560. void kernel_mul_mv_q4_K_f32_impl(
  2561. device const void * src0,
  2562. device const float * src1,
  2563. device float * dst,
  2564. constant int64_t & ne00,
  2565. constant int64_t & ne01,
  2566. constant int64_t & ne02,
  2567. constant int64_t & ne10,
  2568. constant int64_t & ne12,
  2569. constant int64_t & ne0,
  2570. constant int64_t & ne1,
  2571. constant uint & r2,
  2572. constant uint & r3,
  2573. uint3 tgpig[[threadgroup_position_in_grid]],
  2574. uint tiisg[[thread_index_in_simdgroup]],
  2575. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2576. const uint16_t kmask1 = 0x3f3f;
  2577. const uint16_t kmask2 = 0x0f0f;
  2578. const uint16_t kmask3 = 0xc0c0;
  2579. const int ix = tiisg/8; // 0...3
  2580. const int it = tiisg%8; // 0...7
  2581. const int iq = it/4; // 0 or 1
  2582. const int ir = it%4; // 0...3
  2583. const int nb = ne00/QK_K;
  2584. const int r0 = tgpig.x;
  2585. const int r1 = tgpig.y;
  2586. const int im = tgpig.z;
  2587. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2588. const int first_row = r0 * N_DST;
  2589. const int ib_row = first_row * nb;
  2590. const uint i12 = im%ne12;
  2591. const uint i13 = im/ne12;
  2592. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2593. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2594. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2595. float yl[16];
  2596. float yh[16];
  2597. float sumf[N_DST]={0.f}, all_sum;
  2598. const int step = sizeof(block_q4_K) * nb / 2;
  2599. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  2600. uint16_t sc16[4];
  2601. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2602. for (int ib = ix; ib < nb; ib += 4) {
  2603. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2604. for (int i = 0; i < 8; ++i) {
  2605. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  2606. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  2607. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  2608. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  2609. }
  2610. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  2611. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2612. device const half * dh = &x[ib].d;
  2613. for (int row = 0; row < N_DST; row++) {
  2614. sc16[0] = sc[0] & kmask1;
  2615. sc16[1] = sc[2] & kmask1;
  2616. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  2617. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  2618. device const uint16_t * q2 = q1 + 32;
  2619. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2620. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2621. for (int i = 0; i < 8; i += 2) {
  2622. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  2623. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  2624. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  2625. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  2626. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  2627. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  2628. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  2629. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  2630. }
  2631. float dall = dh[0];
  2632. float dmin = dh[1];
  2633. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  2634. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  2635. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  2636. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  2637. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2638. q1 += step;
  2639. sc += step;
  2640. dh += step;
  2641. }
  2642. y4 += 4 * QK_K;
  2643. }
  2644. for (int row = 0; row < N_DST; ++row) {
  2645. all_sum = simd_sum(sumf[row]);
  2646. if (tiisg == 0) {
  2647. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2648. }
  2649. }
  2650. }
  2651. #else
  2652. void kernel_mul_mv_q4_K_f32_impl(
  2653. device const void * src0,
  2654. device const float * src1,
  2655. device float * dst,
  2656. constant int64_t & ne00,
  2657. constant int64_t & ne01,
  2658. constant int64_t & ne02,
  2659. constant int64_t & ne10,
  2660. constant int64_t & ne12,
  2661. constant int64_t & ne0,
  2662. constant int64_t & ne1,
  2663. constant uint & r2,
  2664. constant uint & r3,
  2665. uint3 tgpig[[threadgroup_position_in_grid]],
  2666. uint tiisg[[thread_index_in_simdgroup]],
  2667. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2668. const int ix = tiisg/4; // 0...7
  2669. const int it = tiisg%4; // 0...3
  2670. const int nb = ne00/QK_K;
  2671. const int r0 = tgpig.x;
  2672. const int r1 = tgpig.y;
  2673. const int im = tgpig.z;
  2674. const int first_row = r0 * N_DST;
  2675. const int ib_row = first_row * nb;
  2676. const uint i12 = im%ne12;
  2677. const uint i13 = im/ne12;
  2678. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2679. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2680. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2681. float yl[8];
  2682. float yh[8];
  2683. float sumf[N_DST]={0.f}, all_sum;
  2684. const int step = sizeof(block_q4_K) * nb / 2;
  2685. device const float * y4 = y + ix * QK_K + 8 * it;
  2686. uint16_t sc16[4];
  2687. for (int ib = ix; ib < nb; ib += 8) {
  2688. float2 sumy = {0.f, 0.f};
  2689. for (int i = 0; i < 8; ++i) {
  2690. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  2691. yh[i] = y4[i+32]; sumy[1] += yh[i];
  2692. }
  2693. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  2694. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2695. device const half * dh = x[ib].d;
  2696. for (int row = 0; row < N_DST; row++) {
  2697. sc16[0] = sc[0] & 0x000f;
  2698. sc16[1] = sc[0] & 0x0f00;
  2699. sc16[2] = sc[0] & 0x00f0;
  2700. sc16[3] = sc[0] & 0xf000;
  2701. float2 acc1 = {0.f, 0.f};
  2702. float2 acc2 = {0.f, 0.f};
  2703. for (int i = 0; i < 8; i += 2) {
  2704. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  2705. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  2706. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  2707. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  2708. }
  2709. float dall = dh[0];
  2710. float dmin = dh[1];
  2711. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  2712. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  2713. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  2714. qs += step;
  2715. sc += step;
  2716. dh += step;
  2717. }
  2718. y4 += 8 * QK_K;
  2719. }
  2720. for (int row = 0; row < N_DST; ++row) {
  2721. all_sum = simd_sum(sumf[row]);
  2722. if (tiisg == 0) {
  2723. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2724. }
  2725. }
  2726. }
  2727. #endif
  2728. [[host_name("kernel_mul_mv_q4_K_f32")]]
  2729. kernel void kernel_mul_mv_q4_K_f32(
  2730. device const void * src0,
  2731. device const float * src1,
  2732. device float * dst,
  2733. constant int64_t & ne00,
  2734. constant int64_t & ne01,
  2735. constant int64_t & ne02,
  2736. constant uint64_t & nb00,
  2737. constant uint64_t & nb01,
  2738. constant uint64_t & nb02,
  2739. constant int64_t & ne10,
  2740. constant int64_t & ne11,
  2741. constant int64_t & ne12,
  2742. constant uint64_t & nb10,
  2743. constant uint64_t & nb11,
  2744. constant uint64_t & nb12,
  2745. constant int64_t & ne0,
  2746. constant int64_t & ne1,
  2747. constant uint & r2,
  2748. constant uint & r3,
  2749. uint3 tgpig[[threadgroup_position_in_grid]],
  2750. uint tiisg[[thread_index_in_simdgroup]],
  2751. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2752. kernel_mul_mv_q4_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2753. }
  2754. void kernel_mul_mv_q5_K_f32_impl(
  2755. device const void * src0,
  2756. device const float * src1,
  2757. device float * dst,
  2758. constant int64_t & ne00,
  2759. constant int64_t & ne01,
  2760. constant int64_t & ne02,
  2761. constant int64_t & ne10,
  2762. constant int64_t & ne12,
  2763. constant int64_t & ne0,
  2764. constant int64_t & ne1,
  2765. constant uint & r2,
  2766. constant uint & r3,
  2767. uint3 tgpig[[threadgroup_position_in_grid]],
  2768. uint tiisg[[thread_index_in_simdgroup]],
  2769. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2770. const int nb = ne00/QK_K;
  2771. const int64_t r0 = tgpig.x;
  2772. const int64_t r1 = tgpig.y;
  2773. const int im = tgpig.z;
  2774. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2775. const uint i12 = im%ne12;
  2776. const uint i13 = im/ne12;
  2777. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2778. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  2779. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2780. float sumf[2]={0.f};
  2781. const int step = sizeof(block_q5_K) * nb;
  2782. #if QK_K == 256
  2783. #
  2784. float yl[16], yh[16];
  2785. const uint16_t kmask1 = 0x3f3f;
  2786. const uint16_t kmask2 = 0x0f0f;
  2787. const uint16_t kmask3 = 0xc0c0;
  2788. const int tid = tiisg/4;
  2789. const int ix = tiisg%4;
  2790. const int iq = tid/4;
  2791. const int ir = tid%4;
  2792. const int n = 8;
  2793. const int l0 = n*ir;
  2794. const int q_offset = 32*iq + l0;
  2795. const int y_offset = 64*iq + l0;
  2796. const uint8_t hm1 = 1u << (2*iq);
  2797. const uint8_t hm2 = hm1 << 1;
  2798. const uint8_t hm3 = hm1 << 4;
  2799. const uint8_t hm4 = hm2 << 4;
  2800. uint16_t sc16[4];
  2801. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2802. device const float * y1 = yy + ix*QK_K + y_offset;
  2803. for (int i = ix; i < nb; i += 4) {
  2804. device const uint8_t * q1 = x[i].qs + q_offset;
  2805. device const uint8_t * qh = x[i].qh + l0;
  2806. device const half * dh = &x[i].d;
  2807. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  2808. device const float * y2 = y1 + 128;
  2809. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2810. for (int l = 0; l < 8; ++l) {
  2811. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  2812. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  2813. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  2814. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  2815. }
  2816. for (int row = 0; row < 2; ++row) {
  2817. device const uint8_t * q2 = q1 + 64;
  2818. sc16[0] = a[0] & kmask1;
  2819. sc16[1] = a[2] & kmask1;
  2820. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  2821. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  2822. float4 acc1 = {0.f};
  2823. float4 acc2 = {0.f};
  2824. for (int l = 0; l < n; ++l) {
  2825. uint8_t h = qh[l];
  2826. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  2827. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  2828. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  2829. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  2830. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  2831. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  2832. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  2833. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  2834. }
  2835. const float dall = dh[0];
  2836. const float dmin = dh[1];
  2837. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  2838. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  2839. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  2840. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  2841. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2842. q1 += step;
  2843. qh += step;
  2844. dh += step/2;
  2845. a += step/2;
  2846. }
  2847. y1 += 4 * QK_K;
  2848. }
  2849. #else
  2850. float yl[8], yh[8];
  2851. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  2852. const int ix = tiisg%8;
  2853. const int iq = il/8; // 0, 0, 1, 1
  2854. const int in = il%8; // 0, 4, 0, 4
  2855. device const float * y = yy + ix*QK_K + il;
  2856. for (int i = ix; i < nb; i += 8) {
  2857. for (int l = 0; l < 4; ++l) {
  2858. yl[l+0] = y[l+ 0];
  2859. yl[l+4] = y[l+16];
  2860. yh[l+0] = y[l+32];
  2861. yh[l+4] = y[l+48];
  2862. }
  2863. device const half * dh = &x[i].d;
  2864. device const uint8_t * q = x[i].qs + il;
  2865. device const uint8_t * h = x[i].qh + in;
  2866. device const int8_t * s = x[i].scales;
  2867. for (int row = 0; row < 2; ++row) {
  2868. const float d = dh[0];
  2869. float2 acc = {0.f, 0.f};
  2870. for (int l = 0; l < 4; ++l) {
  2871. const uint8_t hl = h[l] >> iq;
  2872. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  2873. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  2874. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  2875. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  2876. }
  2877. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  2878. q += step;
  2879. h += step;
  2880. s += step;
  2881. dh += step/2;
  2882. }
  2883. y += 8 * QK_K;
  2884. }
  2885. #endif
  2886. for (int row = 0; row < 2; ++row) {
  2887. const float tot = simd_sum(sumf[row]);
  2888. if (tiisg == 0) {
  2889. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  2890. }
  2891. }
  2892. }
  2893. [[host_name("kernel_mul_mv_q5_K_f32")]]
  2894. kernel void kernel_mul_mv_q5_K_f32(
  2895. device const void * src0,
  2896. device const float * src1,
  2897. device float * dst,
  2898. constant int64_t & ne00,
  2899. constant int64_t & ne01,
  2900. constant int64_t & ne02,
  2901. constant uint64_t & nb00,
  2902. constant uint64_t & nb01,
  2903. constant uint64_t & nb02,
  2904. constant int64_t & ne10,
  2905. constant int64_t & ne11,
  2906. constant int64_t & ne12,
  2907. constant uint64_t & nb10,
  2908. constant uint64_t & nb11,
  2909. constant uint64_t & nb12,
  2910. constant int64_t & ne0,
  2911. constant int64_t & ne1,
  2912. constant uint & r2,
  2913. constant uint & r3,
  2914. uint3 tgpig[[threadgroup_position_in_grid]],
  2915. uint tiisg[[thread_index_in_simdgroup]],
  2916. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2917. kernel_mul_mv_q5_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2918. }
  2919. void kernel_mul_mv_q6_K_f32_impl(
  2920. device const void * src0,
  2921. device const float * src1,
  2922. device float * dst,
  2923. constant int64_t & ne00,
  2924. constant int64_t & ne01,
  2925. constant int64_t & ne02,
  2926. constant int64_t & ne10,
  2927. constant int64_t & ne12,
  2928. constant int64_t & ne0,
  2929. constant int64_t & ne1,
  2930. constant uint & r2,
  2931. constant uint & r3,
  2932. uint3 tgpig[[threadgroup_position_in_grid]],
  2933. uint tiisg[[thread_index_in_simdgroup]],
  2934. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2935. const uint8_t kmask1 = 0x03;
  2936. const uint8_t kmask2 = 0x0C;
  2937. const uint8_t kmask3 = 0x30;
  2938. const uint8_t kmask4 = 0xC0;
  2939. const int nb = ne00/QK_K;
  2940. const int64_t r0 = tgpig.x;
  2941. const int64_t r1 = tgpig.y;
  2942. const int im = tgpig.z;
  2943. const int row = 2 * r0 + sgitg;
  2944. const uint i12 = im%ne12;
  2945. const uint i13 = im/ne12;
  2946. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2947. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  2948. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2949. float sumf = 0;
  2950. #if QK_K == 256
  2951. const int tid = tiisg/2;
  2952. const int ix = tiisg%2;
  2953. const int ip = tid/8; // 0 or 1
  2954. const int il = tid%8;
  2955. const int n = 4;
  2956. const int l0 = n*il;
  2957. const int is = 8*ip + l0/16;
  2958. const int y_offset = 128*ip + l0;
  2959. const int q_offset_l = 64*ip + l0;
  2960. const int q_offset_h = 32*ip + l0;
  2961. for (int i = ix; i < nb; i += 2) {
  2962. device const uint8_t * q1 = x[i].ql + q_offset_l;
  2963. device const uint8_t * q2 = q1 + 32;
  2964. device const uint8_t * qh = x[i].qh + q_offset_h;
  2965. device const int8_t * sc = x[i].scales + is;
  2966. device const float * y = yy + i * QK_K + y_offset;
  2967. const float dall = x[i].d;
  2968. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2969. for (int l = 0; l < n; ++l) {
  2970. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2971. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  2972. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  2973. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  2974. }
  2975. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  2976. }
  2977. #else
  2978. const int ix = tiisg/4;
  2979. const int il = 4*(tiisg%4);
  2980. for (int i = ix; i < nb; i += 8) {
  2981. device const float * y = yy + i * QK_K + il;
  2982. device const uint8_t * ql = x[i].ql + il;
  2983. device const uint8_t * qh = x[i].qh + il;
  2984. device const int8_t * s = x[i].scales;
  2985. const float d = x[i].d;
  2986. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2987. for (int l = 0; l < 4; ++l) {
  2988. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2989. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  2990. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  2991. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  2992. }
  2993. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  2994. }
  2995. #endif
  2996. const float tot = simd_sum(sumf);
  2997. if (tiisg == 0) {
  2998. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  2999. }
  3000. }
  3001. [[host_name("kernel_mul_mv_q6_K_f32")]]
  3002. kernel void kernel_mul_mv_q6_K_f32(
  3003. device const void * src0,
  3004. device const float * src1,
  3005. device float * dst,
  3006. constant int64_t & ne00,
  3007. constant int64_t & ne01,
  3008. constant int64_t & ne02,
  3009. constant uint64_t & nb00,
  3010. constant uint64_t & nb01,
  3011. constant uint64_t & nb02,
  3012. constant int64_t & ne10,
  3013. constant int64_t & ne11,
  3014. constant int64_t & ne12,
  3015. constant uint64_t & nb10,
  3016. constant uint64_t & nb11,
  3017. constant uint64_t & nb12,
  3018. constant int64_t & ne0,
  3019. constant int64_t & ne1,
  3020. constant uint & r2,
  3021. constant uint & r3,
  3022. uint3 tgpig[[threadgroup_position_in_grid]],
  3023. uint tiisg[[thread_index_in_simdgroup]],
  3024. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3025. kernel_mul_mv_q6_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  3026. }
  3027. // ======================= "True" 2-bit
  3028. constexpr constant static uint64_t iq2xxs_grid[256] = {
  3029. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3030. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  3031. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  3032. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  3033. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  3034. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  3035. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  3036. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  3037. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  3038. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  3039. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  3040. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  3041. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  3042. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  3043. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  3044. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  3045. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  3046. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  3047. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  3048. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  3049. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  3050. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  3051. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  3052. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  3053. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  3054. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  3055. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  3056. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  3057. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  3058. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  3059. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  3060. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  3061. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  3062. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  3063. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  3064. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  3065. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  3066. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  3067. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  3068. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  3069. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  3070. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  3071. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  3072. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  3073. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  3074. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  3075. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  3076. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  3077. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  3078. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  3079. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  3080. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  3081. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  3082. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  3083. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  3084. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  3085. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  3086. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  3087. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  3088. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  3089. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  3090. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  3091. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  3092. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  3093. };
  3094. constexpr constant static uint64_t iq2xs_grid[512] = {
  3095. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3096. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3097. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3098. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3099. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3100. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  3101. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  3102. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  3103. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  3104. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  3105. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  3106. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  3107. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  3108. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  3109. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  3110. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  3111. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  3112. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  3113. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  3114. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  3115. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  3116. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  3117. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  3118. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  3119. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  3120. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  3121. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  3122. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  3123. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  3124. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  3125. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  3126. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  3127. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  3128. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  3129. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  3130. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  3131. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  3132. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  3133. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  3134. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  3135. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  3136. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  3137. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  3138. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  3139. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  3140. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  3141. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  3142. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  3143. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  3144. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  3145. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  3146. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  3147. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  3148. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  3149. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  3150. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  3151. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  3152. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  3153. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  3154. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  3155. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  3156. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  3157. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  3158. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  3159. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  3160. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  3161. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  3162. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  3163. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  3164. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  3165. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  3166. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  3167. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  3168. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  3169. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  3170. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  3171. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  3172. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  3173. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  3174. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  3175. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  3176. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  3177. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  3178. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  3179. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  3180. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  3181. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  3182. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  3183. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  3184. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  3185. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  3186. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  3187. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  3188. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  3189. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  3190. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  3191. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  3192. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  3193. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  3194. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  3195. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  3196. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  3197. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  3198. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  3199. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  3200. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  3201. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  3202. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  3203. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  3204. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  3205. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  3206. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  3207. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  3208. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  3209. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  3210. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  3211. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  3212. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  3213. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  3214. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  3215. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  3216. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  3217. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  3218. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  3219. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  3220. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  3221. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  3222. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  3223. };
  3224. constexpr constant static uint64_t iq2s_grid[1024] = {
  3225. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3226. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3227. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3228. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3229. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3230. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  3231. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  3232. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  3233. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  3234. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  3235. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  3236. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  3237. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  3238. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  3239. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  3240. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  3241. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  3242. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  3243. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  3244. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  3245. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  3246. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  3247. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  3248. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  3249. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  3250. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  3251. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  3252. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  3253. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  3254. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  3255. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  3256. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  3257. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  3258. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  3259. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  3260. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  3261. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  3262. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  3263. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  3264. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  3265. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  3266. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  3267. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  3268. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  3269. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  3270. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  3271. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  3272. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  3273. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  3274. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  3275. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  3276. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  3277. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  3278. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  3279. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  3280. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  3281. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  3282. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  3283. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  3284. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  3285. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  3286. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  3287. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  3288. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  3289. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  3290. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  3291. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  3292. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  3293. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  3294. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  3295. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  3296. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  3297. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  3298. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  3299. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  3300. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  3301. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  3302. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  3303. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  3304. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  3305. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  3306. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  3307. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  3308. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  3309. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  3310. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  3311. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  3312. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  3313. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  3314. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  3315. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  3316. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  3317. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  3318. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  3319. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  3320. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  3321. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  3322. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  3323. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  3324. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  3325. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  3326. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  3327. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  3328. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  3329. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  3330. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  3331. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  3332. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  3333. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  3334. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  3335. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  3336. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  3337. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  3338. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  3339. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  3340. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  3341. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  3342. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  3343. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  3344. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  3345. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  3346. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  3347. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  3348. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  3349. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  3350. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  3351. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  3352. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  3353. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  3354. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  3355. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  3356. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  3357. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  3358. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  3359. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  3360. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  3361. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  3362. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  3363. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  3364. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  3365. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  3366. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  3367. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  3368. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  3369. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  3370. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  3371. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  3372. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  3373. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  3374. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  3375. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  3376. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  3377. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  3378. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  3379. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  3380. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  3381. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  3382. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  3383. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  3384. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  3385. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  3386. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  3387. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  3388. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  3389. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  3390. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  3391. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  3392. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  3393. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  3394. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  3395. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  3396. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  3397. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  3398. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  3399. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  3400. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  3401. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  3402. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  3403. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  3404. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  3405. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  3406. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  3407. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  3408. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  3409. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  3410. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  3411. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  3412. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  3413. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  3414. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  3415. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  3416. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  3417. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  3418. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  3419. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  3420. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  3421. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  3422. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  3423. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  3424. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  3425. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  3426. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  3427. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  3428. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  3429. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  3430. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  3431. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  3432. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  3433. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  3434. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  3435. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  3436. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  3437. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  3438. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  3439. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  3440. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  3441. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  3442. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  3443. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  3444. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  3445. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  3446. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  3447. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  3448. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  3449. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  3450. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  3451. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  3452. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  3453. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  3454. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  3455. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  3456. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  3457. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  3458. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  3459. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  3460. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  3461. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  3462. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  3463. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  3464. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  3465. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  3466. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  3467. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  3468. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  3469. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  3470. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  3471. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  3472. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  3473. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  3474. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  3475. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  3476. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  3477. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  3478. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  3479. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  3480. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  3481. };
  3482. constexpr constant static uint32_t iq3xxs_grid[256] = {
  3483. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  3484. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  3485. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  3486. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  3487. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  3488. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  3489. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  3490. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  3491. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  3492. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  3493. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  3494. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  3495. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  3496. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  3497. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  3498. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  3499. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  3500. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  3501. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  3502. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  3503. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  3504. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  3505. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  3506. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  3507. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  3508. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  3509. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  3510. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  3511. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  3512. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  3513. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  3514. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  3515. };
  3516. constexpr constant static uint32_t iq3xs_grid[512] = {
  3517. 0x04040404, 0x0404040c, 0x04040414, 0x0404042c, 0x0404043e, 0x04040c04, 0x04040c0c, 0x04040c14,
  3518. 0x04040c24, 0x04040c34, 0x04041404, 0x0404140c, 0x0404142c, 0x04041c1c, 0x04042404, 0x04042414,
  3519. 0x0404242c, 0x0404243e, 0x04042c0c, 0x04042c1c, 0x04043404, 0x04043414, 0x04043e0c, 0x04043e24,
  3520. 0x04043e3e, 0x040c0404, 0x040c040c, 0x040c0414, 0x040c0424, 0x040c0c04, 0x040c0c0c, 0x040c0c2c,
  3521. 0x040c1404, 0x040c141c, 0x040c143e, 0x040c1c0c, 0x040c1c2c, 0x040c2424, 0x040c340c, 0x040c342c,
  3522. 0x040c3e14, 0x04140404, 0x0414040c, 0x0414042c, 0x0414043e, 0x04140c04, 0x04140c1c, 0x04140c34,
  3523. 0x0414140c, 0x0414142c, 0x04141c04, 0x04141c24, 0x04142414, 0x0414242c, 0x0414243e, 0x04142c0c,
  3524. 0x04142c1c, 0x04143e04, 0x04143e1c, 0x041c041c, 0x041c0c0c, 0x041c0c2c, 0x041c1404, 0x041c1414,
  3525. 0x041c1c0c, 0x041c1c1c, 0x041c1c34, 0x041c2424, 0x041c2c04, 0x041c2c14, 0x041c343e, 0x041c3e0c,
  3526. 0x041c3e2c, 0x04240404, 0x04240c1c, 0x04240c3e, 0x0424140c, 0x04241424, 0x04241c14, 0x04242404,
  3527. 0x0424241c, 0x04242c0c, 0x04243e04, 0x042c0414, 0x042c0424, 0x042c1404, 0x042c1414, 0x042c1434,
  3528. 0x042c1c1c, 0x042c240c, 0x042c242c, 0x042c243e, 0x042c3434, 0x042c3e1c, 0x04340434, 0x04340c0c,
  3529. 0x04340c1c, 0x04341c0c, 0x04342c14, 0x04343e0c, 0x043e0404, 0x043e0414, 0x043e0424, 0x043e1404,
  3530. 0x043e1414, 0x043e1434, 0x043e1c1c, 0x043e2c04, 0x043e2c24, 0x0c040404, 0x0c04040c, 0x0c040414,
  3531. 0x0c040424, 0x0c040c04, 0x0c040c0c, 0x0c040c1c, 0x0c040c2c, 0x0c040c3e, 0x0c041404, 0x0c041414,
  3532. 0x0c041c0c, 0x0c041c24, 0x0c041c34, 0x0c042c24, 0x0c042c34, 0x0c04340c, 0x0c043e14, 0x0c0c0404,
  3533. 0x0c0c040c, 0x0c0c041c, 0x0c0c0434, 0x0c0c0c04, 0x0c0c0c24, 0x0c0c140c, 0x0c0c1c04, 0x0c0c1c1c,
  3534. 0x0c0c240c, 0x0c0c2c04, 0x0c0c2c14, 0x0c0c3e04, 0x0c0c3e34, 0x0c140404, 0x0c140c14, 0x0c140c2c,
  3535. 0x0c140c3e, 0x0c141404, 0x0c141424, 0x0c141c14, 0x0c142404, 0x0c14241c, 0x0c142c2c, 0x0c143404,
  3536. 0x0c143e14, 0x0c1c040c, 0x0c1c0424, 0x0c1c043e, 0x0c1c0c04, 0x0c1c0c1c, 0x0c1c140c, 0x0c1c143e,
  3537. 0x0c1c1c04, 0x0c1c1c24, 0x0c1c240c, 0x0c1c3414, 0x0c1c3e04, 0x0c24041c, 0x0c24042c, 0x0c240c14,
  3538. 0x0c240c24, 0x0c241c0c, 0x0c241c1c, 0x0c242414, 0x0c242434, 0x0c242c04, 0x0c242c24, 0x0c2c040c,
  3539. 0x0c2c0c04, 0x0c2c0c1c, 0x0c2c140c, 0x0c2c1c04, 0x0c2c1c14, 0x0c2c2c0c, 0x0c341404, 0x0c341424,
  3540. 0x0c34143e, 0x0c342424, 0x0c342434, 0x0c3e040c, 0x0c3e041c, 0x0c3e0c04, 0x0c3e0c14, 0x0c3e140c,
  3541. 0x0c3e1c2c, 0x0c3e240c, 0x0c3e3414, 0x0c3e3e04, 0x14040404, 0x1404040c, 0x1404041c, 0x1404042c,
  3542. 0x1404043e, 0x14040c04, 0x14040c14, 0x14040c24, 0x14040c34, 0x1404140c, 0x1404141c, 0x1404143e,
  3543. 0x14041c04, 0x14041c14, 0x1404240c, 0x1404241c, 0x1404242c, 0x14042c04, 0x14042c14, 0x1404343e,
  3544. 0x14043e04, 0x14043e1c, 0x14043e2c, 0x140c0404, 0x140c0414, 0x140c0c04, 0x140c0c1c, 0x140c0c3e,
  3545. 0x140c1414, 0x140c142c, 0x140c1c0c, 0x140c1c24, 0x140c2414, 0x140c2c0c, 0x1414040c, 0x14140424,
  3546. 0x1414043e, 0x1414140c, 0x1414141c, 0x14141c04, 0x14141c3e, 0x1414240c, 0x14142c1c, 0x14142c3e,
  3547. 0x14143e0c, 0x14143e24, 0x141c0404, 0x141c0414, 0x141c042c, 0x141c0c0c, 0x141c1414, 0x141c1424,
  3548. 0x141c1c0c, 0x141c1c1c, 0x141c2414, 0x141c2c04, 0x141c3434, 0x1424040c, 0x1424043e, 0x14241404,
  3549. 0x1424141c, 0x14241c14, 0x14241c2c, 0x1424240c, 0x14243e14, 0x14243e2c, 0x142c0424, 0x142c0c0c,
  3550. 0x142c1414, 0x142c1c3e, 0x142c2404, 0x142c2c1c, 0x142c3e04, 0x14340404, 0x14340414, 0x1434043e,
  3551. 0x1434140c, 0x14342c2c, 0x1434340c, 0x143e042c, 0x143e0c0c, 0x143e1434, 0x143e1c04, 0x143e241c,
  3552. 0x143e2c04, 0x1c040414, 0x1c040c0c, 0x1c040c1c, 0x1c040c2c, 0x1c040c3e, 0x1c041414, 0x1c041c0c,
  3553. 0x1c041c1c, 0x1c041c2c, 0x1c042414, 0x1c042424, 0x1c04243e, 0x1c042c0c, 0x1c04341c, 0x1c043e0c,
  3554. 0x1c0c040c, 0x1c0c041c, 0x1c0c042c, 0x1c0c0c24, 0x1c0c140c, 0x1c0c141c, 0x1c0c2404, 0x1c0c3404,
  3555. 0x1c0c3e14, 0x1c0c3e34, 0x1c140404, 0x1c140c14, 0x1c141404, 0x1c141c14, 0x1c141c24, 0x1c142c04,
  3556. 0x1c1c040c, 0x1c1c0c04, 0x1c1c0c24, 0x1c1c140c, 0x1c1c141c, 0x1c1c143e, 0x1c1c1c04, 0x1c1c240c,
  3557. 0x1c1c241c, 0x1c1c243e, 0x1c1c2c2c, 0x1c1c3e1c, 0x1c24041c, 0x1c240c0c, 0x1c240c34, 0x1c241414,
  3558. 0x1c241c0c, 0x1c242c14, 0x1c243404, 0x1c243424, 0x1c2c040c, 0x1c2c0c04, 0x1c2c0c14, 0x1c2c142c,
  3559. 0x1c2c1c14, 0x1c2c2424, 0x1c2c2c34, 0x1c2c3e1c, 0x1c340c34, 0x1c34240c, 0x1c3e040c, 0x1c3e041c,
  3560. 0x1c3e1404, 0x1c3e1414, 0x1c3e1c2c, 0x24040404, 0x24040424, 0x24040c14, 0x24041404, 0x24041424,
  3561. 0x2404143e, 0x24041c14, 0x2404240c, 0x24042c04, 0x24043e04, 0x240c0414, 0x240c043e, 0x240c0c0c,
  3562. 0x240c0c1c, 0x240c1414, 0x240c1c04, 0x240c1c2c, 0x240c241c, 0x240c2c0c, 0x240c2c2c, 0x2414040c,
  3563. 0x2414041c, 0x24140c04, 0x24140c2c, 0x2414140c, 0x24141c1c, 0x24142404, 0x24142c3e, 0x24143414,
  3564. 0x24143e04, 0x241c0424, 0x241c0c0c, 0x241c0c1c, 0x241c1404, 0x241c1414, 0x241c1c0c, 0x241c1c2c,
  3565. 0x24240404, 0x24240414, 0x24241424, 0x24241c3e, 0x24242404, 0x24243e0c, 0x242c042c, 0x242c043e,
  3566. 0x242c140c, 0x242c3414, 0x24340c1c, 0x24341c24, 0x24343404, 0x243e0c04, 0x243e0c2c, 0x243e1c04,
  3567. 0x243e241c, 0x243e2c0c, 0x2c040414, 0x2c040c04, 0x2c040c24, 0x2c041414, 0x2c042404, 0x2c042424,
  3568. 0x2c04243e, 0x2c042c14, 0x2c043434, 0x2c043e24, 0x2c0c040c, 0x2c0c041c, 0x2c0c042c, 0x2c0c0c14,
  3569. 0x2c0c140c, 0x2c0c1c14, 0x2c0c3e14, 0x2c140404, 0x2c140c0c, 0x2c14141c, 0x2c141c04, 0x2c141c34,
  3570. 0x2c142c1c, 0x2c1c0414, 0x2c1c043e, 0x2c1c0c04, 0x2c1c143e, 0x2c1c2424, 0x2c1c2c0c, 0x2c1c342c,
  3571. 0x2c1c3e1c, 0x2c24040c, 0x2c240424, 0x2c241404, 0x2c241c14, 0x2c242434, 0x2c2c0c14, 0x2c2c1434,
  3572. 0x2c2c2c0c, 0x2c2c2c1c, 0x2c342414, 0x2c3e0414, 0x2c3e0424, 0x2c3e1414, 0x34040c0c, 0x34040c1c,
  3573. 0x34040c2c, 0x34041c0c, 0x34041c1c, 0x34043404, 0x340c0404, 0x340c1404, 0x340c143e, 0x340c3424,
  3574. 0x34140c14, 0x34141c24, 0x34142414, 0x34142c2c, 0x34143414, 0x34143e04, 0x341c0404, 0x341c0c24,
  3575. 0x341c140c, 0x341c2404, 0x3424142c, 0x3424241c, 0x34243414, 0x342c0404, 0x342c041c, 0x342c1c24,
  3576. 0x342c3404, 0x3434042c, 0x34342404, 0x343e0c0c, 0x343e0c1c, 0x3e040404, 0x3e040424, 0x3e04043e,
  3577. 0x3e041404, 0x3e041414, 0x3e041c34, 0x3e042404, 0x3e042c24, 0x3e043414, 0x3e0c0414, 0x3e0c0c0c,
  3578. 0x3e0c1424, 0x3e0c241c, 0x3e0c242c, 0x3e14040c, 0x3e140424, 0x3e140c04, 0x3e140c34, 0x3e14140c,
  3579. 0x3e141c04, 0x3e142c0c, 0x3e1c0414, 0x3e1c1c14, 0x3e1c1c2c, 0x3e1c2c1c, 0x3e24040c, 0x3e24042c,
  3580. 0x3e240c1c, 0x3e241404, 0x3e242c04, 0x3e2c1414, 0x3e2c2414, 0x3e340414, 0x3e341c0c, 0x3e3e0404,
  3581. };
  3582. #define NGRID_IQ1S 512
  3583. constexpr constant static uint64_t iq1s_grid[NGRID_IQ1S] = {
  3584. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  3585. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  3586. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  3587. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  3588. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  3589. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  3590. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  3591. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  3592. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  3593. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  3594. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  3595. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  3596. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  3597. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  3598. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  3599. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  3600. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  3601. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  3602. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  3603. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  3604. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  3605. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  3606. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  3607. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  3608. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  3609. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  3610. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  3611. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  3612. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  3613. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  3614. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  3615. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  3616. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  3617. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  3618. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  3619. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  3620. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  3621. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  3622. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  3623. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  3624. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  3625. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  3626. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  3627. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  3628. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  3629. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  3630. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  3631. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  3632. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  3633. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  3634. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  3635. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  3636. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  3637. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  3638. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  3639. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  3640. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  3641. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  3642. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  3643. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  3644. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  3645. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  3646. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  3647. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  3648. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  3649. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  3650. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  3651. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  3652. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  3653. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  3654. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  3655. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  3656. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  3657. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  3658. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  3659. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  3660. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  3661. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  3662. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  3663. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  3664. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  3665. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  3666. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  3667. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  3668. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  3669. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  3670. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  3671. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  3672. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  3673. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  3674. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  3675. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  3676. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  3677. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  3678. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  3679. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  3680. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  3681. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  3682. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  3683. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  3684. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  3685. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  3686. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  3687. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  3688. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  3689. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  3690. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  3691. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  3692. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  3693. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  3694. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  3695. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  3696. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  3697. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  3698. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  3699. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  3700. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  3701. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  3702. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  3703. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  3704. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  3705. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  3706. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  3707. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  3708. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  3709. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  3710. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  3711. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  3712. };
  3713. constexpr constant static uint8_t ksigns_iq2xs[128] = {
  3714. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  3715. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  3716. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  3717. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  3718. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  3719. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  3720. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  3721. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  3722. };
  3723. constexpr constant static uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  3724. void kernel_mul_mv_iq2_xxs_f32_impl(
  3725. device const void * src0,
  3726. device const float * src1,
  3727. device float * dst,
  3728. constant int64_t & ne00,
  3729. constant int64_t & ne01,
  3730. constant int64_t & ne02,
  3731. constant int64_t & ne10,
  3732. constant int64_t & ne12,
  3733. constant int64_t & ne0,
  3734. constant int64_t & ne1,
  3735. constant uint & r2,
  3736. constant uint & r3,
  3737. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3738. uint3 tgpig[[threadgroup_position_in_grid]],
  3739. uint tiisg[[thread_index_in_simdgroup]],
  3740. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3741. const int nb = ne00/QK_K;
  3742. const int r0 = tgpig.x;
  3743. const int r1 = tgpig.y;
  3744. const int im = tgpig.z;
  3745. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3746. const int ib_row = first_row * nb;
  3747. const uint i12 = im%ne12;
  3748. const uint i13 = im/ne12;
  3749. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3750. device const block_iq2_xxs * x = (device const block_iq2_xxs *) src0 + ib_row + offset0;
  3751. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3752. float yl[32];
  3753. float sumf[N_DST]={0.f}, all_sum;
  3754. const int nb32 = nb * (QK_K / 32);
  3755. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3756. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3757. {
  3758. int nval = 4;
  3759. int pos = (32*sgitg + tiisg)*nval;
  3760. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xxs_grid[pos + i];
  3761. nval = 2;
  3762. pos = (32*sgitg + tiisg)*nval;
  3763. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3764. threadgroup_barrier(mem_flags::mem_threadgroup);
  3765. }
  3766. #if QK_K == 256
  3767. const int ix = tiisg;
  3768. device const float * y4 = y + 32 * ix;
  3769. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3770. for (int i = 0; i < 32; ++i) {
  3771. yl[i] = y4[i];
  3772. }
  3773. const int ibl = ib32 / (QK_K / 32);
  3774. const int ib = ib32 % (QK_K / 32);
  3775. device const block_iq2_xxs * xr = x + ibl;
  3776. device const uint16_t * q2 = xr->qs + 4 * ib;
  3777. device const half * dh = &xr->d;
  3778. for (int row = 0; row < N_DST; row++) {
  3779. const float db = dh[0];
  3780. device const uint8_t * aux8 = (device const uint8_t *)q2;
  3781. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3782. const float d = db * (0.5f + (aux32 >> 28));
  3783. float sum = 0;
  3784. for (int l = 0; l < 4; ++l) {
  3785. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + aux8[l]);
  3786. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  3787. for (int j = 0; j < 8; ++j) {
  3788. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3789. }
  3790. }
  3791. sumf[row] += d * sum;
  3792. dh += nb*sizeof(block_iq2_xxs)/2;
  3793. q2 += nb*sizeof(block_iq2_xxs)/2;
  3794. }
  3795. y4 += 32 * 32;
  3796. }
  3797. #else
  3798. (void) x;
  3799. (void) y;
  3800. (void) yl;
  3801. (void) nb32;
  3802. #endif
  3803. for (int row = 0; row < N_DST; ++row) {
  3804. all_sum = simd_sum(sumf[row]);
  3805. if (tiisg == 0) {
  3806. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3807. }
  3808. }
  3809. }
  3810. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  3811. kernel void kernel_mul_mv_iq2_xxs_f32(
  3812. device const void * src0,
  3813. device const float * src1,
  3814. device float * dst,
  3815. constant int64_t & ne00,
  3816. constant int64_t & ne01,
  3817. constant int64_t & ne02,
  3818. constant uint64_t & nb00,
  3819. constant uint64_t & nb01,
  3820. constant uint64_t & nb02,
  3821. constant int64_t & ne10,
  3822. constant int64_t & ne11,
  3823. constant int64_t & ne12,
  3824. constant uint64_t & nb10,
  3825. constant uint64_t & nb11,
  3826. constant uint64_t & nb12,
  3827. constant int64_t & ne0,
  3828. constant int64_t & ne1,
  3829. constant uint & r2,
  3830. constant uint & r3,
  3831. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3832. uint3 tgpig[[threadgroup_position_in_grid]],
  3833. uint tiisg[[thread_index_in_simdgroup]],
  3834. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3835. kernel_mul_mv_iq2_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3836. }
  3837. void kernel_mul_mv_iq2_xs_f32_impl(
  3838. device const void * src0,
  3839. device const float * src1,
  3840. device float * dst,
  3841. constant int64_t & ne00,
  3842. constant int64_t & ne01,
  3843. constant int64_t & ne02,
  3844. constant int64_t & ne10,
  3845. constant int64_t & ne12,
  3846. constant int64_t & ne0,
  3847. constant int64_t & ne1,
  3848. constant uint & r2,
  3849. constant uint & r3,
  3850. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3851. uint3 tgpig[[threadgroup_position_in_grid]],
  3852. uint tiisg[[thread_index_in_simdgroup]],
  3853. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3854. const int nb = ne00/QK_K;
  3855. const int r0 = tgpig.x;
  3856. const int r1 = tgpig.y;
  3857. const int im = tgpig.z;
  3858. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3859. const int ib_row = first_row * nb;
  3860. const uint i12 = im%ne12;
  3861. const uint i13 = im/ne12;
  3862. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3863. device const block_iq2_xs * x = (device const block_iq2_xs *) src0 + ib_row + offset0;
  3864. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3865. float yl[32];
  3866. float sumf[N_DST]={0.f}, all_sum;
  3867. const int nb32 = nb * (QK_K / 32);
  3868. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3869. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 512);
  3870. {
  3871. int nval = 8;
  3872. int pos = (32*sgitg + tiisg)*nval;
  3873. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xs_grid[pos + i];
  3874. nval = 2;
  3875. pos = (32*sgitg + tiisg)*nval;
  3876. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3877. threadgroup_barrier(mem_flags::mem_threadgroup);
  3878. }
  3879. #if QK_K == 256
  3880. const int ix = tiisg;
  3881. device const float * y4 = y + 32 * ix;
  3882. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3883. for (int i = 0; i < 32; ++i) {
  3884. yl[i] = y4[i];
  3885. }
  3886. const int ibl = ib32 / (QK_K / 32);
  3887. const int ib = ib32 % (QK_K / 32);
  3888. device const block_iq2_xs * xr = x + ibl;
  3889. device const uint16_t * q2 = xr->qs + 4 * ib;
  3890. device const uint8_t * sc = xr->scales + ib;
  3891. device const half * dh = &xr->d;
  3892. for (int row = 0; row < N_DST; row++) {
  3893. const float db = dh[0];
  3894. const uint8_t ls1 = sc[0] & 0xf;
  3895. const uint8_t ls2 = sc[0] >> 4;
  3896. const float d1 = db * (0.5f + ls1);
  3897. const float d2 = db * (0.5f + ls2);
  3898. float sum1 = 0, sum2 = 0;
  3899. for (int l = 0; l < 2; ++l) {
  3900. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3901. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3902. for (int j = 0; j < 8; ++j) {
  3903. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3904. }
  3905. }
  3906. for (int l = 2; l < 4; ++l) {
  3907. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3908. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3909. for (int j = 0; j < 8; ++j) {
  3910. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3911. }
  3912. }
  3913. sumf[row] += d1 * sum1 + d2 * sum2;
  3914. dh += nb*sizeof(block_iq2_xs)/2;
  3915. q2 += nb*sizeof(block_iq2_xs)/2;
  3916. sc += nb*sizeof(block_iq2_xs);
  3917. }
  3918. y4 += 32 * 32;
  3919. }
  3920. #else
  3921. (void) x;
  3922. (void) y;
  3923. (void) yl;
  3924. (void) nb32;
  3925. #endif
  3926. for (int row = 0; row < N_DST; ++row) {
  3927. all_sum = simd_sum(sumf[row]);
  3928. if (tiisg == 0) {
  3929. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3930. }
  3931. }
  3932. }
  3933. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  3934. kernel void kernel_mul_mv_iq2_xs_f32(
  3935. device const void * src0,
  3936. device const float * src1,
  3937. device float * dst,
  3938. constant int64_t & ne00,
  3939. constant int64_t & ne01,
  3940. constant int64_t & ne02,
  3941. constant uint64_t & nb00,
  3942. constant uint64_t & nb01,
  3943. constant uint64_t & nb02,
  3944. constant int64_t & ne10,
  3945. constant int64_t & ne11,
  3946. constant int64_t & ne12,
  3947. constant uint64_t & nb10,
  3948. constant uint64_t & nb11,
  3949. constant uint64_t & nb12,
  3950. constant int64_t & ne0,
  3951. constant int64_t & ne1,
  3952. constant uint & r2,
  3953. constant uint & r3,
  3954. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3955. uint3 tgpig[[threadgroup_position_in_grid]],
  3956. uint tiisg[[thread_index_in_simdgroup]],
  3957. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3958. kernel_mul_mv_iq2_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3959. }
  3960. void kernel_mul_mv_iq3_xxs_f32_impl(
  3961. device const void * src0,
  3962. device const float * src1,
  3963. device float * dst,
  3964. constant int64_t & ne00,
  3965. constant int64_t & ne01,
  3966. constant int64_t & ne02,
  3967. constant int64_t & ne10,
  3968. constant int64_t & ne12,
  3969. constant int64_t & ne0,
  3970. constant int64_t & ne1,
  3971. constant uint & r2,
  3972. constant uint & r3,
  3973. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3974. uint3 tgpig[[threadgroup_position_in_grid]],
  3975. uint tiisg[[thread_index_in_simdgroup]],
  3976. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3977. const int nb = ne00/QK_K;
  3978. const int r0 = tgpig.x;
  3979. const int r1 = tgpig.y;
  3980. const int im = tgpig.z;
  3981. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3982. const int ib_row = first_row * nb;
  3983. const uint i12 = im%ne12;
  3984. const uint i13 = im/ne12;
  3985. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3986. device const block_iq3_xxs * x = (device const block_iq3_xxs *) src0 + ib_row + offset0;
  3987. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3988. float yl[32];
  3989. float sumf[N_DST]={0.f}, all_sum;
  3990. const int nb32 = nb * (QK_K / 32);
  3991. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  3992. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3993. {
  3994. int nval = 4;
  3995. int pos = (32*sgitg + tiisg)*nval;
  3996. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xxs_grid[pos + i];
  3997. nval = 2;
  3998. pos = (32*sgitg + tiisg)*nval;
  3999. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  4000. threadgroup_barrier(mem_flags::mem_threadgroup);
  4001. }
  4002. #if QK_K == 256
  4003. const int ix = tiisg;
  4004. device const float * y4 = y + 32 * ix;
  4005. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4006. for (int i = 0; i < 32; ++i) {
  4007. yl[i] = y4[i];
  4008. }
  4009. const int ibl = ib32 / (QK_K / 32);
  4010. const int ib = ib32 % (QK_K / 32);
  4011. device const block_iq3_xxs * xr = x + ibl;
  4012. device const uint8_t * q3 = xr->qs + 8 * ib;
  4013. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  4014. device const half * dh = &xr->d;
  4015. for (int row = 0; row < N_DST; row++) {
  4016. const float db = dh[0];
  4017. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4018. const float d = db * (0.5f + (aux32 >> 28));
  4019. float2 sum = {0};
  4020. for (int l = 0; l < 4; ++l) {
  4021. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + q3[2*l+0]);
  4022. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + q3[2*l+1]);
  4023. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  4024. for (int j = 0; j < 4; ++j) {
  4025. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4026. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4027. }
  4028. }
  4029. sumf[row] += d * (sum[0] + sum[1]);
  4030. dh += nb*sizeof(block_iq3_xxs)/2;
  4031. q3 += nb*sizeof(block_iq3_xxs);
  4032. gas += nb*sizeof(block_iq3_xxs)/2;
  4033. }
  4034. y4 += 32 * 32;
  4035. }
  4036. #else
  4037. (void) x;
  4038. (void) y;
  4039. (void) yl;
  4040. (void) nb32;
  4041. #endif
  4042. for (int row = 0; row < N_DST; ++row) {
  4043. all_sum = simd_sum(sumf[row]);
  4044. if (tiisg == 0) {
  4045. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  4046. }
  4047. }
  4048. }
  4049. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  4050. kernel void kernel_mul_mv_iq3_xxs_f32(
  4051. device const void * src0,
  4052. device const float * src1,
  4053. device float * dst,
  4054. constant int64_t & ne00,
  4055. constant int64_t & ne01,
  4056. constant int64_t & ne02,
  4057. constant uint64_t & nb00,
  4058. constant uint64_t & nb01,
  4059. constant uint64_t & nb02,
  4060. constant int64_t & ne10,
  4061. constant int64_t & ne11,
  4062. constant int64_t & ne12,
  4063. constant uint64_t & nb10,
  4064. constant uint64_t & nb11,
  4065. constant uint64_t & nb12,
  4066. constant int64_t & ne0,
  4067. constant int64_t & ne1,
  4068. constant uint & r2,
  4069. constant uint & r3,
  4070. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4071. uint3 tgpig[[threadgroup_position_in_grid]],
  4072. uint tiisg[[thread_index_in_simdgroup]],
  4073. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4074. kernel_mul_mv_iq3_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4075. }
  4076. void kernel_mul_mv_iq3_s_f32_impl(
  4077. device const void * src0,
  4078. device const float * src1,
  4079. device float * dst,
  4080. constant int64_t & ne00,
  4081. constant int64_t & ne01,
  4082. constant int64_t & ne02,
  4083. constant int64_t & ne10,
  4084. constant int64_t & ne12,
  4085. constant int64_t & ne0,
  4086. constant int64_t & ne1,
  4087. constant uint & r2,
  4088. constant uint & r3,
  4089. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4090. uint3 tgpig[[threadgroup_position_in_grid]],
  4091. uint tiisg[[thread_index_in_simdgroup]],
  4092. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4093. const int nb = ne00/QK_K;
  4094. const int r0 = tgpig.x;
  4095. const int r1 = tgpig.y;
  4096. const int im = tgpig.z;
  4097. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4098. const int ib_row = first_row * nb;
  4099. const uint i12 = im%ne12;
  4100. const uint i13 = im/ne12;
  4101. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4102. device const block_iq3_s * x = (device const block_iq3_s *) src0 + ib_row + offset0;
  4103. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4104. float yl[32];
  4105. float sumf[N_DST]={0.f}, all_sum;
  4106. const int nb32 = nb * (QK_K / 32);
  4107. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  4108. {
  4109. int nval = 8;
  4110. int pos = (32*sgitg + tiisg)*nval;
  4111. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xs_grid[pos + i];
  4112. threadgroup_barrier(mem_flags::mem_threadgroup);
  4113. }
  4114. const int ix = tiisg;
  4115. device const float * y4 = y + 32 * ix;
  4116. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4117. for (int i = 0; i < 32; ++i) {
  4118. yl[i] = y4[i];
  4119. }
  4120. const int ibl = ib32 / (QK_K / 32);
  4121. const int ib = ib32 % (QK_K / 32);
  4122. device const block_iq3_s * xr = x + ibl;
  4123. device const uint8_t * qs = xr->qs + 8 * ib;
  4124. device const uint8_t * qh = xr->qh + ib;
  4125. device const uint8_t * sc = xr->scales + (ib/2);
  4126. device const uint8_t * signs = xr->signs + 4 * ib;
  4127. device const half * dh = &xr->d;
  4128. for (int row = 0; row < N_DST; row++) {
  4129. const float db = dh[0];
  4130. const float d = db * (0.5f + ((sc[0] >> 4*(ib%2)) & 0xf));
  4131. float2 sum = {0};
  4132. for (int l = 0; l < 4; ++l) {
  4133. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  4134. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  4135. for (int j = 0; j < 4; ++j) {
  4136. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  4137. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  4138. }
  4139. }
  4140. sumf[row] += d * (sum[0] + sum[1]);
  4141. dh += nb*sizeof(block_iq3_s)/2;
  4142. qs += nb*sizeof(block_iq3_s);
  4143. qh += nb*sizeof(block_iq3_s);
  4144. sc += nb*sizeof(block_iq3_s);
  4145. signs += nb*sizeof(block_iq3_s);
  4146. }
  4147. y4 += 32 * 32;
  4148. }
  4149. for (int row = 0; row < N_DST; ++row) {
  4150. all_sum = simd_sum(sumf[row]);
  4151. if (tiisg == 0) {
  4152. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  4153. }
  4154. }
  4155. }
  4156. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  4157. kernel void kernel_mul_mv_iq3_s_f32(
  4158. device const void * src0,
  4159. device const float * src1,
  4160. device float * dst,
  4161. constant int64_t & ne00,
  4162. constant int64_t & ne01,
  4163. constant int64_t & ne02,
  4164. constant uint64_t & nb00,
  4165. constant uint64_t & nb01,
  4166. constant uint64_t & nb02,
  4167. constant int64_t & ne10,
  4168. constant int64_t & ne11,
  4169. constant int64_t & ne12,
  4170. constant uint64_t & nb10,
  4171. constant uint64_t & nb11,
  4172. constant uint64_t & nb12,
  4173. constant int64_t & ne0,
  4174. constant int64_t & ne1,
  4175. constant uint & r2,
  4176. constant uint & r3,
  4177. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4178. uint3 tgpig[[threadgroup_position_in_grid]],
  4179. uint tiisg[[thread_index_in_simdgroup]],
  4180. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4181. kernel_mul_mv_iq3_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4182. }
  4183. void kernel_mul_mv_iq2_s_f32_impl(
  4184. device const void * src0,
  4185. device const float * src1,
  4186. device float * dst,
  4187. constant int64_t & ne00,
  4188. constant int64_t & ne01,
  4189. constant int64_t & ne02,
  4190. constant int64_t & ne10,
  4191. constant int64_t & ne12,
  4192. constant int64_t & ne0,
  4193. constant int64_t & ne1,
  4194. constant uint & r2,
  4195. constant uint & r3,
  4196. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4197. uint3 tgpig[[threadgroup_position_in_grid]],
  4198. uint tiisg[[thread_index_in_simdgroup]],
  4199. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4200. const int nb = ne00/QK_K;
  4201. const int r0 = tgpig.x;
  4202. const int r1 = tgpig.y;
  4203. const int im = tgpig.z;
  4204. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4205. const int ib_row = first_row * nb;
  4206. const uint i12 = im%ne12;
  4207. const uint i13 = im/ne12;
  4208. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4209. device const block_iq2_s * x = (device const block_iq2_s *) src0 + ib_row + offset0;
  4210. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4211. float yl[32];
  4212. float sumf[N_DST]={0.f}, all_sum;
  4213. const int nb32 = nb * (QK_K / 32);
  4214. //threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  4215. //{
  4216. // int nval = 32;
  4217. // int pos = (32*sgitg + tiisg)*nval;
  4218. // for (int i = 0; i < nval; ++i) values[pos + i] = iq2s_grid[pos + i];
  4219. // threadgroup_barrier(mem_flags::mem_threadgroup);
  4220. //}
  4221. const int ix = tiisg;
  4222. device const float * y4 = y + 32 * ix;
  4223. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4224. for (int i = 0; i < 32; ++i) {
  4225. yl[i] = y4[i];
  4226. }
  4227. const int ibl = ib32 / (QK_K / 32);
  4228. const int ib = ib32 % (QK_K / 32);
  4229. device const block_iq2_s * xr = x + ibl;
  4230. device const uint8_t * qs = xr->qs + 4 * ib;
  4231. device const uint8_t * qh = xr->qh + ib;
  4232. device const uint8_t * sc = xr->scales + ib;
  4233. device const uint8_t * signs = qs + QK_K/8;
  4234. device const half * dh = &xr->d;
  4235. for (int row = 0; row < N_DST; row++) {
  4236. const float db = dh[0];
  4237. const float d1 = db * (0.5f + (sc[0] & 0xf));
  4238. const float d2 = db * (0.5f + (sc[0] >> 4));
  4239. float2 sum = {0};
  4240. for (int l = 0; l < 2; ++l) {
  4241. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4242. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4243. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4244. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4245. for (int j = 0; j < 8; ++j) {
  4246. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  4247. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  4248. }
  4249. }
  4250. sumf[row] += d1 * sum[0] + d2 * sum[1];
  4251. dh += nb*sizeof(block_iq2_s)/2;
  4252. qs += nb*sizeof(block_iq2_s);
  4253. qh += nb*sizeof(block_iq2_s);
  4254. sc += nb*sizeof(block_iq2_s);
  4255. signs += nb*sizeof(block_iq2_s);
  4256. }
  4257. y4 += 32 * 32;
  4258. }
  4259. for (int row = 0; row < N_DST; ++row) {
  4260. all_sum = simd_sum(sumf[row]);
  4261. if (tiisg == 0) {
  4262. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  4263. }
  4264. }
  4265. }
  4266. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  4267. kernel void kernel_mul_mv_iq2_s_f32(
  4268. device const void * src0,
  4269. device const float * src1,
  4270. device float * dst,
  4271. constant int64_t & ne00,
  4272. constant int64_t & ne01,
  4273. constant int64_t & ne02,
  4274. constant uint64_t & nb00,
  4275. constant uint64_t & nb01,
  4276. constant uint64_t & nb02,
  4277. constant int64_t & ne10,
  4278. constant int64_t & ne11,
  4279. constant int64_t & ne12,
  4280. constant uint64_t & nb10,
  4281. constant uint64_t & nb11,
  4282. constant uint64_t & nb12,
  4283. constant int64_t & ne0,
  4284. constant int64_t & ne1,
  4285. constant uint & r2,
  4286. constant uint & r3,
  4287. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4288. uint3 tgpig[[threadgroup_position_in_grid]],
  4289. uint tiisg[[thread_index_in_simdgroup]],
  4290. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4291. kernel_mul_mv_iq2_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4292. }
  4293. void kernel_mul_mv_iq1_s_f32_impl(
  4294. device const void * src0,
  4295. device const float * src1,
  4296. device float * dst,
  4297. constant int64_t & ne00,
  4298. constant int64_t & ne01,
  4299. constant int64_t & ne02,
  4300. constant int64_t & ne10,
  4301. constant int64_t & ne12,
  4302. constant int64_t & ne0,
  4303. constant int64_t & ne1,
  4304. constant uint & r2,
  4305. constant uint & r3,
  4306. uint3 tgpig[[threadgroup_position_in_grid]],
  4307. uint tiisg[[thread_index_in_simdgroup]],
  4308. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4309. const int nb = ne00/QK_K;
  4310. const int r0 = tgpig.x;
  4311. const int r1 = tgpig.y;
  4312. const int im = tgpig.z;
  4313. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4314. const int ib_row = first_row * nb;
  4315. const uint i12 = im%ne12;
  4316. const uint i13 = im/ne12;
  4317. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4318. device const block_iq1_s * x = (device const block_iq1_s *) src0 + ib_row + offset0;
  4319. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4320. float yl[16];
  4321. float sumf[N_DST]={0.f}, all_sum;
  4322. const int nb32 = nb * (QK_K / 32);
  4323. #if QK_K == 256
  4324. const int ix = tiisg/2;
  4325. const int il = tiisg%2;
  4326. device const float * y4 = y + 32 * ix + 16 * il;
  4327. for (int ib32 = ix; ib32 < nb32; ib32 += 16) {
  4328. for (int i = 0; i < 16; ++i) {
  4329. yl[i] = y4[i];
  4330. }
  4331. const int ibl = ib32 / (QK_K / 32);
  4332. const int ib = ib32 % (QK_K / 32);
  4333. device const block_iq1_s * xr = x + ibl;
  4334. device const uint8_t * qs = xr->qs + 4 * ib + 2 * il;
  4335. device const uint8_t * sc = xr->scales + 2 * ib + il;
  4336. device const half * dh = &xr->d;
  4337. for (int row = 0; row < N_DST; row++) {
  4338. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4339. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4340. float2 sum = {0};
  4341. for (int j = 0; j < 8; ++j) {
  4342. sum[0] += yl[j+ 0] * grid1[j];
  4343. sum[1] += yl[j+ 8] * grid2[j];
  4344. }
  4345. sumf[row] += (float)dh[0] * (sum[0] * (2*(sc[0] & 7) + 1) + sum[1] * (2*((sc[0] >> 4) & 7) + 1));
  4346. dh += nb*sizeof(block_iq1_s)/2;
  4347. qs += nb*sizeof(block_iq1_s);
  4348. sc += nb*sizeof(block_iq1_s);
  4349. }
  4350. y4 += 16 * 32;
  4351. }
  4352. #else
  4353. (void) x;
  4354. (void) y;
  4355. (void) yl;
  4356. (void) nb32;
  4357. #endif
  4358. for (int row = 0; row < N_DST; ++row) {
  4359. all_sum = simd_sum(sumf[row]);
  4360. if (tiisg == 0) {
  4361. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4362. }
  4363. }
  4364. }
  4365. constexpr constant static float kvalues_iq4nl_f[16] = {
  4366. -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
  4367. };
  4368. void kernel_mul_mv_iq4_nl_f32_impl(
  4369. device const void * src0,
  4370. device const float * src1,
  4371. device float * dst,
  4372. constant int64_t & ne00,
  4373. constant int64_t & ne01,
  4374. constant int64_t & ne02,
  4375. constant int64_t & ne10,
  4376. constant int64_t & ne12,
  4377. constant int64_t & ne0,
  4378. constant int64_t & ne1,
  4379. constant uint & r2,
  4380. constant uint & r3,
  4381. threadgroup float * shared_values [[threadgroup(0)]],
  4382. uint3 tgpig[[threadgroup_position_in_grid]],
  4383. uint tiisg[[thread_index_in_simdgroup]],
  4384. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4385. const int nb = ne00/QK4_NL;
  4386. const int r0 = tgpig.x;
  4387. const int r1 = tgpig.y;
  4388. const int im = tgpig.z;
  4389. const int first_row = (r0 * 2 + sgitg) * 2;
  4390. const int ib_row = first_row * nb;
  4391. const uint i12 = im%ne12;
  4392. const uint i13 = im/ne12;
  4393. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4394. device const block_iq4_nl * x = (device const block_iq4_nl *) src0 + ib_row + offset0;
  4395. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4396. const int ix = tiisg/2; // 0...15
  4397. const int it = tiisg%2; // 0 or 1
  4398. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4399. threadgroup_barrier(mem_flags::mem_threadgroup);
  4400. float4 yl[4];
  4401. float sumf[2]={0.f}, all_sum;
  4402. device const float * yb = y + ix * QK4_NL + it * 8;
  4403. uint32_t aux32[2];
  4404. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4405. float4 qf1, qf2;
  4406. for (int ib = ix; ib < nb; ib += 16) {
  4407. device const float4 * y4 = (device const float4 *)yb;
  4408. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4409. for (int row = 0; row < 2; ++row) {
  4410. device const block_iq4_nl & xb = x[row*nb + ib];
  4411. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  4412. float4 acc1 = {0.f}, acc2 = {0.f};
  4413. aux32[0] = q4[0] | (q4[1] << 16);
  4414. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4415. aux32[0] &= 0x0f0f0f0f;
  4416. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4417. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4418. acc1 += yl[0] * qf1;
  4419. acc2 += yl[1] * qf2;
  4420. aux32[0] = q4[2] | (q4[3] << 16);
  4421. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4422. aux32[0] &= 0x0f0f0f0f;
  4423. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4424. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4425. acc1 += yl[2] * qf1;
  4426. acc2 += yl[3] * qf2;
  4427. acc1 += acc2;
  4428. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4429. }
  4430. yb += 16 * QK4_NL;
  4431. }
  4432. for (int row = 0; row < 2; ++row) {
  4433. all_sum = simd_sum(sumf[row]);
  4434. if (tiisg == 0) {
  4435. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4436. }
  4437. }
  4438. }
  4439. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  4440. kernel void kernel_mul_mv_iq1_s_f32(
  4441. device const void * src0,
  4442. device const float * src1,
  4443. device float * dst,
  4444. constant int64_t & ne00,
  4445. constant int64_t & ne01,
  4446. constant int64_t & ne02,
  4447. constant uint64_t & nb00,
  4448. constant uint64_t & nb01,
  4449. constant uint64_t & nb02,
  4450. constant int64_t & ne10,
  4451. constant int64_t & ne11,
  4452. constant int64_t & ne12,
  4453. constant uint64_t & nb10,
  4454. constant uint64_t & nb11,
  4455. constant uint64_t & nb12,
  4456. constant int64_t & ne0,
  4457. constant int64_t & ne1,
  4458. constant uint & r2,
  4459. constant uint & r3,
  4460. uint3 tgpig[[threadgroup_position_in_grid]],
  4461. uint tiisg[[thread_index_in_simdgroup]],
  4462. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4463. kernel_mul_mv_iq1_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  4464. }
  4465. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  4466. kernel void kernel_mul_mv_iq4_nl_f32(
  4467. device const void * src0,
  4468. device const float * src1,
  4469. device float * dst,
  4470. constant int64_t & ne00,
  4471. constant int64_t & ne01,
  4472. constant int64_t & ne02,
  4473. constant uint64_t & nb00,
  4474. constant uint64_t & nb01,
  4475. constant uint64_t & nb02,
  4476. constant int64_t & ne10,
  4477. constant int64_t & ne11,
  4478. constant int64_t & ne12,
  4479. constant uint64_t & nb10,
  4480. constant uint64_t & nb11,
  4481. constant uint64_t & nb12,
  4482. constant int64_t & ne0,
  4483. constant int64_t & ne1,
  4484. constant uint & r2,
  4485. constant uint & r3,
  4486. threadgroup float * shared_values [[threadgroup(0)]],
  4487. uint3 tgpig[[threadgroup_position_in_grid]],
  4488. uint tiisg[[thread_index_in_simdgroup]],
  4489. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4490. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4491. }
  4492. //============================= templates and their specializations =============================
  4493. // NOTE: this is not dequantizing - we are simply fitting the template
  4494. template <typename type4x4>
  4495. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  4496. float4x4 temp = *(((device float4x4 *)src));
  4497. for (int i = 0; i < 16; i++){
  4498. reg[i/4][i%4] = temp[i/4][i%4];
  4499. }
  4500. }
  4501. template <typename type4x4>
  4502. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  4503. half4x4 temp = *(((device half4x4 *)src));
  4504. for (int i = 0; i < 16; i++){
  4505. reg[i/4][i%4] = temp[i/4][i%4];
  4506. }
  4507. }
  4508. template <typename type4x4>
  4509. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  4510. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  4511. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4512. const float d2 = d1 / 256.f;
  4513. const float md = -8.h * xb->d;
  4514. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4515. const ushort mask1 = mask0 << 8;
  4516. for (int i=0;i<8;i++) {
  4517. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  4518. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  4519. }
  4520. }
  4521. template <typename type4x4>
  4522. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  4523. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  4524. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4525. const float d2 = d1 / 256.f;
  4526. const float m = xb->m;
  4527. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4528. const ushort mask1 = mask0 << 8;
  4529. for (int i=0;i<8;i++) {
  4530. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  4531. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  4532. }
  4533. }
  4534. template <typename type4x4>
  4535. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  4536. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  4537. const float d = xb->d;
  4538. const float md = -16.h * xb->d;
  4539. const ushort mask = il ? 0x00F0 : 0x000F;
  4540. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4541. const int x_mv = il ? 4 : 0;
  4542. const int gh_mv = il ? 12 : 0;
  4543. const int gh_bk = il ? 0 : 4;
  4544. for (int i = 0; i < 8; i++) {
  4545. // extract the 5-th bits for x0 and x1
  4546. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4547. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4548. // combine the 4-bits from qs with the 5th bit
  4549. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4550. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4551. reg[i/2][2*(i%2)+0] = d * x0 + md;
  4552. reg[i/2][2*(i%2)+1] = d * x1 + md;
  4553. }
  4554. }
  4555. template <typename type4x4>
  4556. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  4557. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  4558. const float d = xb->d;
  4559. const float m = xb->m;
  4560. const ushort mask = il ? 0x00F0 : 0x000F;
  4561. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4562. const int x_mv = il ? 4 : 0;
  4563. const int gh_mv = il ? 12 : 0;
  4564. const int gh_bk = il ? 0 : 4;
  4565. for (int i = 0; i < 8; i++) {
  4566. // extract the 5-th bits for x0 and x1
  4567. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4568. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4569. // combine the 4-bits from qs with the 5th bit
  4570. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4571. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4572. reg[i/2][2*(i%2)+0] = d * x0 + m;
  4573. reg[i/2][2*(i%2)+1] = d * x1 + m;
  4574. }
  4575. }
  4576. template <typename type4x4>
  4577. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  4578. device const int8_t * qs = ((device const int8_t *)xb->qs);
  4579. const half d = xb->d;
  4580. for (int i = 0; i < 16; i++) {
  4581. reg[i/4][i%4] = (qs[i + 16*il] * d);
  4582. }
  4583. }
  4584. template <typename type4x4>
  4585. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  4586. const float d = xb->d;
  4587. const float min = xb->dmin;
  4588. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4589. float dl, ml;
  4590. uint8_t sc = xb->scales[il];
  4591. #if QK_K == 256
  4592. q = q + 32*(il/8) + 16*(il&1);
  4593. il = (il/2)%4;
  4594. #endif
  4595. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4596. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4597. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  4598. for (int i = 0; i < 16; ++i) {
  4599. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4600. }
  4601. }
  4602. template <typename type4x4>
  4603. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  4604. const half d_all = xb->d;
  4605. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4606. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  4607. device const int8_t * scales = (device const int8_t *)xb->scales;
  4608. #if QK_K == 256
  4609. q = q + 32 * (il/8) + 16 * (il&1);
  4610. h = h + 16 * (il&1);
  4611. uint8_t m = 1 << (il/2);
  4612. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  4613. ((il/4)>0 ? 12 : 3);
  4614. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  4615. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  4616. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  4617. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  4618. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  4619. const float ml = 4.f * dl;
  4620. il = (il/2) & 3;
  4621. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4622. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4623. dl *= coef;
  4624. for (int i = 0; i < 16; ++i) {
  4625. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  4626. }
  4627. #else
  4628. float kcoef = il&1 ? 1.f/16.f : 1.f;
  4629. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  4630. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  4631. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4632. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4633. uint8_t m = 1<<(il*2);
  4634. for (int i = 0; i < 16; ++i) {
  4635. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  4636. }
  4637. #endif
  4638. }
  4639. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  4640. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  4641. : 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))};
  4642. }
  4643. template <typename type4x4>
  4644. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  4645. device const uchar * q = xb->qs;
  4646. #if QK_K == 256
  4647. short is = (il/4) * 2;
  4648. q = q + (il/4) * 32 + 16 * (il&1);
  4649. il = il & 3;
  4650. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4651. const float d = il < 2 ? xb->d : xb->d / 16.h;
  4652. const float min = xb->dmin;
  4653. const float dl = d * sc[0];
  4654. const float ml = min * sc[1];
  4655. #else
  4656. (void) get_scale_min_k4_just2;
  4657. q = q + 16 * (il&1);
  4658. device const uint8_t * s = xb->scales;
  4659. device const half2 * dh = (device const half2 *)xb->d;
  4660. const float2 d = (float2)dh[0];
  4661. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  4662. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  4663. #endif
  4664. const ushort mask = il<2 ? 0x0F : 0xF0;
  4665. for (int i = 0; i < 16; ++i) {
  4666. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4667. }
  4668. }
  4669. template <typename type4x4>
  4670. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  4671. device const uint8_t * q = xb->qs;
  4672. device const uint8_t * qh = xb->qh;
  4673. #if QK_K == 256
  4674. short is = (il/4) * 2;
  4675. q = q + 32 * (il/4) + 16 * (il&1);
  4676. qh = qh + 16 * (il&1);
  4677. uint8_t ul = 1 << (il/2);
  4678. il = il & 3;
  4679. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4680. const float d = il < 2 ? xb->d : xb->d / 16.f;
  4681. const float min = xb->dmin;
  4682. const float dl = d * sc[0];
  4683. const float ml = min * sc[1];
  4684. const ushort mask = il<2 ? 0x0F : 0xF0;
  4685. const float qh_val = il<2 ? 16.f : 256.f;
  4686. for (int i = 0; i < 16; ++i) {
  4687. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  4688. }
  4689. #else
  4690. q = q + 16 * (il&1);
  4691. device const int8_t * s = xb->scales;
  4692. const float dl = xb->d * s[il];
  4693. uint8_t m = 1<<(il*2);
  4694. const float coef = il<2 ? 1.f : 1.f/16.f;
  4695. const ushort mask = il<2 ? 0x0F : 0xF0;
  4696. for (int i = 0; i < 16; ++i) {
  4697. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  4698. }
  4699. #endif
  4700. }
  4701. template <typename type4x4>
  4702. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  4703. const half d_all = xb->d;
  4704. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  4705. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  4706. device const int8_t * scales = (device const int8_t *)xb->scales;
  4707. #if QK_K == 256
  4708. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  4709. qh = qh + 32*(il/8) + 16*(il&1);
  4710. float sc = scales[(il%2) + 2 * ((il/2))];
  4711. il = (il/2) & 3;
  4712. #else
  4713. ql = ql + 16 * (il&1);
  4714. float sc = scales[il];
  4715. #endif
  4716. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4717. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  4718. const float coef = il>1 ? 1.f/16.f : 1.f;
  4719. const float ml = d_all * sc * 32.f;
  4720. const float dl = d_all * sc * coef;
  4721. for (int i = 0; i < 16; ++i) {
  4722. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  4723. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  4724. reg[i/4][i%4] = dl * q - ml;
  4725. }
  4726. }
  4727. template <typename type4x4>
  4728. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  4729. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4730. const float d = xb->d;
  4731. const int ib32 = il/2;
  4732. il = il%2;
  4733. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4734. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  4735. device const uint16_t * q2 = xb->qs + 4*ib32;
  4736. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  4737. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  4738. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  4739. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  4740. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4741. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  4742. for (int i = 0; i < 8; ++i) {
  4743. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4744. }
  4745. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4746. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  4747. for (int i = 0; i < 8; ++i) {
  4748. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4749. }
  4750. }
  4751. template <typename type4x4>
  4752. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  4753. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4754. const float d = xb->d;
  4755. const int ib32 = il/2;
  4756. il = il%2;
  4757. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4758. device const uint16_t * q2 = xb->qs + 4*ib32;
  4759. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4760. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  4761. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  4762. for (int i = 0; i < 8; ++i) {
  4763. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4764. }
  4765. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  4766. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  4767. for (int i = 0; i < 8; ++i) {
  4768. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4769. }
  4770. }
  4771. template <typename type4x4>
  4772. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  4773. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4774. const float d = xb->d;
  4775. const int ib32 = il/2;
  4776. il = il%2;
  4777. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4778. device const uint8_t * q3 = xb->qs + 8*ib32;
  4779. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  4780. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4781. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  4782. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  4783. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  4784. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4785. for (int i = 0; i < 4; ++i) {
  4786. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4787. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4788. }
  4789. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  4790. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  4791. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  4792. for (int i = 0; i < 4; ++i) {
  4793. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4794. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4795. }
  4796. }
  4797. template <typename type4x4>
  4798. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  4799. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4800. const float d = xb->d;
  4801. const int ib32 = il/2;
  4802. il = il%2;
  4803. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4804. device const uint8_t * qs = xb->qs + 8*ib32;
  4805. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  4806. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4807. const float dl = d * (0.5f + ((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * 0.5f;
  4808. constant uint8_t * grid1 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  4809. constant uint8_t * grid2 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  4810. for (int i = 0; i < 4; ++i) {
  4811. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  4812. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  4813. }
  4814. grid1 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  4815. grid2 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  4816. for (int i = 0; i < 4; ++i) {
  4817. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  4818. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  4819. }
  4820. }
  4821. template <typename type4x4>
  4822. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  4823. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4824. const float d = xb->d;
  4825. const int ib32 = il/2;
  4826. il = il%2;
  4827. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4828. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4829. device const uint8_t * signs = qs + QK_K/8;
  4830. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4831. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4832. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  4833. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  4834. for (int i = 0; i < 8; ++i) {
  4835. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  4836. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  4837. }
  4838. }
  4839. template <typename type4x4>
  4840. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  4841. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4842. const float d = xb->d;
  4843. device const uint8_t * qs = xb->qs + 2*il;
  4844. device const uint8_t * sc = xb->scales + il;
  4845. const float dl1 = d * (2*(sc[0] & 7) + 1);
  4846. const float dl2 = d * (2*((sc[0] >> 4) & 7) + 1);
  4847. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4848. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4849. for (int i = 0; i < 8; ++i) {
  4850. reg[i/4+0][i%4] = dl1 * grid1[i];
  4851. reg[i/4+2][i%4] = dl2 * grid2[i];
  4852. }
  4853. }
  4854. template <typename type4x4>
  4855. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  4856. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  4857. const float d = xb->d;
  4858. uint32_t aux32;
  4859. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4860. for (int i = 0; i < 4; ++i) {
  4861. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  4862. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4863. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4864. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4865. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4866. }
  4867. }
  4868. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  4869. kernel void kernel_get_rows(
  4870. device const void * src0,
  4871. device const char * src1,
  4872. device float * dst,
  4873. constant int64_t & ne00,
  4874. constant uint64_t & nb01,
  4875. constant uint64_t & nb02,
  4876. constant int64_t & ne10,
  4877. constant uint64_t & nb10,
  4878. constant uint64_t & nb11,
  4879. constant uint64_t & nb1,
  4880. constant uint64_t & nb2,
  4881. uint3 tgpig[[threadgroup_position_in_grid]],
  4882. uint tiitg[[thread_index_in_threadgroup]],
  4883. uint3 tptg [[threads_per_threadgroup]]) {
  4884. //const int64_t i = tgpig;
  4885. //const int64_t r = ((device int32_t *) src1)[i];
  4886. const int64_t i10 = tgpig.x;
  4887. const int64_t i11 = tgpig.y;
  4888. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4889. const int64_t i02 = i11;
  4890. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  4891. float4x4 temp;
  4892. dequantize_func(
  4893. ((device const block_q *) ((device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  4894. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  4895. }
  4896. }
  4897. kernel void kernel_get_rows_f32(
  4898. device const void * src0,
  4899. device const char * src1,
  4900. device float * dst,
  4901. constant int64_t & ne00,
  4902. constant uint64_t & nb01,
  4903. constant uint64_t & nb02,
  4904. constant int64_t & ne10,
  4905. constant uint64_t & nb10,
  4906. constant uint64_t & nb11,
  4907. constant uint64_t & nb1,
  4908. constant uint64_t & nb2,
  4909. uint3 tgpig[[threadgroup_position_in_grid]],
  4910. uint tiitg[[thread_index_in_threadgroup]],
  4911. uint3 tptg [[threads_per_threadgroup]]) {
  4912. const int64_t i10 = tgpig.x;
  4913. const int64_t i11 = tgpig.y;
  4914. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4915. const int64_t i02 = i11;
  4916. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4917. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4918. ((device float *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4919. }
  4920. }
  4921. kernel void kernel_get_rows_f16(
  4922. device const void * src0,
  4923. device const char * src1,
  4924. device float * dst,
  4925. constant int64_t & ne00,
  4926. constant uint64_t & nb01,
  4927. constant uint64_t & nb02,
  4928. constant int64_t & ne10,
  4929. constant uint64_t & nb10,
  4930. constant uint64_t & nb11,
  4931. constant uint64_t & nb1,
  4932. constant uint64_t & nb2,
  4933. uint3 tgpig[[threadgroup_position_in_grid]],
  4934. uint tiitg[[thread_index_in_threadgroup]],
  4935. uint3 tptg [[threads_per_threadgroup]]) {
  4936. const int64_t i10 = tgpig.x;
  4937. const int64_t i11 = tgpig.y;
  4938. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4939. const int64_t i02 = i11;
  4940. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4941. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4942. ((device half *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4943. }
  4944. }
  4945. kernel void kernel_get_rows_i32(
  4946. device const void * src0,
  4947. device const char * src1,
  4948. device int32_t * dst,
  4949. constant int64_t & ne00,
  4950. constant uint64_t & nb01,
  4951. constant uint64_t & nb02,
  4952. constant int64_t & ne10,
  4953. constant uint64_t & nb10,
  4954. constant uint64_t & nb11,
  4955. constant uint64_t & nb1,
  4956. constant uint64_t & nb2,
  4957. uint3 tgpig[[threadgroup_position_in_grid]],
  4958. uint tiitg[[thread_index_in_threadgroup]],
  4959. uint3 tptg [[threads_per_threadgroup]]) {
  4960. const int64_t i10 = tgpig.x;
  4961. const int64_t i11 = tgpig.y;
  4962. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4963. const int64_t i02 = i11;
  4964. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4965. ((device int32_t *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4966. ((device int32_t *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4967. }
  4968. }
  4969. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  4970. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  4971. #define BLOCK_SIZE_K 32
  4972. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  4973. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  4974. #define THREAD_PER_BLOCK 128
  4975. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  4976. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  4977. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  4978. #define SG_MAT_ROW 8
  4979. // each block_q contains 16*nl weights
  4980. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  4981. void kernel_mul_mm_impl(device const uchar * src0,
  4982. device const uchar * src1,
  4983. device float * dst,
  4984. constant int64_t & ne00,
  4985. constant int64_t & ne02,
  4986. constant uint64_t & nb01,
  4987. constant uint64_t & nb02,
  4988. constant int64_t & ne12,
  4989. constant uint64_t & nb10,
  4990. constant uint64_t & nb11,
  4991. constant uint64_t & nb12,
  4992. constant int64_t & ne0,
  4993. constant int64_t & ne1,
  4994. constant uint & r2,
  4995. constant uint & r3,
  4996. threadgroup uchar * shared_memory [[threadgroup(0)]],
  4997. uint3 tgpig[[threadgroup_position_in_grid]],
  4998. uint tiitg[[thread_index_in_threadgroup]],
  4999. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5000. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5001. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5002. const uint r0 = tgpig.y;
  5003. const uint r1 = tgpig.x;
  5004. const uint im = tgpig.z;
  5005. // if this block is of 64x32 shape or smaller
  5006. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5007. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5008. // a thread shouldn't load data outside of the matrix
  5009. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5010. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5011. simdgroup_half8x8 ma[4];
  5012. simdgroup_float8x8 mb[2];
  5013. simdgroup_float8x8 c_res[8];
  5014. for (int i = 0; i < 8; i++){
  5015. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5016. }
  5017. short il = (tiitg % THREAD_PER_ROW);
  5018. const uint i12 = im%ne12;
  5019. const uint i13 = im/ne12;
  5020. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5021. ushort offset1 = il/nl;
  5022. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5023. device const float * y = (device const float *)(src1
  5024. + nb12 * im
  5025. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  5026. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5027. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5028. // load data and store to threadgroup memory
  5029. half4x4 temp_a;
  5030. dequantize_func(x, il, temp_a);
  5031. threadgroup_barrier(mem_flags::mem_threadgroup);
  5032. #pragma unroll(16)
  5033. for (int i = 0; i < 16; i++) {
  5034. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5035. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5036. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5037. }
  5038. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5039. il = (il + 2 < nl) ? il + 2 : il % 2;
  5040. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5041. y += BLOCK_SIZE_K;
  5042. threadgroup_barrier(mem_flags::mem_threadgroup);
  5043. // load matrices from threadgroup memory and conduct outer products
  5044. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5045. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5046. #pragma unroll(4)
  5047. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5048. #pragma unroll(4)
  5049. for (int i = 0; i < 4; i++) {
  5050. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5051. }
  5052. simdgroup_barrier(mem_flags::mem_none);
  5053. #pragma unroll(2)
  5054. for (int i = 0; i < 2; i++) {
  5055. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5056. }
  5057. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5058. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5059. #pragma unroll(8)
  5060. for (int i = 0; i < 8; i++){
  5061. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5062. }
  5063. }
  5064. }
  5065. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  5066. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  5067. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  5068. for (int i = 0; i < 8; i++) {
  5069. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  5070. }
  5071. } else {
  5072. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  5073. threadgroup_barrier(mem_flags::mem_threadgroup);
  5074. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5075. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5076. for (int i = 0; i < 8; i++) {
  5077. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5078. }
  5079. threadgroup_barrier(mem_flags::mem_threadgroup);
  5080. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  5081. if (sgitg == 0) {
  5082. for (int i = 0; i < n_rows; i++) {
  5083. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5084. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5085. }
  5086. }
  5087. }
  5088. }
  5089. }
  5090. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in src1ids
  5091. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5092. void kernel_mul_mm_id_impl(
  5093. device const uchar * src0,
  5094. device const uchar * src1,
  5095. thread short * src1ids,
  5096. device float * dst,
  5097. constant int64_t & ne00,
  5098. constant int64_t & ne02,
  5099. constant uint64_t & nb01,
  5100. constant uint64_t & nb02,
  5101. constant int64_t & ne12,
  5102. constant uint64_t & nb10,
  5103. constant uint64_t & nb11,
  5104. constant uint64_t & nb12,
  5105. constant int64_t & ne0,
  5106. int64_t ne1,
  5107. constant uint & r2,
  5108. constant uint & r3,
  5109. threadgroup uchar * shared_memory,
  5110. uint3 tgpig[[threadgroup_position_in_grid]],
  5111. uint tiitg[[thread_index_in_threadgroup]],
  5112. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5113. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5114. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5115. const uint r0 = tgpig.y;
  5116. const uint r1 = tgpig.x;
  5117. const uint im = tgpig.z;
  5118. if (r1 * BLOCK_SIZE_N >= ne1) return;
  5119. // if this block is of 64x32 shape or smaller
  5120. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5121. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5122. // a thread shouldn't load data outside of the matrix
  5123. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5124. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5125. simdgroup_half8x8 ma[4];
  5126. simdgroup_float8x8 mb[2];
  5127. simdgroup_float8x8 c_res[8];
  5128. for (int i = 0; i < 8; i++){
  5129. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5130. }
  5131. short il = (tiitg % THREAD_PER_ROW);
  5132. const uint i12 = im%ne12;
  5133. const uint i13 = im/ne12;
  5134. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5135. ushort offset1 = il/nl;
  5136. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5137. device const float * y = (device const float *)(src1
  5138. + nb12 * im
  5139. + nb11 * src1ids[r1 * BLOCK_SIZE_N + thread_col]
  5140. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5141. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5142. // load data and store to threadgroup memory
  5143. half4x4 temp_a;
  5144. dequantize_func(x, il, temp_a);
  5145. threadgroup_barrier(mem_flags::mem_threadgroup);
  5146. for (int i = 0; i < 16; i++) {
  5147. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5148. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5149. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5150. }
  5151. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5152. il = (il + 2 < nl) ? il + 2 : il % 2;
  5153. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5154. y += BLOCK_SIZE_K;
  5155. threadgroup_barrier(mem_flags::mem_threadgroup);
  5156. // load matrices from threadgroup memory and conduct outer products
  5157. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5158. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5159. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5160. for (int i = 0; i < 4; i++) {
  5161. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5162. }
  5163. simdgroup_barrier(mem_flags::mem_none);
  5164. for (int i = 0; i < 2; i++) {
  5165. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5166. }
  5167. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5168. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5169. for (int i = 0; i < 8; i++){
  5170. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5171. }
  5172. }
  5173. }
  5174. {
  5175. threadgroup_barrier(mem_flags::mem_threadgroup);
  5176. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5177. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5178. for (int i = 0; i < 8; i++) {
  5179. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5180. }
  5181. threadgroup_barrier(mem_flags::mem_threadgroup);
  5182. device float * C = dst + (BLOCK_SIZE_M * r0) + im*ne1*ne0;
  5183. if (sgitg == 0) {
  5184. for (int i = 0; i < n_rows; i++) {
  5185. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5186. *(C + i + src1ids[j + r1*BLOCK_SIZE_N] * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5187. }
  5188. }
  5189. }
  5190. }
  5191. }
  5192. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5193. kernel void kernel_mul_mm(device const uchar * src0,
  5194. device const uchar * src1,
  5195. device float * dst,
  5196. constant int64_t & ne00,
  5197. constant int64_t & ne02,
  5198. constant uint64_t & nb01,
  5199. constant uint64_t & nb02,
  5200. constant int64_t & ne12,
  5201. constant uint64_t & nb10,
  5202. constant uint64_t & nb11,
  5203. constant uint64_t & nb12,
  5204. constant int64_t & ne0,
  5205. constant int64_t & ne1,
  5206. constant uint & r2,
  5207. constant uint & r3,
  5208. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5209. uint3 tgpig[[threadgroup_position_in_grid]],
  5210. uint tiitg[[thread_index_in_threadgroup]],
  5211. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5212. kernel_mul_mm_impl<block_q, nl, dequantize_func>(
  5213. src0,
  5214. src1,
  5215. dst,
  5216. ne00,
  5217. ne02,
  5218. nb01,
  5219. nb02,
  5220. ne12,
  5221. nb10,
  5222. nb11,
  5223. nb12,
  5224. ne0,
  5225. ne1,
  5226. r2,
  5227. r3,
  5228. shared_memory,
  5229. tgpig,
  5230. tiitg,
  5231. sgitg);
  5232. }
  5233. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5234. kernel void kernel_mul_mm_id(
  5235. device const uchar * ids,
  5236. device const uchar * src1,
  5237. device float * dst,
  5238. constant uint64_t & nbi1,
  5239. constant int64_t & ne00,
  5240. constant int64_t & ne02,
  5241. constant uint64_t & nb01,
  5242. constant uint64_t & nb02,
  5243. constant int64_t & ne12,
  5244. constant int64_t & ne13,
  5245. constant uint64_t & nb10,
  5246. constant uint64_t & nb11,
  5247. constant uint64_t & nb12,
  5248. constant int64_t & ne0,
  5249. constant int64_t & ne1,
  5250. constant uint64_t & nb1,
  5251. constant uint & r2,
  5252. constant uint & r3,
  5253. constant int & idx,
  5254. device const uchar * src00,
  5255. device const uchar * src01,
  5256. device const uchar * src02,
  5257. device const uchar * src03,
  5258. device const uchar * src04,
  5259. device const uchar * src05,
  5260. device const uchar * src06,
  5261. device const uchar * src07,
  5262. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5263. uint3 tgpig[[threadgroup_position_in_grid]],
  5264. uint tiitg[[thread_index_in_threadgroup]],
  5265. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5266. device const uchar * src0s[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5267. // expert id
  5268. const int32_t id = tgpig.z/(ne12*ne13);
  5269. tgpig.z = tgpig.z%(ne12*ne13);
  5270. // row indices of src1 for expert id
  5271. int64_t _ne1 = 0;
  5272. short src1ids[512];
  5273. for (int64_t i1 = 0; i1 < ne1; i1++) {
  5274. if (((device int32_t *) (ids + i1*nbi1))[idx] == id) {
  5275. src1ids[_ne1++] = i1;
  5276. }
  5277. }
  5278. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  5279. src0s[id],
  5280. src1,
  5281. src1ids,
  5282. dst,
  5283. ne00,
  5284. ne02,
  5285. nb01,
  5286. nb02,
  5287. ne12,
  5288. nb10,
  5289. nb11,
  5290. nb12,
  5291. ne0,
  5292. _ne1,
  5293. r2,
  5294. r3,
  5295. shared_memory,
  5296. tgpig,
  5297. tiitg,
  5298. sgitg);
  5299. }
  5300. #if QK_K == 256
  5301. #define QK_NL 16
  5302. #else
  5303. #define QK_NL 4
  5304. #endif
  5305. //
  5306. // get rows
  5307. //
  5308. typedef void (get_rows_t)(
  5309. device const void * src0,
  5310. device const char * src1,
  5311. device float * dst,
  5312. constant int64_t & ne00,
  5313. constant uint64_t & nb01,
  5314. constant uint64_t & nb02,
  5315. constant int64_t & ne10,
  5316. constant uint64_t & nb10,
  5317. constant uint64_t & nb11,
  5318. constant uint64_t & nb1,
  5319. constant uint64_t & nb2,
  5320. uint3, uint, uint3);
  5321. //template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  5322. //template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  5323. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  5324. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  5325. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  5326. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  5327. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  5328. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  5329. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  5330. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  5331. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  5332. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  5333. template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_t kernel_get_rows<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5334. template [[host_name("kernel_get_rows_iq2_xs")]] kernel get_rows_t kernel_get_rows<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5335. template [[host_name("kernel_get_rows_iq3_xxs")]] kernel get_rows_t kernel_get_rows<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5336. template [[host_name("kernel_get_rows_iq3_s")]] kernel get_rows_t kernel_get_rows<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5337. template [[host_name("kernel_get_rows_iq2_s")]] kernel get_rows_t kernel_get_rows<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5338. template [[host_name("kernel_get_rows_iq1_s")]] kernel get_rows_t kernel_get_rows<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5339. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_t kernel_get_rows<block_iq4_nl, 2, dequantize_iq4_nl>;
  5340. //
  5341. // matrix-matrix multiplication
  5342. //
  5343. typedef void (mat_mm_t)(
  5344. device const uchar * src0,
  5345. device const uchar * src1,
  5346. device float * dst,
  5347. constant int64_t & ne00,
  5348. constant int64_t & ne02,
  5349. constant uint64_t & nb01,
  5350. constant uint64_t & nb02,
  5351. constant int64_t & ne12,
  5352. constant uint64_t & nb10,
  5353. constant uint64_t & nb11,
  5354. constant uint64_t & nb12,
  5355. constant int64_t & ne0,
  5356. constant int64_t & ne1,
  5357. constant uint & r2,
  5358. constant uint & r3,
  5359. threadgroup uchar *,
  5360. uint3, uint, uint);
  5361. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  5362. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  5363. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  5364. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  5365. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  5366. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  5367. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  5368. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  5369. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  5370. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  5371. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  5372. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;
  5373. template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5374. template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5375. template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5376. template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5377. template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5378. template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5379. template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_nl>;
  5380. //
  5381. // indirect matrix-matrix multiplication
  5382. //
  5383. typedef void (mat_mm_id_t)(
  5384. device const uchar * ids,
  5385. device const uchar * src1,
  5386. device float * dst,
  5387. constant uint64_t & nbi1,
  5388. constant int64_t & ne00,
  5389. constant int64_t & ne02,
  5390. constant uint64_t & nb01,
  5391. constant uint64_t & nb02,
  5392. constant int64_t & ne12,
  5393. constant int64_t & ne13,
  5394. constant uint64_t & nb10,
  5395. constant uint64_t & nb11,
  5396. constant uint64_t & nb12,
  5397. constant int64_t & ne0,
  5398. constant int64_t & ne1,
  5399. constant uint64_t & nb1,
  5400. constant uint & r2,
  5401. constant uint & r3,
  5402. constant int & idx,
  5403. device const uchar * src00,
  5404. device const uchar * src01,
  5405. device const uchar * src02,
  5406. device const uchar * src03,
  5407. device const uchar * src04,
  5408. device const uchar * src05,
  5409. device const uchar * src06,
  5410. device const uchar * src07,
  5411. threadgroup uchar *,
  5412. uint3, uint, uint);
  5413. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  5414. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  5415. 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>;
  5416. 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>;
  5417. 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>;
  5418. 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>;
  5419. 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>;
  5420. 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>;
  5421. 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>;
  5422. 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>;
  5423. 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>;
  5424. 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>;
  5425. 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>;
  5426. 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>;
  5427. 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>;
  5428. 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>;
  5429. 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>;
  5430. 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>;
  5431. 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>;
  5432. //
  5433. // matrix-vector multiplication
  5434. //
  5435. [[host_name("kernel_mul_mv_id_f32_f32")]]
  5436. kernel void kernel_mul_mv_id_f32_f32(
  5437. device const char * ids,
  5438. device const char * src1,
  5439. device float * dst,
  5440. constant uint64_t & nbi1,
  5441. constant int64_t & ne00,
  5442. constant int64_t & ne01,
  5443. constant int64_t & ne02,
  5444. constant uint64_t & nb00,
  5445. constant uint64_t & nb01,
  5446. constant uint64_t & nb02,
  5447. constant int64_t & ne10,
  5448. constant int64_t & ne11,
  5449. constant int64_t & ne12,
  5450. constant int64_t & ne13,
  5451. constant uint64_t & nb10,
  5452. constant uint64_t & nb11,
  5453. constant uint64_t & nb12,
  5454. constant int64_t & ne0,
  5455. constant int64_t & ne1,
  5456. constant uint64_t & nb1,
  5457. constant uint & r2,
  5458. constant uint & r3,
  5459. constant int & idx,
  5460. device const char * src00,
  5461. device const char * src01,
  5462. device const char * src02,
  5463. device const char * src03,
  5464. device const char * src04,
  5465. device const char * src05,
  5466. device const char * src06,
  5467. device const char * src07,
  5468. uint3 tgpig[[threadgroup_position_in_grid]],
  5469. uint tiitg[[thread_index_in_threadgroup]],
  5470. uint tiisg[[thread_index_in_simdgroup]],
  5471. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5472. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5473. const int64_t bid = tgpig.z/(ne12*ne13);
  5474. tgpig.z = tgpig.z%(ne12*ne13);
  5475. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5476. kernel_mul_mv_f32_f32_impl(
  5477. src0[id],
  5478. src1 + bid*nb11,
  5479. dst + bid*ne0,
  5480. ne00,
  5481. ne01,
  5482. ne02,
  5483. nb00,
  5484. nb01,
  5485. nb02,
  5486. ne10,
  5487. ne11,
  5488. ne12,
  5489. nb10,
  5490. nb11,
  5491. nb12,
  5492. ne0,
  5493. ne1,
  5494. r2,
  5495. r3,
  5496. tgpig,
  5497. tiisg);
  5498. }
  5499. [[host_name("kernel_mul_mv_id_f16_f32")]]
  5500. kernel void kernel_mul_mv_id_f16_f32(
  5501. device const char * ids,
  5502. device const char * src1,
  5503. device float * dst,
  5504. constant uint64_t & nbi1,
  5505. constant int64_t & ne00,
  5506. constant int64_t & ne01,
  5507. constant int64_t & ne02,
  5508. constant uint64_t & nb00,
  5509. constant uint64_t & nb01,
  5510. constant uint64_t & nb02,
  5511. constant int64_t & ne10,
  5512. constant int64_t & ne11,
  5513. constant int64_t & ne12,
  5514. constant int64_t & ne13,
  5515. constant uint64_t & nb10,
  5516. constant uint64_t & nb11,
  5517. constant uint64_t & nb12,
  5518. constant int64_t & ne0,
  5519. constant int64_t & ne1,
  5520. constant uint64_t & nb1,
  5521. constant uint & r2,
  5522. constant uint & r3,
  5523. constant int & idx,
  5524. device const char * src00,
  5525. device const char * src01,
  5526. device const char * src02,
  5527. device const char * src03,
  5528. device const char * src04,
  5529. device const char * src05,
  5530. device const char * src06,
  5531. device const char * src07,
  5532. uint3 tgpig[[threadgroup_position_in_grid]],
  5533. uint tiitg[[thread_index_in_threadgroup]],
  5534. uint tiisg[[thread_index_in_simdgroup]],
  5535. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5536. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5537. const int64_t bid = tgpig.z/(ne12*ne13);
  5538. tgpig.z = tgpig.z%(ne12*ne13);
  5539. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5540. kernel_mul_mv_f16_f32_impl(
  5541. src0[id],
  5542. src1 + bid*nb11,
  5543. dst + bid*ne0,
  5544. ne00,
  5545. ne01,
  5546. ne02,
  5547. nb00,
  5548. nb01,
  5549. nb02,
  5550. ne10,
  5551. ne11,
  5552. ne12,
  5553. nb10,
  5554. nb11,
  5555. nb12,
  5556. ne0,
  5557. ne1,
  5558. r2,
  5559. r3,
  5560. tgpig,
  5561. tiisg);
  5562. }
  5563. [[host_name("kernel_mul_mv_id_q8_0_f32")]]
  5564. kernel void kernel_mul_mv_id_q8_0_f32(
  5565. device const char * ids,
  5566. device const char * src1,
  5567. device float * dst,
  5568. constant uint64_t & nbi1,
  5569. constant int64_t & ne00,
  5570. constant int64_t & ne01,
  5571. constant int64_t & ne02,
  5572. constant uint64_t & nb00,
  5573. constant uint64_t & nb01,
  5574. constant uint64_t & nb02,
  5575. constant int64_t & ne10,
  5576. constant int64_t & ne11,
  5577. constant int64_t & ne12,
  5578. constant int64_t & ne13,
  5579. constant uint64_t & nb10,
  5580. constant uint64_t & nb11,
  5581. constant uint64_t & nb12,
  5582. constant int64_t & ne0,
  5583. constant int64_t & ne1,
  5584. constant uint64_t & nb1,
  5585. constant uint & r2,
  5586. constant uint & r3,
  5587. constant int & idx,
  5588. device const char * src00,
  5589. device const char * src01,
  5590. device const char * src02,
  5591. device const char * src03,
  5592. device const char * src04,
  5593. device const char * src05,
  5594. device const char * src06,
  5595. device const char * src07,
  5596. uint3 tgpig[[threadgroup_position_in_grid]],
  5597. uint tiitg[[thread_index_in_threadgroup]],
  5598. uint tiisg[[thread_index_in_simdgroup]],
  5599. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5600. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5601. const int64_t bid = tgpig.z/(ne12*ne13);
  5602. tgpig.z = tgpig.z%(ne12*ne13);
  5603. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5604. kernel_mul_mv_q8_0_f32_impl(
  5605. src0[id],
  5606. (device const float *) (src1 + bid*nb11),
  5607. dst + bid*ne0,
  5608. ne00,
  5609. ne01,
  5610. ne02,
  5611. ne10,
  5612. ne12,
  5613. ne0,
  5614. ne1,
  5615. r2,
  5616. r3,
  5617. tgpig,
  5618. tiisg,
  5619. sgitg);
  5620. }
  5621. [[host_name("kernel_mul_mv_id_q4_0_f32")]]
  5622. kernel void kernel_mul_mv_id_q4_0_f32(
  5623. device const char * ids,
  5624. device const char * src1,
  5625. device float * dst,
  5626. constant uint64_t & nbi1,
  5627. constant int64_t & ne00,
  5628. constant int64_t & ne01,
  5629. constant int64_t & ne02,
  5630. constant uint64_t & nb00,
  5631. constant uint64_t & nb01,
  5632. constant uint64_t & nb02,
  5633. constant int64_t & ne10,
  5634. constant int64_t & ne11,
  5635. constant int64_t & ne12,
  5636. constant int64_t & ne13,
  5637. constant uint64_t & nb10,
  5638. constant uint64_t & nb11,
  5639. constant uint64_t & nb12,
  5640. constant int64_t & ne0,
  5641. constant int64_t & ne1,
  5642. constant uint64_t & nb1,
  5643. constant uint & r2,
  5644. constant uint & r3,
  5645. constant int & idx,
  5646. device const char * src00,
  5647. device const char * src01,
  5648. device const char * src02,
  5649. device const char * src03,
  5650. device const char * src04,
  5651. device const char * src05,
  5652. device const char * src06,
  5653. device const char * src07,
  5654. uint3 tgpig[[threadgroup_position_in_grid]],
  5655. uint tiitg[[thread_index_in_threadgroup]],
  5656. uint tiisg[[thread_index_in_simdgroup]],
  5657. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5658. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5659. const int64_t bid = tgpig.z/(ne12*ne13);
  5660. tgpig.z = tgpig.z%(ne12*ne13);
  5661. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5662. mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5663. src0[id],
  5664. (device const float *) (src1 + bid*nb11),
  5665. dst + bid*ne0,
  5666. ne00,
  5667. ne01,
  5668. ne02,
  5669. ne10,
  5670. ne12,
  5671. ne0,
  5672. ne1,
  5673. r2,
  5674. r3,
  5675. tgpig,
  5676. tiisg,
  5677. sgitg);
  5678. }
  5679. [[host_name("kernel_mul_mv_id_q4_1_f32")]]
  5680. kernel void kernel_mul_mv_id_q4_1_f32(
  5681. device const char * ids,
  5682. device const char * src1,
  5683. device float * dst,
  5684. constant uint64_t & nbi1,
  5685. constant int64_t & ne00,
  5686. constant int64_t & ne01,
  5687. constant int64_t & ne02,
  5688. constant uint64_t & nb00,
  5689. constant uint64_t & nb01,
  5690. constant uint64_t & nb02,
  5691. constant int64_t & ne10,
  5692. constant int64_t & ne11,
  5693. constant int64_t & ne12,
  5694. constant int64_t & ne13,
  5695. constant uint64_t & nb10,
  5696. constant uint64_t & nb11,
  5697. constant uint64_t & nb12,
  5698. constant int64_t & ne0,
  5699. constant int64_t & ne1,
  5700. constant uint64_t & nb1,
  5701. constant uint & r2,
  5702. constant uint & r3,
  5703. constant int & idx,
  5704. device const char * src00,
  5705. device const char * src01,
  5706. device const char * src02,
  5707. device const char * src03,
  5708. device const char * src04,
  5709. device const char * src05,
  5710. device const char * src06,
  5711. device const char * src07,
  5712. uint3 tgpig[[threadgroup_position_in_grid]],
  5713. uint tiitg[[thread_index_in_threadgroup]],
  5714. uint tiisg[[thread_index_in_simdgroup]],
  5715. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5716. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5717. const int64_t bid = tgpig.z/(ne12*ne13);
  5718. tgpig.z = tgpig.z%(ne12*ne13);
  5719. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5720. mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5721. src0[id],
  5722. (device const float *) (src1 + bid*nb11),
  5723. dst + bid*ne0,
  5724. ne00,
  5725. ne01,
  5726. ne02,
  5727. ne10,
  5728. ne12,
  5729. ne0,
  5730. ne1,
  5731. r2,
  5732. r3,
  5733. tgpig,
  5734. tiisg,
  5735. sgitg);
  5736. }
  5737. [[host_name("kernel_mul_mv_id_q5_0_f32")]]
  5738. kernel void kernel_mul_mv_id_q5_0_f32(
  5739. device const char * ids,
  5740. device const char * src1,
  5741. device float * dst,
  5742. constant uint64_t & nbi1,
  5743. constant int64_t & ne00,
  5744. constant int64_t & ne01,
  5745. constant int64_t & ne02,
  5746. constant uint64_t & nb00,
  5747. constant uint64_t & nb01,
  5748. constant uint64_t & nb02,
  5749. constant int64_t & ne10,
  5750. constant int64_t & ne11,
  5751. constant int64_t & ne12,
  5752. constant int64_t & ne13,
  5753. constant uint64_t & nb10,
  5754. constant uint64_t & nb11,
  5755. constant uint64_t & nb12,
  5756. constant int64_t & ne0,
  5757. constant int64_t & ne1,
  5758. constant uint64_t & nb1,
  5759. constant uint & r2,
  5760. constant uint & r3,
  5761. constant int & idx,
  5762. device const char * src00,
  5763. device const char * src01,
  5764. device const char * src02,
  5765. device const char * src03,
  5766. device const char * src04,
  5767. device const char * src05,
  5768. device const char * src06,
  5769. device const char * src07,
  5770. uint3 tgpig[[threadgroup_position_in_grid]],
  5771. uint tiitg[[thread_index_in_threadgroup]],
  5772. uint tiisg[[thread_index_in_simdgroup]],
  5773. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5774. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5775. const int64_t bid = tgpig.z/(ne12*ne13);
  5776. tgpig.z = tgpig.z%(ne12*ne13);
  5777. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5778. mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5779. src0[id],
  5780. (device const float *) (src1 + bid*nb11),
  5781. dst + bid*ne0,
  5782. ne00,
  5783. ne01,
  5784. ne02,
  5785. ne10,
  5786. ne12,
  5787. ne0,
  5788. ne1,
  5789. r2,
  5790. r3,
  5791. tgpig,
  5792. tiisg,
  5793. sgitg);
  5794. }
  5795. [[host_name("kernel_mul_mv_id_q5_1_f32")]]
  5796. kernel void kernel_mul_mv_id_q5_1_f32(
  5797. device const char * ids,
  5798. device const char * src1,
  5799. device float * dst,
  5800. constant uint64_t & nbi1,
  5801. constant int64_t & ne00,
  5802. constant int64_t & ne01,
  5803. constant int64_t & ne02,
  5804. constant uint64_t & nb00,
  5805. constant uint64_t & nb01,
  5806. constant uint64_t & nb02,
  5807. constant int64_t & ne10,
  5808. constant int64_t & ne11,
  5809. constant int64_t & ne12,
  5810. constant int64_t & ne13,
  5811. constant uint64_t & nb10,
  5812. constant uint64_t & nb11,
  5813. constant uint64_t & nb12,
  5814. constant int64_t & ne0,
  5815. constant int64_t & ne1,
  5816. constant uint64_t & nb1,
  5817. constant uint & r2,
  5818. constant uint & r3,
  5819. constant int & idx,
  5820. device const char * src00,
  5821. device const char * src01,
  5822. device const char * src02,
  5823. device const char * src03,
  5824. device const char * src04,
  5825. device const char * src05,
  5826. device const char * src06,
  5827. device const char * src07,
  5828. uint3 tgpig[[threadgroup_position_in_grid]],
  5829. uint tiitg[[thread_index_in_threadgroup]],
  5830. uint tiisg[[thread_index_in_simdgroup]],
  5831. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5832. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5833. const int64_t bid = tgpig.z/(ne12*ne13);
  5834. tgpig.z = tgpig.z%(ne12*ne13);
  5835. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5836. mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5837. src0[id],
  5838. (device const float *) (src1 + bid*nb11),
  5839. dst + bid*ne0,
  5840. ne00,
  5841. ne01,
  5842. ne02,
  5843. ne10,
  5844. ne12,
  5845. ne0,
  5846. ne1,
  5847. r2,
  5848. r3,
  5849. tgpig,
  5850. tiisg,
  5851. sgitg);
  5852. }
  5853. [[host_name("kernel_mul_mv_id_q2_K_f32")]]
  5854. kernel void kernel_mul_mv_id_q2_K_f32(
  5855. device const char * ids,
  5856. device const char * src1,
  5857. device float * dst,
  5858. constant uint64_t & nbi1,
  5859. constant int64_t & ne00,
  5860. constant int64_t & ne01,
  5861. constant int64_t & ne02,
  5862. constant uint64_t & nb00,
  5863. constant uint64_t & nb01,
  5864. constant uint64_t & nb02,
  5865. constant int64_t & ne10,
  5866. constant int64_t & ne11,
  5867. constant int64_t & ne12,
  5868. constant int64_t & ne13,
  5869. constant uint64_t & nb10,
  5870. constant uint64_t & nb11,
  5871. constant uint64_t & nb12,
  5872. constant int64_t & ne0,
  5873. constant int64_t & ne1,
  5874. constant uint64_t & nb1,
  5875. constant uint & r2,
  5876. constant uint & r3,
  5877. constant int & idx,
  5878. device const char * src00,
  5879. device const char * src01,
  5880. device const char * src02,
  5881. device const char * src03,
  5882. device const char * src04,
  5883. device const char * src05,
  5884. device const char * src06,
  5885. device const char * src07,
  5886. uint3 tgpig[[threadgroup_position_in_grid]],
  5887. uint tiitg[[thread_index_in_threadgroup]],
  5888. uint tiisg[[thread_index_in_simdgroup]],
  5889. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5890. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5891. const int64_t bid = tgpig.z/(ne12*ne13);
  5892. tgpig.z = tgpig.z%(ne12*ne13);
  5893. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5894. kernel_mul_mv_q2_K_f32_impl(
  5895. src0[id],
  5896. (device const float *) (src1 + bid*nb11),
  5897. dst + bid*ne0,
  5898. ne00,
  5899. ne01,
  5900. ne02,
  5901. ne10,
  5902. ne12,
  5903. ne0,
  5904. ne1,
  5905. r2,
  5906. r3,
  5907. tgpig,
  5908. tiisg,
  5909. sgitg);
  5910. }
  5911. [[host_name("kernel_mul_mv_id_q3_K_f32")]]
  5912. kernel void kernel_mul_mv_id_q3_K_f32(
  5913. device const char * ids,
  5914. device const char * src1,
  5915. device float * dst,
  5916. constant uint64_t & nbi1,
  5917. constant int64_t & ne00,
  5918. constant int64_t & ne01,
  5919. constant int64_t & ne02,
  5920. constant uint64_t & nb00,
  5921. constant uint64_t & nb01,
  5922. constant uint64_t & nb02,
  5923. constant int64_t & ne10,
  5924. constant int64_t & ne11,
  5925. constant int64_t & ne12,
  5926. constant int64_t & ne13,
  5927. constant uint64_t & nb10,
  5928. constant uint64_t & nb11,
  5929. constant uint64_t & nb12,
  5930. constant int64_t & ne0,
  5931. constant int64_t & ne1,
  5932. constant uint64_t & nb1,
  5933. constant uint & r2,
  5934. constant uint & r3,
  5935. constant int & idx,
  5936. device const char * src00,
  5937. device const char * src01,
  5938. device const char * src02,
  5939. device const char * src03,
  5940. device const char * src04,
  5941. device const char * src05,
  5942. device const char * src06,
  5943. device const char * src07,
  5944. uint3 tgpig[[threadgroup_position_in_grid]],
  5945. uint tiitg[[thread_index_in_threadgroup]],
  5946. uint tiisg[[thread_index_in_simdgroup]],
  5947. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5948. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5949. const int64_t bid = tgpig.z/(ne12*ne13);
  5950. tgpig.z = tgpig.z%(ne12*ne13);
  5951. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5952. kernel_mul_mv_q3_K_f32_impl(
  5953. src0[id],
  5954. (device const float *) (src1 + bid*nb11),
  5955. dst + bid*ne0,
  5956. ne00,
  5957. ne01,
  5958. ne02,
  5959. ne10,
  5960. ne12,
  5961. ne0,
  5962. ne1,
  5963. r2,
  5964. r3,
  5965. tgpig,
  5966. tiisg,
  5967. sgitg);
  5968. }
  5969. [[host_name("kernel_mul_mv_id_q4_K_f32")]]
  5970. kernel void kernel_mul_mv_id_q4_K_f32(
  5971. device const char * ids,
  5972. device const char * src1,
  5973. device float * dst,
  5974. constant uint64_t & nbi1,
  5975. constant int64_t & ne00,
  5976. constant int64_t & ne01,
  5977. constant int64_t & ne02,
  5978. constant uint64_t & nb00,
  5979. constant uint64_t & nb01,
  5980. constant uint64_t & nb02,
  5981. constant int64_t & ne10,
  5982. constant int64_t & ne11,
  5983. constant int64_t & ne12,
  5984. constant int64_t & ne13,
  5985. constant uint64_t & nb10,
  5986. constant uint64_t & nb11,
  5987. constant uint64_t & nb12,
  5988. constant int64_t & ne0,
  5989. constant int64_t & ne1,
  5990. constant uint64_t & nb1,
  5991. constant uint & r2,
  5992. constant uint & r3,
  5993. constant int & idx,
  5994. device const char * src00,
  5995. device const char * src01,
  5996. device const char * src02,
  5997. device const char * src03,
  5998. device const char * src04,
  5999. device const char * src05,
  6000. device const char * src06,
  6001. device const char * src07,
  6002. uint3 tgpig[[threadgroup_position_in_grid]],
  6003. uint tiitg[[thread_index_in_threadgroup]],
  6004. uint tiisg[[thread_index_in_simdgroup]],
  6005. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6006. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6007. const int64_t bid = tgpig.z/(ne12*ne13);
  6008. tgpig.z = tgpig.z%(ne12*ne13);
  6009. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6010. kernel_mul_mv_q4_K_f32_impl(
  6011. src0[id],
  6012. (device const float *) (src1 + bid*nb11),
  6013. dst + bid*ne0,
  6014. ne00,
  6015. ne01,
  6016. ne02,
  6017. ne10,
  6018. ne12,
  6019. ne0,
  6020. ne1,
  6021. r2,
  6022. r3,
  6023. tgpig,
  6024. tiisg,
  6025. sgitg);
  6026. }
  6027. [[host_name("kernel_mul_mv_id_q5_K_f32")]]
  6028. kernel void kernel_mul_mv_id_q5_K_f32(
  6029. device const char * ids,
  6030. device const char * src1,
  6031. device float * dst,
  6032. constant uint64_t & nbi1,
  6033. constant int64_t & ne00,
  6034. constant int64_t & ne01,
  6035. constant int64_t & ne02,
  6036. constant uint64_t & nb00,
  6037. constant uint64_t & nb01,
  6038. constant uint64_t & nb02,
  6039. constant int64_t & ne10,
  6040. constant int64_t & ne11,
  6041. constant int64_t & ne12,
  6042. constant int64_t & ne13,
  6043. constant uint64_t & nb10,
  6044. constant uint64_t & nb11,
  6045. constant uint64_t & nb12,
  6046. constant int64_t & ne0,
  6047. constant int64_t & ne1,
  6048. constant uint64_t & nb1,
  6049. constant uint & r2,
  6050. constant uint & r3,
  6051. constant int & idx,
  6052. device const char * src00,
  6053. device const char * src01,
  6054. device const char * src02,
  6055. device const char * src03,
  6056. device const char * src04,
  6057. device const char * src05,
  6058. device const char * src06,
  6059. device const char * src07,
  6060. uint3 tgpig[[threadgroup_position_in_grid]],
  6061. uint tiitg[[thread_index_in_threadgroup]],
  6062. uint tiisg[[thread_index_in_simdgroup]],
  6063. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6064. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6065. const int64_t bid = tgpig.z/(ne12*ne13);
  6066. tgpig.z = tgpig.z%(ne12*ne13);
  6067. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6068. kernel_mul_mv_q5_K_f32_impl(
  6069. src0[id],
  6070. (device const float *) (src1 + bid*nb11),
  6071. dst + bid*ne0,
  6072. ne00,
  6073. ne01,
  6074. ne02,
  6075. ne10,
  6076. ne12,
  6077. ne0,
  6078. ne1,
  6079. r2,
  6080. r3,
  6081. tgpig,
  6082. tiisg,
  6083. sgitg);
  6084. }
  6085. [[host_name("kernel_mul_mv_id_q6_K_f32")]]
  6086. kernel void kernel_mul_mv_id_q6_K_f32(
  6087. device const char * ids,
  6088. device const char * src1,
  6089. device float * dst,
  6090. constant uint64_t & nbi1,
  6091. constant int64_t & ne00,
  6092. constant int64_t & ne01,
  6093. constant int64_t & ne02,
  6094. constant uint64_t & nb00,
  6095. constant uint64_t & nb01,
  6096. constant uint64_t & nb02,
  6097. constant int64_t & ne10,
  6098. constant int64_t & ne11,
  6099. constant int64_t & ne12,
  6100. constant int64_t & ne13,
  6101. constant uint64_t & nb10,
  6102. constant uint64_t & nb11,
  6103. constant uint64_t & nb12,
  6104. constant int64_t & ne0,
  6105. constant int64_t & ne1,
  6106. constant uint64_t & nb1,
  6107. constant uint & r2,
  6108. constant uint & r3,
  6109. constant int & idx,
  6110. device const char * src00,
  6111. device const char * src01,
  6112. device const char * src02,
  6113. device const char * src03,
  6114. device const char * src04,
  6115. device const char * src05,
  6116. device const char * src06,
  6117. device const char * src07,
  6118. uint3 tgpig[[threadgroup_position_in_grid]],
  6119. uint tiitg[[thread_index_in_threadgroup]],
  6120. uint tiisg[[thread_index_in_simdgroup]],
  6121. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6122. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6123. const int64_t bid = tgpig.z/(ne12*ne13);
  6124. tgpig.z = tgpig.z%(ne12*ne13);
  6125. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6126. kernel_mul_mv_q6_K_f32_impl(
  6127. src0[id],
  6128. (device const float *) (src1 + bid*nb11),
  6129. dst + bid*ne0,
  6130. ne00,
  6131. ne01,
  6132. ne02,
  6133. ne10,
  6134. ne12,
  6135. ne0,
  6136. ne1,
  6137. r2,
  6138. r3,
  6139. tgpig,
  6140. tiisg,
  6141. sgitg);
  6142. }
  6143. [[host_name("kernel_mul_mv_id_iq2_xxs_f32")]]
  6144. kernel void kernel_mul_mv_id_iq2_xxs_f32(
  6145. device const char * ids,
  6146. device const char * src1,
  6147. device float * dst,
  6148. constant uint64_t & nbi1,
  6149. constant int64_t & ne00,
  6150. constant int64_t & ne01,
  6151. constant int64_t & ne02,
  6152. constant uint64_t & nb00,
  6153. constant uint64_t & nb01,
  6154. constant uint64_t & nb02,
  6155. constant int64_t & ne10,
  6156. constant int64_t & ne11,
  6157. constant int64_t & ne12,
  6158. constant int64_t & ne13,
  6159. constant uint64_t & nb10,
  6160. constant uint64_t & nb11,
  6161. constant uint64_t & nb12,
  6162. constant int64_t & ne0,
  6163. constant int64_t & ne1,
  6164. constant uint64_t & nb1,
  6165. constant uint & r2,
  6166. constant uint & r3,
  6167. constant int & idx,
  6168. device const char * src00,
  6169. device const char * src01,
  6170. device const char * src02,
  6171. device const char * src03,
  6172. device const char * src04,
  6173. device const char * src05,
  6174. device const char * src06,
  6175. device const char * src07,
  6176. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6177. uint3 tgpig[[threadgroup_position_in_grid]],
  6178. uint tiitg[[thread_index_in_threadgroup]],
  6179. uint tiisg[[thread_index_in_simdgroup]],
  6180. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6181. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6182. const int64_t bid = tgpig.z/(ne12*ne13);
  6183. tgpig.z = tgpig.z%(ne12*ne13);
  6184. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6185. kernel_mul_mv_iq2_xxs_f32_impl(
  6186. src0[id],
  6187. (device const float *) (src1 + bid*nb11),
  6188. dst + bid*ne0,
  6189. ne00,
  6190. ne01,
  6191. ne02,
  6192. ne10,
  6193. ne12,
  6194. ne0,
  6195. ne1,
  6196. r2,
  6197. r3,
  6198. shared_values,
  6199. tgpig,
  6200. tiisg,
  6201. sgitg);
  6202. }
  6203. [[host_name("kernel_mul_mv_id_iq2_xs_f32")]]
  6204. kernel void kernel_mul_mv_id_iq2_xs_f32(
  6205. device const char * ids,
  6206. device const char * src1,
  6207. device float * dst,
  6208. constant uint64_t & nbi1,
  6209. constant int64_t & ne00,
  6210. constant int64_t & ne01,
  6211. constant int64_t & ne02,
  6212. constant uint64_t & nb00,
  6213. constant uint64_t & nb01,
  6214. constant uint64_t & nb02,
  6215. constant int64_t & ne10,
  6216. constant int64_t & ne11,
  6217. constant int64_t & ne12,
  6218. constant int64_t & ne13,
  6219. constant uint64_t & nb10,
  6220. constant uint64_t & nb11,
  6221. constant uint64_t & nb12,
  6222. constant int64_t & ne0,
  6223. constant int64_t & ne1,
  6224. constant uint64_t & nb1,
  6225. constant uint & r2,
  6226. constant uint & r3,
  6227. constant int & idx,
  6228. device const char * src00,
  6229. device const char * src01,
  6230. device const char * src02,
  6231. device const char * src03,
  6232. device const char * src04,
  6233. device const char * src05,
  6234. device const char * src06,
  6235. device const char * src07,
  6236. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6237. uint3 tgpig[[threadgroup_position_in_grid]],
  6238. uint tiitg[[thread_index_in_threadgroup]],
  6239. uint tiisg[[thread_index_in_simdgroup]],
  6240. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6241. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6242. const int64_t bid = tgpig.z/(ne12*ne13);
  6243. tgpig.z = tgpig.z%(ne12*ne13);
  6244. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6245. kernel_mul_mv_iq2_xs_f32_impl(
  6246. src0[id],
  6247. (device const float *) (src1 + bid*nb11),
  6248. dst + bid*ne0,
  6249. ne00,
  6250. ne01,
  6251. ne02,
  6252. ne10,
  6253. ne12,
  6254. ne0,
  6255. ne1,
  6256. r2,
  6257. r3,
  6258. shared_values,
  6259. tgpig,
  6260. tiisg,
  6261. sgitg);
  6262. }
  6263. [[host_name("kernel_mul_mv_id_iq3_xxs_f32")]]
  6264. kernel void kernel_mul_mv_id_iq3_xxs_f32(
  6265. device const char * ids,
  6266. device const char * src1,
  6267. device float * dst,
  6268. constant uint64_t & nbi1,
  6269. constant int64_t & ne00,
  6270. constant int64_t & ne01,
  6271. constant int64_t & ne02,
  6272. constant uint64_t & nb00,
  6273. constant uint64_t & nb01,
  6274. constant uint64_t & nb02,
  6275. constant int64_t & ne10,
  6276. constant int64_t & ne11,
  6277. constant int64_t & ne12,
  6278. constant int64_t & ne13,
  6279. constant uint64_t & nb10,
  6280. constant uint64_t & nb11,
  6281. constant uint64_t & nb12,
  6282. constant int64_t & ne0,
  6283. constant int64_t & ne1,
  6284. constant uint64_t & nb1,
  6285. constant uint & r2,
  6286. constant uint & r3,
  6287. constant int & idx,
  6288. device const char * src00,
  6289. device const char * src01,
  6290. device const char * src02,
  6291. device const char * src03,
  6292. device const char * src04,
  6293. device const char * src05,
  6294. device const char * src06,
  6295. device const char * src07,
  6296. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6297. uint3 tgpig[[threadgroup_position_in_grid]],
  6298. uint tiitg[[thread_index_in_threadgroup]],
  6299. uint tiisg[[thread_index_in_simdgroup]],
  6300. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6301. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6302. const int64_t bid = tgpig.z/(ne12*ne13);
  6303. tgpig.z = tgpig.z%(ne12*ne13);
  6304. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6305. kernel_mul_mv_iq3_xxs_f32_impl(
  6306. src0[id],
  6307. (device const float *) (src1 + bid*nb11),
  6308. dst + bid*ne0,
  6309. ne00,
  6310. ne01,
  6311. ne02,
  6312. ne10,
  6313. ne12,
  6314. ne0,
  6315. ne1,
  6316. r2,
  6317. r3,
  6318. shared_values,
  6319. tgpig,
  6320. tiisg,
  6321. sgitg);
  6322. }
  6323. [[host_name("kernel_mul_mv_id_iq3_s_f32")]]
  6324. kernel void kernel_mul_mv_id_iq3_s_f32(
  6325. device const char * ids,
  6326. device const char * src1,
  6327. device float * dst,
  6328. constant uint64_t & nbi1,
  6329. constant int64_t & ne00,
  6330. constant int64_t & ne01,
  6331. constant int64_t & ne02,
  6332. constant uint64_t & nb00,
  6333. constant uint64_t & nb01,
  6334. constant uint64_t & nb02,
  6335. constant int64_t & ne10,
  6336. constant int64_t & ne11,
  6337. constant int64_t & ne12,
  6338. constant int64_t & ne13,
  6339. constant uint64_t & nb10,
  6340. constant uint64_t & nb11,
  6341. constant uint64_t & nb12,
  6342. constant int64_t & ne0,
  6343. constant int64_t & ne1,
  6344. constant uint64_t & nb1,
  6345. constant uint & r2,
  6346. constant uint & r3,
  6347. constant int & idx,
  6348. device const char * src00,
  6349. device const char * src01,
  6350. device const char * src02,
  6351. device const char * src03,
  6352. device const char * src04,
  6353. device const char * src05,
  6354. device const char * src06,
  6355. device const char * src07,
  6356. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6357. uint3 tgpig[[threadgroup_position_in_grid]],
  6358. uint tiitg[[thread_index_in_threadgroup]],
  6359. uint tiisg[[thread_index_in_simdgroup]],
  6360. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6361. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6362. const int64_t bid = tgpig.z/(ne12*ne13);
  6363. tgpig.z = tgpig.z%(ne12*ne13);
  6364. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6365. kernel_mul_mv_iq3_s_f32_impl(
  6366. src0[id],
  6367. (device const float *) (src1 + bid*nb11),
  6368. dst + bid*ne0,
  6369. ne00,
  6370. ne01,
  6371. ne02,
  6372. ne10,
  6373. ne12,
  6374. ne0,
  6375. ne1,
  6376. r2,
  6377. r3,
  6378. shared_values,
  6379. tgpig,
  6380. tiisg,
  6381. sgitg);
  6382. }
  6383. [[host_name("kernel_mul_mv_id_iq2_s_f32")]]
  6384. kernel void kernel_mul_mv_id_iq2_s_f32(
  6385. device const char * ids,
  6386. device const char * src1,
  6387. device float * dst,
  6388. constant uint64_t & nbi1,
  6389. constant int64_t & ne00,
  6390. constant int64_t & ne01,
  6391. constant int64_t & ne02,
  6392. constant uint64_t & nb00,
  6393. constant uint64_t & nb01,
  6394. constant uint64_t & nb02,
  6395. constant int64_t & ne10,
  6396. constant int64_t & ne11,
  6397. constant int64_t & ne12,
  6398. constant int64_t & ne13,
  6399. constant uint64_t & nb10,
  6400. constant uint64_t & nb11,
  6401. constant uint64_t & nb12,
  6402. constant int64_t & ne0,
  6403. constant int64_t & ne1,
  6404. constant uint64_t & nb1,
  6405. constant uint & r2,
  6406. constant uint & r3,
  6407. constant int & idx,
  6408. device const char * src00,
  6409. device const char * src01,
  6410. device const char * src02,
  6411. device const char * src03,
  6412. device const char * src04,
  6413. device const char * src05,
  6414. device const char * src06,
  6415. device const char * src07,
  6416. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6417. uint3 tgpig[[threadgroup_position_in_grid]],
  6418. uint tiitg[[thread_index_in_threadgroup]],
  6419. uint tiisg[[thread_index_in_simdgroup]],
  6420. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6421. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6422. const int64_t bid = tgpig.z/(ne12*ne13);
  6423. tgpig.z = tgpig.z%(ne12*ne13);
  6424. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6425. kernel_mul_mv_iq2_s_f32_impl(
  6426. src0[id],
  6427. (device const float *) (src1 + bid*nb11),
  6428. dst + bid*ne0,
  6429. ne00,
  6430. ne01,
  6431. ne02,
  6432. ne10,
  6433. ne12,
  6434. ne0,
  6435. ne1,
  6436. r2,
  6437. r3,
  6438. shared_values,
  6439. tgpig,
  6440. tiisg,
  6441. sgitg);
  6442. }
  6443. [[host_name("kernel_mul_mv_id_iq1_s_f32")]]
  6444. kernel void kernel_mul_mv_id_iq1_s_f32(
  6445. device const char * ids,
  6446. device const char * src1,
  6447. device float * dst,
  6448. constant uint64_t & nbi1,
  6449. constant int64_t & ne00,
  6450. constant int64_t & ne01,
  6451. constant int64_t & ne02,
  6452. constant uint64_t & nb00,
  6453. constant uint64_t & nb01,
  6454. constant uint64_t & nb02,
  6455. constant int64_t & ne10,
  6456. constant int64_t & ne11,
  6457. constant int64_t & ne12,
  6458. constant int64_t & ne13,
  6459. constant uint64_t & nb10,
  6460. constant uint64_t & nb11,
  6461. constant uint64_t & nb12,
  6462. constant int64_t & ne0,
  6463. constant int64_t & ne1,
  6464. constant uint64_t & nb1,
  6465. constant uint & r2,
  6466. constant uint & r3,
  6467. constant int & idx,
  6468. device const char * src00,
  6469. device const char * src01,
  6470. device const char * src02,
  6471. device const char * src03,
  6472. device const char * src04,
  6473. device const char * src05,
  6474. device const char * src06,
  6475. device const char * src07,
  6476. uint3 tgpig[[threadgroup_position_in_grid]],
  6477. uint tiitg[[thread_index_in_threadgroup]],
  6478. uint tiisg[[thread_index_in_simdgroup]],
  6479. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6480. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6481. const int64_t bid = tgpig.z/(ne12*ne13);
  6482. tgpig.z = tgpig.z%(ne12*ne13);
  6483. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6484. kernel_mul_mv_iq1_s_f32_impl(
  6485. src0[id],
  6486. (device const float *) (src1 + bid*nb11),
  6487. dst + bid*ne0,
  6488. ne00,
  6489. ne01,
  6490. ne02,
  6491. ne10,
  6492. ne12,
  6493. ne0,
  6494. ne1,
  6495. r2,
  6496. r3,
  6497. tgpig,
  6498. tiisg,
  6499. sgitg);
  6500. }
  6501. [[host_name("kernel_mul_mv_id_iq4_nl_f32")]]
  6502. kernel void kernel_mul_mv_id_iq4_nl_f32(
  6503. device const char * ids,
  6504. device const char * src1,
  6505. device float * dst,
  6506. constant uint64_t & nbi1,
  6507. constant int64_t & ne00,
  6508. constant int64_t & ne01,
  6509. constant int64_t & ne02,
  6510. constant uint64_t & nb00,
  6511. constant uint64_t & nb01,
  6512. constant uint64_t & nb02,
  6513. constant int64_t & ne10,
  6514. constant int64_t & ne11,
  6515. constant int64_t & ne12,
  6516. constant int64_t & ne13,
  6517. constant uint64_t & nb10,
  6518. constant uint64_t & nb11,
  6519. constant uint64_t & nb12,
  6520. constant int64_t & ne0,
  6521. constant int64_t & ne1,
  6522. constant uint64_t & nb1,
  6523. constant uint & r2,
  6524. constant uint & r3,
  6525. constant int & idx,
  6526. device const char * src00,
  6527. device const char * src01,
  6528. device const char * src02,
  6529. device const char * src03,
  6530. device const char * src04,
  6531. device const char * src05,
  6532. device const char * src06,
  6533. device const char * src07,
  6534. threadgroup float * shared_values [[threadgroup(0)]],
  6535. uint3 tgpig[[threadgroup_position_in_grid]],
  6536. uint tiitg[[thread_index_in_threadgroup]],
  6537. uint tiisg[[thread_index_in_simdgroup]],
  6538. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6539. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6540. const int64_t bid = tgpig.z/(ne12*ne13);
  6541. tgpig.z = tgpig.z%(ne12*ne13);
  6542. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6543. kernel_mul_mv_iq4_nl_f32_impl(
  6544. src0[id],
  6545. (device const float *) (src1 + bid*nb11),
  6546. dst + bid*ne0,
  6547. ne00,
  6548. ne01,
  6549. ne02,
  6550. ne10,
  6551. ne12,
  6552. ne0,
  6553. ne1,
  6554. r2,
  6555. r3,
  6556. shared_values,
  6557. tgpig,
  6558. tiisg,
  6559. sgitg);
  6560. }