ggml-metal.metal 245 KB

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