ggml-metal.metal 232 KB

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