ggml-metal.metal 231 KB

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