ggml-metal.metal 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540
  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. // Get n-d rotational scaling corrected for extrapolation
  1414. float theta_interp = freq_scale * theta_extrap;
  1415. float theta = theta_interp;
  1416. if (ext_factor != 0.0f) {
  1417. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  1418. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1419. // Get n-d magnitude scaling corrected for interpolation
  1420. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  1421. }
  1422. *cos_theta = cos(theta) * mscale;
  1423. *sin_theta = sin(theta) * mscale;
  1424. }
  1425. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  1426. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  1427. static float rope_yarn_corr_factor(int n_dims, int n_ctx_orig, float n_rot, float base) {
  1428. return n_dims * log(n_ctx_orig / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  1429. }
  1430. static void rope_yarn_corr_dims(
  1431. int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]
  1432. ) {
  1433. // start and end correction dims
  1434. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_fast, freq_base)));
  1435. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_ctx_orig, beta_slow, freq_base)));
  1436. }
  1437. template<typename T>
  1438. kernel void kernel_rope_norm(
  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 & n_ctx_orig,
  1462. constant float & freq_base,
  1463. constant float & freq_scale,
  1464. constant float & ext_factor,
  1465. constant float & attn_factor,
  1466. constant float & beta_fast,
  1467. constant float & beta_slow,
  1468. uint tiitg[[thread_index_in_threadgroup]],
  1469. uint3 tptg[[threads_per_threadgroup]],
  1470. uint3 tgpig[[threadgroup_position_in_grid]]) {
  1471. const int64_t i3 = tgpig[2];
  1472. const int64_t i2 = tgpig[1];
  1473. const int64_t i1 = tgpig[0];
  1474. float corr_dims[2];
  1475. rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  1476. device const int32_t * pos = src1;
  1477. const float theta_base = (float) pos[i2];
  1478. const float inv_ndims = -1.f/n_dims;
  1479. float cos_theta;
  1480. float sin_theta;
  1481. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  1482. if (i0 < n_dims) {
  1483. const int64_t ic = i0/2;
  1484. const float theta = theta_base * pow(freq_base, inv_ndims*i0);
  1485. const float freq_factor = src2 != src0 ? src2[ic] : 1.0f;
  1486. rope_yarn(theta/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1487. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1488. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1489. const float x0 = src[0];
  1490. const float x1 = src[1];
  1491. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1492. dst_data[1] = x0*sin_theta + x1*cos_theta;
  1493. } else {
  1494. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1495. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1496. dst_data[0] = src[0];
  1497. dst_data[1] = src[1];
  1498. }
  1499. }
  1500. }
  1501. template<typename T>
  1502. kernel void kernel_rope_neox(
  1503. device const void * src0,
  1504. device const int32_t * src1,
  1505. device const float * src2,
  1506. device float * dst,
  1507. constant int64_t & ne00,
  1508. constant int64_t & ne01,
  1509. constant int64_t & ne02,
  1510. constant int64_t & ne03,
  1511. constant uint64_t & nb00,
  1512. constant uint64_t & nb01,
  1513. constant uint64_t & nb02,
  1514. constant uint64_t & nb03,
  1515. constant int64_t & ne0,
  1516. constant int64_t & ne1,
  1517. constant int64_t & ne2,
  1518. constant int64_t & ne3,
  1519. constant uint64_t & nb0,
  1520. constant uint64_t & nb1,
  1521. constant uint64_t & nb2,
  1522. constant uint64_t & nb3,
  1523. constant int & n_past,
  1524. constant int & n_dims,
  1525. constant int & n_ctx_orig,
  1526. constant float & freq_base,
  1527. constant float & freq_scale,
  1528. constant float & ext_factor,
  1529. constant float & attn_factor,
  1530. constant float & beta_fast,
  1531. constant float & beta_slow,
  1532. uint tiitg[[thread_index_in_threadgroup]],
  1533. uint3 tptg[[threads_per_threadgroup]],
  1534. uint3 tgpig[[threadgroup_position_in_grid]]) {
  1535. const int64_t i3 = tgpig[2];
  1536. const int64_t i2 = tgpig[1];
  1537. const int64_t i1 = tgpig[0];
  1538. float corr_dims[2];
  1539. rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  1540. device const int32_t * pos = src1;
  1541. const float theta_base = (float) pos[i2];
  1542. const float inv_ndims = -1.f/n_dims;
  1543. float cos_theta;
  1544. float sin_theta;
  1545. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  1546. if (i0 < n_dims) {
  1547. const int64_t ic = i0/2;
  1548. const float theta = theta_base * pow(freq_base, inv_ndims*i0);
  1549. const float freq_factor = src2 != src0 ? src2[ic] : 1.0f;
  1550. rope_yarn(theta/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1551. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  1552. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  1553. const float x0 = src[0];
  1554. const float x1 = src[n_dims/2];
  1555. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1556. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  1557. } else {
  1558. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1559. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1560. dst_data[0] = src[0];
  1561. dst_data[1] = src[1];
  1562. }
  1563. }
  1564. }
  1565. typedef decltype(kernel_rope_norm<float>) kernel_rope_norm_t;
  1566. typedef decltype(kernel_rope_neox<float>) kernel_rope_neox_t;
  1567. template [[host_name("kernel_rope_norm_f32")]] kernel kernel_rope_norm_t kernel_rope_norm<float>;
  1568. template [[host_name("kernel_rope_norm_f16")]] kernel kernel_rope_norm_t kernel_rope_norm<half>;
  1569. template [[host_name("kernel_rope_neox_f32")]] kernel kernel_rope_neox_t kernel_rope_neox<float>;
  1570. template [[host_name("kernel_rope_neox_f16")]] kernel kernel_rope_neox_t kernel_rope_neox<half>;
  1571. typedef void (im2col_t)(
  1572. device const float * x,
  1573. device char * dst,
  1574. constant int32_t & ofs0,
  1575. constant int32_t & ofs1,
  1576. constant int32_t & IW,
  1577. constant int32_t & IH,
  1578. constant int32_t & CHW,
  1579. constant int32_t & s0,
  1580. constant int32_t & s1,
  1581. constant int32_t & p0,
  1582. constant int32_t & p1,
  1583. constant int32_t & d0,
  1584. constant int32_t & d1,
  1585. uint3 tgpig[[threadgroup_position_in_grid]],
  1586. uint3 tgpg[[threadgroups_per_grid]],
  1587. uint3 tpitg[[thread_position_in_threadgroup]],
  1588. uint3 ntg[[threads_per_threadgroup]]);
  1589. template <typename T>
  1590. kernel void kernel_im2col(
  1591. device const float * x,
  1592. device char * dst,
  1593. constant int32_t & ofs0,
  1594. constant int32_t & ofs1,
  1595. constant int32_t & IW,
  1596. constant int32_t & IH,
  1597. constant int32_t & CHW,
  1598. constant int32_t & s0,
  1599. constant int32_t & s1,
  1600. constant int32_t & p0,
  1601. constant int32_t & p1,
  1602. constant int32_t & d0,
  1603. constant int32_t & d1,
  1604. uint3 tgpig[[threadgroup_position_in_grid]],
  1605. uint3 tgpg[[threadgroups_per_grid]],
  1606. uint3 tpitg[[thread_position_in_threadgroup]],
  1607. uint3 ntg[[threads_per_threadgroup]]) {
  1608. const int32_t iiw = tgpig[2] * s0 + tpitg[2] * d0 - p0;
  1609. const int32_t iih = tgpig[1] * s1 + tpitg[1] * d1 - p1;
  1610. const int32_t offset_dst =
  1611. (tpitg[0] * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  1612. (tgpig[0] * (ntg[1] * ntg[2]) + tpitg[1] * ntg[2] + tpitg[2]);
  1613. device T * pdst = (device T *) (dst);
  1614. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  1615. pdst[offset_dst] = 0.0f;
  1616. } else {
  1617. const int32_t offset_src = tpitg[0] * ofs0 + tgpig[0] * ofs1;
  1618. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  1619. }
  1620. }
  1621. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  1622. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  1623. kernel void kernel_upscale_f32(
  1624. device const char * src0,
  1625. device char * dst,
  1626. constant int64_t & ne00,
  1627. constant int64_t & ne01,
  1628. constant int64_t & ne02,
  1629. constant int64_t & ne03,
  1630. constant uint64_t & nb00,
  1631. constant uint64_t & nb01,
  1632. constant uint64_t & nb02,
  1633. constant uint64_t & nb03,
  1634. constant int64_t & ne0,
  1635. constant int64_t & ne1,
  1636. constant int64_t & ne2,
  1637. constant int64_t & ne3,
  1638. constant uint64_t & nb0,
  1639. constant uint64_t & nb1,
  1640. constant uint64_t & nb2,
  1641. constant uint64_t & nb3,
  1642. constant float & sf0,
  1643. constant float & sf1,
  1644. constant float & sf2,
  1645. constant float & sf3,
  1646. uint3 tgpig[[threadgroup_position_in_grid]],
  1647. uint3 tpitg[[thread_position_in_threadgroup]],
  1648. uint3 ntg[[threads_per_threadgroup]]) {
  1649. const int64_t i3 = tgpig.z;
  1650. const int64_t i2 = tgpig.y;
  1651. const int64_t i1 = tgpig.x;
  1652. const int64_t i03 = i3/sf3;
  1653. const int64_t i02 = i2/sf2;
  1654. const int64_t i01 = i1/sf1;
  1655. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1656. const int64_t i00 = i0/sf0;
  1657. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1658. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1659. dst_ptr[0] = src0_ptr[0];
  1660. }
  1661. }
  1662. kernel void kernel_pad_f32(
  1663. device const char * src0,
  1664. device char * dst,
  1665. constant int64_t & ne00,
  1666. constant int64_t & ne01,
  1667. constant int64_t & ne02,
  1668. constant int64_t & ne03,
  1669. constant uint64_t & nb00,
  1670. constant uint64_t & nb01,
  1671. constant uint64_t & nb02,
  1672. constant uint64_t & nb03,
  1673. constant int64_t & ne0,
  1674. constant int64_t & ne1,
  1675. constant int64_t & ne2,
  1676. constant int64_t & ne3,
  1677. constant uint64_t & nb0,
  1678. constant uint64_t & nb1,
  1679. constant uint64_t & nb2,
  1680. constant uint64_t & nb3,
  1681. uint3 tgpig[[threadgroup_position_in_grid]],
  1682. uint3 tpitg[[thread_position_in_threadgroup]],
  1683. uint3 ntg[[threads_per_threadgroup]]) {
  1684. const int64_t i3 = tgpig.z;
  1685. const int64_t i2 = tgpig.y;
  1686. const int64_t i1 = tgpig.x;
  1687. const int64_t i03 = i3;
  1688. const int64_t i02 = i2;
  1689. const int64_t i01 = i1;
  1690. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  1691. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  1692. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  1693. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1694. if (i0 < ne00) {
  1695. dst_ptr[i0] = src0_ptr[i0];
  1696. } else {
  1697. dst_ptr[i0] = 0.0f;
  1698. }
  1699. }
  1700. return;
  1701. }
  1702. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1703. dst_ptr[i0] = 0.0f;
  1704. }
  1705. }
  1706. kernel void kernel_arange_f32(
  1707. device char * dst,
  1708. constant int64_t & ne0,
  1709. constant float & start,
  1710. constant float & step,
  1711. uint3 tgpig[[threadgroup_position_in_grid]],
  1712. uint3 tpitg[[thread_position_in_threadgroup]],
  1713. uint3 ntg[[threads_per_threadgroup]]) {
  1714. device float * dst_ptr = (device float *) dst;
  1715. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1716. dst_ptr[i0] = start + step * i0;
  1717. }
  1718. }
  1719. kernel void kernel_timestep_embedding_f32(
  1720. device const char * src0,
  1721. device char * dst,
  1722. constant uint64_t & nb1,
  1723. constant int & dim,
  1724. constant int & max_period,
  1725. uint3 tgpig[[threadgroup_position_in_grid]],
  1726. uint3 tpitg[[thread_position_in_threadgroup]],
  1727. uint3 ntg[[threads_per_threadgroup]]) {
  1728. int i = tgpig.x;
  1729. device float * embed_data = (device float *)(dst + i*nb1);
  1730. int half_ = dim / 2;
  1731. for (int j = tpitg.x; j < half_; j += ntg.x) {
  1732. float timestep = ((device float *)src0)[i];
  1733. float freq = (float)exp(-log((float)max_period) * j / half_);
  1734. float arg = timestep * freq;
  1735. embed_data[j ] = cos(arg);
  1736. embed_data[j + half_] = sin(arg);
  1737. }
  1738. if (dim % 2 != 0 && tpitg.x == 0) {
  1739. embed_data[dim] = 0.f;
  1740. }
  1741. }
  1742. // bitonic sort implementation following the CUDA kernels as reference
  1743. typedef void (argsort_t)(
  1744. device const float * x,
  1745. device int32_t * dst,
  1746. constant int64_t & ncols,
  1747. constant int64_t & ncols_pad,
  1748. threadgroup int32_t * shared_values [[threadgroup(0)]],
  1749. uint3 tgpig[[threadgroup_position_in_grid]],
  1750. uint3 tpitg[[thread_position_in_threadgroup]]);
  1751. template<ggml_sort_order order>
  1752. kernel void kernel_argsort_f32_i32(
  1753. device const float * x,
  1754. device int32_t * dst,
  1755. constant int64_t & ncols,
  1756. constant int64_t & ncols_pad,
  1757. threadgroup int32_t * shared_values [[threadgroup(0)]],
  1758. uint3 tgpig[[threadgroup_position_in_grid]],
  1759. uint3 tpitg[[thread_position_in_threadgroup]]) {
  1760. // bitonic sort
  1761. int col = tpitg[0];
  1762. int row = tgpig[1];
  1763. if (col >= ncols_pad) return;
  1764. device const float * x_row = x + row * ncols;
  1765. threadgroup int32_t * dst_row = shared_values;
  1766. // initialize indices
  1767. dst_row[col] = col;
  1768. threadgroup_barrier(mem_flags::mem_threadgroup);
  1769. for (int k = 2; k <= ncols_pad; k *= 2) {
  1770. for (int j = k / 2; j > 0; j /= 2) {
  1771. int ixj = col ^ j;
  1772. if (ixj > col) {
  1773. if ((col & k) == 0) {
  1774. if (dst_row[col] >= ncols ||
  1775. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  1776. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  1777. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  1778. ) {
  1779. SWAP(dst_row[col], dst_row[ixj]);
  1780. }
  1781. } else {
  1782. if (dst_row[ixj] >= ncols ||
  1783. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  1784. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  1785. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  1786. ) {
  1787. SWAP(dst_row[col], dst_row[ixj]);
  1788. }
  1789. }
  1790. }
  1791. threadgroup_barrier(mem_flags::mem_threadgroup);
  1792. }
  1793. }
  1794. // copy the result to dst without the padding
  1795. if (col < ncols) {
  1796. dst[row * ncols + col] = dst_row[col];
  1797. }
  1798. }
  1799. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_ASC>;
  1800. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ORDER_DESC>;
  1801. kernel void kernel_leaky_relu_f32(
  1802. device const float * src0,
  1803. device float * dst,
  1804. constant float & slope,
  1805. uint tpig[[thread_position_in_grid]]) {
  1806. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  1807. }
  1808. typedef void (flash_attn_ext_f16_t)(
  1809. device const char * q,
  1810. device const char * k,
  1811. device const char * v,
  1812. device const char * mask,
  1813. device float * dst,
  1814. constant int64_t & ne01,
  1815. constant int64_t & ne02,
  1816. constant int64_t & ne03,
  1817. constant uint64_t & nb01,
  1818. constant uint64_t & nb02,
  1819. constant uint64_t & nb03,
  1820. constant int64_t & ne11,
  1821. constant int64_t & ne12,
  1822. constant int64_t & ne13,
  1823. constant uint64_t & nb11,
  1824. constant uint64_t & nb12,
  1825. constant uint64_t & nb13,
  1826. constant uint64_t & nb21,
  1827. constant uint64_t & nb22,
  1828. constant uint64_t & nb23,
  1829. constant uint64_t & nb31,
  1830. constant int64_t & ne1,
  1831. constant int64_t & ne2,
  1832. constant float & scale,
  1833. constant float & max_bias,
  1834. constant float & m0,
  1835. constant float & m1,
  1836. constant uint32_t & n_head_log2,
  1837. threadgroup half * shared,
  1838. uint3 tgpig[[threadgroup_position_in_grid]],
  1839. uint3 tpitg[[thread_position_in_threadgroup]],
  1840. uint3 ntg[[threads_per_threadgroup]],
  1841. ushort tiisg[[thread_index_in_simdgroup]],
  1842. ushort sgitg[[simdgroup_index_in_threadgroup]]);
  1843. // ref: https://arxiv.org/pdf/2307.08691.pdf
  1844. template<int64_t D, int64_t Q = 8, int64_t C = 32> // head size, queries per threadgroup, cache items per threadgroup
  1845. kernel void kernel_flash_attn_ext_f16(
  1846. device const char * q,
  1847. device const char * k,
  1848. device const char * v,
  1849. device const char * mask,
  1850. device float * dst,
  1851. constant int64_t & ne01,
  1852. constant int64_t & ne02,
  1853. constant int64_t & ne03,
  1854. constant uint64_t & nb01,
  1855. constant uint64_t & nb02,
  1856. constant uint64_t & nb03,
  1857. constant int64_t & ne11,
  1858. constant int64_t & ne12,
  1859. constant int64_t & ne13,
  1860. constant uint64_t & nb11,
  1861. constant uint64_t & nb12,
  1862. constant uint64_t & nb13,
  1863. constant uint64_t & nb21,
  1864. constant uint64_t & nb22,
  1865. constant uint64_t & nb23,
  1866. constant uint64_t & nb31,
  1867. constant int64_t & ne1,
  1868. constant int64_t & ne2,
  1869. constant float & scale,
  1870. constant float & max_bias,
  1871. constant float & m0,
  1872. constant float & m1,
  1873. constant uint32_t & n_head_log2,
  1874. threadgroup half * shared [[threadgroup(0)]],
  1875. uint3 tgpig[[threadgroup_position_in_grid]],
  1876. uint3 tpitg[[thread_position_in_threadgroup]],
  1877. uint3 ntg[[threads_per_threadgroup]],
  1878. ushort tiisg[[thread_index_in_simdgroup]],
  1879. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  1880. const short nsg = ntg.y; // number of simdgroups
  1881. const short iq3 = tgpig[2];
  1882. const short iq2 = tgpig[1];
  1883. const short iq1 = tgpig[0]*Q;
  1884. const short D4 = D/4;
  1885. const short D8 = D/8;
  1886. //const short Q8 = Q/8;
  1887. const short NW = N_SIMDWIDTH;
  1888. const short SH = (C + Q); // shared memory per simdgroup in (half)
  1889. const short T = D + 2*nsg*SH; // shared memory size per query in (half)
  1890. const short TF = T/2; // shared memory size per query in (float)
  1891. const short T4 = T/4; // shared memory size per query in (half4)
  1892. threadgroup half * sq = (threadgroup half *) (shared + 0*D); // holds the query data
  1893. threadgroup half4 * sq4 = (threadgroup half4 *) (shared + 0*D); // same as above but in half4
  1894. threadgroup float * ss = (threadgroup float *) (shared + 2*sgitg*SH + 1*D); // scratch buffer for attention and diagonal matrix
  1895. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  1896. simdgroup_half8x8 lo[D8];
  1897. // load heads from Q to shared memory
  1898. for (short j = sgitg; j < Q; j += nsg) {
  1899. device const float4 * q4 = (device const float4 *) ((device const char *) q + ((iq1 + j)*nb01 + iq2*nb02 + iq3*nb03));
  1900. for (short i = tiisg; i < D4; i += NW) {
  1901. if (iq1 + j < ne01) {
  1902. sq4[j*T4 + i] = (half4) q4[i];
  1903. } else {
  1904. sq4[j*T4 + i] = 0.0h;
  1905. }
  1906. }
  1907. }
  1908. // zero out lo
  1909. for (short i = 0; i < D8; ++i) {
  1910. lo[i] = make_filled_simdgroup_matrix<half, 8>(0.0h);
  1911. }
  1912. // zero out shared memory SH
  1913. for (short j = 0; j < Q; ++j) {
  1914. for (short i = tiisg; i < SH; i += NW) {
  1915. ss[j*TF + i] = 0.0f;
  1916. }
  1917. }
  1918. threadgroup_barrier(mem_flags::mem_threadgroup);
  1919. {
  1920. float S[Q] = { [0 ... Q-1] = 0.0h };
  1921. float M[Q] = { [0 ... Q-1] = -FLT_MAX/2 };
  1922. // assume K and V are same shape
  1923. const short ne22 = ne12;
  1924. const short ne23 = ne13;
  1925. // broadcast
  1926. const short rk2 = ne02/ne12;
  1927. const short rk3 = ne03/ne13;
  1928. const short rv2 = ne02/ne22;
  1929. const short rv3 = ne03/ne23;
  1930. // k indices
  1931. const short ik2 = iq2/rk2;
  1932. const short ik3 = iq3/rk3;
  1933. // v indices
  1934. const short iv2 = iq2/rv2;
  1935. const short iv3 = iq3/rv3;
  1936. // load the queries from shared memory into local memory
  1937. simdgroup_half8x8 mq[D8];
  1938. for (short i = 0; i < D8; ++i) {
  1939. simdgroup_load(mq[i], sq + i*8, T);
  1940. }
  1941. // pointer to the mask
  1942. device const half * mp = (device const half *) (mask + iq1*nb31);
  1943. float slope = 1.0f;
  1944. // ALiBi
  1945. if (max_bias > 0.0f) {
  1946. const uint32_t h = iq2;
  1947. const float base = h < n_head_log2 ? m0 : m1;
  1948. const int exph = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  1949. slope = pow(base, exph);
  1950. }
  1951. // loop over the KV cache
  1952. // each simdgroup handles blocks of Q rows and C columns
  1953. for (int ic0 = 0; ic0 < ne11; ic0 += C*nsg) {
  1954. const int ic = ic0 + C*sgitg;
  1955. if (ic >= ne11) {
  1956. break;
  1957. }
  1958. // Q*K^T
  1959. {
  1960. for (short cc = 0; cc < C/8; ++cc) {
  1961. simdgroup_float8x8 mqk = make_filled_simdgroup_matrix<float, 8>(0.h);
  1962. device const half * pk = (device const half *) ((device const char *) k + ((ic + 8*cc)*nb11 + ik2*nb12 + ik3*nb13));
  1963. for (short i = 0; i < D8; ++i) {
  1964. simdgroup_half8x8 mk;
  1965. simdgroup_load(mk, pk + i*8, nb11/sizeof(half), 0, true); // transpose
  1966. simdgroup_multiply_accumulate(mqk, mq[i], mk, mqk);
  1967. }
  1968. simdgroup_store(mqk, ss + 8*cc, TF, 0, false);
  1969. const short tx = tiisg%4;
  1970. const short ty = tiisg/4;
  1971. if (mask != q) {
  1972. // mqk = mqk*scale + mask*slope
  1973. 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];
  1974. 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];
  1975. } else {
  1976. // mqk = mqk*scale
  1977. ss[8*cc + ty*TF + 2*tx + 0] *= scale;
  1978. ss[8*cc + ty*TF + 2*tx + 1] *= scale;
  1979. }
  1980. }
  1981. }
  1982. // used to detect blocks full of -INF
  1983. float smax = -INFINITY;
  1984. // online softmax
  1985. {
  1986. float ms[Q];
  1987. for (short j = 0; j < Q; ++j) {
  1988. const short p = tiisg;
  1989. const float m = M[j];
  1990. const float s = ss[j*TF + p];
  1991. smax = simd_max(max(smax, s));
  1992. M[j] = simd_max(max(M[j], s));
  1993. ms[j] = exp(m - M[j]);
  1994. const float vs = exp(s - M[j]);
  1995. S[j] = S[j]*ms[j] + simd_sum(vs);
  1996. // the P matrix from the paper (Q rows, C columns)
  1997. ss[j*TF + p] = vs;
  1998. }
  1999. // create a QxQ diagonal matrix for rescaling the output
  2000. if (tiisg < Q) {
  2001. ss[tiisg*TF + C + tiisg] = ms[tiisg];
  2002. }
  2003. }
  2004. // skip -INF blocks
  2005. if (smax == -INFINITY) {
  2006. continue;
  2007. }
  2008. // O = diag(ms)*O
  2009. {
  2010. simdgroup_float8x8 mm;
  2011. simdgroup_load(mm, ss + C, TF, 0, false);
  2012. for (short i = 0; i < D8; ++i) {
  2013. simdgroup_multiply(lo[i], mm, lo[i]);
  2014. }
  2015. }
  2016. // O = O + (Q*K^T)*V
  2017. {
  2018. for (short cc = 0; cc < C/8; ++cc) {
  2019. device const half * pv = (device const half *) ((device const char *) v + ((ic + 8*cc)*nb21 + iv2*nb22 + iv3*nb23));
  2020. for (short i = 0; i < D8; ++i) {
  2021. simdgroup_half8x8 mk;
  2022. simdgroup_load(mk, pv + i*8, nb21/sizeof(half), 0, false);
  2023. simdgroup_float8x8 mv;
  2024. simdgroup_load(mv, ss + 8*cc, TF, 0, false);
  2025. simdgroup_multiply_accumulate(lo[i], mv, mk, lo[i]);
  2026. }
  2027. }
  2028. }
  2029. }
  2030. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  2031. for (short j = 0; j < Q; ++j) {
  2032. if (tiisg == 0) {
  2033. ss[j*TF + 0] = S[j];
  2034. ss[j*TF + 1] = M[j];
  2035. }
  2036. }
  2037. }
  2038. // reduce the warps sequentially
  2039. for (short sg = 1; sg < nsg; ++sg) {
  2040. float S = { 0.0h };
  2041. float M = { -FLT_MAX/2 };
  2042. threadgroup_barrier(mem_flags::mem_threadgroup);
  2043. // each simdgroup stores its output to shared memory, reusing sq
  2044. if (sgitg == sg) {
  2045. for (short i = 0; i < D8; ++i) {
  2046. simdgroup_store(lo[i], sq + i*8, T, 0, false);
  2047. }
  2048. }
  2049. threadgroup_barrier(mem_flags::mem_threadgroup);
  2050. // the first simdgroup accumulates the results from the other simdgroups
  2051. if (sgitg == 0) {
  2052. for (short j = 0; j < Q; ++j) {
  2053. const float S0 = ss[j*TF + 0];
  2054. const float S1 = ss[j*TF + sg*SH + 0];
  2055. const float M0 = ss[j*TF + 1];
  2056. const float M1 = ss[j*TF + sg*SH + 1];
  2057. M = max(M0, M1);
  2058. const float ms0 = exp(M0 - M);
  2059. const float ms1 = exp(M1 - M);
  2060. S = S0*ms0 + S1*ms1;
  2061. if (tiisg == 0) {
  2062. ss[j*TF + 0] = S;
  2063. ss[j*TF + 1] = M;
  2064. ss[j*TF + C + j ] = ms0;
  2065. ss[j*TF + C + j + sg*SH] = ms1;
  2066. }
  2067. }
  2068. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  2069. {
  2070. simdgroup_half8x8 t;
  2071. simdgroup_float8x8 ms0;
  2072. simdgroup_float8x8 ms1;
  2073. simdgroup_load(ms0, ss + C, TF, 0, false);
  2074. simdgroup_load(ms1, ss + C + sg*SH, TF, 0, false);
  2075. for (short i = 0; i < D8; ++i) {
  2076. simdgroup_load (t, sq + i*8, T, 0, false);
  2077. simdgroup_multiply(t, ms1, t);
  2078. simdgroup_multiply_accumulate(lo[i], ms0, lo[i], t);
  2079. }
  2080. }
  2081. }
  2082. }
  2083. // store result to shared memory (reuse sq)
  2084. if (sgitg == 0) {
  2085. for (short i = 0; i < D8; ++i) {
  2086. simdgroup_store(lo[i], sq + i*8, T, 0, false);
  2087. }
  2088. }
  2089. device float4 * dst4 = (device float4 *) dst;
  2090. // final rescale with 1/S and store to global memory
  2091. if (sgitg == 0) {
  2092. for (short j = 0; j < Q && iq1 + j < ne01; ++j) {
  2093. const float S = ss[j*TF + 0];
  2094. for (short i = tiisg; i < D4; i += NW) {
  2095. dst4[(iq3*ne2*ne1 + iq2 + (iq1 + j)*ne1)*D4 + i] = (float4) sq4[j*T4 + i]/S;
  2096. }
  2097. }
  2098. }
  2099. }
  2100. template [[host_name("kernel_flash_attn_ext_f16_h64" )]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<64>;
  2101. template [[host_name("kernel_flash_attn_ext_f16_h80" )]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<80>;
  2102. template [[host_name("kernel_flash_attn_ext_f16_h96" )]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<96>;
  2103. template [[host_name("kernel_flash_attn_ext_f16_h112")]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<112>;
  2104. template [[host_name("kernel_flash_attn_ext_f16_h128")]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<128>;
  2105. //template [[host_name("kernel_flash_attn_ext_f16_h256")]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_f16<256>;
  2106. template<int64_t D, int64_t Q = 1, int64_t C = 32> // head size, queries per threadgroup, cache items per threadgroup
  2107. kernel void kernel_flash_attn_ext_vec_f16(
  2108. device const char * q,
  2109. device const char * k,
  2110. device const char * v,
  2111. device const char * mask,
  2112. device float * dst,
  2113. constant int64_t & ne01,
  2114. constant int64_t & ne02,
  2115. constant int64_t & ne03,
  2116. constant uint64_t & nb01,
  2117. constant uint64_t & nb02,
  2118. constant uint64_t & nb03,
  2119. constant int64_t & ne11,
  2120. constant int64_t & ne12,
  2121. constant int64_t & ne13,
  2122. constant uint64_t & nb11,
  2123. constant uint64_t & nb12,
  2124. constant uint64_t & nb13,
  2125. constant uint64_t & nb21,
  2126. constant uint64_t & nb22,
  2127. constant uint64_t & nb23,
  2128. constant uint64_t & nb31,
  2129. constant int64_t & ne1,
  2130. constant int64_t & ne2,
  2131. constant float & scale,
  2132. constant float & max_bias,
  2133. constant float & m0,
  2134. constant float & m1,
  2135. constant uint32_t & n_head_log2,
  2136. threadgroup half * shared [[threadgroup(0)]],
  2137. uint3 tgpig[[threadgroup_position_in_grid]],
  2138. uint3 tpitg[[thread_position_in_threadgroup]],
  2139. uint3 ntg[[threads_per_threadgroup]],
  2140. ushort tiisg[[thread_index_in_simdgroup]],
  2141. ushort sgitg[[simdgroup_index_in_threadgroup]]) {
  2142. const short nsg = ntg.y; // number of simdgroups
  2143. const short iq3 = tgpig[2];
  2144. const short iq2 = tgpig[1];
  2145. const short iq1 = tgpig[0];
  2146. const short D4 = D/4;
  2147. const short NW = N_SIMDWIDTH;
  2148. const short SH = (C + Q); // shared memory per simdgroup in (half)
  2149. const short T = D + 2*nsg*SH; // shared memory size per query in (half)
  2150. float slope = 1.0f;
  2151. // ALiBi
  2152. if (max_bias > 0.0f) {
  2153. const uint32_t h = iq2;
  2154. const float base = h < n_head_log2 ? m0 : m1;
  2155. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  2156. slope = pow(base, exp);
  2157. }
  2158. //threadgroup half * sq = (threadgroup half *) (shared + 0*D); // holds the query data
  2159. threadgroup half4 * sq4 = (threadgroup half4 *) (shared + 0*D); // same as above but in half4
  2160. threadgroup float * ss = (threadgroup float *) (shared + 2*sgitg*SH + 1*D); // scratch buffer for attention and diagonal matrix
  2161. threadgroup float4 * ss4 = (threadgroup float4 *) (shared + 2*sgitg*SH + 1*D); // same as above but in half4
  2162. threadgroup half4 * sr4 = (threadgroup half4 *) (shared + sgitg*D + 1*T); // scratch buffer for the results
  2163. // store the result for all queries in local memory in 8x8 matrices (the O matrix from the paper)
  2164. half4 lo[D4/NW];
  2165. // load heads from Q to shared memory
  2166. device const float4 * q4 = (device const float4 *) ((device const char *) q + (iq1*nb01 + iq2*nb02 + iq3*nb03));
  2167. for (short i = tiisg; i < D4; i += NW) {
  2168. if (iq1 < ne01) {
  2169. sq4[i] = (half4) q4[i];
  2170. } else {
  2171. sq4[i] = 0.0h;
  2172. }
  2173. }
  2174. // zero out lo
  2175. for (short i = tiisg; i < D4; i += NW) {
  2176. lo[i/NW] = 0.0h;
  2177. }
  2178. // zero out shared memory SH
  2179. for (short i = tiisg; i < SH/4; i += NW) {
  2180. ss4[i] = 0.0h;
  2181. }
  2182. threadgroup_barrier(mem_flags::mem_threadgroup);
  2183. {
  2184. float S = { 0.0h };
  2185. float M = { -FLT_MAX/2 };
  2186. // assume K and V are same shape
  2187. const short ne22 = ne12;
  2188. const short ne23 = ne13;
  2189. // broadcast
  2190. const short rk2 = ne02/ne12;
  2191. const short rk3 = ne03/ne13;
  2192. const short rv2 = ne02/ne22;
  2193. const short rv3 = ne03/ne23;
  2194. // k indices
  2195. const short ik2 = iq2 / rk2;
  2196. const short ik3 = iq3 / rk3;
  2197. // v indices
  2198. const short iv2 = iq2 / rv2;
  2199. const short iv3 = iq3 / rv3;
  2200. // load the queries from shared memory into local memory
  2201. half4 mq[D4];
  2202. for (short ii = 0; ii < D4; ii += NW) {
  2203. short i = ii + tiisg;
  2204. mq[i] = sq4[i];
  2205. }
  2206. // pointer to the mask
  2207. device const half4 * mp4 = (device const half4 *) (mask + iq1*nb31);
  2208. // loop over the KV cache
  2209. // each simdgroup handles blocks of Q rows and C columns
  2210. for (int ic0 = 0; ic0 < ne11; ic0 += C*nsg) {
  2211. const int ic = ic0 + C*sgitg;
  2212. if (ic >= ne11) {
  2213. break;
  2214. }
  2215. // Q*K^T
  2216. {
  2217. #pragma unroll
  2218. for (short cc = 0; cc < C/4; ++cc) {
  2219. float4 mqk = { 0.0h };
  2220. device const half4 * pk4 = (device const half4 *) ((device const char *) k + ((ic + 4*cc)*nb11 + ik2*nb12 + ik3*nb13));
  2221. #pragma unroll
  2222. for (short ii = 0; ii < D4; ii += NW) {
  2223. const short i = ii + tiisg;
  2224. half4x4 mk;
  2225. mk[0] = pk4[i + 0*(nb11/8)];
  2226. mk[1] = pk4[i + 1*(nb11/8)];
  2227. mk[2] = pk4[i + 2*(nb11/8)];
  2228. mk[3] = pk4[i + 3*(nb11/8)];
  2229. mqk += (float4) (mq[i] * mk);
  2230. }
  2231. // reduce the results from the threads in the simdgroup
  2232. mqk += simd_shuffle_down(mqk, 16);
  2233. mqk += simd_shuffle_down(mqk, 8);
  2234. mqk += simd_shuffle_down(mqk, 4);
  2235. mqk += simd_shuffle_down(mqk, 2);
  2236. mqk += simd_shuffle_down(mqk, 1);
  2237. // mqk = mqk*scale + mask*slope
  2238. if (tiisg == 0) {
  2239. mqk = mqk*scale + ((mask != q) ? ((float4) mp4[ic/4 + cc])*slope : (float4) 0.0f);
  2240. ss4[cc] = mqk;
  2241. }
  2242. }
  2243. }
  2244. // online softmax
  2245. {
  2246. const short p = tiisg;
  2247. const float m = M;
  2248. const float s = ss[p];
  2249. M = simd_max(max(M, s));
  2250. const float ms = exp(m - M);
  2251. const float vs = exp(s - M);
  2252. S = S*ms + simd_sum(vs);
  2253. // the P matrix from the paper (Q rows, C columns)
  2254. ss[p] = vs;
  2255. // O = diag(ms)*O
  2256. #pragma unroll
  2257. for (short ii = 0; ii < D4; ii += NW) {
  2258. const short i = ii + tiisg;
  2259. lo[i/NW] *= ms;
  2260. }
  2261. }
  2262. // O = O + (Q*K^T)*V
  2263. {
  2264. #pragma unroll
  2265. for (short cc = 0; cc < C/4; ++cc) {
  2266. device const half4 * pv4 = (device const half4 *) ((device const char *) v + ((ic + 4*cc)*nb21 + iv2*nb22 + iv3*nb23));
  2267. #pragma unroll
  2268. for (short ii = 0; ii < D4; ii += NW) {
  2269. const short i = ii + tiisg;
  2270. lo[i/NW] += pv4[i + 0*(nb21/8)] * ss[4*cc + 0];
  2271. lo[i/NW] += pv4[i + 1*(nb21/8)] * ss[4*cc + 1];
  2272. lo[i/NW] += pv4[i + 2*(nb21/8)] * ss[4*cc + 2];
  2273. lo[i/NW] += pv4[i + 3*(nb21/8)] * ss[4*cc + 3];
  2274. }
  2275. }
  2276. }
  2277. }
  2278. // these are needed for reducing the results from the simdgroups (reuse the ss buffer)
  2279. if (tiisg == 0) {
  2280. ss[0] = S;
  2281. ss[1] = M;
  2282. }
  2283. }
  2284. // store results to shared memory
  2285. for (short ii = 0; ii < D4; ii += NW) {
  2286. short i = ii + tiisg;
  2287. sr4[i] = lo[ii/NW];
  2288. }
  2289. threadgroup_barrier(mem_flags::mem_threadgroup);
  2290. // parallel reduce
  2291. for (short r = nsg/2; r > 0; r >>= 1) {
  2292. if (sgitg < r) {
  2293. const float S0 = ss[ 0];
  2294. const float S1 = ss[r*SH + 0];
  2295. const float M0 = ss[ 1];
  2296. const float M1 = ss[r*SH + 1];
  2297. const float M = max(M0, M1);
  2298. const float ms0 = exp(M0 - M);
  2299. const float ms1 = exp(M1 - M);
  2300. const float S = S0*ms0 + S1*ms1;
  2301. if (tiisg == 0) {
  2302. ss[0] = S;
  2303. ss[1] = M;
  2304. }
  2305. // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
  2306. for (short ii = 0; ii < D4; ii += NW) {
  2307. short i = ii + tiisg;
  2308. sr4[i] = sr4[i]*ms0 + sr4[i + r*D4]*ms1;
  2309. }
  2310. }
  2311. threadgroup_barrier(mem_flags::mem_threadgroup);
  2312. }
  2313. device float4 * dst4 = (device float4 *) dst;
  2314. // final rescale with 1/S and store to global memory
  2315. if (sgitg == 0) {
  2316. const float S = ss[0];
  2317. for (short ii = 0; ii < D4; ii += NW) {
  2318. short i = ii + tiisg;
  2319. dst4[(iq3*ne2*ne1 + iq2 + (iq1)*ne1)*D4 + i] = (float4) sr4[i]/S;
  2320. }
  2321. }
  2322. }
  2323. template [[host_name("kernel_flash_attn_ext_vec_f16_h128")]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_vec_f16<128>;
  2324. //template [[host_name("kernel_flash_attn_ext_vec_f16_h256")]] kernel flash_attn_ext_f16_t kernel_flash_attn_ext_vec_f16<256>;
  2325. kernel void kernel_cpy_f16_f16(
  2326. device const half * src0,
  2327. device half * dst,
  2328. constant int64_t & ne00,
  2329. constant int64_t & ne01,
  2330. constant int64_t & ne02,
  2331. constant int64_t & ne03,
  2332. constant uint64_t & nb00,
  2333. constant uint64_t & nb01,
  2334. constant uint64_t & nb02,
  2335. constant uint64_t & nb03,
  2336. constant int64_t & ne0,
  2337. constant int64_t & ne1,
  2338. constant int64_t & ne2,
  2339. constant int64_t & ne3,
  2340. constant uint64_t & nb0,
  2341. constant uint64_t & nb1,
  2342. constant uint64_t & nb2,
  2343. constant uint64_t & nb3,
  2344. uint3 tgpig[[threadgroup_position_in_grid]],
  2345. uint3 tpitg[[thread_position_in_threadgroup]],
  2346. uint3 ntg[[threads_per_threadgroup]]) {
  2347. const int64_t i03 = tgpig[2];
  2348. const int64_t i02 = tgpig[1];
  2349. const int64_t i01 = tgpig[0];
  2350. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2351. const int64_t i3 = n / (ne2*ne1*ne0);
  2352. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2353. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2354. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  2355. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2356. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  2357. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2358. dst_data[i00] = src[0];
  2359. }
  2360. }
  2361. kernel void kernel_cpy_f16_f32(
  2362. device const half * src0,
  2363. device float * dst,
  2364. constant int64_t & ne00,
  2365. constant int64_t & ne01,
  2366. constant int64_t & ne02,
  2367. constant int64_t & ne03,
  2368. constant uint64_t & nb00,
  2369. constant uint64_t & nb01,
  2370. constant uint64_t & nb02,
  2371. constant uint64_t & nb03,
  2372. constant int64_t & ne0,
  2373. constant int64_t & ne1,
  2374. constant int64_t & ne2,
  2375. constant int64_t & ne3,
  2376. constant uint64_t & nb0,
  2377. constant uint64_t & nb1,
  2378. constant uint64_t & nb2,
  2379. constant uint64_t & nb3,
  2380. uint3 tgpig[[threadgroup_position_in_grid]],
  2381. uint3 tpitg[[thread_position_in_threadgroup]],
  2382. uint3 ntg[[threads_per_threadgroup]]) {
  2383. const int64_t i03 = tgpig[2];
  2384. const int64_t i02 = tgpig[1];
  2385. const int64_t i01 = tgpig[0];
  2386. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2387. const int64_t i3 = n / (ne2*ne1*ne0);
  2388. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2389. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2390. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  2391. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2392. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  2393. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2394. dst_data[i00] = src[0];
  2395. }
  2396. }
  2397. kernel void kernel_cpy_f32_f16(
  2398. device const float * src0,
  2399. device half * dst,
  2400. constant int64_t & ne00,
  2401. constant int64_t & ne01,
  2402. constant int64_t & ne02,
  2403. constant int64_t & ne03,
  2404. constant uint64_t & nb00,
  2405. constant uint64_t & nb01,
  2406. constant uint64_t & nb02,
  2407. constant uint64_t & nb03,
  2408. constant int64_t & ne0,
  2409. constant int64_t & ne1,
  2410. constant int64_t & ne2,
  2411. constant int64_t & ne3,
  2412. constant uint64_t & nb0,
  2413. constant uint64_t & nb1,
  2414. constant uint64_t & nb2,
  2415. constant uint64_t & nb3,
  2416. uint3 tgpig[[threadgroup_position_in_grid]],
  2417. uint3 tpitg[[thread_position_in_threadgroup]],
  2418. uint3 ntg[[threads_per_threadgroup]]) {
  2419. const int64_t i03 = tgpig[2];
  2420. const int64_t i02 = tgpig[1];
  2421. const int64_t i01 = tgpig[0];
  2422. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2423. const int64_t i3 = n / (ne2*ne1*ne0);
  2424. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2425. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2426. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  2427. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2428. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  2429. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2430. dst_data[i00] = src[0];
  2431. }
  2432. }
  2433. kernel void kernel_cpy_f32_f32(
  2434. device const float * src0,
  2435. device float * dst,
  2436. constant int64_t & ne00,
  2437. constant int64_t & ne01,
  2438. constant int64_t & ne02,
  2439. constant int64_t & ne03,
  2440. constant uint64_t & nb00,
  2441. constant uint64_t & nb01,
  2442. constant uint64_t & nb02,
  2443. constant uint64_t & nb03,
  2444. constant int64_t & ne0,
  2445. constant int64_t & ne1,
  2446. constant int64_t & ne2,
  2447. constant int64_t & ne3,
  2448. constant uint64_t & nb0,
  2449. constant uint64_t & nb1,
  2450. constant uint64_t & nb2,
  2451. constant uint64_t & nb3,
  2452. uint3 tgpig[[threadgroup_position_in_grid]],
  2453. uint3 tpitg[[thread_position_in_threadgroup]],
  2454. uint3 ntg[[threads_per_threadgroup]]) {
  2455. const int64_t i03 = tgpig[2];
  2456. const int64_t i02 = tgpig[1];
  2457. const int64_t i01 = tgpig[0];
  2458. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2459. const int64_t i3 = n / (ne2*ne1*ne0);
  2460. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2461. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2462. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  2463. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2464. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  2465. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2466. dst_data[i00] = src[0];
  2467. }
  2468. }
  2469. kernel void kernel_cpy_f32_q8_0(
  2470. device const float * src0,
  2471. device void * dst,
  2472. constant int64_t & ne00,
  2473. constant int64_t & ne01,
  2474. constant int64_t & ne02,
  2475. constant int64_t & ne03,
  2476. constant uint64_t & nb00,
  2477. constant uint64_t & nb01,
  2478. constant uint64_t & nb02,
  2479. constant uint64_t & nb03,
  2480. constant int64_t & ne0,
  2481. constant int64_t & ne1,
  2482. constant int64_t & ne2,
  2483. constant int64_t & ne3,
  2484. constant uint64_t & nb0,
  2485. constant uint64_t & nb1,
  2486. constant uint64_t & nb2,
  2487. constant uint64_t & nb3,
  2488. uint3 tgpig[[threadgroup_position_in_grid]],
  2489. uint3 tpitg[[thread_position_in_threadgroup]],
  2490. uint3 ntg[[threads_per_threadgroup]]) {
  2491. const int64_t i03 = tgpig[2];
  2492. const int64_t i02 = tgpig[1];
  2493. const int64_t i01 = tgpig[0];
  2494. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2495. const int64_t i3 = n / (ne2*ne1*ne0);
  2496. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2497. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2498. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK8_0;
  2499. device block_q8_0 * dst_data = (device block_q8_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2500. for (int64_t i00 = tpitg.x*QK8_0; i00 < ne00; i00 += ntg.x*QK8_0) {
  2501. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2502. float amax = 0.0f; // absolute max
  2503. for (int j = 0; j < QK8_0; j++) {
  2504. const float v = src[j];
  2505. amax = MAX(amax, fabs(v));
  2506. }
  2507. const float d = amax / ((1 << 7) - 1);
  2508. const float id = d ? 1.0f/d : 0.0f;
  2509. dst_data[i00/QK8_0].d = d;
  2510. for (int j = 0; j < QK8_0; ++j) {
  2511. const float x0 = src[j]*id;
  2512. dst_data[i00/QK8_0].qs[j] = round(x0);
  2513. }
  2514. }
  2515. }
  2516. kernel void kernel_cpy_f32_q4_0(
  2517. device const float * src0,
  2518. device void * dst,
  2519. constant int64_t & ne00,
  2520. constant int64_t & ne01,
  2521. constant int64_t & ne02,
  2522. constant int64_t & ne03,
  2523. constant uint64_t & nb00,
  2524. constant uint64_t & nb01,
  2525. constant uint64_t & nb02,
  2526. constant uint64_t & nb03,
  2527. constant int64_t & ne0,
  2528. constant int64_t & ne1,
  2529. constant int64_t & ne2,
  2530. constant int64_t & ne3,
  2531. constant uint64_t & nb0,
  2532. constant uint64_t & nb1,
  2533. constant uint64_t & nb2,
  2534. constant uint64_t & nb3,
  2535. uint3 tgpig[[threadgroup_position_in_grid]],
  2536. uint3 tpitg[[thread_position_in_threadgroup]],
  2537. uint3 ntg[[threads_per_threadgroup]]) {
  2538. const int64_t i03 = tgpig[2];
  2539. const int64_t i02 = tgpig[1];
  2540. const int64_t i01 = tgpig[0];
  2541. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2542. const int64_t i3 = n / (ne2*ne1*ne0);
  2543. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2544. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2545. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_0;
  2546. device block_q4_0 * dst_data = (device block_q4_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2547. for (int64_t i00 = tpitg.x*QK4_0; i00 < ne00; i00 += ntg.x*QK4_0) {
  2548. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2549. float amax = 0.0f; // absolute max
  2550. float max = 0.0f;
  2551. for (int j = 0; j < QK4_0; j++) {
  2552. const float v = src[j];
  2553. if (amax < fabs(v)) {
  2554. amax = fabs(v);
  2555. max = v;
  2556. }
  2557. }
  2558. const float d = max / -8;
  2559. const float id = d ? 1.0f/d : 0.0f;
  2560. dst_data[i00/QK4_0].d = d;
  2561. for (int j = 0; j < QK4_0/2; ++j) {
  2562. const float x0 = src[0 + j]*id;
  2563. const float x1 = src[QK4_0/2 + j]*id;
  2564. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  2565. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  2566. dst_data[i00/QK4_0].qs[j] = xi0;
  2567. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  2568. }
  2569. }
  2570. }
  2571. kernel void kernel_cpy_f32_q4_1(
  2572. device const float * src0,
  2573. device void * dst,
  2574. constant int64_t & ne00,
  2575. constant int64_t & ne01,
  2576. constant int64_t & ne02,
  2577. constant int64_t & ne03,
  2578. constant uint64_t & nb00,
  2579. constant uint64_t & nb01,
  2580. constant uint64_t & nb02,
  2581. constant uint64_t & nb03,
  2582. constant int64_t & ne0,
  2583. constant int64_t & ne1,
  2584. constant int64_t & ne2,
  2585. constant int64_t & ne3,
  2586. constant uint64_t & nb0,
  2587. constant uint64_t & nb1,
  2588. constant uint64_t & nb2,
  2589. constant uint64_t & nb3,
  2590. uint3 tgpig[[threadgroup_position_in_grid]],
  2591. uint3 tpitg[[thread_position_in_threadgroup]],
  2592. uint3 ntg[[threads_per_threadgroup]]) {
  2593. const int64_t i03 = tgpig[2];
  2594. const int64_t i02 = tgpig[1];
  2595. const int64_t i01 = tgpig[0];
  2596. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2597. const int64_t i3 = n / (ne2*ne1*ne0);
  2598. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2599. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2600. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_1;
  2601. device block_q4_1 * dst_data = (device block_q4_1 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2602. for (int64_t i00 = tpitg.x*QK4_1; i00 < ne00; i00 += ntg.x*QK4_1) {
  2603. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2604. float min = FLT_MAX;
  2605. float max = -FLT_MAX;
  2606. for (int j = 0; j < QK4_1; j++) {
  2607. const float v = src[j];
  2608. if (min > v) min = v;
  2609. if (max < v) max = v;
  2610. }
  2611. const float d = (max - min) / ((1 << 4) - 1);
  2612. const float id = d ? 1.0f/d : 0.0f;
  2613. dst_data[i00/QK4_1].d = d;
  2614. dst_data[i00/QK4_1].m = min;
  2615. for (int j = 0; j < QK4_1/2; ++j) {
  2616. const float x0 = (src[0 + j] - min)*id;
  2617. const float x1 = (src[QK4_1/2 + j] - min)*id;
  2618. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  2619. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  2620. dst_data[i00/QK4_1].qs[j] = xi0;
  2621. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  2622. }
  2623. }
  2624. }
  2625. kernel void kernel_cpy_f32_q5_0(
  2626. device const float * src0,
  2627. device void * dst,
  2628. constant int64_t & ne00,
  2629. constant int64_t & ne01,
  2630. constant int64_t & ne02,
  2631. constant int64_t & ne03,
  2632. constant uint64_t & nb00,
  2633. constant uint64_t & nb01,
  2634. constant uint64_t & nb02,
  2635. constant uint64_t & nb03,
  2636. constant int64_t & ne0,
  2637. constant int64_t & ne1,
  2638. constant int64_t & ne2,
  2639. constant int64_t & ne3,
  2640. constant uint64_t & nb0,
  2641. constant uint64_t & nb1,
  2642. constant uint64_t & nb2,
  2643. constant uint64_t & nb3,
  2644. uint3 tgpig[[threadgroup_position_in_grid]],
  2645. uint3 tpitg[[thread_position_in_threadgroup]],
  2646. uint3 ntg[[threads_per_threadgroup]]) {
  2647. const int64_t i03 = tgpig[2];
  2648. const int64_t i02 = tgpig[1];
  2649. const int64_t i01 = tgpig[0];
  2650. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2651. const int64_t i3 = n / (ne2*ne1*ne0);
  2652. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2653. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2654. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK5_0;
  2655. device block_q5_0 * dst_data = (device block_q5_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2656. for (int64_t i00 = tpitg.x*QK5_0; i00 < ne00; i00 += ntg.x*QK5_0) {
  2657. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2658. float amax = 0.0f; // absolute max
  2659. float max = 0.0f;
  2660. for (int j = 0; j < QK5_0; j++) {
  2661. const float v = src[j];
  2662. if (amax < fabs(v)) {
  2663. amax = fabs(v);
  2664. max = v;
  2665. }
  2666. }
  2667. const float d = max / -16;
  2668. const float id = d ? 1.0f/d : 0.0f;
  2669. dst_data[i00/QK5_0].d = d;
  2670. uint32_t qh = 0;
  2671. for (int j = 0; j < QK5_0/2; ++j) {
  2672. const float x0 = src[0 + j]*id;
  2673. const float x1 = src[QK5_0/2 + j]*id;
  2674. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  2675. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  2676. dst_data[i00/QK5_0].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  2677. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2678. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2679. }
  2680. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  2681. for (int j = 0; j < 4; ++j) {
  2682. dst_data[i00/QK5_0].qh[j] = qh8[j];
  2683. }
  2684. }
  2685. }
  2686. kernel void kernel_cpy_f32_q5_1(
  2687. device const float * src0,
  2688. device void * dst,
  2689. constant int64_t & ne00,
  2690. constant int64_t & ne01,
  2691. constant int64_t & ne02,
  2692. constant int64_t & ne03,
  2693. constant uint64_t & nb00,
  2694. constant uint64_t & nb01,
  2695. constant uint64_t & nb02,
  2696. constant uint64_t & nb03,
  2697. constant int64_t & ne0,
  2698. constant int64_t & ne1,
  2699. constant int64_t & ne2,
  2700. constant int64_t & ne3,
  2701. constant uint64_t & nb0,
  2702. constant uint64_t & nb1,
  2703. constant uint64_t & nb2,
  2704. constant uint64_t & nb3,
  2705. uint3 tgpig[[threadgroup_position_in_grid]],
  2706. uint3 tpitg[[thread_position_in_threadgroup]],
  2707. uint3 ntg[[threads_per_threadgroup]]) {
  2708. const int64_t i03 = tgpig[2];
  2709. const int64_t i02 = tgpig[1];
  2710. const int64_t i01 = tgpig[0];
  2711. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2712. const int64_t i3 = n / (ne2*ne1*ne0);
  2713. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2714. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2715. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK5_1;
  2716. device block_q5_1 * dst_data = (device block_q5_1 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2717. for (int64_t i00 = tpitg.x*QK5_1; i00 < ne00; i00 += ntg.x*QK5_1) {
  2718. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2719. float max = src[0];
  2720. float min = src[0];
  2721. for (int j = 1; j < QK5_1; j++) {
  2722. const float v = src[j];
  2723. min = v < min ? v : min;
  2724. max = v > max ? v : max;
  2725. }
  2726. const float d = (max - min) / 31;
  2727. const float id = d ? 1.0f/d : 0.0f;
  2728. dst_data[i00/QK5_1].d = d;
  2729. dst_data[i00/QK5_1].m = min;
  2730. uint32_t qh = 0;
  2731. for (int j = 0; j < QK5_1/2; ++j) {
  2732. const float x0 = (src[0 + j] - min)*id;
  2733. const float x1 = (src[QK5_1/2 + j] - min)*id;
  2734. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  2735. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  2736. dst_data[i00/QK5_1].qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
  2737. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2738. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
  2739. }
  2740. thread const uint8_t * qh8 = (thread const uint8_t *)&qh;
  2741. for (int j = 0; j < 4; ++j) {
  2742. dst_data[i00/QK5_1].qh[j] = qh8[j];
  2743. }
  2744. }
  2745. }
  2746. static inline int best_index_int8(int n, constant float * val, float x) {
  2747. if (x <= val[0]) return 0;
  2748. if (x >= val[n-1]) return n-1;
  2749. int ml = 0, mu = n-1;
  2750. while (mu-ml > 1) {
  2751. int mav = (ml+mu)/2;
  2752. if (x < val[mav]) mu = mav; else ml = mav;
  2753. }
  2754. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  2755. }
  2756. constexpr constant static float kvalues_iq4nl_f[16] = {
  2757. -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
  2758. };
  2759. kernel void kernel_cpy_f32_iq4_nl(
  2760. device const float * src0,
  2761. device void * dst,
  2762. constant int64_t & ne00,
  2763. constant int64_t & ne01,
  2764. constant int64_t & ne02,
  2765. constant int64_t & ne03,
  2766. constant uint64_t & nb00,
  2767. constant uint64_t & nb01,
  2768. constant uint64_t & nb02,
  2769. constant uint64_t & nb03,
  2770. constant int64_t & ne0,
  2771. constant int64_t & ne1,
  2772. constant int64_t & ne2,
  2773. constant int64_t & ne3,
  2774. constant uint64_t & nb0,
  2775. constant uint64_t & nb1,
  2776. constant uint64_t & nb2,
  2777. constant uint64_t & nb3,
  2778. uint3 tgpig[[threadgroup_position_in_grid]],
  2779. uint3 tpitg[[thread_position_in_threadgroup]],
  2780. uint3 ntg[[threads_per_threadgroup]]) {
  2781. const int64_t i03 = tgpig[2];
  2782. const int64_t i02 = tgpig[1];
  2783. const int64_t i01 = tgpig[0];
  2784. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2785. const int64_t i3 = n / (ne2*ne1*ne0);
  2786. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2787. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2788. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_NL;
  2789. device block_iq4_nl * dst_data = (device block_iq4_nl *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2790. for (int64_t i00 = tpitg.x*QK4_NL; i00 < ne00; i00 += ntg.x*QK4_NL) {
  2791. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2792. float amax = 0.0f; // absolute max
  2793. float max = 0.0f;
  2794. for (int j = 0; j < QK4_0; j++) {
  2795. const float v = src[j];
  2796. if (amax < fabs(v)) {
  2797. amax = fabs(v);
  2798. max = v;
  2799. }
  2800. }
  2801. const float d = max / kvalues_iq4nl_f[0];
  2802. const float id = d ? 1.0f/d : 0.0f;
  2803. float sumqx = 0, sumq2 = 0;
  2804. for (int j = 0; j < QK4_NL/2; ++j) {
  2805. const float x0 = src[0 + j]*id;
  2806. const float x1 = src[QK4_NL/2 + j]*id;
  2807. const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl_f, x0);
  2808. const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl_f, x1);
  2809. dst_data[i00/QK4_NL].qs[j] = xi0 | (xi1 << 4);
  2810. const float v0 = kvalues_iq4nl_f[xi0];
  2811. const float v1 = kvalues_iq4nl_f[xi1];
  2812. const float w0 = src[0 + j]*src[0 + j];
  2813. const float w1 = src[QK4_NL/2 + j]*src[QK4_NL/2 + j];
  2814. sumqx += w0*v0*src[j] + w1*v1*src[QK4_NL/2 + j];
  2815. sumq2 += w0*v0*v0 + w1*v1*v1;
  2816. }
  2817. dst_data[i00/QK4_NL].d = sumq2 > 0 ? sumqx/sumq2 : d;
  2818. }
  2819. }
  2820. kernel void kernel_concat(
  2821. device const char * src0,
  2822. device const char * src1,
  2823. device char * dst,
  2824. constant int64_t & ne00,
  2825. constant int64_t & ne01,
  2826. constant int64_t & ne02,
  2827. constant int64_t & ne03,
  2828. constant uint64_t & nb00,
  2829. constant uint64_t & nb01,
  2830. constant uint64_t & nb02,
  2831. constant uint64_t & nb03,
  2832. constant int64_t & ne10,
  2833. constant int64_t & ne11,
  2834. constant int64_t & ne12,
  2835. constant int64_t & ne13,
  2836. constant uint64_t & nb10,
  2837. constant uint64_t & nb11,
  2838. constant uint64_t & nb12,
  2839. constant uint64_t & nb13,
  2840. constant int64_t & ne0,
  2841. constant int64_t & ne1,
  2842. constant int64_t & ne2,
  2843. constant int64_t & ne3,
  2844. constant uint64_t & nb0,
  2845. constant uint64_t & nb1,
  2846. constant uint64_t & nb2,
  2847. constant uint64_t & nb3,
  2848. constant int32_t & dim,
  2849. uint3 tgpig[[threadgroup_position_in_grid]],
  2850. uint3 tpitg[[thread_position_in_threadgroup]],
  2851. uint3 ntg[[threads_per_threadgroup]]) {
  2852. const int64_t i3 = tgpig.z;
  2853. const int64_t i2 = tgpig.y;
  2854. const int64_t i1 = tgpig.x;
  2855. int64_t o[4] = {0, 0, 0, 0};
  2856. o[dim] = dim == 0 ? ne00 : (dim == 1 ? ne01 : (dim == 2 ? ne02 : ne03));
  2857. device const float * x;
  2858. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2859. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  2860. x = (device const float *)(src0 + (i3 )*nb03 + (i2 )*nb02 + (i1 )*nb01 + (i0 )*nb00);
  2861. } else {
  2862. x = (device const float *)(src1 + (i3 - o[3])*nb13 + (i2 - o[2])*nb12 + (i1 - o[1])*nb11 + (i0 - o[0])*nb10);
  2863. }
  2864. device float * y = (device float *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2865. *y = *x;
  2866. }
  2867. }
  2868. void kernel_mul_mv_q2_K_f32_impl(
  2869. device const void * src0,
  2870. device const float * src1,
  2871. device float * dst,
  2872. int64_t ne00,
  2873. int64_t ne01,
  2874. int64_t ne02,
  2875. int64_t ne10,
  2876. int64_t ne12,
  2877. int64_t ne0,
  2878. int64_t ne1,
  2879. uint r2,
  2880. uint r3,
  2881. threadgroup int8_t * shared_values,
  2882. uint3 tgpig,
  2883. uint tiisg,
  2884. uint sgitg) {
  2885. const int nb = ne00/QK_K;
  2886. const int r0 = tgpig.x;
  2887. const int r1 = tgpig.y;
  2888. const int im = tgpig.z;
  2889. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2890. const int ib_row = first_row * nb;
  2891. const uint i12 = im%ne12;
  2892. const uint i13 = im/ne12;
  2893. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2894. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  2895. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2896. float yl[32];
  2897. float sumf[N_DST]={0.f}, all_sum;
  2898. const int step = sizeof(block_q2_K) * nb;
  2899. const int ix = tiisg/8; // 0...3
  2900. const int it = tiisg%8; // 0...7
  2901. const int iq = it/4; // 0 or 1
  2902. const int ir = it%4; // 0...3
  2903. const int is = (8*ir)/16;// 0 or 1
  2904. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  2905. for (int ib = ix; ib < nb; ib += 4) {
  2906. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2907. for (int i = 0; i < 8; ++i) {
  2908. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2909. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  2910. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  2911. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  2912. }
  2913. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  2914. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2915. device const half * dh = &x[ib].d;
  2916. for (int row = 0; row < N_DST; row++) {
  2917. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2918. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2919. for (int i = 0; i < 8; i += 2) {
  2920. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2921. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2922. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2923. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2924. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2925. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2926. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2927. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2928. }
  2929. float dall = dh[0];
  2930. float dmin = dh[1] * 1.f/16.f;
  2931. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2932. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  2933. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  2934. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  2935. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  2936. qs += step/2;
  2937. sc += step;
  2938. dh += step/2;
  2939. }
  2940. y4 += 4 * QK_K;
  2941. }
  2942. for (int row = 0; row < N_DST; ++row) {
  2943. all_sum = simd_sum(sumf[row]);
  2944. if (tiisg == 0) {
  2945. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2946. }
  2947. }
  2948. }
  2949. [[host_name("kernel_mul_mv_q2_K_f32")]]
  2950. kernel void kernel_mul_mv_q2_K_f32(
  2951. device const void * src0,
  2952. device const float * src1,
  2953. device float * dst,
  2954. constant int64_t & ne00,
  2955. constant int64_t & ne01,
  2956. constant int64_t & ne02,
  2957. constant uint64_t & nb00,
  2958. constant uint64_t & nb01,
  2959. constant uint64_t & nb02,
  2960. constant int64_t & ne10,
  2961. constant int64_t & ne11,
  2962. constant int64_t & ne12,
  2963. constant uint64_t & nb10,
  2964. constant uint64_t & nb11,
  2965. constant uint64_t & nb12,
  2966. constant int64_t & ne0,
  2967. constant int64_t & ne1,
  2968. constant uint & r2,
  2969. constant uint & r3,
  2970. uint3 tgpig[[threadgroup_position_in_grid]],
  2971. uint tiisg[[thread_index_in_simdgroup]],
  2972. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2973. kernel_mul_mv_q2_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  2974. }
  2975. void kernel_mul_mv_q3_K_f32_impl(
  2976. device const void * src0,
  2977. device const float * src1,
  2978. device float * dst,
  2979. int64_t ne00,
  2980. int64_t ne01,
  2981. int64_t ne02,
  2982. int64_t ne10,
  2983. int64_t ne12,
  2984. int64_t ne0,
  2985. int64_t ne1,
  2986. uint r2,
  2987. uint r3,
  2988. threadgroup int8_t * shared_values,
  2989. uint3 tgpig,
  2990. uint tiisg,
  2991. uint sgitg) {
  2992. const int nb = ne00/QK_K;
  2993. const int64_t r0 = tgpig.x;
  2994. const int64_t r1 = tgpig.y;
  2995. const int64_t im = tgpig.z;
  2996. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2997. const uint i12 = im%ne12;
  2998. const uint i13 = im/ne12;
  2999. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3000. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  3001. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3002. float yl[32];
  3003. //const uint16_t kmask1 = 0x3030;
  3004. //const uint16_t kmask2 = 0x0f0f;
  3005. const int tid = tiisg/4;
  3006. const int ix = tiisg%4;
  3007. const int ip = tid/4; // 0 or 1
  3008. const int il = 2*((tid%4)/2); // 0 or 2
  3009. const int ir = tid%2;
  3010. const int n = 8;
  3011. const int l0 = n*ir;
  3012. // One would think that the Metal compiler would figure out that ip and il can only have
  3013. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  3014. // with these two tales.
  3015. //
  3016. // Possible masks for the high bit
  3017. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  3018. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  3019. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  3020. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  3021. // Possible masks for the low 2 bits
  3022. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  3023. const ushort4 hm = mm[2*ip + il/2];
  3024. const int shift = 2*il;
  3025. const float v1 = il == 0 ? 4.f : 64.f;
  3026. const float v2 = 4.f * v1;
  3027. const uint16_t s_shift1 = 4*ip;
  3028. const uint16_t s_shift2 = s_shift1 + il;
  3029. const int q_offset = 32*ip + l0;
  3030. const int y_offset = 128*ip + 32*il + l0;
  3031. const int step = sizeof(block_q3_K) * nb / 2;
  3032. device const float * y1 = yy + ix*QK_K + y_offset;
  3033. uint32_t scales32, aux32;
  3034. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  3035. thread const int8_t * scales = (thread const int8_t *)&scales32;
  3036. float sumf1[2] = {0.f};
  3037. float sumf2[2] = {0.f};
  3038. for (int i = ix; i < nb; i += 4) {
  3039. for (int l = 0; l < 8; ++l) {
  3040. yl[l+ 0] = y1[l+ 0];
  3041. yl[l+ 8] = y1[l+16];
  3042. yl[l+16] = y1[l+32];
  3043. yl[l+24] = y1[l+48];
  3044. }
  3045. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  3046. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  3047. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  3048. device const half * dh = &x[i].d;
  3049. for (int row = 0; row < 2; ++row) {
  3050. const float d_all = (float)dh[0];
  3051. scales16[0] = a[4];
  3052. scales16[1] = a[5];
  3053. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  3054. scales16[0] = a[il+0];
  3055. scales16[1] = a[il+1];
  3056. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  3057. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  3058. for (int l = 0; l < n; l += 2) {
  3059. const int32_t qs = q[l/2];
  3060. s1 += yl[l+0] * (qs & qm[il/2][0]);
  3061. s2 += yl[l+1] * (qs & qm[il/2][1]);
  3062. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  3063. s4 += yl[l+16] * (qs & qm[il/2][2]);
  3064. s5 += yl[l+17] * (qs & qm[il/2][3]);
  3065. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  3066. }
  3067. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  3068. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  3069. sumf1[row] += d1 * (scales[0] - 32);
  3070. sumf2[row] += d2 * (scales[2] - 32);
  3071. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  3072. for (int l = 0; l < n; l += 2) {
  3073. const int32_t qs = q[l/2+8];
  3074. s1 += yl[l+8] * (qs & qm[il/2][0]);
  3075. s2 += yl[l+9] * (qs & qm[il/2][1]);
  3076. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  3077. s4 += yl[l+24] * (qs & qm[il/2][2]);
  3078. s5 += yl[l+25] * (qs & qm[il/2][3]);
  3079. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  3080. }
  3081. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  3082. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  3083. sumf1[row] += d1 * (scales[1] - 32);
  3084. sumf2[row] += d2 * (scales[3] - 32);
  3085. q += step;
  3086. h += step;
  3087. a += step;
  3088. dh += step;
  3089. }
  3090. y1 += 4 * QK_K;
  3091. }
  3092. for (int row = 0; row < 2; ++row) {
  3093. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  3094. sumf1[row] = simd_sum(sumf);
  3095. }
  3096. if (tiisg == 0) {
  3097. for (int row = 0; row < 2; ++row) {
  3098. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = sumf1[row];
  3099. }
  3100. }
  3101. }
  3102. [[host_name("kernel_mul_mv_q3_K_f32")]]
  3103. kernel void kernel_mul_mv_q3_K_f32(
  3104. device const void * src0,
  3105. device const float * src1,
  3106. device float * dst,
  3107. constant int64_t & ne00,
  3108. constant int64_t & ne01,
  3109. constant int64_t & ne02,
  3110. constant uint64_t & nb00,
  3111. constant uint64_t & nb01,
  3112. constant uint64_t & nb02,
  3113. constant int64_t & ne10,
  3114. constant int64_t & ne11,
  3115. constant int64_t & ne12,
  3116. constant uint64_t & nb10,
  3117. constant uint64_t & nb11,
  3118. constant uint64_t & nb12,
  3119. constant int64_t & ne0,
  3120. constant int64_t & ne1,
  3121. constant uint & r2,
  3122. constant uint & r3,
  3123. uint3 tgpig[[threadgroup_position_in_grid]],
  3124. uint tiisg[[thread_index_in_simdgroup]],
  3125. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3126. kernel_mul_mv_q3_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  3127. }
  3128. void kernel_mul_mv_q4_K_f32_impl(
  3129. device const void * src0,
  3130. device const float * src1,
  3131. device float * dst,
  3132. int64_t ne00,
  3133. int64_t ne01,
  3134. int64_t ne02,
  3135. int64_t ne10,
  3136. int64_t ne12,
  3137. int64_t ne0,
  3138. int64_t ne1,
  3139. uint r2,
  3140. uint r3,
  3141. threadgroup int8_t * shared_values,
  3142. uint3 tgpig,
  3143. uint tiisg,
  3144. uint sgitg) {
  3145. const uint16_t kmask1 = 0x3f3f;
  3146. const uint16_t kmask2 = 0x0f0f;
  3147. const uint16_t kmask3 = 0xc0c0;
  3148. const int ix = tiisg/8; // 0...3
  3149. const int it = tiisg%8; // 0...7
  3150. const int iq = it/4; // 0 or 1
  3151. const int ir = it%4; // 0...3
  3152. const int nb = ne00/QK_K;
  3153. const int r0 = tgpig.x;
  3154. const int r1 = tgpig.y;
  3155. const int im = tgpig.z;
  3156. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3157. const int first_row = r0 * N_DST;
  3158. const int ib_row = first_row * nb;
  3159. const uint i12 = im%ne12;
  3160. const uint i13 = im/ne12;
  3161. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3162. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  3163. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3164. float yl[16];
  3165. float yh[16];
  3166. float sumf[N_DST]={0.f}, all_sum;
  3167. const int step = sizeof(block_q4_K) * nb / 2;
  3168. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  3169. uint16_t sc16[4];
  3170. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  3171. for (int ib = ix; ib < nb; ib += 4) {
  3172. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  3173. for (int i = 0; i < 8; ++i) {
  3174. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  3175. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  3176. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  3177. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  3178. }
  3179. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  3180. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  3181. device const half * dh = &x[ib].d;
  3182. for (int row = 0; row < N_DST; row++) {
  3183. sc16[0] = sc[0] & kmask1;
  3184. sc16[1] = sc[2] & kmask1;
  3185. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  3186. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  3187. device const uint16_t * q2 = q1 + 32;
  3188. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  3189. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  3190. for (int i = 0; i < 8; i += 2) {
  3191. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  3192. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  3193. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  3194. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  3195. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  3196. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  3197. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  3198. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  3199. }
  3200. float dall = dh[0];
  3201. float dmin = dh[1];
  3202. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  3203. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  3204. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  3205. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  3206. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  3207. q1 += step;
  3208. sc += step;
  3209. dh += step;
  3210. }
  3211. y4 += 4 * QK_K;
  3212. }
  3213. for (int row = 0; row < N_DST; ++row) {
  3214. all_sum = simd_sum(sumf[row]);
  3215. if (tiisg == 0) {
  3216. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  3217. }
  3218. }
  3219. }
  3220. [[host_name("kernel_mul_mv_q4_K_f32")]]
  3221. kernel void kernel_mul_mv_q4_K_f32(
  3222. device const void * src0,
  3223. device const float * src1,
  3224. device float * dst,
  3225. constant int64_t & ne00,
  3226. constant int64_t & ne01,
  3227. constant int64_t & ne02,
  3228. constant uint64_t & nb00,
  3229. constant uint64_t & nb01,
  3230. constant uint64_t & nb02,
  3231. constant int64_t & ne10,
  3232. constant int64_t & ne11,
  3233. constant int64_t & ne12,
  3234. constant uint64_t & nb10,
  3235. constant uint64_t & nb11,
  3236. constant uint64_t & nb12,
  3237. constant int64_t & ne0,
  3238. constant int64_t & ne1,
  3239. constant uint & r2,
  3240. constant uint & r3,
  3241. uint3 tgpig[[threadgroup_position_in_grid]],
  3242. uint tiisg[[thread_index_in_simdgroup]],
  3243. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3244. kernel_mul_mv_q4_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  3245. }
  3246. void kernel_mul_mv_q5_K_f32_impl(
  3247. device const void * src0,
  3248. device const float * src1,
  3249. device float * dst,
  3250. int64_t ne00,
  3251. int64_t ne01,
  3252. int64_t ne02,
  3253. int64_t ne10,
  3254. int64_t ne12,
  3255. int64_t ne0,
  3256. int64_t ne1,
  3257. uint r2,
  3258. uint r3,
  3259. threadgroup int8_t * shared_values,
  3260. uint3 tgpig,
  3261. uint tiisg,
  3262. uint sgitg) {
  3263. const int nb = ne00/QK_K;
  3264. const int64_t r0 = tgpig.x;
  3265. const int64_t r1 = tgpig.y;
  3266. const int im = tgpig.z;
  3267. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  3268. const uint i12 = im%ne12;
  3269. const uint i13 = im/ne12;
  3270. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3271. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  3272. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3273. float sumf[2]={0.f};
  3274. const int step = sizeof(block_q5_K) * nb;
  3275. float yl[16], yh[16];
  3276. const uint16_t kmask1 = 0x3f3f;
  3277. const uint16_t kmask2 = 0x0f0f;
  3278. const uint16_t kmask3 = 0xc0c0;
  3279. const int tid = tiisg/4;
  3280. const int ix = tiisg%4;
  3281. const int iq = tid/4;
  3282. const int ir = tid%4;
  3283. const int n = 8;
  3284. const int l0 = n*ir;
  3285. const int q_offset = 32*iq + l0;
  3286. const int y_offset = 64*iq + l0;
  3287. const uint8_t hm1 = 1u << (2*iq);
  3288. const uint8_t hm2 = hm1 << 1;
  3289. const uint8_t hm3 = hm1 << 4;
  3290. const uint8_t hm4 = hm2 << 4;
  3291. uint16_t sc16[4];
  3292. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  3293. device const float * y1 = yy + ix*QK_K + y_offset;
  3294. for (int i = ix; i < nb; i += 4) {
  3295. device const uint8_t * q1 = x[i].qs + q_offset;
  3296. device const uint8_t * qh = x[i].qh + l0;
  3297. device const half * dh = &x[i].d;
  3298. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  3299. device const float * y2 = y1 + 128;
  3300. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  3301. for (int l = 0; l < 8; ++l) {
  3302. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  3303. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  3304. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  3305. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  3306. }
  3307. for (int row = 0; row < 2; ++row) {
  3308. device const uint8_t * q2 = q1 + 64;
  3309. sc16[0] = a[0] & kmask1;
  3310. sc16[1] = a[2] & kmask1;
  3311. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  3312. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  3313. float4 acc1 = {0.f};
  3314. float4 acc2 = {0.f};
  3315. for (int l = 0; l < n; ++l) {
  3316. uint8_t h = qh[l];
  3317. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  3318. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  3319. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  3320. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  3321. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  3322. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  3323. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  3324. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  3325. }
  3326. const float dall = dh[0];
  3327. const float dmin = dh[1];
  3328. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  3329. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  3330. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  3331. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  3332. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  3333. q1 += step;
  3334. qh += step;
  3335. dh += step/2;
  3336. a += step/2;
  3337. }
  3338. y1 += 4 * QK_K;
  3339. }
  3340. for (int row = 0; row < 2; ++row) {
  3341. const float tot = simd_sum(sumf[row]);
  3342. if (tiisg == 0) {
  3343. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  3344. }
  3345. }
  3346. }
  3347. [[host_name("kernel_mul_mv_q5_K_f32")]]
  3348. kernel void kernel_mul_mv_q5_K_f32(
  3349. device const void * src0,
  3350. device const float * src1,
  3351. device float * dst,
  3352. constant int64_t & ne00,
  3353. constant int64_t & ne01,
  3354. constant int64_t & ne02,
  3355. constant uint64_t & nb00,
  3356. constant uint64_t & nb01,
  3357. constant uint64_t & nb02,
  3358. constant int64_t & ne10,
  3359. constant int64_t & ne11,
  3360. constant int64_t & ne12,
  3361. constant uint64_t & nb10,
  3362. constant uint64_t & nb11,
  3363. constant uint64_t & nb12,
  3364. constant int64_t & ne0,
  3365. constant int64_t & ne1,
  3366. constant uint & r2,
  3367. constant uint & r3,
  3368. uint3 tgpig[[threadgroup_position_in_grid]],
  3369. uint tiisg[[thread_index_in_simdgroup]],
  3370. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3371. kernel_mul_mv_q5_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  3372. }
  3373. void kernel_mul_mv_q6_K_f32_impl(
  3374. device const void * src0,
  3375. device const float * src1,
  3376. device float * dst,
  3377. int64_t ne00,
  3378. int64_t ne01,
  3379. int64_t ne02,
  3380. int64_t ne10,
  3381. int64_t ne12,
  3382. int64_t ne0,
  3383. int64_t ne1,
  3384. uint r2,
  3385. uint r3,
  3386. threadgroup int8_t * shared_values,
  3387. uint3 tgpig,
  3388. uint tiisg,
  3389. uint sgitg) {
  3390. const uint8_t kmask1 = 0x03;
  3391. const uint8_t kmask2 = 0x0C;
  3392. const uint8_t kmask3 = 0x30;
  3393. const uint8_t kmask4 = 0xC0;
  3394. const int nb = ne00/QK_K;
  3395. const int64_t r0 = tgpig.x;
  3396. const int64_t r1 = tgpig.y;
  3397. const int im = tgpig.z;
  3398. const int row = 2 * r0 + sgitg;
  3399. const uint i12 = im%ne12;
  3400. const uint i13 = im/ne12;
  3401. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3402. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  3403. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3404. float sumf = 0;
  3405. const int tid = tiisg/2;
  3406. const int ix = tiisg%2;
  3407. const int ip = tid/8; // 0 or 1
  3408. const int il = tid%8;
  3409. const int n = 4;
  3410. const int l0 = n*il;
  3411. const int is = 8*ip + l0/16;
  3412. const int y_offset = 128*ip + l0;
  3413. const int q_offset_l = 64*ip + l0;
  3414. const int q_offset_h = 32*ip + l0;
  3415. for (int i = ix; i < nb; i += 2) {
  3416. device const uint8_t * q1 = x[i].ql + q_offset_l;
  3417. device const uint8_t * q2 = q1 + 32;
  3418. device const uint8_t * qh = x[i].qh + q_offset_h;
  3419. device const int8_t * sc = x[i].scales + is;
  3420. device const float * y = yy + i * QK_K + y_offset;
  3421. const float dall = x[i].d;
  3422. float4 sums = {0.f, 0.f, 0.f, 0.f};
  3423. for (int l = 0; l < n; ++l) {
  3424. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  3425. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  3426. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  3427. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  3428. }
  3429. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  3430. }
  3431. const float tot = simd_sum(sumf);
  3432. if (tiisg == 0) {
  3433. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  3434. }
  3435. }
  3436. [[host_name("kernel_mul_mv_q6_K_f32")]]
  3437. kernel void kernel_mul_mv_q6_K_f32(
  3438. device const void * src0,
  3439. device const float * src1,
  3440. device float * dst,
  3441. constant int64_t & ne00,
  3442. constant int64_t & ne01,
  3443. constant int64_t & ne02,
  3444. constant uint64_t & nb00,
  3445. constant uint64_t & nb01,
  3446. constant uint64_t & nb02,
  3447. constant int64_t & ne10,
  3448. constant int64_t & ne11,
  3449. constant int64_t & ne12,
  3450. constant uint64_t & nb10,
  3451. constant uint64_t & nb11,
  3452. constant uint64_t & nb12,
  3453. constant int64_t & ne0,
  3454. constant int64_t & ne1,
  3455. constant uint & r2,
  3456. constant uint & r3,
  3457. uint3 tgpig[[threadgroup_position_in_grid]],
  3458. uint tiisg[[thread_index_in_simdgroup]],
  3459. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3460. kernel_mul_mv_q6_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  3461. }
  3462. // ======================= "True" 2-bit
  3463. void kernel_mul_mv_iq2_xxs_f32_impl(
  3464. device const void * src0,
  3465. device const float * src1,
  3466. device float * dst,
  3467. int64_t ne00,
  3468. int64_t ne01,
  3469. int64_t ne02,
  3470. int64_t ne10,
  3471. int64_t ne12,
  3472. int64_t ne0,
  3473. int64_t ne1,
  3474. uint r2,
  3475. uint r3,
  3476. threadgroup int8_t * shared_values,
  3477. uint3 tgpig,
  3478. uint tiisg,
  3479. uint sgitg) {
  3480. const int nb = ne00/QK_K;
  3481. const int r0 = tgpig.x;
  3482. const int r1 = tgpig.y;
  3483. const int im = tgpig.z;
  3484. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3485. const int ib_row = first_row * nb;
  3486. const uint i12 = im%ne12;
  3487. const uint i13 = im/ne12;
  3488. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3489. device const block_iq2_xxs * x = (device const block_iq2_xxs *) src0 + ib_row + offset0;
  3490. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3491. float yl[32];
  3492. float sumf[N_DST]={0.f}, all_sum;
  3493. const int nb32 = nb * (QK_K / 32);
  3494. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3495. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3496. {
  3497. int nval = 4;
  3498. int pos = (32*sgitg + tiisg)*nval;
  3499. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xxs_grid[pos + i];
  3500. nval = 2;
  3501. pos = (32*sgitg + tiisg)*nval;
  3502. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3503. threadgroup_barrier(mem_flags::mem_threadgroup);
  3504. }
  3505. const int ix = tiisg;
  3506. device const float * y4 = y + 32 * ix;
  3507. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3508. for (int i = 0; i < 32; ++i) {
  3509. yl[i] = y4[i];
  3510. }
  3511. const int ibl = ib32 / (QK_K / 32);
  3512. const int ib = ib32 % (QK_K / 32);
  3513. device const block_iq2_xxs * xr = x + ibl;
  3514. device const uint16_t * q2 = xr->qs + 4 * ib;
  3515. device const half * dh = &xr->d;
  3516. for (int row = 0; row < N_DST; row++) {
  3517. const float db = dh[0];
  3518. device const uint8_t * aux8 = (device const uint8_t *)q2;
  3519. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3520. const float d = db * (0.5f + (aux32 >> 28));
  3521. float sum = 0;
  3522. for (int l = 0; l < 4; ++l) {
  3523. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + aux8[l]);
  3524. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  3525. for (int j = 0; j < 8; ++j) {
  3526. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3527. }
  3528. }
  3529. sumf[row] += d * sum;
  3530. dh += nb*sizeof(block_iq2_xxs)/2;
  3531. q2 += nb*sizeof(block_iq2_xxs)/2;
  3532. }
  3533. y4 += 32 * 32;
  3534. }
  3535. for (int row = 0; row < N_DST; ++row) {
  3536. all_sum = simd_sum(sumf[row]);
  3537. if (tiisg == 0) {
  3538. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3539. }
  3540. }
  3541. }
  3542. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  3543. kernel void kernel_mul_mv_iq2_xxs_f32(
  3544. device const void * src0,
  3545. device const float * src1,
  3546. device float * dst,
  3547. constant int64_t & ne00,
  3548. constant int64_t & ne01,
  3549. constant int64_t & ne02,
  3550. constant uint64_t & nb00,
  3551. constant uint64_t & nb01,
  3552. constant uint64_t & nb02,
  3553. constant int64_t & ne10,
  3554. constant int64_t & ne11,
  3555. constant int64_t & ne12,
  3556. constant uint64_t & nb10,
  3557. constant uint64_t & nb11,
  3558. constant uint64_t & nb12,
  3559. constant int64_t & ne0,
  3560. constant int64_t & ne1,
  3561. constant uint & r2,
  3562. constant uint & r3,
  3563. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3564. uint3 tgpig[[threadgroup_position_in_grid]],
  3565. uint tiisg[[thread_index_in_simdgroup]],
  3566. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3567. kernel_mul_mv_iq2_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3568. }
  3569. void kernel_mul_mv_iq2_xs_f32_impl(
  3570. device const void * src0,
  3571. device const float * src1,
  3572. device float * dst,
  3573. int64_t ne00,
  3574. int64_t ne01,
  3575. int64_t ne02,
  3576. int64_t ne10,
  3577. int64_t ne12,
  3578. int64_t ne0,
  3579. int64_t ne1,
  3580. uint r2,
  3581. uint r3,
  3582. threadgroup int8_t * shared_values,
  3583. uint3 tgpig,
  3584. uint tiisg,
  3585. uint sgitg) {
  3586. const int nb = ne00/QK_K;
  3587. const int r0 = tgpig.x;
  3588. const int r1 = tgpig.y;
  3589. const int im = tgpig.z;
  3590. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3591. const int ib_row = first_row * nb;
  3592. const uint i12 = im%ne12;
  3593. const uint i13 = im/ne12;
  3594. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3595. device const block_iq2_xs * x = (device const block_iq2_xs *) src0 + ib_row + offset0;
  3596. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3597. float yl[32];
  3598. float sumf[N_DST]={0.f}, all_sum;
  3599. const int nb32 = nb * (QK_K / 32);
  3600. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3601. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 512);
  3602. {
  3603. int nval = 8;
  3604. int pos = (32*sgitg + tiisg)*nval;
  3605. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xs_grid[pos + i];
  3606. nval = 2;
  3607. pos = (32*sgitg + tiisg)*nval;
  3608. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3609. threadgroup_barrier(mem_flags::mem_threadgroup);
  3610. }
  3611. const int ix = tiisg;
  3612. device const float * y4 = y + 32 * ix;
  3613. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3614. for (int i = 0; i < 32; ++i) {
  3615. yl[i] = y4[i];
  3616. }
  3617. const int ibl = ib32 / (QK_K / 32);
  3618. const int ib = ib32 % (QK_K / 32);
  3619. device const block_iq2_xs * xr = x + ibl;
  3620. device const uint16_t * q2 = xr->qs + 4 * ib;
  3621. device const uint8_t * sc = xr->scales + ib;
  3622. device const half * dh = &xr->d;
  3623. for (int row = 0; row < N_DST; row++) {
  3624. const float db = dh[0];
  3625. const uint8_t ls1 = sc[0] & 0xf;
  3626. const uint8_t ls2 = sc[0] >> 4;
  3627. const float d1 = db * (0.5f + ls1);
  3628. const float d2 = db * (0.5f + ls2);
  3629. float sum1 = 0, sum2 = 0;
  3630. for (int l = 0; l < 2; ++l) {
  3631. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3632. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3633. for (int j = 0; j < 8; ++j) {
  3634. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3635. }
  3636. }
  3637. for (int l = 2; l < 4; ++l) {
  3638. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3639. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3640. for (int j = 0; j < 8; ++j) {
  3641. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3642. }
  3643. }
  3644. sumf[row] += d1 * sum1 + d2 * sum2;
  3645. dh += nb*sizeof(block_iq2_xs)/2;
  3646. q2 += nb*sizeof(block_iq2_xs)/2;
  3647. sc += nb*sizeof(block_iq2_xs);
  3648. }
  3649. y4 += 32 * 32;
  3650. }
  3651. for (int row = 0; row < N_DST; ++row) {
  3652. all_sum = simd_sum(sumf[row]);
  3653. if (tiisg == 0) {
  3654. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3655. }
  3656. }
  3657. }
  3658. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  3659. kernel void kernel_mul_mv_iq2_xs_f32(
  3660. device const void * src0,
  3661. device const float * src1,
  3662. device float * dst,
  3663. constant int64_t & ne00,
  3664. constant int64_t & ne01,
  3665. constant int64_t & ne02,
  3666. constant uint64_t & nb00,
  3667. constant uint64_t & nb01,
  3668. constant uint64_t & nb02,
  3669. constant int64_t & ne10,
  3670. constant int64_t & ne11,
  3671. constant int64_t & ne12,
  3672. constant uint64_t & nb10,
  3673. constant uint64_t & nb11,
  3674. constant uint64_t & nb12,
  3675. constant int64_t & ne0,
  3676. constant int64_t & ne1,
  3677. constant uint & r2,
  3678. constant uint & r3,
  3679. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3680. uint3 tgpig[[threadgroup_position_in_grid]],
  3681. uint tiisg[[thread_index_in_simdgroup]],
  3682. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3683. kernel_mul_mv_iq2_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3684. }
  3685. void kernel_mul_mv_iq3_xxs_f32_impl(
  3686. device const void * src0,
  3687. device const float * src1,
  3688. device float * dst,
  3689. int64_t ne00,
  3690. int64_t ne01,
  3691. int64_t ne02,
  3692. int64_t ne10,
  3693. int64_t ne12,
  3694. int64_t ne0,
  3695. int64_t ne1,
  3696. uint r2,
  3697. uint r3,
  3698. threadgroup int8_t * shared_values,
  3699. uint3 tgpig,
  3700. uint tiisg,
  3701. uint sgitg) {
  3702. const int nb = ne00/QK_K;
  3703. const int r0 = tgpig.x;
  3704. const int r1 = tgpig.y;
  3705. const int im = tgpig.z;
  3706. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3707. const int ib_row = first_row * nb;
  3708. const uint i12 = im%ne12;
  3709. const uint i13 = im/ne12;
  3710. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3711. device const block_iq3_xxs * x = (device const block_iq3_xxs *) src0 + ib_row + offset0;
  3712. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3713. float yl[32];
  3714. float sumf[N_DST]={0.f}, all_sum;
  3715. const int nb32 = nb * (QK_K / 32);
  3716. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  3717. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3718. {
  3719. int nval = 4;
  3720. int pos = (32*sgitg + tiisg)*nval;
  3721. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xxs_grid[pos + i];
  3722. nval = 2;
  3723. pos = (32*sgitg + tiisg)*nval;
  3724. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3725. threadgroup_barrier(mem_flags::mem_threadgroup);
  3726. }
  3727. const int ix = tiisg;
  3728. device const float * y4 = y + 32 * ix;
  3729. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3730. for (int i = 0; i < 32; ++i) {
  3731. yl[i] = y4[i];
  3732. }
  3733. const int ibl = ib32 / (QK_K / 32);
  3734. const int ib = ib32 % (QK_K / 32);
  3735. device const block_iq3_xxs * xr = x + ibl;
  3736. device const uint8_t * q3 = xr->qs + 8 * ib;
  3737. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  3738. device const half * dh = &xr->d;
  3739. for (int row = 0; row < N_DST; row++) {
  3740. const float db = dh[0];
  3741. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  3742. const float d = db * (0.5f + (aux32 >> 28));
  3743. float2 sum = {0};
  3744. for (int l = 0; l < 4; ++l) {
  3745. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + q3[2*l+0]);
  3746. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + q3[2*l+1]);
  3747. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  3748. for (int j = 0; j < 4; ++j) {
  3749. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  3750. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  3751. }
  3752. }
  3753. sumf[row] += d * (sum[0] + sum[1]);
  3754. dh += nb*sizeof(block_iq3_xxs)/2;
  3755. q3 += nb*sizeof(block_iq3_xxs);
  3756. gas += nb*sizeof(block_iq3_xxs)/2;
  3757. }
  3758. y4 += 32 * 32;
  3759. }
  3760. for (int row = 0; row < N_DST; ++row) {
  3761. all_sum = simd_sum(sumf[row]);
  3762. if (tiisg == 0) {
  3763. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  3764. }
  3765. }
  3766. }
  3767. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  3768. kernel void kernel_mul_mv_iq3_xxs_f32(
  3769. device const void * src0,
  3770. device const float * src1,
  3771. device float * dst,
  3772. constant int64_t & ne00,
  3773. constant int64_t & ne01,
  3774. constant int64_t & ne02,
  3775. constant uint64_t & nb00,
  3776. constant uint64_t & nb01,
  3777. constant uint64_t & nb02,
  3778. constant int64_t & ne10,
  3779. constant int64_t & ne11,
  3780. constant int64_t & ne12,
  3781. constant uint64_t & nb10,
  3782. constant uint64_t & nb11,
  3783. constant uint64_t & nb12,
  3784. constant int64_t & ne0,
  3785. constant int64_t & ne1,
  3786. constant uint & r2,
  3787. constant uint & r3,
  3788. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3789. uint3 tgpig[[threadgroup_position_in_grid]],
  3790. uint tiisg[[thread_index_in_simdgroup]],
  3791. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3792. kernel_mul_mv_iq3_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3793. }
  3794. void kernel_mul_mv_iq3_s_f32_impl(
  3795. device const void * src0,
  3796. device const float * src1,
  3797. device float * dst,
  3798. int64_t ne00,
  3799. int64_t ne01,
  3800. int64_t ne02,
  3801. int64_t ne10,
  3802. int64_t ne12,
  3803. int64_t ne0,
  3804. int64_t ne1,
  3805. uint r2,
  3806. uint r3,
  3807. threadgroup int8_t * shared_values,
  3808. uint3 tgpig,
  3809. uint tiisg,
  3810. uint sgitg) {
  3811. const int nb = ne00/QK_K;
  3812. const int r0 = tgpig.x;
  3813. const int r1 = tgpig.y;
  3814. const int im = tgpig.z;
  3815. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3816. const int ib_row = first_row * nb;
  3817. const uint i12 = im%ne12;
  3818. const uint i13 = im/ne12;
  3819. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3820. device const block_iq3_s * x = (device const block_iq3_s *) src0 + ib_row + offset0;
  3821. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3822. float yl[32];
  3823. float sumf[N_DST]={0.f}, all_sum;
  3824. const int nb32 = nb * (QK_K / 32);
  3825. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  3826. {
  3827. int nval = 8;
  3828. int pos = (32*sgitg + tiisg)*nval;
  3829. for (int i = 0; i < nval; ++i) values[pos + i] = iq3s_grid[pos + i];
  3830. threadgroup_barrier(mem_flags::mem_threadgroup);
  3831. }
  3832. const int ix = tiisg;
  3833. device const float * y4 = y + 32 * ix;
  3834. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3835. for (int i = 0; i < 32; ++i) {
  3836. yl[i] = y4[i];
  3837. }
  3838. const int ibl = ib32 / (QK_K / 32);
  3839. const int ib = ib32 % (QK_K / 32);
  3840. device const block_iq3_s * xr = x + ibl;
  3841. device const uint8_t * qs = xr->qs + 8 * ib;
  3842. device const uint8_t * qh = xr->qh + ib;
  3843. device const uint8_t * sc = xr->scales + (ib/2);
  3844. device const uint8_t * signs = xr->signs + 4 * ib;
  3845. device const half * dh = &xr->d;
  3846. for (int row = 0; row < N_DST; row++) {
  3847. const float db = dh[0];
  3848. const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf));
  3849. float2 sum = {0};
  3850. for (int l = 0; l < 4; ++l) {
  3851. const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? values + 256 : values;
  3852. const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? values + 256 : values;
  3853. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]);
  3854. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]);
  3855. for (int j = 0; j < 4; ++j) {
  3856. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  3857. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  3858. }
  3859. }
  3860. sumf[row] += d * (sum[0] + sum[1]);
  3861. dh += nb*sizeof(block_iq3_s)/2;
  3862. qs += nb*sizeof(block_iq3_s);
  3863. qh += nb*sizeof(block_iq3_s);
  3864. sc += nb*sizeof(block_iq3_s);
  3865. signs += nb*sizeof(block_iq3_s);
  3866. }
  3867. y4 += 32 * 32;
  3868. }
  3869. for (int row = 0; row < N_DST; ++row) {
  3870. all_sum = simd_sum(sumf[row]);
  3871. if (tiisg == 0) {
  3872. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  3873. }
  3874. }
  3875. }
  3876. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  3877. kernel void kernel_mul_mv_iq3_s_f32(
  3878. device const void * src0,
  3879. device const float * src1,
  3880. device float * dst,
  3881. constant int64_t & ne00,
  3882. constant int64_t & ne01,
  3883. constant int64_t & ne02,
  3884. constant uint64_t & nb00,
  3885. constant uint64_t & nb01,
  3886. constant uint64_t & nb02,
  3887. constant int64_t & ne10,
  3888. constant int64_t & ne11,
  3889. constant int64_t & ne12,
  3890. constant uint64_t & nb10,
  3891. constant uint64_t & nb11,
  3892. constant uint64_t & nb12,
  3893. constant int64_t & ne0,
  3894. constant int64_t & ne1,
  3895. constant uint & r2,
  3896. constant uint & r3,
  3897. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3898. uint3 tgpig[[threadgroup_position_in_grid]],
  3899. uint tiisg[[thread_index_in_simdgroup]],
  3900. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3901. kernel_mul_mv_iq3_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3902. }
  3903. void kernel_mul_mv_iq2_s_f32_impl(
  3904. device const void * src0,
  3905. device const float * src1,
  3906. device float * dst,
  3907. int64_t ne00,
  3908. int64_t ne01,
  3909. int64_t ne02,
  3910. int64_t ne10,
  3911. int64_t ne12,
  3912. int64_t ne0,
  3913. int64_t ne1,
  3914. uint r2,
  3915. uint r3,
  3916. threadgroup int8_t * shared_values,
  3917. uint3 tgpig,
  3918. uint tiisg,
  3919. uint sgitg) {
  3920. const int nb = ne00/QK_K;
  3921. const int r0 = tgpig.x;
  3922. const int r1 = tgpig.y;
  3923. const int im = tgpig.z;
  3924. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3925. const int ib_row = first_row * nb;
  3926. const uint i12 = im%ne12;
  3927. const uint i13 = im/ne12;
  3928. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3929. device const block_iq2_s * x = (device const block_iq2_s *) src0 + ib_row + offset0;
  3930. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3931. float yl[32];
  3932. float sumf[N_DST]={0.f}, all_sum;
  3933. const int nb32 = nb * (QK_K / 32);
  3934. //threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3935. //{
  3936. // int nval = 32;
  3937. // int pos = (32*sgitg + tiisg)*nval;
  3938. // for (int i = 0; i < nval; ++i) values[pos + i] = iq2s_grid[pos + i];
  3939. // threadgroup_barrier(mem_flags::mem_threadgroup);
  3940. //}
  3941. const int ix = tiisg;
  3942. device const float * y4 = y + 32 * ix;
  3943. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3944. for (int i = 0; i < 32; ++i) {
  3945. yl[i] = y4[i];
  3946. }
  3947. const int ibl = ib32 / (QK_K / 32);
  3948. const int ib = ib32 % (QK_K / 32);
  3949. device const block_iq2_s * xr = x + ibl;
  3950. device const uint8_t * qs = xr->qs + 4 * ib;
  3951. device const uint8_t * qh = xr->qh + ib;
  3952. device const uint8_t * sc = xr->scales + ib;
  3953. device const uint8_t * signs = qs + QK_K/8;
  3954. device const half * dh = &xr->d;
  3955. for (int row = 0; row < N_DST; row++) {
  3956. const float db = dh[0];
  3957. const float d1 = db * (0.5f + (sc[0] & 0xf));
  3958. const float d2 = db * (0.5f + (sc[0] >> 4));
  3959. float2 sum = {0};
  3960. for (int l = 0; l < 2; ++l) {
  3961. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  3962. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  3963. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  3964. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  3965. for (int j = 0; j < 8; ++j) {
  3966. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  3967. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  3968. }
  3969. }
  3970. sumf[row] += d1 * sum[0] + d2 * sum[1];
  3971. dh += nb*sizeof(block_iq2_s)/2;
  3972. qs += nb*sizeof(block_iq2_s);
  3973. qh += nb*sizeof(block_iq2_s);
  3974. sc += nb*sizeof(block_iq2_s);
  3975. signs += nb*sizeof(block_iq2_s);
  3976. }
  3977. y4 += 32 * 32;
  3978. }
  3979. for (int row = 0; row < N_DST; ++row) {
  3980. all_sum = simd_sum(sumf[row]);
  3981. if (tiisg == 0) {
  3982. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3983. }
  3984. }
  3985. }
  3986. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  3987. kernel void kernel_mul_mv_iq2_s_f32(
  3988. device const void * src0,
  3989. device const float * src1,
  3990. device float * dst,
  3991. constant int64_t & ne00,
  3992. constant int64_t & ne01,
  3993. constant int64_t & ne02,
  3994. constant uint64_t & nb00,
  3995. constant uint64_t & nb01,
  3996. constant uint64_t & nb02,
  3997. constant int64_t & ne10,
  3998. constant int64_t & ne11,
  3999. constant int64_t & ne12,
  4000. constant uint64_t & nb10,
  4001. constant uint64_t & nb11,
  4002. constant uint64_t & nb12,
  4003. constant int64_t & ne0,
  4004. constant int64_t & ne1,
  4005. constant uint & r2,
  4006. constant uint & r3,
  4007. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4008. uint3 tgpig[[threadgroup_position_in_grid]],
  4009. uint tiisg[[thread_index_in_simdgroup]],
  4010. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4011. kernel_mul_mv_iq2_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4012. }
  4013. void kernel_mul_mv_iq1_s_f32_impl(
  4014. device const void * src0,
  4015. device const float * src1,
  4016. device float * dst,
  4017. int64_t ne00,
  4018. int64_t ne01,
  4019. int64_t ne02,
  4020. int64_t ne10,
  4021. int64_t ne12,
  4022. int64_t ne0,
  4023. int64_t ne1,
  4024. uint r2,
  4025. uint r3,
  4026. threadgroup int8_t * shared_value,
  4027. uint3 tgpig,
  4028. uint tiisg,
  4029. uint sgitg) {
  4030. const int nb = ne00/QK_K;
  4031. const int r0 = tgpig.x;
  4032. const int r1 = tgpig.y;
  4033. const int im = tgpig.z;
  4034. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4035. const int ib_row = first_row * nb;
  4036. const uint i12 = im%ne12;
  4037. const uint i13 = im/ne12;
  4038. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4039. device const block_iq1_s * x = (device const block_iq1_s *) src0 + ib_row + offset0;
  4040. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4041. float yl[32];
  4042. float sumf[N_DST]={0.f}, all_sum;
  4043. const int nb32 = nb * (QK_K / 32);
  4044. const int ix = tiisg;
  4045. device const float * y4 = y + 32 * ix;
  4046. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4047. float sumy = 0;
  4048. for (int i = 0; i < 32; ++i) {
  4049. yl[i] = y4[i];
  4050. sumy += yl[i];
  4051. }
  4052. const int ibl = ib32 / (QK_K / 32);
  4053. const int ib = ib32 % (QK_K / 32);
  4054. device const block_iq1_s * xr = x + ibl;
  4055. device const uint8_t * qs = xr->qs + 4 * ib;
  4056. device const uint16_t * qh = xr->qh + ib;
  4057. device const half * dh = &xr->d;
  4058. for (int row = 0; row < N_DST; row++) {
  4059. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  4060. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 5) & 0x700)));
  4061. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[0] << 2) & 0x700)));
  4062. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[0] >> 1) & 0x700)));
  4063. float sum = 0;
  4064. for (int j = 0; j < 4; ++j) {
  4065. sum += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  4066. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4)
  4067. + yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  4068. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  4069. }
  4070. sumf[row] += (float)dh[0] * (sum + sumy * (qh[0] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA)) * (2*((qh[0] >> 12) & 7) + 1);
  4071. dh += nb*sizeof(block_iq1_s)/2;
  4072. qs += nb*sizeof(block_iq1_s);
  4073. qh += nb*sizeof(block_iq1_s)/2;
  4074. }
  4075. y4 += 32 * 32;
  4076. }
  4077. for (int row = 0; row < N_DST; ++row) {
  4078. all_sum = simd_sum(sumf[row]);
  4079. if (tiisg == 0) {
  4080. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4081. }
  4082. }
  4083. }
  4084. void kernel_mul_mv_iq1_m_f32_impl(
  4085. device const void * src0,
  4086. device const float * src1,
  4087. device float * dst,
  4088. int64_t ne00,
  4089. int64_t ne01,
  4090. int64_t ne02,
  4091. int64_t ne10,
  4092. int64_t ne12,
  4093. int64_t ne0,
  4094. int64_t ne1,
  4095. uint r2,
  4096. uint r3,
  4097. threadgroup int8_t * shared_value,
  4098. uint3 tgpig,
  4099. uint tiisg,
  4100. uint sgitg) {
  4101. const int nb = ne00/QK_K;
  4102. const int r0 = tgpig.x;
  4103. const int r1 = tgpig.y;
  4104. const int im = tgpig.z;
  4105. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4106. const int ib_row = first_row * nb;
  4107. const uint i12 = im%ne12;
  4108. const uint i13 = im/ne12;
  4109. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4110. device const block_iq1_m * x = (device const block_iq1_m *) src0 + ib_row + offset0;
  4111. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4112. float yl[32];
  4113. float sumf[N_DST]={0.f}, all_sum;
  4114. const int nb32 = nb * (QK_K / 32);
  4115. const int ix = tiisg;
  4116. device const float * y4 = y + 32 * ix;
  4117. iq1m_scale_t scale;
  4118. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4119. float4 sumy = {0.f};
  4120. for (int i = 0; i < 8; ++i) {
  4121. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  4122. yl[i+ 8] = y4[i+ 8]; sumy[1] += yl[i+ 8];
  4123. yl[i+16] = y4[i+16]; sumy[2] += yl[i+16];
  4124. yl[i+24] = y4[i+24]; sumy[3] += yl[i+24];
  4125. }
  4126. const int ibl = ib32 / (QK_K / 32);
  4127. const int ib = ib32 % (QK_K / 32);
  4128. device const block_iq1_m * xr = x + ibl;
  4129. device const uint8_t * qs = xr->qs + 4 * ib;
  4130. device const uint8_t * qh = xr->qh + 2 * ib;
  4131. device const uint16_t * sc = (device const uint16_t *)xr->scales;
  4132. for (int row = 0; row < N_DST; row++) {
  4133. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4134. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  4135. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  4136. constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[1] << 8) & 0x700)));
  4137. constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[1] << 4) & 0x700)));
  4138. float2 sum = {0.f};
  4139. for (int j = 0; j < 4; ++j) {
  4140. sum[0] += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4)
  4141. + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4);
  4142. sum[1] += yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4)
  4143. + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4);
  4144. }
  4145. 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);
  4146. 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);
  4147. sumf[row] += (float)scale.f16 * ((sum[0] + delta1) * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 7) + 1) +
  4148. (sum[1] + delta2) * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 7) + 1));
  4149. sc += nb*sizeof(block_iq1_m)/2;
  4150. qs += nb*sizeof(block_iq1_m);
  4151. qh += nb*sizeof(block_iq1_m);
  4152. }
  4153. y4 += 32 * 32;
  4154. }
  4155. for (int row = 0; row < N_DST; ++row) {
  4156. all_sum = simd_sum(sumf[row]);
  4157. if (tiisg == 0) {
  4158. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4159. }
  4160. }
  4161. }
  4162. void kernel_mul_mv_iq4_nl_f32_impl(
  4163. device const void * src0,
  4164. device const float * src1,
  4165. device float * dst,
  4166. int64_t ne00,
  4167. int64_t ne01,
  4168. int64_t ne02,
  4169. int64_t ne10,
  4170. int64_t ne12,
  4171. int64_t ne0,
  4172. int64_t ne1,
  4173. uint r2,
  4174. uint r3,
  4175. threadgroup int8_t * shared_values_i8,
  4176. uint3 tgpig,
  4177. uint tiisg,
  4178. uint sgitg) {
  4179. threadgroup float * shared_values = (threadgroup float *)shared_values_i8;
  4180. const int nb = ne00/QK4_NL;
  4181. const int r0 = tgpig.x;
  4182. const int r1 = tgpig.y;
  4183. const int im = tgpig.z;
  4184. const int first_row = (r0 * 2 + sgitg) * 2;
  4185. const int ib_row = first_row * nb;
  4186. const uint i12 = im%ne12;
  4187. const uint i13 = im/ne12;
  4188. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4189. device const block_iq4_nl * x = (device const block_iq4_nl *) src0 + ib_row + offset0;
  4190. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4191. const int ix = tiisg/2; // 0...15
  4192. const int it = tiisg%2; // 0 or 1
  4193. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4194. threadgroup_barrier(mem_flags::mem_threadgroup);
  4195. float4 yl[4];
  4196. float sumf[2]={0.f}, all_sum;
  4197. device const float * yb = y + ix * QK4_NL + it * 8;
  4198. uint32_t aux32[2];
  4199. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4200. float4 qf1, qf2;
  4201. for (int ib = ix; ib < nb; ib += 16) {
  4202. device const float4 * y4 = (device const float4 *)yb;
  4203. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4204. for (int row = 0; row < 2; ++row) {
  4205. device const block_iq4_nl & xb = x[row*nb + ib];
  4206. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  4207. float4 acc1 = {0.f}, acc2 = {0.f};
  4208. aux32[0] = q4[0] | (q4[1] << 16);
  4209. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4210. aux32[0] &= 0x0f0f0f0f;
  4211. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4212. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4213. acc1 += yl[0] * qf1;
  4214. acc2 += yl[1] * qf2;
  4215. aux32[0] = q4[2] | (q4[3] << 16);
  4216. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4217. aux32[0] &= 0x0f0f0f0f;
  4218. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4219. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4220. acc1 += yl[2] * qf1;
  4221. acc2 += yl[3] * qf2;
  4222. acc1 += acc2;
  4223. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4224. }
  4225. yb += 16 * QK4_NL;
  4226. }
  4227. for (int row = 0; row < 2; ++row) {
  4228. all_sum = simd_sum(sumf[row]);
  4229. if (tiisg == 0) {
  4230. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4231. }
  4232. }
  4233. }
  4234. void kernel_mul_mv_iq4_xs_f32_impl(
  4235. device const void * src0,
  4236. device const float * src1,
  4237. device float * dst,
  4238. int64_t ne00,
  4239. int64_t ne01,
  4240. int64_t ne02,
  4241. int64_t ne10,
  4242. int64_t ne12,
  4243. int64_t ne0,
  4244. int64_t ne1,
  4245. uint r2,
  4246. uint r3,
  4247. threadgroup int8_t * shared_values_i8,
  4248. uint3 tgpig,
  4249. uint tiisg,
  4250. uint sgitg) {
  4251. threadgroup float * shared_values = (threadgroup float *)shared_values_i8;
  4252. const int nb = ne00/QK_K;
  4253. const int r0 = tgpig.x;
  4254. const int r1 = tgpig.y;
  4255. const int im = tgpig.z;
  4256. const int first_row = (r0 * 2 + sgitg) * 2;
  4257. const int ib_row = first_row * nb;
  4258. const uint i12 = im%ne12;
  4259. const uint i13 = im/ne12;
  4260. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4261. device const block_iq4_xs * x = (device const block_iq4_xs *) src0 + ib_row + offset0;
  4262. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4263. const int ix = tiisg/16; // 0 or 1
  4264. const int it = tiisg%16; // 0...15
  4265. const int ib = it/2;
  4266. const int il = it%2;
  4267. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4268. threadgroup_barrier(mem_flags::mem_threadgroup);
  4269. float4 yl[4];
  4270. float sumf[2]={0.f}, all_sum;
  4271. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  4272. uint32_t aux32[2];
  4273. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4274. float4 qf1, qf2;
  4275. for (int ibl = ix; ibl < nb; ibl += 2) {
  4276. device const float4 * y4 = (device const float4 *)yb;
  4277. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4278. for (int row = 0; row < 2; ++row) {
  4279. device const block_iq4_xs & xb = x[row*nb + ibl];
  4280. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  4281. float4 acc1 = {0.f}, acc2 = {0.f};
  4282. aux32[0] = q4[0] & 0x0f0f0f0f;
  4283. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  4284. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4285. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4286. acc1 += yl[0] * qf1;
  4287. acc2 += yl[1] * qf2;
  4288. aux32[0] = q4[1] & 0x0f0f0f0f;
  4289. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  4290. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4291. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4292. acc1 += yl[2] * qf1;
  4293. acc2 += yl[3] * qf2;
  4294. acc1 += acc2;
  4295. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  4296. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4297. }
  4298. yb += 2 * QK_K;
  4299. }
  4300. for (int row = 0; row < 2; ++row) {
  4301. all_sum = simd_sum(sumf[row]);
  4302. if (tiisg == 0) {
  4303. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4304. }
  4305. }
  4306. }
  4307. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  4308. kernel void kernel_mul_mv_iq1_s_f32(
  4309. device const void * src0,
  4310. device const float * src1,
  4311. device float * dst,
  4312. constant int64_t & ne00,
  4313. constant int64_t & ne01,
  4314. constant int64_t & ne02,
  4315. constant uint64_t & nb00,
  4316. constant uint64_t & nb01,
  4317. constant uint64_t & nb02,
  4318. constant int64_t & ne10,
  4319. constant int64_t & ne11,
  4320. constant int64_t & ne12,
  4321. constant uint64_t & nb10,
  4322. constant uint64_t & nb11,
  4323. constant uint64_t & nb12,
  4324. constant int64_t & ne0,
  4325. constant int64_t & ne1,
  4326. constant uint & r2,
  4327. constant uint & r3,
  4328. uint3 tgpig[[threadgroup_position_in_grid]],
  4329. uint tiisg[[thread_index_in_simdgroup]],
  4330. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4331. kernel_mul_mv_iq1_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  4332. }
  4333. [[host_name("kernel_mul_mv_iq1_m_f32")]]
  4334. kernel void kernel_mul_mv_iq1_m_f32(
  4335. device const void * src0,
  4336. device const float * src1,
  4337. device float * dst,
  4338. constant int64_t & ne00,
  4339. constant int64_t & ne01,
  4340. constant int64_t & ne02,
  4341. constant uint64_t & nb00,
  4342. constant uint64_t & nb01,
  4343. constant uint64_t & nb02,
  4344. constant int64_t & ne10,
  4345. constant int64_t & ne11,
  4346. constant int64_t & ne12,
  4347. constant uint64_t & nb10,
  4348. constant uint64_t & nb11,
  4349. constant uint64_t & nb12,
  4350. constant int64_t & ne0,
  4351. constant int64_t & ne1,
  4352. constant uint & r2,
  4353. constant uint & r3,
  4354. uint3 tgpig[[threadgroup_position_in_grid]],
  4355. uint tiisg[[thread_index_in_simdgroup]],
  4356. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4357. kernel_mul_mv_iq1_m_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, nullptr, tgpig, tiisg, sgitg);
  4358. }
  4359. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  4360. kernel void kernel_mul_mv_iq4_nl_f32(
  4361. device const void * src0,
  4362. device const float * src1,
  4363. device float * dst,
  4364. constant int64_t & ne00,
  4365. constant int64_t & ne01,
  4366. constant int64_t & ne02,
  4367. constant uint64_t & nb00,
  4368. constant uint64_t & nb01,
  4369. constant uint64_t & nb02,
  4370. constant int64_t & ne10,
  4371. constant int64_t & ne11,
  4372. constant int64_t & ne12,
  4373. constant uint64_t & nb10,
  4374. constant uint64_t & nb11,
  4375. constant uint64_t & nb12,
  4376. constant int64_t & ne0,
  4377. constant int64_t & ne1,
  4378. constant uint & r2,
  4379. constant uint & r3,
  4380. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4381. uint3 tgpig[[threadgroup_position_in_grid]],
  4382. uint tiisg[[thread_index_in_simdgroup]],
  4383. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4384. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4385. }
  4386. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  4387. kernel void kernel_mul_mv_iq4_xs_f32(
  4388. device const void * src0,
  4389. device const float * src1,
  4390. device float * dst,
  4391. constant int64_t & ne00,
  4392. constant int64_t & ne01,
  4393. constant int64_t & ne02,
  4394. constant uint64_t & nb00,
  4395. constant uint64_t & nb01,
  4396. constant uint64_t & nb02,
  4397. constant int64_t & ne10,
  4398. constant int64_t & ne11,
  4399. constant int64_t & ne12,
  4400. constant uint64_t & nb10,
  4401. constant uint64_t & nb11,
  4402. constant uint64_t & nb12,
  4403. constant int64_t & ne0,
  4404. constant int64_t & ne1,
  4405. constant uint & r2,
  4406. constant uint & r3,
  4407. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4408. uint3 tgpig[[threadgroup_position_in_grid]],
  4409. uint tiisg[[thread_index_in_simdgroup]],
  4410. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4411. kernel_mul_mv_iq4_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4412. }
  4413. //============================= templates and their specializations =============================
  4414. // NOTE: this is not dequantizing - we are simply fitting the template
  4415. template <typename type4x4>
  4416. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  4417. float4x4 temp = *(((device float4x4 *)src));
  4418. for (int i = 0; i < 16; i++){
  4419. reg[i/4][i%4] = temp[i/4][i%4];
  4420. }
  4421. }
  4422. template <typename type4x4>
  4423. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  4424. half4x4 temp = *(((device half4x4 *)src));
  4425. for (int i = 0; i < 16; i++){
  4426. reg[i/4][i%4] = temp[i/4][i%4];
  4427. }
  4428. }
  4429. template <typename type4x4>
  4430. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  4431. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  4432. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4433. const float d2 = d1 / 256.f;
  4434. const float md = -8.h * xb->d;
  4435. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4436. const ushort mask1 = mask0 << 8;
  4437. for (int i=0;i<8;i++) {
  4438. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  4439. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  4440. }
  4441. }
  4442. template <typename type4x4>
  4443. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  4444. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  4445. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4446. const float d2 = d1 / 256.f;
  4447. const float m = xb->m;
  4448. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4449. const ushort mask1 = mask0 << 8;
  4450. for (int i=0;i<8;i++) {
  4451. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  4452. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  4453. }
  4454. }
  4455. template <typename type4x4>
  4456. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  4457. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  4458. const float d = xb->d;
  4459. const float md = -16.h * xb->d;
  4460. const ushort mask = il ? 0x00F0 : 0x000F;
  4461. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4462. const int x_mv = il ? 4 : 0;
  4463. const int gh_mv = il ? 12 : 0;
  4464. const int gh_bk = il ? 0 : 4;
  4465. for (int i = 0; i < 8; i++) {
  4466. // extract the 5-th bits for x0 and x1
  4467. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4468. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4469. // combine the 4-bits from qs with the 5th bit
  4470. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4471. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4472. reg[i/2][2*(i%2)+0] = d * x0 + md;
  4473. reg[i/2][2*(i%2)+1] = d * x1 + md;
  4474. }
  4475. }
  4476. template <typename type4x4>
  4477. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  4478. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  4479. const float d = xb->d;
  4480. const float m = xb->m;
  4481. const ushort mask = il ? 0x00F0 : 0x000F;
  4482. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4483. const int x_mv = il ? 4 : 0;
  4484. const int gh_mv = il ? 12 : 0;
  4485. const int gh_bk = il ? 0 : 4;
  4486. for (int i = 0; i < 8; i++) {
  4487. // extract the 5-th bits for x0 and x1
  4488. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4489. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4490. // combine the 4-bits from qs with the 5th bit
  4491. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4492. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4493. reg[i/2][2*(i%2)+0] = d * x0 + m;
  4494. reg[i/2][2*(i%2)+1] = d * x1 + m;
  4495. }
  4496. }
  4497. template <typename type4x4>
  4498. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  4499. device const int8_t * qs = ((device const int8_t *)xb->qs);
  4500. const half d = xb->d;
  4501. for (int i = 0; i < 16; i++) {
  4502. reg[i/4][i%4] = (qs[i + 16*il] * d);
  4503. }
  4504. }
  4505. template <typename type4x4>
  4506. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  4507. const float d = xb->d;
  4508. const float min = xb->dmin;
  4509. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4510. float dl, ml;
  4511. uint8_t sc = xb->scales[il];
  4512. q = q + 32*(il/8) + 16*(il&1);
  4513. il = (il/2)%4;
  4514. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4515. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4516. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  4517. for (int i = 0; i < 16; ++i) {
  4518. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4519. }
  4520. }
  4521. template <typename type4x4>
  4522. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  4523. const half d_all = xb->d;
  4524. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4525. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  4526. device const int8_t * scales = (device const int8_t *)xb->scales;
  4527. q = q + 32 * (il/8) + 16 * (il&1);
  4528. h = h + 16 * (il&1);
  4529. uint8_t m = 1 << (il/2);
  4530. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  4531. ((il/4)>0 ? 12 : 3);
  4532. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  4533. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  4534. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  4535. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  4536. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  4537. const float ml = 4.f * dl;
  4538. il = (il/2) & 3;
  4539. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4540. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4541. dl *= coef;
  4542. for (int i = 0; i < 16; ++i) {
  4543. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  4544. }
  4545. }
  4546. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  4547. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  4548. : 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))};
  4549. }
  4550. template <typename type4x4>
  4551. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  4552. device const uchar * q = xb->qs;
  4553. short is = (il/4) * 2;
  4554. q = q + (il/4) * 32 + 16 * (il&1);
  4555. il = il & 3;
  4556. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4557. const float d = il < 2 ? xb->d : xb->d / 16.h;
  4558. const float min = xb->dmin;
  4559. const float dl = d * sc[0];
  4560. const float ml = min * sc[1];
  4561. const ushort mask = il<2 ? 0x0F : 0xF0;
  4562. for (int i = 0; i < 16; ++i) {
  4563. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4564. }
  4565. }
  4566. template <typename type4x4>
  4567. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  4568. device const uint8_t * q = xb->qs;
  4569. device const uint8_t * qh = xb->qh;
  4570. short is = (il/4) * 2;
  4571. q = q + 32 * (il/4) + 16 * (il&1);
  4572. qh = qh + 16 * (il&1);
  4573. uint8_t ul = 1 << (il/2);
  4574. il = il & 3;
  4575. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4576. const float d = il < 2 ? xb->d : xb->d / 16.f;
  4577. const float min = xb->dmin;
  4578. const float dl = d * sc[0];
  4579. const float ml = min * sc[1];
  4580. const ushort mask = il<2 ? 0x0F : 0xF0;
  4581. const float qh_val = il<2 ? 16.f : 256.f;
  4582. for (int i = 0; i < 16; ++i) {
  4583. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  4584. }
  4585. }
  4586. template <typename type4x4>
  4587. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  4588. const half d_all = xb->d;
  4589. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  4590. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  4591. device const int8_t * scales = (device const int8_t *)xb->scales;
  4592. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  4593. qh = qh + 32*(il/8) + 16*(il&1);
  4594. float sc = scales[(il%2) + 2 * ((il/2))];
  4595. il = (il/2) & 3;
  4596. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4597. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  4598. const float coef = il>1 ? 1.f/16.f : 1.f;
  4599. const float ml = d_all * sc * 32.f;
  4600. const float dl = d_all * sc * coef;
  4601. for (int i = 0; i < 16; ++i) {
  4602. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  4603. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  4604. reg[i/4][i%4] = dl * q - ml;
  4605. }
  4606. }
  4607. template <typename type4x4>
  4608. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  4609. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4610. const float d = xb->d;
  4611. const int ib32 = il/2;
  4612. il = il%2;
  4613. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4614. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  4615. device const uint16_t * q2 = xb->qs + 4*ib32;
  4616. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  4617. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  4618. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  4619. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  4620. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4621. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  4622. for (int i = 0; i < 8; ++i) {
  4623. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4624. }
  4625. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4626. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  4627. for (int i = 0; i < 8; ++i) {
  4628. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4629. }
  4630. }
  4631. template <typename type4x4>
  4632. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  4633. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4634. const float d = xb->d;
  4635. const int ib32 = il/2;
  4636. il = il%2;
  4637. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4638. device const uint16_t * q2 = xb->qs + 4*ib32;
  4639. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4640. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  4641. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  4642. for (int i = 0; i < 8; ++i) {
  4643. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4644. }
  4645. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  4646. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  4647. for (int i = 0; i < 8; ++i) {
  4648. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4649. }
  4650. }
  4651. template <typename type4x4>
  4652. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  4653. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4654. const float d = xb->d;
  4655. const int ib32 = il/2;
  4656. il = il%2;
  4657. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4658. device const uint8_t * q3 = xb->qs + 8*ib32;
  4659. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  4660. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4661. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  4662. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  4663. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  4664. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4665. for (int i = 0; i < 4; ++i) {
  4666. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4667. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4668. }
  4669. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  4670. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  4671. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  4672. for (int i = 0; i < 4; ++i) {
  4673. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4674. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4675. }
  4676. }
  4677. template <typename type4x4>
  4678. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  4679. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4680. const float d = xb->d;
  4681. const int ib32 = il/2;
  4682. il = il%2;
  4683. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4684. device const uint8_t * qs = xb->qs + 8*ib32;
  4685. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  4686. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4687. const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
  4688. constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  4689. constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  4690. for (int i = 0; i < 4; ++i) {
  4691. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  4692. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  4693. }
  4694. grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  4695. grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  4696. for (int i = 0; i < 4; ++i) {
  4697. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  4698. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  4699. }
  4700. }
  4701. template <typename type4x4>
  4702. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  4703. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4704. const float d = xb->d;
  4705. const int ib32 = il/2;
  4706. il = il%2;
  4707. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4708. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4709. device const uint8_t * signs = qs + QK_K/8;
  4710. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4711. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4712. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  4713. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  4714. for (int i = 0; i < 8; ++i) {
  4715. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  4716. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  4717. }
  4718. }
  4719. template <typename type4x4>
  4720. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  4721. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4722. const int ib32 = il/2;
  4723. il = il%2;
  4724. const float d = xb->d;
  4725. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4726. device const uint16_t * qh = xb->qh;
  4727. const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1);
  4728. const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA);
  4729. const uint16_t h = qh[ib32] >> 6*il;
  4730. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700)));
  4731. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700)));
  4732. for (int i = 0; i < 4; ++i) {
  4733. reg[0][i] = dl * (grid1[i] & 0xf) + ml;
  4734. reg[1][i] = dl * (grid1[i] >> 4) + ml;
  4735. reg[2][i] = dl * (grid2[i] & 0xf) + ml;
  4736. reg[3][i] = dl * (grid2[i] >> 4) + ml;
  4737. }
  4738. }
  4739. template <typename type4x4>
  4740. void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) {
  4741. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4742. const int ib32 = il/2;
  4743. il = il%2;
  4744. device const uint16_t * sc = (device const uint16_t *)xb->scales;
  4745. iq1m_scale_t scale;
  4746. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4747. const float d = scale.f16;
  4748. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4749. device const uint8_t * qh = xb->qh + 2*ib32 + il;
  4750. const float dl = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1);
  4751. const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  4752. const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
  4753. constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
  4754. constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
  4755. for (int i = 0; i < 4; ++i) {
  4756. reg[0][i] = dl * (grid1[i] & 0xf) + ml1;
  4757. reg[1][i] = dl * (grid1[i] >> 4) + ml1;
  4758. reg[2][i] = dl * (grid2[i] & 0xf) + ml2;
  4759. reg[3][i] = dl * (grid2[i] >> 4) + ml2;
  4760. }
  4761. }
  4762. template <typename type4x4>
  4763. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  4764. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  4765. const float d = xb->d;
  4766. uint32_t aux32;
  4767. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4768. for (int i = 0; i < 4; ++i) {
  4769. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  4770. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4771. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4772. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4773. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4774. }
  4775. }
  4776. template <typename type4x4>
  4777. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  4778. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4779. const int ib32 = il/2;
  4780. il = il%2;
  4781. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4782. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  4783. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  4784. const float d = (float)xb->d * (ls - 32);
  4785. uint32_t aux32;
  4786. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4787. for (int i = 0; i < 4; ++i) {
  4788. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  4789. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4790. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4791. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4792. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4793. }
  4794. }
  4795. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  4796. kernel void kernel_get_rows(
  4797. device const void * src0,
  4798. device const char * src1,
  4799. device float * dst,
  4800. constant int64_t & ne00,
  4801. constant uint64_t & nb01,
  4802. constant uint64_t & nb02,
  4803. constant int64_t & ne10,
  4804. constant uint64_t & nb10,
  4805. constant uint64_t & nb11,
  4806. constant uint64_t & nb1,
  4807. constant uint64_t & nb2,
  4808. uint3 tgpig[[threadgroup_position_in_grid]],
  4809. uint tiitg[[thread_index_in_threadgroup]],
  4810. uint3 tptg [[threads_per_threadgroup]]) {
  4811. //const int64_t i = tgpig;
  4812. //const int64_t r = ((device int32_t *) src1)[i];
  4813. const int64_t i10 = tgpig.x;
  4814. const int64_t i11 = tgpig.y;
  4815. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4816. const int64_t i02 = i11;
  4817. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  4818. float4x4 temp;
  4819. dequantize_func(
  4820. ((device const block_q *) ((device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  4821. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  4822. }
  4823. }
  4824. kernel void kernel_get_rows_f32(
  4825. device const void * src0,
  4826. device const char * src1,
  4827. device float * dst,
  4828. constant int64_t & ne00,
  4829. constant uint64_t & nb01,
  4830. constant uint64_t & nb02,
  4831. constant int64_t & ne10,
  4832. constant uint64_t & nb10,
  4833. constant uint64_t & nb11,
  4834. constant uint64_t & nb1,
  4835. constant uint64_t & nb2,
  4836. uint3 tgpig[[threadgroup_position_in_grid]],
  4837. uint tiitg[[thread_index_in_threadgroup]],
  4838. uint3 tptg [[threads_per_threadgroup]]) {
  4839. const int64_t i10 = tgpig.x;
  4840. const int64_t i11 = tgpig.y;
  4841. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4842. const int64_t i02 = i11;
  4843. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4844. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4845. ((device float *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4846. }
  4847. }
  4848. kernel void kernel_get_rows_f16(
  4849. device const void * src0,
  4850. device const char * src1,
  4851. device float * dst,
  4852. constant int64_t & ne00,
  4853. constant uint64_t & nb01,
  4854. constant uint64_t & nb02,
  4855. constant int64_t & ne10,
  4856. constant uint64_t & nb10,
  4857. constant uint64_t & nb11,
  4858. constant uint64_t & nb1,
  4859. constant uint64_t & nb2,
  4860. uint3 tgpig[[threadgroup_position_in_grid]],
  4861. uint tiitg[[thread_index_in_threadgroup]],
  4862. uint3 tptg [[threads_per_threadgroup]]) {
  4863. const int64_t i10 = tgpig.x;
  4864. const int64_t i11 = tgpig.y;
  4865. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4866. const int64_t i02 = i11;
  4867. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4868. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4869. ((device half *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4870. }
  4871. }
  4872. kernel void kernel_get_rows_i32(
  4873. device const void * src0,
  4874. device const char * src1,
  4875. device int32_t * dst,
  4876. constant int64_t & ne00,
  4877. constant uint64_t & nb01,
  4878. constant uint64_t & nb02,
  4879. constant int64_t & ne10,
  4880. constant uint64_t & nb10,
  4881. constant uint64_t & nb11,
  4882. constant uint64_t & nb1,
  4883. constant uint64_t & nb2,
  4884. uint3 tgpig[[threadgroup_position_in_grid]],
  4885. uint tiitg[[thread_index_in_threadgroup]],
  4886. uint3 tptg [[threads_per_threadgroup]]) {
  4887. const int64_t i10 = tgpig.x;
  4888. const int64_t i11 = tgpig.y;
  4889. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4890. const int64_t i02 = i11;
  4891. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  4892. ((device int32_t *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  4893. ((device int32_t *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  4894. }
  4895. }
  4896. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  4897. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  4898. #define BLOCK_SIZE_K 32
  4899. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  4900. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  4901. #define THREAD_PER_BLOCK 128
  4902. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  4903. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  4904. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  4905. #define SG_MAT_ROW 8
  4906. // each block_q contains 16*nl weights
  4907. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  4908. void kernel_mul_mm_impl(device const uchar * src0,
  4909. device const uchar * src1,
  4910. device float * dst,
  4911. constant int64_t & ne00,
  4912. constant int64_t & ne02,
  4913. constant uint64_t & nb01,
  4914. constant uint64_t & nb02,
  4915. constant int64_t & ne12,
  4916. constant uint64_t & nb10,
  4917. constant uint64_t & nb11,
  4918. constant uint64_t & nb12,
  4919. constant int64_t & ne0,
  4920. constant int64_t & ne1,
  4921. constant uint & r2,
  4922. constant uint & r3,
  4923. threadgroup uchar * shared_memory [[threadgroup(0)]],
  4924. uint3 tgpig[[threadgroup_position_in_grid]],
  4925. uint tiitg[[thread_index_in_threadgroup]],
  4926. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4927. threadgroup half * sa = (threadgroup half *)(shared_memory);
  4928. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  4929. const uint r0 = tgpig.y;
  4930. const uint r1 = tgpig.x;
  4931. const uint im = tgpig.z;
  4932. // if this block is of 64x32 shape or smaller
  4933. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  4934. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  4935. // a thread shouldn't load data outside of the matrix
  4936. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  4937. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  4938. simdgroup_half8x8 ma[4];
  4939. simdgroup_float8x8 mb[2];
  4940. simdgroup_float8x8 c_res[8];
  4941. for (int i = 0; i < 8; i++){
  4942. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  4943. }
  4944. short il = (tiitg % THREAD_PER_ROW);
  4945. const uint i12 = im%ne12;
  4946. const uint i13 = im/ne12;
  4947. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  4948. ushort offset1 = il/nl;
  4949. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  4950. device const float * y = (device const float *)(src1
  4951. + nb12 * im
  4952. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  4953. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  4954. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  4955. // load data and store to threadgroup memory
  4956. half4x4 temp_a;
  4957. dequantize_func(x, il, temp_a);
  4958. threadgroup_barrier(mem_flags::mem_threadgroup);
  4959. #pragma unroll(16)
  4960. for (int i = 0; i < 16; i++) {
  4961. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  4962. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  4963. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  4964. }
  4965. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  4966. il = (il + 2 < nl) ? il + 2 : il % 2;
  4967. x = (il < 2) ? x + (2+nl-1)/nl : x;
  4968. y += BLOCK_SIZE_K;
  4969. threadgroup_barrier(mem_flags::mem_threadgroup);
  4970. // load matrices from threadgroup memory and conduct outer products
  4971. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  4972. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  4973. #pragma unroll(4)
  4974. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  4975. #pragma unroll(4)
  4976. for (int i = 0; i < 4; i++) {
  4977. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  4978. }
  4979. simdgroup_barrier(mem_flags::mem_none);
  4980. #pragma unroll(2)
  4981. for (int i = 0; i < 2; i++) {
  4982. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  4983. }
  4984. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  4985. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  4986. #pragma unroll(8)
  4987. for (int i = 0; i < 8; i++){
  4988. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  4989. }
  4990. }
  4991. }
  4992. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  4993. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  4994. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  4995. for (int i = 0; i < 8; i++) {
  4996. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  4997. }
  4998. } else {
  4999. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  5000. threadgroup_barrier(mem_flags::mem_threadgroup);
  5001. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5002. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5003. for (int i = 0; i < 8; i++) {
  5004. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5005. }
  5006. threadgroup_barrier(mem_flags::mem_threadgroup);
  5007. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  5008. if (sgitg == 0) {
  5009. for (int i = 0; i < n_rows; i++) {
  5010. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5011. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5012. }
  5013. }
  5014. }
  5015. }
  5016. }
  5017. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in rowids
  5018. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5019. void kernel_mul_mm_id_impl(
  5020. device const uchar * src0,
  5021. device const uchar * src1,
  5022. threadgroup ushort2 * rowids,
  5023. device float * dst,
  5024. constant int64_t & ne00,
  5025. constant int64_t & ne02,
  5026. constant uint64_t & nb01,
  5027. constant uint64_t & nb02,
  5028. constant int64_t & ne11,
  5029. constant int64_t & ne12,
  5030. constant uint64_t & nb10,
  5031. constant uint64_t & nb11,
  5032. constant uint64_t & nb12,
  5033. constant int64_t & ne0,
  5034. int64_t ne1,
  5035. int64_t ne0ne1,
  5036. threadgroup uchar * shared_memory,
  5037. uint3 tgpig[[threadgroup_position_in_grid]],
  5038. uint tiitg[[thread_index_in_threadgroup]],
  5039. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5040. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5041. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5042. const uint r0 = tgpig.y;
  5043. const uint r1 = tgpig.x;
  5044. if (r1 * BLOCK_SIZE_N >= ne1) return;
  5045. // if this block is of 64x32 shape or smaller
  5046. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5047. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5048. // a thread shouldn't load data outside of the matrix
  5049. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5050. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5051. simdgroup_half8x8 ma[4];
  5052. simdgroup_float8x8 mb[2];
  5053. simdgroup_float8x8 c_res[8];
  5054. for (int i = 0; i < 8; i++){
  5055. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5056. }
  5057. short il = (tiitg % THREAD_PER_ROW);
  5058. ushort offset1 = il/nl;
  5059. threadgroup const auto & id = rowids[r1 * BLOCK_SIZE_N + thread_col];
  5060. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01) + offset1;
  5061. device const float * y = (device const float *)(src1
  5062. + nb12 * id[1]
  5063. + nb11 * (id[0] % ne11)
  5064. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5065. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5066. // load data and store to threadgroup memory
  5067. half4x4 temp_a;
  5068. dequantize_func(x, il, temp_a);
  5069. threadgroup_barrier(mem_flags::mem_threadgroup);
  5070. for (int i = 0; i < 16; i++) {
  5071. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5072. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5073. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5074. }
  5075. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5076. il = (il + 2 < nl) ? il + 2 : il % 2;
  5077. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5078. y += BLOCK_SIZE_K;
  5079. threadgroup_barrier(mem_flags::mem_threadgroup);
  5080. // load matrices from threadgroup memory and conduct outer products
  5081. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5082. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5083. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5084. for (int i = 0; i < 4; i++) {
  5085. simdgroup_load(ma[i], lsma + SG_MAT_SIZE * i);
  5086. }
  5087. simdgroup_barrier(mem_flags::mem_none);
  5088. for (int i = 0; i < 2; i++) {
  5089. simdgroup_load(mb[i], lsmb + SG_MAT_SIZE * i);
  5090. }
  5091. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5092. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5093. for (int i = 0; i < 8; i++){
  5094. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5095. }
  5096. }
  5097. }
  5098. {
  5099. threadgroup_barrier(mem_flags::mem_threadgroup);
  5100. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5101. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5102. for (int i = 0; i < 8; i++) {
  5103. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5104. }
  5105. threadgroup_barrier(mem_flags::mem_threadgroup);
  5106. device float * C = dst + (BLOCK_SIZE_M * r0);
  5107. if (sgitg == 0) {
  5108. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5109. threadgroup const auto & jid = rowids[r1 * BLOCK_SIZE_N + j];
  5110. int joff = jid[0] * ne0 + jid[1] * ne0ne1;
  5111. for (int i = 0; i < n_rows; i++) {
  5112. *(C + i + joff) = *(temp_str + i + j * BLOCK_SIZE_M);
  5113. }
  5114. }
  5115. }
  5116. }
  5117. }
  5118. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5119. kernel void kernel_mul_mm(device const uchar * src0,
  5120. device const uchar * src1,
  5121. device float * dst,
  5122. constant int64_t & ne00,
  5123. constant int64_t & ne02,
  5124. constant uint64_t & nb01,
  5125. constant uint64_t & nb02,
  5126. constant int64_t & ne12,
  5127. constant uint64_t & nb10,
  5128. constant uint64_t & nb11,
  5129. constant uint64_t & nb12,
  5130. constant int64_t & ne0,
  5131. constant int64_t & ne1,
  5132. constant uint & r2,
  5133. constant uint & r3,
  5134. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5135. uint3 tgpig[[threadgroup_position_in_grid]],
  5136. uint tiitg[[thread_index_in_threadgroup]],
  5137. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5138. kernel_mul_mm_impl<block_q, nl, dequantize_func>(
  5139. src0,
  5140. src1,
  5141. dst,
  5142. ne00,
  5143. ne02,
  5144. nb01,
  5145. nb02,
  5146. ne12,
  5147. nb10,
  5148. nb11,
  5149. nb12,
  5150. ne0,
  5151. ne1,
  5152. r2,
  5153. r3,
  5154. shared_memory,
  5155. tgpig,
  5156. tiitg,
  5157. sgitg);
  5158. }
  5159. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5160. kernel void kernel_mul_mm_id(
  5161. device const uchar * src0s,
  5162. device const uchar * src1,
  5163. device float * dst,
  5164. device const uchar * ids,
  5165. constant int64_t & nei0,
  5166. constant int64_t & nei1,
  5167. constant uint64_t & nbi1,
  5168. constant int64_t & ne00,
  5169. constant int64_t & ne02,
  5170. constant uint64_t & nb01,
  5171. constant uint64_t & nb02,
  5172. constant int64_t & ne11,
  5173. constant int64_t & ne12,
  5174. constant int64_t & ne13,
  5175. constant uint64_t & nb10,
  5176. constant uint64_t & nb11,
  5177. constant uint64_t & nb12,
  5178. constant int64_t & ne0,
  5179. constant int64_t & ne1,
  5180. constant uint64_t & nb1,
  5181. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5182. uint3 tgpig[[threadgroup_position_in_grid]],
  5183. uint tiitg[[thread_index_in_threadgroup]],
  5184. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5185. const int32_t i02 = tgpig.z;
  5186. tgpig.z = 0;
  5187. device const uchar * src0 = src0s + i02*nb02;
  5188. // row indices
  5189. threadgroup ushort2 * rowids = (threadgroup ushort2 *)(shared_memory + 8192);
  5190. // TODO: parallelize this loop
  5191. int64_t _ne1 = 0;
  5192. for (ushort ii1 = 0; ii1 < nei1; ii1++) {
  5193. for (ushort ii0 = 0; ii0 < nei0; ii0++) {
  5194. int32_t id = ((device int32_t *) (ids + ii1*nbi1))[ii0];
  5195. if (id == i02) {
  5196. //if (tiitg == 0) {
  5197. rowids[_ne1] = ushort2(ii0, ii1);
  5198. //}
  5199. _ne1++;
  5200. }
  5201. }
  5202. }
  5203. threadgroup_barrier(mem_flags::mem_threadgroup);
  5204. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  5205. src0,
  5206. src1,
  5207. rowids,
  5208. dst,
  5209. ne00,
  5210. ne02,
  5211. nb01,
  5212. nb02,
  5213. ne11,
  5214. ne12,
  5215. nb10,
  5216. nb11,
  5217. nb12,
  5218. ne0,
  5219. _ne1,
  5220. ne0*ne1,
  5221. shared_memory,
  5222. tgpig,
  5223. tiitg,
  5224. sgitg);
  5225. }
  5226. #define QK_NL 16
  5227. //
  5228. // get rows
  5229. //
  5230. typedef void (get_rows_t)(
  5231. device const void * src0,
  5232. device const char * src1,
  5233. device float * dst,
  5234. constant int64_t & ne00,
  5235. constant uint64_t & nb01,
  5236. constant uint64_t & nb02,
  5237. constant int64_t & ne10,
  5238. constant uint64_t & nb10,
  5239. constant uint64_t & nb11,
  5240. constant uint64_t & nb1,
  5241. constant uint64_t & nb2,
  5242. uint3, uint, uint3);
  5243. //template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  5244. //template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  5245. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  5246. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  5247. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  5248. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  5249. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  5250. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  5251. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  5252. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  5253. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  5254. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  5255. template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_t kernel_get_rows<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5256. template [[host_name("kernel_get_rows_iq2_xs")]] kernel get_rows_t kernel_get_rows<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5257. template [[host_name("kernel_get_rows_iq3_xxs")]] kernel get_rows_t kernel_get_rows<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5258. template [[host_name("kernel_get_rows_iq3_s")]] kernel get_rows_t kernel_get_rows<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5259. template [[host_name("kernel_get_rows_iq2_s")]] kernel get_rows_t kernel_get_rows<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5260. template [[host_name("kernel_get_rows_iq1_s")]] kernel get_rows_t kernel_get_rows<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5261. template [[host_name("kernel_get_rows_iq1_m")]] kernel get_rows_t kernel_get_rows<block_iq1_m, QK_NL, dequantize_iq1_m>;
  5262. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_t kernel_get_rows<block_iq4_nl, 2, dequantize_iq4_nl>;
  5263. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_t kernel_get_rows<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5264. //
  5265. // matrix-matrix multiplication
  5266. //
  5267. typedef decltype(kernel_mul_mm<float4x4, 1, dequantize_f32>) mat_mm_t;
  5268. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  5269. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  5270. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  5271. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  5272. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  5273. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  5274. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  5275. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  5276. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  5277. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  5278. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  5279. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;
  5280. template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5281. template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5282. template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5283. template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5284. template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5285. template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5286. template [[host_name("kernel_mul_mm_iq1_m_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq1_m, QK_NL, dequantize_iq1_m>;
  5287. template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_nl>;
  5288. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5289. //
  5290. // indirect matrix-matrix multiplication
  5291. //
  5292. typedef decltype(kernel_mul_mm_id<float4x4, 1, dequantize_f32>) mat_mm_id_t;
  5293. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  5294. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  5295. 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>;
  5296. 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>;
  5297. 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>;
  5298. 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>;
  5299. 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>;
  5300. 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>;
  5301. 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>;
  5302. 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>;
  5303. 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>;
  5304. 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>;
  5305. 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>;
  5306. 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>;
  5307. 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>;
  5308. 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>;
  5309. 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>;
  5310. 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>;
  5311. 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>;
  5312. 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>;
  5313. 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>;
  5314. //
  5315. // matrix-vector multiplication
  5316. //
  5317. typedef void (kernel_mul_mv_impl_t)(
  5318. device const char * src0,
  5319. device const char * src1,
  5320. device float * dst,
  5321. int64_t ne00,
  5322. int64_t ne01,
  5323. int64_t ne02,
  5324. uint64_t nb00,
  5325. uint64_t nb01,
  5326. uint64_t nb02,
  5327. int64_t ne10,
  5328. int64_t ne11,
  5329. int64_t ne12,
  5330. uint64_t nb10,
  5331. uint64_t nb11,
  5332. uint64_t nb12,
  5333. int64_t ne0,
  5334. int64_t ne1,
  5335. uint r2,
  5336. uint r3,
  5337. uint3 tgpig,
  5338. uint tiisg);
  5339. typedef void (kernel_mul_mv2_impl_t)(
  5340. device const void * src0,
  5341. device const float * src1,
  5342. device float * dst,
  5343. int64_t ne00,
  5344. int64_t ne01,
  5345. int64_t ne02,
  5346. int64_t ne10,
  5347. int64_t ne12,
  5348. int64_t ne0,
  5349. int64_t ne1,
  5350. uint r2,
  5351. uint r3,
  5352. threadgroup int8_t * shared_values,
  5353. uint3 tgpig,
  5354. uint tiisg,
  5355. uint sgitg);
  5356. template<kernel_mul_mv_impl_t impl_fn>
  5357. void mmv_fn(
  5358. device const char * src0,
  5359. device const char * src1,
  5360. device float * dst,
  5361. int64_t ne00,
  5362. int64_t ne01,
  5363. int64_t ne02,
  5364. uint64_t nb00,
  5365. uint64_t nb01,
  5366. uint64_t nb02,
  5367. int64_t ne10,
  5368. int64_t ne11,
  5369. int64_t ne12,
  5370. int64_t ne13,
  5371. uint64_t nb10,
  5372. uint64_t nb11,
  5373. uint64_t nb12,
  5374. int64_t ne0,
  5375. int64_t ne1,
  5376. uint64_t nb1,
  5377. uint r2,
  5378. uint r3,
  5379. threadgroup int8_t * shared_values,
  5380. uint3 tgpig,
  5381. uint tiitg,
  5382. uint tiisg,
  5383. uint sgitg) {
  5384. impl_fn(src0,src1,dst,ne00,ne01,ne02,nb00,nb01,nb02,ne10,ne11,ne12,nb10,nb11,nb12,ne0,ne1,r2,r3,tgpig,tiisg);
  5385. }
  5386. template<kernel_mul_mv2_impl_t impl_fn>
  5387. void mmv_fn(
  5388. device const char * src0,
  5389. device const char * src1,
  5390. device float * dst,
  5391. int64_t ne00,
  5392. int64_t ne01,
  5393. int64_t ne02,
  5394. uint64_t nb00,
  5395. uint64_t nb01,
  5396. uint64_t nb02,
  5397. int64_t ne10,
  5398. int64_t ne11,
  5399. int64_t ne12,
  5400. int64_t ne13,
  5401. uint64_t nb10,
  5402. uint64_t nb11,
  5403. uint64_t nb12,
  5404. int64_t ne0,
  5405. int64_t ne1,
  5406. uint64_t nb1,
  5407. uint r2,
  5408. uint r3,
  5409. threadgroup int8_t * shared_values,
  5410. uint3 tgpig,
  5411. uint tiitg,
  5412. uint tiisg,
  5413. uint sgitg) {
  5414. impl_fn(src0,(const device float *)src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,shared_values,tgpig,tiisg,sgitg);
  5415. }
  5416. typedef decltype(mmv_fn<kernel_mul_mv_f32_f32_impl>) mul_mv_impl_fn_t;
  5417. template<mul_mv_impl_fn_t impl_fn>
  5418. kernel void kernel_mul_mv_id(
  5419. device const char * src0s,
  5420. device const char * src1,
  5421. device float * dst,
  5422. device const char * ids,
  5423. constant int64_t & nei0,
  5424. constant int64_t & nei1,
  5425. constant uint64_t & nbi1,
  5426. constant int64_t & ne00,
  5427. constant int64_t & ne01,
  5428. constant int64_t & ne02,
  5429. constant uint64_t & nb00,
  5430. constant uint64_t & nb01,
  5431. constant uint64_t & nb02,
  5432. constant int64_t & ne10,
  5433. constant int64_t & ne11,
  5434. constant int64_t & ne12,
  5435. constant int64_t & ne13,
  5436. constant uint64_t & nb10,
  5437. constant uint64_t & nb11,
  5438. constant uint64_t & nb12,
  5439. constant int64_t & ne0,
  5440. constant int64_t & ne1,
  5441. constant uint64_t & nb1,
  5442. threadgroup int8_t * shared_values [[threadgroup(0)]],
  5443. uint3 tgpig[[threadgroup_position_in_grid]],
  5444. uint tiitg[[thread_index_in_threadgroup]],
  5445. uint tiisg[[thread_index_in_simdgroup]],
  5446. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5447. const int iid1 = tgpig.z/nei0;
  5448. const int idx = tgpig.z%nei0;
  5449. tgpig.z = 0;
  5450. const int32_t i02 = ((device const int32_t *) (ids + iid1*nbi1))[idx];
  5451. const int64_t i11 = idx % ne11;
  5452. const int64_t i12 = iid1;
  5453. const int64_t i1 = idx;
  5454. const int64_t i2 = i12;
  5455. device const char * src0_cur = src0s + i02*nb02;
  5456. device const char * src1_cur = src1 + i11*nb11 + i12*nb12;
  5457. device float * dst_cur = dst + i1*ne0 + i2*ne1*ne0;
  5458. impl_fn(
  5459. /* src0 */ src0_cur,
  5460. /* src1 */ src1_cur,
  5461. /* dst */ dst_cur,
  5462. /* ne00 */ ne00,
  5463. /* ne01 */ ne01,
  5464. /* ne02 */ 1,//ne02,
  5465. /* nb00 */ nb00,
  5466. /* nb01 */ nb01,
  5467. /* nb02 */ nb02,
  5468. /* ne10 */ ne10,
  5469. /* ne11 */ 1,//ne11,
  5470. /* ne12 */ 1,//ne12,
  5471. /* ne13 */ 1,//ne13,
  5472. /* nb10 */ nb10,
  5473. /* nb11 */ nb11,
  5474. /* nb12 */ nb12,
  5475. /* ne0 */ ne0,
  5476. /* ne1 */ 1,//ne1,
  5477. /* nb1 */ nb1,
  5478. /* r2 */ 1,
  5479. /* r3 */ 1,
  5480. shared_values,
  5481. tgpig,
  5482. tiitg,
  5483. tiisg,
  5484. sgitg);
  5485. }
  5486. typedef decltype(kernel_mul_mv_id<mmv_fn<kernel_mul_mv_f32_f32_impl>>) kernel_mul_mv_id_t;
  5487. 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>>;
  5488. 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>>;
  5489. 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>>;
  5490. 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>>>;
  5491. 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>>>;
  5492. 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>>>;
  5493. 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>>>;
  5494. 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>>;
  5495. 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>>;
  5496. 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>>;
  5497. 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>>;
  5498. 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>>;
  5499. 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>>;
  5500. 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>>;
  5501. 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>>;
  5502. 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>>;
  5503. 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>>;
  5504. 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>>;
  5505. 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>>;
  5506. 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>>;
  5507. 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>>;