ggml-metal.metal 243 KB

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