1
0

ggml-metal.metal 223 KB

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