ggml-metal.metal 232 KB

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