ggml-metal.metal 244 KB

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