ggml-metal.metal 300 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  5. #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; }
  6. #define QK4_0 32
  7. #define QR4_0 2
  8. typedef struct {
  9. half d; // delta
  10. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  11. } block_q4_0;
  12. #define QK4_1 32
  13. typedef struct {
  14. half d; // delta
  15. half m; // min
  16. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  17. } block_q4_1;
  18. #define QK5_0 32
  19. typedef struct {
  20. half d; // delta
  21. uint8_t qh[4]; // 5-th bit of quants
  22. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  23. } block_q5_0;
  24. #define QK5_1 32
  25. typedef struct {
  26. half d; // delta
  27. half m; // min
  28. uint8_t qh[4]; // 5-th bit of quants
  29. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  30. } block_q5_1;
  31. #define QK8_0 32
  32. typedef struct {
  33. half d; // delta
  34. int8_t qs[QK8_0]; // quants
  35. } block_q8_0;
  36. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  37. enum ggml_sort_order {
  38. GGML_SORT_ASC,
  39. GGML_SORT_DESC,
  40. };
  41. // general-purpose kernel for addition, multiplication and division of two tensors
  42. // pros: works for non-contiguous tensors, supports broadcast across all dims
  43. // cons: not very efficient
  44. kernel void kernel_add(
  45. device const char * src0,
  46. device const char * src1,
  47. device char * dst,
  48. constant int64_t & ne00,
  49. constant int64_t & ne01,
  50. constant int64_t & ne02,
  51. constant int64_t & ne03,
  52. constant uint64_t & nb00,
  53. constant uint64_t & nb01,
  54. constant uint64_t & nb02,
  55. constant uint64_t & nb03,
  56. constant int64_t & ne10,
  57. constant int64_t & ne11,
  58. constant int64_t & ne12,
  59. constant int64_t & ne13,
  60. constant uint64_t & nb10,
  61. constant uint64_t & nb11,
  62. constant uint64_t & nb12,
  63. constant uint64_t & nb13,
  64. constant int64_t & ne0,
  65. constant int64_t & ne1,
  66. constant int64_t & ne2,
  67. constant int64_t & ne3,
  68. constant uint64_t & nb0,
  69. constant uint64_t & nb1,
  70. constant uint64_t & nb2,
  71. constant uint64_t & nb3,
  72. constant int64_t & offs,
  73. uint3 tgpig[[threadgroup_position_in_grid]],
  74. uint3 tpitg[[thread_position_in_threadgroup]],
  75. uint3 ntg[[threads_per_threadgroup]]) {
  76. const int64_t i03 = tgpig.z;
  77. const int64_t i02 = tgpig.y;
  78. const int64_t i01 = tgpig.x;
  79. const int64_t i13 = i03 % ne13;
  80. const int64_t i12 = i02 % ne12;
  81. const int64_t i11 = i01 % ne11;
  82. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01 + offs;
  83. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  84. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + offs;
  85. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  86. const int i10 = i0 % ne10;
  87. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) + *((device float *)(src1_ptr + i10*nb10));
  88. }
  89. }
  90. kernel void kernel_mul(
  91. device const char * src0,
  92. device const char * src1,
  93. device char * dst,
  94. constant int64_t & ne00,
  95. constant int64_t & ne01,
  96. constant int64_t & ne02,
  97. constant int64_t & ne03,
  98. constant uint64_t & nb00,
  99. constant uint64_t & nb01,
  100. constant uint64_t & nb02,
  101. constant uint64_t & nb03,
  102. constant int64_t & ne10,
  103. constant int64_t & ne11,
  104. constant int64_t & ne12,
  105. constant int64_t & ne13,
  106. constant uint64_t & nb10,
  107. constant uint64_t & nb11,
  108. constant uint64_t & nb12,
  109. constant uint64_t & nb13,
  110. constant int64_t & ne0,
  111. constant int64_t & ne1,
  112. constant int64_t & ne2,
  113. constant int64_t & ne3,
  114. constant uint64_t & nb0,
  115. constant uint64_t & nb1,
  116. constant uint64_t & nb2,
  117. constant uint64_t & nb3,
  118. uint3 tgpig[[threadgroup_position_in_grid]],
  119. uint3 tpitg[[thread_position_in_threadgroup]],
  120. uint3 ntg[[threads_per_threadgroup]]) {
  121. const int64_t i03 = tgpig.z;
  122. const int64_t i02 = tgpig.y;
  123. const int64_t i01 = tgpig.x;
  124. const int64_t i13 = i03 % ne13;
  125. const int64_t i12 = i02 % ne12;
  126. const int64_t i11 = i01 % ne11;
  127. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01;
  128. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  129. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1;
  130. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  131. const int i10 = i0 % ne10;
  132. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) * *((device float *)(src1_ptr + i10*nb10));
  133. }
  134. }
  135. kernel void kernel_div(
  136. device const char * src0,
  137. device const char * src1,
  138. device char * dst,
  139. constant int64_t & ne00,
  140. constant int64_t & ne01,
  141. constant int64_t & ne02,
  142. constant int64_t & ne03,
  143. constant uint64_t & nb00,
  144. constant uint64_t & nb01,
  145. constant uint64_t & nb02,
  146. constant uint64_t & nb03,
  147. constant int64_t & ne10,
  148. constant int64_t & ne11,
  149. constant int64_t & ne12,
  150. constant int64_t & ne13,
  151. constant uint64_t & nb10,
  152. constant uint64_t & nb11,
  153. constant uint64_t & nb12,
  154. constant uint64_t & nb13,
  155. constant int64_t & ne0,
  156. constant int64_t & ne1,
  157. constant int64_t & ne2,
  158. constant int64_t & ne3,
  159. constant uint64_t & nb0,
  160. constant uint64_t & nb1,
  161. constant uint64_t & nb2,
  162. constant uint64_t & nb3,
  163. uint3 tgpig[[threadgroup_position_in_grid]],
  164. uint3 tpitg[[thread_position_in_threadgroup]],
  165. uint3 ntg[[threads_per_threadgroup]]) {
  166. const int64_t i03 = tgpig.z;
  167. const int64_t i02 = tgpig.y;
  168. const int64_t i01 = tgpig.x;
  169. const int64_t i13 = i03 % ne13;
  170. const int64_t i12 = i02 % ne12;
  171. const int64_t i11 = i01 % ne11;
  172. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01;
  173. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11;
  174. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1;
  175. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  176. const int i10 = i0 % ne10;
  177. *((device float *)(dst_ptr + i0*nb0)) = *((device float *)(src0_ptr + i0*nb00)) / *((device float *)(src1_ptr + i10*nb10));
  178. }
  179. }
  180. // assumption: src1 is a row
  181. // broadcast src1 into src0
  182. kernel void kernel_add_row(
  183. device const float4 * src0,
  184. device const float4 * src1,
  185. device float4 * dst,
  186. constant uint64_t & nb [[buffer(28)]],
  187. uint tpig[[thread_position_in_grid]]) {
  188. dst[tpig] = src0[tpig] + src1[tpig % nb];
  189. }
  190. kernel void kernel_mul_row(
  191. device const float4 * src0,
  192. device const float4 * src1,
  193. device float4 * dst,
  194. constant uint64_t & nb [[buffer(28)]],
  195. uint tpig[[thread_position_in_grid]]) {
  196. dst[tpig] = src0[tpig] * src1[tpig % nb];
  197. }
  198. kernel void kernel_div_row(
  199. device const float4 * src0,
  200. device const float4 * src1,
  201. device float4 * dst,
  202. constant uint64_t & nb [[buffer(28)]],
  203. uint tpig[[thread_position_in_grid]]) {
  204. dst[tpig] = src0[tpig] / src1[tpig % nb];
  205. }
  206. kernel void kernel_scale(
  207. device const float * src0,
  208. device float * dst,
  209. constant float & scale,
  210. uint tpig[[thread_position_in_grid]]) {
  211. dst[tpig] = src0[tpig] * scale;
  212. }
  213. kernel void kernel_scale_4(
  214. device const float4 * src0,
  215. device float4 * dst,
  216. constant float & scale,
  217. uint tpig[[thread_position_in_grid]]) {
  218. dst[tpig] = src0[tpig] * scale;
  219. }
  220. kernel void kernel_relu(
  221. device const float * src0,
  222. device float * dst,
  223. uint tpig[[thread_position_in_grid]]) {
  224. dst[tpig] = max(0.0f, src0[tpig]);
  225. }
  226. kernel void kernel_tanh(
  227. device const float * src0,
  228. device float * dst,
  229. uint tpig[[thread_position_in_grid]]) {
  230. device const float & x = src0[tpig];
  231. dst[tpig] = precise::tanh(x);
  232. }
  233. constant float GELU_COEF_A = 0.044715f;
  234. constant float GELU_QUICK_COEF = -1.702f;
  235. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  236. kernel void kernel_gelu(
  237. device const float4 * src0,
  238. device float4 * dst,
  239. uint tpig[[thread_position_in_grid]]) {
  240. device const float4 & x = src0[tpig];
  241. // BEWARE !!!
  242. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  243. // This was observed with Falcon 7B and 40B models
  244. //
  245. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  246. }
  247. kernel void kernel_gelu_quick(
  248. device const float4 * src0,
  249. device float4 * dst,
  250. uint tpig[[thread_position_in_grid]]) {
  251. device const float4 & x = src0[tpig];
  252. dst[tpig] = x*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x)));
  253. }
  254. kernel void kernel_silu(
  255. device const float4 * src0,
  256. device float4 * dst,
  257. uint tpig[[thread_position_in_grid]]) {
  258. device const float4 & x = src0[tpig];
  259. dst[tpig] = x / (1.0f + exp(-x));
  260. }
  261. kernel void kernel_sqr(
  262. device const float * src0,
  263. device float * dst,
  264. uint tpig[[thread_position_in_grid]]) {
  265. dst[tpig] = src0[tpig] * src0[tpig];
  266. }
  267. kernel void kernel_sum_rows(
  268. device const float * src0,
  269. device float * dst,
  270. constant int64_t & ne00,
  271. constant int64_t & ne01,
  272. constant int64_t & ne02,
  273. constant int64_t & ne03,
  274. constant uint64_t & nb00,
  275. constant uint64_t & nb01,
  276. constant uint64_t & nb02,
  277. constant uint64_t & nb03,
  278. constant int64_t & ne10,
  279. constant int64_t & ne11,
  280. constant int64_t & ne12,
  281. constant int64_t & ne13,
  282. constant uint64_t & nb10,
  283. constant uint64_t & nb11,
  284. constant uint64_t & nb12,
  285. constant uint64_t & nb13,
  286. constant int64_t & ne0,
  287. constant int64_t & ne1,
  288. constant int64_t & ne2,
  289. constant int64_t & ne3,
  290. constant uint64_t & nb0,
  291. constant uint64_t & nb1,
  292. constant uint64_t & nb2,
  293. constant uint64_t & nb3,
  294. uint3 tpig[[thread_position_in_grid]]) {
  295. int64_t i3 = tpig.z;
  296. int64_t i2 = tpig.y;
  297. int64_t i1 = tpig.x;
  298. if (i3 >= ne03 || i2 >= ne02 || i1 >= ne01) {
  299. return;
  300. }
  301. device const float * src_row = (device const float *) ((device const char *) src0 + i1*nb01 + i2*nb02 + i3*nb03);
  302. device float * dst_row = (device float *) ((device char *) dst + i1*nb1 + i2*nb2 + i3*nb3);
  303. float row_sum = 0;
  304. for (int64_t i0 = 0; i0 < ne00; i0++) {
  305. row_sum += src_row[i0];
  306. }
  307. dst_row[0] = row_sum;
  308. }
  309. kernel void kernel_soft_max(
  310. device const float * src0,
  311. device const float * src1,
  312. device const float * src2,
  313. device float * dst,
  314. constant int64_t & ne00,
  315. constant int64_t & ne01,
  316. constant int64_t & ne02,
  317. constant float & scale,
  318. constant float & max_bias,
  319. constant float & m0,
  320. constant float & m1,
  321. constant uint32_t & n_head_log2,
  322. threadgroup float * buf [[threadgroup(0)]],
  323. uint tgpig[[threadgroup_position_in_grid]],
  324. uint tpitg[[thread_position_in_threadgroup]],
  325. uint sgitg[[simdgroup_index_in_threadgroup]],
  326. uint tiisg[[thread_index_in_simdgroup]],
  327. uint ntg[[threads_per_threadgroup]]) {
  328. const int64_t i03 = (tgpig) / (ne02*ne01);
  329. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  330. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  331. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  332. device const float * pmask = src1 != src0 ? src1 + i01*ne00 : nullptr;
  333. device const float * ppos = src2 != src0 ? src2 : nullptr;
  334. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  335. float slope = 0.0f;
  336. // ALiBi
  337. if (max_bias > 0.0f) {
  338. const int64_t h = i02;
  339. const float base = h < n_head_log2 ? m0 : m1;
  340. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  341. slope = pow(base, exp);
  342. }
  343. // parallel max
  344. float lmax = -INFINITY;
  345. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  346. lmax = MAX(lmax, psrc0[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f));
  347. }
  348. // find the max value in the block
  349. float max_val = simd_max(lmax);
  350. if (ntg > N_SIMDWIDTH) {
  351. if (sgitg == 0) {
  352. buf[tiisg] = -INFINITY;
  353. }
  354. threadgroup_barrier(mem_flags::mem_threadgroup);
  355. if (tiisg == 0) {
  356. buf[sgitg] = max_val;
  357. }
  358. threadgroup_barrier(mem_flags::mem_threadgroup);
  359. max_val = buf[tiisg];
  360. max_val = simd_max(max_val);
  361. }
  362. // parallel sum
  363. float lsum = 0.0f;
  364. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  365. const float exp_psrc0 = exp((psrc0[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f)) - max_val);
  366. lsum += exp_psrc0;
  367. pdst[i00] = exp_psrc0;
  368. }
  369. // This barrier fixes a failing test
  370. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  371. threadgroup_barrier(mem_flags::mem_none);
  372. float sum = simd_sum(lsum);
  373. if (ntg > N_SIMDWIDTH) {
  374. if (sgitg == 0) {
  375. buf[tiisg] = 0.0f;
  376. }
  377. threadgroup_barrier(mem_flags::mem_threadgroup);
  378. if (tiisg == 0) {
  379. buf[sgitg] = sum;
  380. }
  381. threadgroup_barrier(mem_flags::mem_threadgroup);
  382. sum = buf[tiisg];
  383. sum = simd_sum(sum);
  384. }
  385. const float inv_sum = 1.0f/sum;
  386. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  387. pdst[i00] *= inv_sum;
  388. }
  389. }
  390. kernel void kernel_soft_max_4(
  391. device const float * src0,
  392. device const float * src1,
  393. device const float * src2,
  394. device float * dst,
  395. constant int64_t & ne00,
  396. constant int64_t & ne01,
  397. constant int64_t & ne02,
  398. constant float & scale,
  399. constant float & max_bias,
  400. constant float & m0,
  401. constant float & m1,
  402. constant uint32_t & n_head_log2,
  403. threadgroup float * buf [[threadgroup(0)]],
  404. uint tgpig[[threadgroup_position_in_grid]],
  405. uint tpitg[[thread_position_in_threadgroup]],
  406. uint sgitg[[simdgroup_index_in_threadgroup]],
  407. uint tiisg[[thread_index_in_simdgroup]],
  408. uint ntg[[threads_per_threadgroup]]) {
  409. const int64_t i03 = (tgpig) / (ne02*ne01);
  410. const int64_t i02 = (tgpig - i03*ne02*ne01) / ne01;
  411. const int64_t i01 = (tgpig - i03*ne02*ne01 - i02*ne01);
  412. device const float4 * psrc4 = (device const float4 *)(src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  413. device const float4 * pmask = src1 != src0 ? (device const float4 *)(src1 + i01*ne00) : nullptr;
  414. device const float4 * ppos = src2 != src0 ? (device const float4 *)(src2) : nullptr;
  415. device float4 * pdst4 = (device float4 *)(dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00);
  416. float slope = 0.0f;
  417. if (max_bias > 0.0f) {
  418. const int64_t h = i02;
  419. const float base = h < n_head_log2 ? m0 : m1;
  420. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  421. slope = pow(base, exp);
  422. }
  423. // parallel max
  424. float4 lmax4 = -INFINITY;
  425. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  426. lmax4 = fmax(lmax4, psrc4[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f));
  427. }
  428. const float lmax = MAX(MAX(lmax4[0], lmax4[1]), MAX(lmax4[2], lmax4[3]));
  429. float max_val = simd_max(lmax);
  430. if (ntg > N_SIMDWIDTH) {
  431. if (sgitg == 0) {
  432. buf[tiisg] = -INFINITY;
  433. }
  434. threadgroup_barrier(mem_flags::mem_threadgroup);
  435. if (tiisg == 0) {
  436. buf[sgitg] = max_val;
  437. }
  438. threadgroup_barrier(mem_flags::mem_threadgroup);
  439. max_val = buf[tiisg];
  440. max_val = simd_max(max_val);
  441. }
  442. // parallel sum
  443. float4 lsum4 = 0.0f;
  444. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  445. const float4 exp_psrc4 = exp((psrc4[i00]*scale + (pmask ? pmask[i00] : 0.0f) + (ppos ? slope*ppos[i00] : 0.0f)) - max_val);
  446. lsum4 += exp_psrc4;
  447. pdst4[i00] = exp_psrc4;
  448. }
  449. const float lsum = lsum4[0] + lsum4[1] + lsum4[2] + lsum4[3];
  450. // This barrier fixes a failing test
  451. // ref: https://github.com/ggerganov/ggml/pull/621#discussion_r1425156335
  452. threadgroup_barrier(mem_flags::mem_none);
  453. float sum = simd_sum(lsum);
  454. if (ntg > N_SIMDWIDTH) {
  455. if (sgitg == 0) {
  456. buf[tiisg] = 0.0f;
  457. }
  458. threadgroup_barrier(mem_flags::mem_threadgroup);
  459. if (tiisg == 0) {
  460. buf[sgitg] = sum;
  461. }
  462. threadgroup_barrier(mem_flags::mem_threadgroup);
  463. sum = buf[tiisg];
  464. sum = simd_sum(sum);
  465. }
  466. const float inv_sum = 1.0f/sum;
  467. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  468. pdst4[i00] *= inv_sum;
  469. }
  470. }
  471. kernel void kernel_diag_mask_inf(
  472. device const float * src0,
  473. device float * dst,
  474. constant int64_t & ne00,
  475. constant int64_t & ne01,
  476. constant int & n_past,
  477. uint3 tpig[[thread_position_in_grid]]) {
  478. const int64_t i02 = tpig[2];
  479. const int64_t i01 = tpig[1];
  480. const int64_t i00 = tpig[0];
  481. if (i00 > n_past + i01) {
  482. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  483. } else {
  484. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  485. }
  486. }
  487. kernel void kernel_diag_mask_inf_8(
  488. device const float4 * src0,
  489. device float4 * dst,
  490. constant int64_t & ne00,
  491. constant int64_t & ne01,
  492. constant int & n_past,
  493. uint3 tpig[[thread_position_in_grid]]) {
  494. const int64_t i = 2*tpig[0];
  495. dst[i+0] = src0[i+0];
  496. dst[i+1] = src0[i+1];
  497. int64_t i4 = 4*i;
  498. const int64_t i02 = i4/(ne00*ne01); i4 -= i02*ne00*ne01;
  499. const int64_t i01 = i4/(ne00); i4 -= i01*ne00;
  500. const int64_t i00 = i4;
  501. for (int k = 3; k >= 0; --k) {
  502. if (i00 + 4 + k <= n_past + i01) {
  503. break;
  504. }
  505. dst[i+1][k] = -INFINITY;
  506. if (i00 + k > n_past + i01) {
  507. dst[i][k] = -INFINITY;
  508. }
  509. }
  510. }
  511. kernel void kernel_norm(
  512. device const void * src0,
  513. device float * dst,
  514. constant int64_t & ne00,
  515. constant uint64_t & nb01,
  516. constant float & eps,
  517. threadgroup float * sum [[threadgroup(0)]],
  518. uint tgpig[[threadgroup_position_in_grid]],
  519. uint tpitg[[thread_position_in_threadgroup]],
  520. uint ntg[[threads_per_threadgroup]]) {
  521. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  522. // MEAN
  523. // parallel sum
  524. sum[tpitg] = 0.0f;
  525. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  526. sum[tpitg] += x[i00];
  527. }
  528. // reduce
  529. threadgroup_barrier(mem_flags::mem_threadgroup);
  530. for (uint i = ntg/2; i > 0; i /= 2) {
  531. if (tpitg < i) {
  532. sum[tpitg] += sum[tpitg + i];
  533. }
  534. threadgroup_barrier(mem_flags::mem_threadgroup);
  535. }
  536. const float mean = sum[0] / ne00;
  537. // recenter and VARIANCE
  538. threadgroup_barrier(mem_flags::mem_threadgroup);
  539. device float * y = dst + tgpig*ne00;
  540. sum[tpitg] = 0.0f;
  541. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  542. y[i00] = x[i00] - mean;
  543. sum[tpitg] += y[i00] * y[i00];
  544. }
  545. // reduce
  546. threadgroup_barrier(mem_flags::mem_threadgroup);
  547. for (uint i = ntg/2; i > 0; i /= 2) {
  548. if (tpitg < i) {
  549. sum[tpitg] += sum[tpitg + i];
  550. }
  551. threadgroup_barrier(mem_flags::mem_threadgroup);
  552. }
  553. const float variance = sum[0] / ne00;
  554. const float scale = 1.0f/sqrt(variance + eps);
  555. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  556. y[i00] = y[i00] * scale;
  557. }
  558. }
  559. kernel void kernel_rms_norm(
  560. device const void * src0,
  561. device float * dst,
  562. constant int64_t & ne00,
  563. constant uint64_t & nb01,
  564. constant float & eps,
  565. threadgroup float * buf [[threadgroup(0)]],
  566. uint tgpig[[threadgroup_position_in_grid]],
  567. uint tpitg[[thread_position_in_threadgroup]],
  568. uint sgitg[[simdgroup_index_in_threadgroup]],
  569. uint tiisg[[thread_index_in_simdgroup]],
  570. uint ntg[[threads_per_threadgroup]]) {
  571. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  572. float4 sumf = 0;
  573. float all_sum = 0;
  574. // parallel sum
  575. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  576. sumf += x[i00] * x[i00];
  577. }
  578. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  579. all_sum = simd_sum(all_sum);
  580. if (ntg > N_SIMDWIDTH) {
  581. if (sgitg == 0) {
  582. buf[tiisg] = 0.0f;
  583. }
  584. threadgroup_barrier(mem_flags::mem_threadgroup);
  585. if (tiisg == 0) {
  586. buf[sgitg] = all_sum;
  587. }
  588. threadgroup_barrier(mem_flags::mem_threadgroup);
  589. all_sum = buf[tiisg];
  590. all_sum = simd_sum(all_sum);
  591. }
  592. const float mean = all_sum/ne00;
  593. const float scale = 1.0f/sqrt(mean + eps);
  594. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  595. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  596. y[i00] = x[i00] * scale;
  597. }
  598. }
  599. kernel void kernel_group_norm(
  600. device const float * src0,
  601. device float * dst,
  602. constant int64_t & ne00,
  603. constant int64_t & ne01,
  604. constant int64_t & ne02,
  605. constant uint64_t & nb00,
  606. constant uint64_t & nb01,
  607. constant uint64_t & nb02,
  608. constant int32_t & n_groups,
  609. constant float & eps,
  610. threadgroup float * buf [[threadgroup(0)]],
  611. uint tgpig[[threadgroup_position_in_grid]],
  612. uint tpitg[[thread_position_in_threadgroup]],
  613. uint sgitg[[simdgroup_index_in_threadgroup]],
  614. uint tiisg[[thread_index_in_simdgroup]],
  615. uint ntg[[threads_per_threadgroup]]) {
  616. const int64_t ne = ne00*ne01*ne02;
  617. const int64_t gs = ne00*ne01*((ne02 + n_groups - 1) / n_groups);
  618. int start = tgpig * gs;
  619. int end = start + gs;
  620. start += tpitg;
  621. if (end >= ne) {
  622. end = ne;
  623. }
  624. float tmp = 0.0f; // partial sum for thread in warp
  625. for (int j = start; j < end; j += ntg) {
  626. tmp += src0[j];
  627. }
  628. threadgroup_barrier(mem_flags::mem_threadgroup);
  629. tmp = simd_sum(tmp);
  630. if (ntg > N_SIMDWIDTH) {
  631. if (sgitg == 0) {
  632. buf[tiisg] = 0.0f;
  633. }
  634. threadgroup_barrier(mem_flags::mem_threadgroup);
  635. if (tiisg == 0) {
  636. buf[sgitg] = tmp;
  637. }
  638. threadgroup_barrier(mem_flags::mem_threadgroup);
  639. tmp = buf[tiisg];
  640. tmp = simd_sum(tmp);
  641. }
  642. const float mean = tmp / gs;
  643. tmp = 0.0f;
  644. for (int j = start; j < end; j += ntg) {
  645. float xi = src0[j] - mean;
  646. dst[j] = xi;
  647. tmp += xi * xi;
  648. }
  649. tmp = simd_sum(tmp);
  650. if (ntg > N_SIMDWIDTH) {
  651. if (sgitg == 0) {
  652. buf[tiisg] = 0.0f;
  653. }
  654. threadgroup_barrier(mem_flags::mem_threadgroup);
  655. if (tiisg == 0) {
  656. buf[sgitg] = tmp;
  657. }
  658. threadgroup_barrier(mem_flags::mem_threadgroup);
  659. tmp = buf[tiisg];
  660. tmp = simd_sum(tmp);
  661. }
  662. const float variance = tmp / gs;
  663. const float scale = 1.0f/sqrt(variance + eps);
  664. for (int j = start; j < end; j += ntg) {
  665. dst[j] *= scale;
  666. }
  667. }
  668. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  669. // il indicates where the q4 quants begin (0 or QK4_0/4)
  670. // we assume that the yl's have been multiplied with the appropriate scale factor
  671. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  672. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  673. float d = qb_curr->d;
  674. float2 acc = 0.f;
  675. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 1 + il/2);
  676. for (int i = 0; i < 8; i+=2) {
  677. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  678. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  679. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  680. + yl[i + 9] * (qs[i / 2] & 0xF000);
  681. }
  682. return d * (sumy * -8.f + acc[0] + acc[1]);
  683. }
  684. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  685. // il indicates where the q4 quants begin (0 or QK4_0/4)
  686. // we assume that the yl's have been multiplied with the appropriate scale factor
  687. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  688. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  689. float d = qb_curr->d;
  690. float m = qb_curr->m;
  691. float2 acc = 0.f;
  692. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  693. for (int i = 0; i < 8; i+=2) {
  694. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  695. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  696. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  697. + yl[i + 9] * (qs[i / 2] & 0xF000);
  698. }
  699. return d * (acc[0] + acc[1]) + sumy * m;
  700. }
  701. // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i])
  702. // il indicates where the q5 quants begin (0 or QK5_0/4)
  703. // we assume that the yl's have been multiplied with the appropriate scale factor
  704. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  705. inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) {
  706. float d = qb_curr->d;
  707. float2 acc = 0.f;
  708. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2);
  709. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  710. for (int i = 0; i < 8; i+=2) {
  711. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  712. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  713. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  714. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  715. }
  716. return d * (sumy * -16.f + acc[0] + acc[1]);
  717. }
  718. // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i])
  719. // il indicates where the q5 quants begin (0 or QK5_1/4)
  720. // we assume that the yl's have been multiplied with the appropriate scale factor
  721. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  722. inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) {
  723. float d = qb_curr->d;
  724. float m = qb_curr->m;
  725. float2 acc = 0.f;
  726. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2);
  727. const uint32_t qh = *((device const uint32_t *)qb_curr->qh);
  728. for (int i = 0; i < 8; i+=2) {
  729. acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010))
  730. + yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000));
  731. acc[1] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100))
  732. + yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000));
  733. }
  734. return d * (acc[0] + acc[1]) + sumy * m;
  735. }
  736. // putting them in the kernel cause a significant performance penalty
  737. #define N_DST 4 // each SIMD group works on 4 rows
  738. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  739. //Note: This is a template, but strictly speaking it only applies to
  740. // quantizations where the block size is 32. It also does not
  741. // guard against the number of rows not being divisible by
  742. // N_DST, so this is another explicit assumption of the implementation.
  743. template<typename block_q_type, int nr, int nsg, int nw>
  744. void mul_vec_q_n_f32_impl(
  745. device const void * src0,
  746. device const float * src1,
  747. device float * dst,
  748. int64_t ne00,
  749. int64_t ne01,
  750. int64_t ne02,
  751. int64_t ne10,
  752. int64_t ne12,
  753. int64_t ne0,
  754. int64_t ne1,
  755. uint r2,
  756. uint r3,
  757. uint3 tgpig, uint tiisg, uint sgitg) {
  758. const int nb = ne00/QK4_0;
  759. const int r0 = tgpig.x;
  760. const int r1 = tgpig.y;
  761. const int im = tgpig.z;
  762. const int first_row = (r0 * nsg + sgitg) * nr;
  763. const uint i12 = im%ne12;
  764. const uint i13 = im/ne12;
  765. const uint offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  766. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  767. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  768. float yl[16]; // src1 vector cache
  769. float sumf[nr] = {0.f};
  770. const int ix = (tiisg/2);
  771. const int il = (tiisg%2)*8;
  772. device const float * yb = y + ix * QK4_0 + il;
  773. // each thread in a SIMD group deals with half a block.
  774. for (int ib = ix; ib < nb; ib += nw/2) {
  775. float sumy = 0;
  776. for (int i = 0; i < 8; i += 2) {
  777. sumy += yb[i] + yb[i+1];
  778. yl[i+0] = yb[i+ 0];
  779. yl[i+1] = yb[i+ 1]/256.f;
  780. sumy += yb[i+16] + yb[i+17];
  781. yl[i+8] = yb[i+16]/16.f;
  782. yl[i+9] = yb[i+17]/4096.f;
  783. }
  784. for (int row = 0; row < nr; row++) {
  785. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  786. }
  787. yb += QK4_0 * 16;
  788. }
  789. for (int row = 0; row < nr; ++row) {
  790. const float tot = simd_sum(sumf[row]);
  791. if (tiisg == 0 && first_row + row < ne01) {
  792. dst[im*ne0*ne1 + r1*ne0 + first_row + row] = tot;
  793. }
  794. }
  795. }
  796. kernel void kernel_mul_mv_q4_0_f32(
  797. device const void * src0,
  798. device const float * src1,
  799. device float * dst,
  800. constant int64_t & ne00,
  801. constant int64_t & ne01,
  802. constant int64_t & ne02,
  803. constant uint64_t & nb00,
  804. constant uint64_t & nb01,
  805. constant uint64_t & nb02,
  806. constant int64_t & ne10,
  807. constant int64_t & ne11,
  808. constant int64_t & ne12,
  809. constant uint64_t & nb10,
  810. constant uint64_t & nb11,
  811. constant uint64_t & nb12,
  812. constant int64_t & ne0,
  813. constant int64_t & ne1,
  814. constant uint & r2,
  815. constant uint & r3,
  816. uint3 tgpig[[threadgroup_position_in_grid]],
  817. uint tiisg[[thread_index_in_simdgroup]],
  818. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  819. 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,tgpig,tiisg,sgitg);
  820. }
  821. kernel void kernel_mul_mv_q4_1_f32(
  822. device const void * src0,
  823. device const float * src1,
  824. device float * dst,
  825. constant int64_t & ne00,
  826. constant int64_t & ne01,
  827. constant int64_t & ne02,
  828. constant uint64_t & nb00,
  829. constant uint64_t & nb01,
  830. constant uint64_t & nb02,
  831. constant int64_t & ne10,
  832. constant int64_t & ne11,
  833. constant int64_t & ne12,
  834. constant uint64_t & nb10,
  835. constant uint64_t & nb11,
  836. constant uint64_t & nb12,
  837. constant int64_t & ne0,
  838. constant int64_t & ne1,
  839. constant uint & r2,
  840. constant uint & r3,
  841. uint3 tgpig[[threadgroup_position_in_grid]],
  842. uint tiisg[[thread_index_in_simdgroup]],
  843. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  844. 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,tgpig,tiisg,sgitg);
  845. }
  846. kernel void kernel_mul_mv_q5_0_f32(
  847. device const void * src0,
  848. device const float * src1,
  849. device float * dst,
  850. constant int64_t & ne00,
  851. constant int64_t & ne01,
  852. constant int64_t & ne02,
  853. constant uint64_t & nb00,
  854. constant uint64_t & nb01,
  855. constant uint64_t & nb02,
  856. constant int64_t & ne10,
  857. constant int64_t & ne11,
  858. constant int64_t & ne12,
  859. constant uint64_t & nb10,
  860. constant uint64_t & nb11,
  861. constant uint64_t & nb12,
  862. constant int64_t & ne0,
  863. constant int64_t & ne1,
  864. constant uint & r2,
  865. constant uint & r3,
  866. uint3 tgpig[[threadgroup_position_in_grid]],
  867. uint tiisg[[thread_index_in_simdgroup]],
  868. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  869. 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,tgpig,tiisg,sgitg);
  870. }
  871. kernel void kernel_mul_mv_q5_1_f32(
  872. device const void * src0,
  873. device const float * src1,
  874. device float * dst,
  875. constant int64_t & ne00,
  876. constant int64_t & ne01,
  877. constant int64_t & ne02,
  878. constant uint64_t & nb00,
  879. constant uint64_t & nb01,
  880. constant uint64_t & nb02,
  881. constant int64_t & ne10,
  882. constant int64_t & ne11,
  883. constant int64_t & ne12,
  884. constant uint64_t & nb10,
  885. constant uint64_t & nb11,
  886. constant uint64_t & nb12,
  887. constant int64_t & ne0,
  888. constant int64_t & ne1,
  889. constant uint & r2,
  890. constant uint & r3,
  891. uint3 tgpig[[threadgroup_position_in_grid]],
  892. uint tiisg[[thread_index_in_simdgroup]],
  893. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  894. 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,tgpig,tiisg,sgitg);
  895. }
  896. #define NB_Q8_0 8
  897. void kernel_mul_mv_q8_0_f32_impl(
  898. device const void * src0,
  899. device const float * src1,
  900. device float * dst,
  901. constant int64_t & ne00,
  902. constant int64_t & ne01,
  903. constant int64_t & ne02,
  904. constant int64_t & ne10,
  905. constant int64_t & ne12,
  906. constant int64_t & ne0,
  907. constant int64_t & ne1,
  908. constant uint & r2,
  909. constant uint & r3,
  910. uint3 tgpig[[threadgroup_position_in_grid]],
  911. uint tiisg[[thread_index_in_simdgroup]],
  912. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  913. const int nr = N_DST;
  914. const int nsg = N_SIMDGROUP;
  915. const int nw = N_SIMDWIDTH;
  916. const int nb = ne00/QK8_0;
  917. const int r0 = tgpig.x;
  918. const int r1 = tgpig.y;
  919. const int im = tgpig.z;
  920. const int first_row = (r0 * nsg + sgitg) * nr;
  921. const uint i12 = im%ne12;
  922. const uint i13 = im/ne12;
  923. const uint offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  924. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  925. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  926. float yl[NB_Q8_0];
  927. float sumf[nr]={0.f};
  928. const int ix = tiisg/4;
  929. const int il = tiisg%4;
  930. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  931. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  932. for (int ib = ix; ib < nb; ib += nw/4) {
  933. for (int i = 0; i < NB_Q8_0; ++i) {
  934. yl[i] = yb[i];
  935. }
  936. for (int row = 0; row < nr; row++) {
  937. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  938. float sumq = 0.f;
  939. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  940. sumq += qs[iq] * yl[iq];
  941. }
  942. sumf[row] += sumq*x[ib+row*nb].d;
  943. }
  944. yb += NB_Q8_0 * nw;
  945. }
  946. for (int row = 0; row < nr; ++row) {
  947. const float tot = simd_sum(sumf[row]);
  948. if (tiisg == 0 && first_row + row < ne01) {
  949. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  950. }
  951. }
  952. }
  953. [[host_name("kernel_mul_mv_q8_0_f32")]]
  954. kernel void kernel_mul_mv_q8_0_f32(
  955. device const void * src0,
  956. device const float * src1,
  957. device float * dst,
  958. constant int64_t & ne00,
  959. constant int64_t & ne01,
  960. constant int64_t & ne02,
  961. constant uint64_t & nb00,
  962. constant uint64_t & nb01,
  963. constant uint64_t & nb02,
  964. constant int64_t & ne10,
  965. constant int64_t & ne11,
  966. constant int64_t & ne12,
  967. constant uint64_t & nb10,
  968. constant uint64_t & nb11,
  969. constant uint64_t & nb12,
  970. constant int64_t & ne0,
  971. constant int64_t & ne1,
  972. constant uint & r2,
  973. constant uint & r3,
  974. uint3 tgpig[[threadgroup_position_in_grid]],
  975. uint tiisg[[thread_index_in_simdgroup]],
  976. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  977. kernel_mul_mv_q8_0_f32_impl(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,r2,r3,tgpig,tiisg,sgitg);
  978. }
  979. #define N_F32_F32 4
  980. void kernel_mul_mv_f32_f32_impl(
  981. device const char * src0,
  982. device const char * src1,
  983. device float * dst,
  984. constant int64_t & ne00,
  985. constant int64_t & ne01,
  986. constant int64_t & ne02,
  987. constant uint64_t & nb00,
  988. constant uint64_t & nb01,
  989. constant uint64_t & nb02,
  990. constant int64_t & ne10,
  991. constant int64_t & ne11,
  992. constant int64_t & ne12,
  993. constant uint64_t & nb10,
  994. constant uint64_t & nb11,
  995. constant uint64_t & nb12,
  996. constant int64_t & ne0,
  997. constant int64_t & ne1,
  998. constant uint & r2,
  999. constant uint & r3,
  1000. uint3 tgpig[[threadgroup_position_in_grid]],
  1001. uint tiisg[[thread_index_in_simdgroup]]) {
  1002. const int64_t r0 = tgpig.x;
  1003. const int64_t rb = tgpig.y*N_F32_F32;
  1004. const int64_t im = tgpig.z;
  1005. const uint i12 = im%ne12;
  1006. const uint i13 = im/ne12;
  1007. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1008. device const float * x = (device const float *) (src0 + offset0);
  1009. if (ne00 < 128) {
  1010. for (int row = 0; row < N_F32_F32; ++row) {
  1011. int r1 = rb + row;
  1012. if (r1 >= ne11) {
  1013. break;
  1014. }
  1015. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1016. float sumf = 0;
  1017. for (int i = tiisg; i < ne00; i += 32) {
  1018. sumf += (float) x[i] * (float) y[i];
  1019. }
  1020. float all_sum = simd_sum(sumf);
  1021. if (tiisg == 0) {
  1022. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1023. }
  1024. }
  1025. } else {
  1026. device const float4 * x4 = (device const float4 *)x;
  1027. for (int row = 0; row < N_F32_F32; ++row) {
  1028. int r1 = rb + row;
  1029. if (r1 >= ne11) {
  1030. break;
  1031. }
  1032. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1033. device const float4 * y4 = (device const float4 *) y;
  1034. float sumf = 0;
  1035. for (int i = tiisg; i < ne00/4; i += 32) {
  1036. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1037. }
  1038. float all_sum = simd_sum(sumf);
  1039. if (tiisg == 0) {
  1040. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1041. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. [[host_name("kernel_mul_mv_f32_f32")]]
  1047. kernel void kernel_mul_mv_f32_f32(
  1048. device const char * src0,
  1049. device const char * src1,
  1050. device float * dst,
  1051. constant int64_t & ne00,
  1052. constant int64_t & ne01,
  1053. constant int64_t & ne02,
  1054. constant uint64_t & nb00,
  1055. constant uint64_t & nb01,
  1056. constant uint64_t & nb02,
  1057. constant int64_t & ne10,
  1058. constant int64_t & ne11,
  1059. constant int64_t & ne12,
  1060. constant uint64_t & nb10,
  1061. constant uint64_t & nb11,
  1062. constant uint64_t & nb12,
  1063. constant int64_t & ne0,
  1064. constant int64_t & ne1,
  1065. constant uint & r2,
  1066. constant uint & r3,
  1067. uint3 tgpig[[threadgroup_position_in_grid]],
  1068. uint tiisg[[thread_index_in_simdgroup]]) {
  1069. 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);
  1070. }
  1071. #define N_F16_F16 4
  1072. kernel void kernel_mul_mv_f16_f16(
  1073. device const char * src0,
  1074. device const char * src1,
  1075. device float * dst,
  1076. constant int64_t & ne00,
  1077. constant int64_t & ne01,
  1078. constant int64_t & ne02,
  1079. constant uint64_t & nb00,
  1080. constant uint64_t & nb01,
  1081. constant uint64_t & nb02,
  1082. constant int64_t & ne10,
  1083. constant int64_t & ne11,
  1084. constant int64_t & ne12,
  1085. constant uint64_t & nb10,
  1086. constant uint64_t & nb11,
  1087. constant uint64_t & nb12,
  1088. constant int64_t & ne0,
  1089. constant int64_t & ne1,
  1090. constant uint & r2,
  1091. constant uint & r3,
  1092. uint3 tgpig[[threadgroup_position_in_grid]],
  1093. uint tiisg[[thread_index_in_simdgroup]]) {
  1094. const int64_t r0 = tgpig.x;
  1095. const int64_t rb = tgpig.y*N_F16_F16;
  1096. const int64_t im = tgpig.z;
  1097. const uint i12 = im%ne12;
  1098. const uint i13 = im/ne12;
  1099. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1100. device const half * x = (device const half *) (src0 + offset0);
  1101. if (ne00 < 128) {
  1102. for (int row = 0; row < N_F16_F16; ++row) {
  1103. int r1 = rb + row;
  1104. if (r1 >= ne11) {
  1105. break;
  1106. }
  1107. device const half * y = (device const half *) (src1 + r1*nb11 + im*nb12);
  1108. float sumf = 0;
  1109. for (int i = tiisg; i < ne00; i += 32) {
  1110. sumf += (half) x[i] * (half) y[i];
  1111. }
  1112. float all_sum = simd_sum(sumf);
  1113. if (tiisg == 0) {
  1114. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1115. }
  1116. }
  1117. } else {
  1118. device const half4 * x4 = (device const half4 *)x;
  1119. for (int row = 0; row < N_F16_F16; ++row) {
  1120. int r1 = rb + row;
  1121. if (r1 >= ne11) {
  1122. break;
  1123. }
  1124. device const half * y = (device const half *) (src1 + r1*nb11 + im*nb12);
  1125. device const half4 * y4 = (device const half4 *) y;
  1126. float sumf = 0;
  1127. for (int i = tiisg; i < ne00/4; i += 32) {
  1128. for (int k = 0; k < 4; ++k) sumf += (half) x4[i][k] * y4[i][k];
  1129. }
  1130. float all_sum = simd_sum(sumf);
  1131. if (tiisg == 0) {
  1132. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (half) x[i] * y[i];
  1133. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. void kernel_mul_mv_f16_f32_1row_impl(
  1139. device const char * src0,
  1140. device const char * src1,
  1141. device float * dst,
  1142. constant int64_t & ne00,
  1143. constant int64_t & ne01,
  1144. constant int64_t & ne02,
  1145. constant uint64_t & nb00,
  1146. constant uint64_t & nb01,
  1147. constant uint64_t & nb02,
  1148. constant int64_t & ne10,
  1149. constant int64_t & ne11,
  1150. constant int64_t & ne12,
  1151. constant uint64_t & nb10,
  1152. constant uint64_t & nb11,
  1153. constant uint64_t & nb12,
  1154. constant int64_t & ne0,
  1155. constant int64_t & ne1,
  1156. constant uint & r2,
  1157. constant uint & r3,
  1158. uint3 tgpig[[threadgroup_position_in_grid]],
  1159. uint tiisg[[thread_index_in_simdgroup]]) {
  1160. const int64_t r0 = tgpig.x;
  1161. const int64_t r1 = tgpig.y;
  1162. const int64_t im = tgpig.z;
  1163. const uint i12 = im%ne12;
  1164. const uint i13 = im/ne12;
  1165. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1166. device const half * x = (device const half *) (src0 + offset0);
  1167. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1168. float sumf = 0;
  1169. if (ne00 < 128) {
  1170. for (int i = tiisg; i < ne00; i += 32) {
  1171. sumf += (float) x[i] * (float) y[i];
  1172. }
  1173. float all_sum = simd_sum(sumf);
  1174. if (tiisg == 0) {
  1175. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1176. }
  1177. } else {
  1178. device const half4 * x4 = (device const half4 *) x;
  1179. device const float4 * y4 = (device const float4 *) y;
  1180. for (int i = tiisg; i < ne00/4; i += 32) {
  1181. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  1182. }
  1183. float all_sum = simd_sum(sumf);
  1184. if (tiisg == 0) {
  1185. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1186. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1187. }
  1188. }
  1189. }
  1190. [[host_name("kernel_mul_mv_f16_f32_1row")]]
  1191. kernel void kernel_mul_mv_f16_f32_1row(
  1192. device const char * src0,
  1193. device const char * src1,
  1194. device float * dst,
  1195. constant int64_t & ne00,
  1196. constant int64_t & ne01,
  1197. constant int64_t & ne02,
  1198. constant uint64_t & nb00,
  1199. constant uint64_t & nb01,
  1200. constant uint64_t & nb02,
  1201. constant int64_t & ne10,
  1202. constant int64_t & ne11,
  1203. constant int64_t & ne12,
  1204. constant uint64_t & nb10,
  1205. constant uint64_t & nb11,
  1206. constant uint64_t & nb12,
  1207. constant int64_t & ne0,
  1208. constant int64_t & ne1,
  1209. constant uint & r2,
  1210. constant uint & r3,
  1211. uint3 tgpig[[threadgroup_position_in_grid]],
  1212. uint tiisg[[thread_index_in_simdgroup]]) {
  1213. 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);
  1214. }
  1215. #define N_F16_F32 4
  1216. void kernel_mul_mv_f16_f32_impl(
  1217. device const char * src0,
  1218. device const char * src1,
  1219. device float * dst,
  1220. constant int64_t & ne00,
  1221. constant int64_t & ne01,
  1222. constant int64_t & ne02,
  1223. constant uint64_t & nb00,
  1224. constant uint64_t & nb01,
  1225. constant uint64_t & nb02,
  1226. constant int64_t & ne10,
  1227. constant int64_t & ne11,
  1228. constant int64_t & ne12,
  1229. constant uint64_t & nb10,
  1230. constant uint64_t & nb11,
  1231. constant uint64_t & nb12,
  1232. constant int64_t & ne0,
  1233. constant int64_t & ne1,
  1234. constant uint & r2,
  1235. constant uint & r3,
  1236. uint3 tgpig[[threadgroup_position_in_grid]],
  1237. uint tiisg[[thread_index_in_simdgroup]]) {
  1238. const int64_t r0 = tgpig.x;
  1239. const int64_t rb = tgpig.y*N_F16_F32;
  1240. const int64_t im = tgpig.z;
  1241. const uint i12 = im%ne12;
  1242. const uint i13 = im/ne12;
  1243. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1244. device const half * x = (device const half *) (src0 + offset0);
  1245. if (ne00 < 128) {
  1246. for (int row = 0; row < N_F16_F32; ++row) {
  1247. int r1 = rb + row;
  1248. if (r1 >= ne11) {
  1249. break;
  1250. }
  1251. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1252. float sumf = 0;
  1253. for (int i = tiisg; i < ne00; i += 32) {
  1254. sumf += (float) x[i] * (float) y[i];
  1255. }
  1256. float all_sum = simd_sum(sumf);
  1257. if (tiisg == 0) {
  1258. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1259. }
  1260. }
  1261. } else {
  1262. device const half4 * x4 = (device const half4 *)x;
  1263. for (int row = 0; row < N_F16_F32; ++row) {
  1264. int r1 = rb + row;
  1265. if (r1 >= ne11) {
  1266. break;
  1267. }
  1268. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  1269. device const float4 * y4 = (device const float4 *) y;
  1270. float sumf = 0;
  1271. for (int i = tiisg; i < ne00/4; i += 32) {
  1272. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1273. }
  1274. float all_sum = simd_sum(sumf);
  1275. if (tiisg == 0) {
  1276. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  1277. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1278. }
  1279. }
  1280. }
  1281. }
  1282. [[host_name("kernel_mul_mv_f16_f32")]]
  1283. kernel void kernel_mul_mv_f16_f32(
  1284. device const char * src0,
  1285. device const char * src1,
  1286. device float * dst,
  1287. constant int64_t & ne00,
  1288. constant int64_t & ne01,
  1289. constant int64_t & ne02,
  1290. constant uint64_t & nb00,
  1291. constant uint64_t & nb01,
  1292. constant uint64_t & nb02,
  1293. constant int64_t & ne10,
  1294. constant int64_t & ne11,
  1295. constant int64_t & ne12,
  1296. constant uint64_t & nb10,
  1297. constant uint64_t & nb11,
  1298. constant uint64_t & nb12,
  1299. constant int64_t & ne0,
  1300. constant int64_t & ne1,
  1301. constant uint & r2,
  1302. constant uint & r3,
  1303. uint3 tgpig[[threadgroup_position_in_grid]],
  1304. uint tiisg[[thread_index_in_simdgroup]]) {
  1305. 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);
  1306. }
  1307. // Assumes row size (ne00) is a multiple of 4
  1308. kernel void kernel_mul_mv_f16_f32_l4(
  1309. device const char * src0,
  1310. device const char * src1,
  1311. device float * dst,
  1312. constant int64_t & ne00,
  1313. constant int64_t & ne01,
  1314. constant int64_t & ne02,
  1315. constant uint64_t & nb00,
  1316. constant uint64_t & nb01,
  1317. constant uint64_t & nb02,
  1318. constant int64_t & ne10,
  1319. constant int64_t & ne11,
  1320. constant int64_t & ne12,
  1321. constant uint64_t & nb10,
  1322. constant uint64_t & nb11,
  1323. constant uint64_t & nb12,
  1324. constant int64_t & ne0,
  1325. constant int64_t & ne1,
  1326. constant uint & r2,
  1327. constant uint & r3,
  1328. uint3 tgpig[[threadgroup_position_in_grid]],
  1329. uint tiisg[[thread_index_in_simdgroup]]) {
  1330. const int nrows = ne11;
  1331. const int64_t r0 = tgpig.x;
  1332. const int64_t im = tgpig.z;
  1333. const uint i12 = im%ne12;
  1334. const uint i13 = im/ne12;
  1335. const uint offset0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb02*ne02;
  1336. device const half4 * x4 = (device const half4 *) (src0 + offset0);
  1337. for (int r1 = 0; r1 < nrows; ++r1) {
  1338. device const float4 * y4 = (device const float4 *) (src1 + r1*nb11 + im*nb12);
  1339. float sumf = 0;
  1340. for (int i = tiisg; i < ne00/4; i += 32) {
  1341. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  1342. }
  1343. float all_sum = simd_sum(sumf);
  1344. if (tiisg == 0) {
  1345. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  1346. }
  1347. }
  1348. }
  1349. kernel void kernel_alibi_f32(
  1350. device const float * src0,
  1351. device float * dst,
  1352. constant int64_t & ne00,
  1353. constant int64_t & ne01,
  1354. constant int64_t & ne02,
  1355. constant int64_t & ne03,
  1356. constant uint64_t & nb00,
  1357. constant uint64_t & nb01,
  1358. constant uint64_t & nb02,
  1359. constant uint64_t & nb03,
  1360. constant int64_t & ne0,
  1361. constant int64_t & ne1,
  1362. constant int64_t & ne2,
  1363. constant int64_t & ne3,
  1364. constant uint64_t & nb0,
  1365. constant uint64_t & nb1,
  1366. constant uint64_t & nb2,
  1367. constant uint64_t & nb3,
  1368. constant float & m0,
  1369. constant float & m1,
  1370. constant int & n_heads_log2_floor,
  1371. uint3 tgpig[[threadgroup_position_in_grid]],
  1372. uint3 tpitg[[thread_position_in_threadgroup]],
  1373. uint3 ntg[[threads_per_threadgroup]]) {
  1374. const int64_t i03 = tgpig[2];
  1375. const int64_t i02 = tgpig[1];
  1376. const int64_t i01 = tgpig[0];
  1377. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1378. const int64_t i3 = n / (ne2*ne1*ne0);
  1379. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1380. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1381. //const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1382. const int64_t k = i3*ne3 + i2;
  1383. float m_k;
  1384. if (k < n_heads_log2_floor) {
  1385. m_k = pow(m0, k + 1);
  1386. } else {
  1387. m_k = pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  1388. }
  1389. device char * dst_row = (device char *) dst + i3*nb3 + i2*nb2 + i1*nb1;
  1390. device const char * src_row = (device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01;
  1391. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1392. const float src_v = *(device float *)(src_row + i00*nb00);
  1393. device float * dst_v = (device float *)(dst_row + i00*nb0);
  1394. *dst_v = i00 * m_k + src_v;
  1395. }
  1396. }
  1397. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  1398. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  1399. return 1.0f - min(1.0f, max(0.0f, y));
  1400. }
  1401. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  1402. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  1403. static void rope_yarn(
  1404. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  1405. thread float * cos_theta, thread float * sin_theta
  1406. ) {
  1407. // Get n-d rotational scaling corrected for extrapolation
  1408. float theta_interp = freq_scale * theta_extrap;
  1409. float theta = theta_interp;
  1410. if (ext_factor != 0.0f) {
  1411. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  1412. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  1413. // Get n-d magnitude scaling corrected for interpolation
  1414. mscale *= 1.0f + 0.1f * log(1.0f / freq_scale);
  1415. }
  1416. *cos_theta = cos(theta) * mscale;
  1417. *sin_theta = sin(theta) * mscale;
  1418. }
  1419. // Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)` for x, we get
  1420. // `corr_fac(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`
  1421. static float rope_yarn_corr_factor(int n_dims, int n_orig_ctx, float n_rot, float base) {
  1422. return n_dims * log(n_orig_ctx / (n_rot * 2 * M_PI_F)) / (2 * log(base));
  1423. }
  1424. static void rope_yarn_corr_dims(
  1425. int n_dims, int n_orig_ctx, float freq_base, float beta_fast, float beta_slow, float dims[2]
  1426. ) {
  1427. // start and end correction dims
  1428. dims[0] = max(0.0f, floor(rope_yarn_corr_factor(n_dims, n_orig_ctx, beta_fast, freq_base)));
  1429. dims[1] = min(n_dims - 1.0f, ceil(rope_yarn_corr_factor(n_dims, n_orig_ctx, beta_slow, freq_base)));
  1430. }
  1431. typedef void (rope_t)(
  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. template<typename T>
  1465. kernel void kernel_rope(
  1466. device const void * src0,
  1467. device const int32_t * src1,
  1468. device float * dst,
  1469. constant int64_t & ne00,
  1470. constant int64_t & ne01,
  1471. constant int64_t & ne02,
  1472. constant int64_t & ne03,
  1473. constant uint64_t & nb00,
  1474. constant uint64_t & nb01,
  1475. constant uint64_t & nb02,
  1476. constant uint64_t & nb03,
  1477. constant int64_t & ne0,
  1478. constant int64_t & ne1,
  1479. constant int64_t & ne2,
  1480. constant int64_t & ne3,
  1481. constant uint64_t & nb0,
  1482. constant uint64_t & nb1,
  1483. constant uint64_t & nb2,
  1484. constant uint64_t & nb3,
  1485. constant int & n_past,
  1486. constant int & n_dims,
  1487. constant int & mode,
  1488. constant int & n_orig_ctx,
  1489. constant float & freq_base,
  1490. constant float & freq_scale,
  1491. constant float & ext_factor,
  1492. constant float & attn_factor,
  1493. constant float & beta_fast,
  1494. constant float & beta_slow,
  1495. uint tiitg[[thread_index_in_threadgroup]],
  1496. uint3 tptg[[threads_per_threadgroup]],
  1497. uint3 tgpig[[threadgroup_position_in_grid]]) {
  1498. const int64_t i3 = tgpig[2];
  1499. const int64_t i2 = tgpig[1];
  1500. const int64_t i1 = tgpig[0];
  1501. const bool is_neox = mode & 2;
  1502. float corr_dims[2];
  1503. rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims);
  1504. device const int32_t * pos = src1;
  1505. const int64_t p = pos[i2];
  1506. const float theta_0 = (float)p;
  1507. const float inv_ndims = -1.f/n_dims;
  1508. if (!is_neox) {
  1509. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  1510. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  1511. float cos_theta, sin_theta;
  1512. rope_yarn(theta, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1513. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1514. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1515. const T x0 = src[0];
  1516. const T x1 = src[1];
  1517. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1518. dst_data[1] = x0*sin_theta + x1*cos_theta;
  1519. }
  1520. } else {
  1521. for (int64_t ic = 2*tiitg; ic < ne0; ic += 2*tptg.x) {
  1522. if (ic < n_dims) {
  1523. const int64_t ib = 0;
  1524. // simplified from `(ib * n_dims + ic) * inv_ndims`
  1525. const float cur_rot = inv_ndims*ic - ib;
  1526. const float theta = theta_0 * pow(freq_base, cur_rot);
  1527. float cos_theta, sin_theta;
  1528. rope_yarn(theta, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  1529. const int64_t i0 = ib*n_dims + ic/2;
  1530. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1531. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1532. const float x0 = src[0];
  1533. const float x1 = src[n_dims/2];
  1534. dst_data[0] = x0*cos_theta - x1*sin_theta;
  1535. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  1536. } else {
  1537. const int64_t i0 = ic;
  1538. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  1539. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1540. dst_data[0] = src[0];
  1541. dst_data[1] = src[1];
  1542. }
  1543. }
  1544. }
  1545. }
  1546. template [[host_name("kernel_rope_f32")]] kernel rope_t kernel_rope<float>;
  1547. template [[host_name("kernel_rope_f16")]] kernel rope_t kernel_rope<half>;
  1548. typedef void (im2col_t)(
  1549. device const float * x,
  1550. device char * dst,
  1551. constant int32_t & ofs0,
  1552. constant int32_t & ofs1,
  1553. constant int32_t & IW,
  1554. constant int32_t & IH,
  1555. constant int32_t & CHW,
  1556. constant int32_t & s0,
  1557. constant int32_t & s1,
  1558. constant int32_t & p0,
  1559. constant int32_t & p1,
  1560. constant int32_t & d0,
  1561. constant int32_t & d1,
  1562. uint3 tgpig[[threadgroup_position_in_grid]],
  1563. uint3 tgpg[[threadgroups_per_grid]],
  1564. uint3 tpitg[[thread_position_in_threadgroup]],
  1565. uint3 ntg[[threads_per_threadgroup]]);
  1566. template <typename T>
  1567. kernel void kernel_im2col(
  1568. device const float * x,
  1569. device char * dst,
  1570. constant int32_t & ofs0,
  1571. constant int32_t & ofs1,
  1572. constant int32_t & IW,
  1573. constant int32_t & IH,
  1574. constant int32_t & CHW,
  1575. constant int32_t & s0,
  1576. constant int32_t & s1,
  1577. constant int32_t & p0,
  1578. constant int32_t & p1,
  1579. constant int32_t & d0,
  1580. constant int32_t & d1,
  1581. uint3 tgpig[[threadgroup_position_in_grid]],
  1582. uint3 tgpg[[threadgroups_per_grid]],
  1583. uint3 tpitg[[thread_position_in_threadgroup]],
  1584. uint3 ntg[[threads_per_threadgroup]]) {
  1585. const int32_t iiw = tgpig[2] * s0 + tpitg[2] * d0 - p0;
  1586. const int32_t iih = tgpig[1] * s1 + tpitg[1] * d1 - p1;
  1587. const int32_t offset_dst =
  1588. (tpitg[0] * tgpg[1] * tgpg[2] + tgpig[1] * tgpg[2] + tgpig[2]) * CHW +
  1589. (tgpig[0] * (ntg[1] * ntg[2]) + tpitg[1] * ntg[2] + tpitg[2]);
  1590. device T * pdst = (device T *) (dst);
  1591. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  1592. pdst[offset_dst] = 0.0f;
  1593. } else {
  1594. const int32_t offset_src = tpitg[0] * ofs0 + tgpig[0] * ofs1;
  1595. pdst[offset_dst] = x[offset_src + iih * IW + iiw];
  1596. }
  1597. }
  1598. template [[host_name("kernel_im2col_f32")]] kernel im2col_t kernel_im2col<float>;
  1599. template [[host_name("kernel_im2col_f16")]] kernel im2col_t kernel_im2col<half>;
  1600. kernel void kernel_upscale_f32(
  1601. device const char * src0,
  1602. device char * dst,
  1603. constant int64_t & ne00,
  1604. constant int64_t & ne01,
  1605. constant int64_t & ne02,
  1606. constant int64_t & ne03,
  1607. constant uint64_t & nb00,
  1608. constant uint64_t & nb01,
  1609. constant uint64_t & nb02,
  1610. constant uint64_t & nb03,
  1611. constant int64_t & ne0,
  1612. constant int64_t & ne1,
  1613. constant int64_t & ne2,
  1614. constant int64_t & ne3,
  1615. constant uint64_t & nb0,
  1616. constant uint64_t & nb1,
  1617. constant uint64_t & nb2,
  1618. constant uint64_t & nb3,
  1619. constant int32_t & sf,
  1620. uint3 tgpig[[threadgroup_position_in_grid]],
  1621. uint3 tpitg[[thread_position_in_threadgroup]],
  1622. uint3 ntg[[threads_per_threadgroup]]) {
  1623. const int64_t i3 = tgpig.z;
  1624. const int64_t i2 = tgpig.y;
  1625. const int64_t i1 = tgpig.x;
  1626. const int64_t i03 = i3;
  1627. const int64_t i02 = i2;
  1628. const int64_t i01 = i1/sf;
  1629. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  1630. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  1631. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1632. dst_ptr[i0] = src0_ptr[i0/sf];
  1633. }
  1634. }
  1635. kernel void kernel_pad_f32(
  1636. device const char * src0,
  1637. device char * dst,
  1638. constant int64_t & ne00,
  1639. constant int64_t & ne01,
  1640. constant int64_t & ne02,
  1641. constant int64_t & ne03,
  1642. constant uint64_t & nb00,
  1643. constant uint64_t & nb01,
  1644. constant uint64_t & nb02,
  1645. constant uint64_t & nb03,
  1646. constant int64_t & ne0,
  1647. constant int64_t & ne1,
  1648. constant int64_t & ne2,
  1649. constant int64_t & ne3,
  1650. constant uint64_t & nb0,
  1651. constant uint64_t & nb1,
  1652. constant uint64_t & nb2,
  1653. constant uint64_t & nb3,
  1654. uint3 tgpig[[threadgroup_position_in_grid]],
  1655. uint3 tpitg[[thread_position_in_threadgroup]],
  1656. uint3 ntg[[threads_per_threadgroup]]) {
  1657. const int64_t i3 = tgpig.z;
  1658. const int64_t i2 = tgpig.y;
  1659. const int64_t i1 = tgpig.x;
  1660. const int64_t i03 = i3;
  1661. const int64_t i02 = i2;
  1662. const int64_t i01 = i1;
  1663. device const float * src0_ptr = (device const float *) (src0 + i03*nb03 + i02*nb02 + i01*nb01);
  1664. device float * dst_ptr = (device float *) (dst + i3*nb3 + i2*nb2 + i1*nb1);
  1665. if (i1 < ne01 && i2 < ne02 && i3 < ne03) {
  1666. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1667. if (i0 < ne00) {
  1668. dst_ptr[i0] = src0_ptr[i0];
  1669. } else {
  1670. dst_ptr[i0] = 0.0f;
  1671. }
  1672. }
  1673. return;
  1674. }
  1675. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  1676. dst_ptr[i0] = 0.0f;
  1677. }
  1678. }
  1679. // bitonic sort implementation following the CUDA kernels as reference
  1680. typedef void (argsort_t)(
  1681. device const float * x,
  1682. device int32_t * dst,
  1683. constant int64_t & ncols,
  1684. uint3 tgpig[[threadgroup_position_in_grid]],
  1685. uint3 tpitg[[thread_position_in_threadgroup]]);
  1686. template<ggml_sort_order order>
  1687. kernel void kernel_argsort_f32_i32(
  1688. device const float * x,
  1689. device int32_t * dst,
  1690. constant int64_t & ncols,
  1691. uint3 tgpig[[threadgroup_position_in_grid]],
  1692. uint3 tpitg[[thread_position_in_threadgroup]]) {
  1693. // bitonic sort
  1694. int col = tpitg[0];
  1695. int row = tgpig[1];
  1696. if (col >= ncols) return;
  1697. device const float * x_row = x + row * ncols;
  1698. device int32_t * dst_row = dst + row * ncols;
  1699. // initialize indices
  1700. if (col < ncols) {
  1701. dst_row[col] = col;
  1702. }
  1703. threadgroup_barrier(mem_flags::mem_threadgroup);
  1704. for (int k = 2; k <= ncols; k *= 2) {
  1705. for (int j = k / 2; j > 0; j /= 2) {
  1706. int ixj = col ^ j;
  1707. if (ixj > col) {
  1708. if ((col & k) == 0) {
  1709. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  1710. SWAP(dst_row[col], dst_row[ixj]);
  1711. }
  1712. } else {
  1713. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  1714. SWAP(dst_row[col], dst_row[ixj]);
  1715. }
  1716. }
  1717. }
  1718. threadgroup_barrier(mem_flags::mem_threadgroup);
  1719. }
  1720. }
  1721. }
  1722. template [[host_name("kernel_argsort_f32_i32_asc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_ASC>;
  1723. template [[host_name("kernel_argsort_f32_i32_desc")]] kernel argsort_t kernel_argsort_f32_i32<GGML_SORT_DESC>;
  1724. kernel void kernel_leaky_relu_f32(
  1725. device const float * src0,
  1726. device float * dst,
  1727. constant float & slope,
  1728. uint tpig[[thread_position_in_grid]]) {
  1729. dst[tpig] = src0[tpig] > 0.0f ? src0[tpig] : src0[tpig] * slope;
  1730. }
  1731. kernel void kernel_cpy_f16_f16(
  1732. device const half * src0,
  1733. device half * dst,
  1734. constant int64_t & ne00,
  1735. constant int64_t & ne01,
  1736. constant int64_t & ne02,
  1737. constant int64_t & ne03,
  1738. constant uint64_t & nb00,
  1739. constant uint64_t & nb01,
  1740. constant uint64_t & nb02,
  1741. constant uint64_t & nb03,
  1742. constant int64_t & ne0,
  1743. constant int64_t & ne1,
  1744. constant int64_t & ne2,
  1745. constant int64_t & ne3,
  1746. constant uint64_t & nb0,
  1747. constant uint64_t & nb1,
  1748. constant uint64_t & nb2,
  1749. constant uint64_t & nb3,
  1750. uint3 tgpig[[threadgroup_position_in_grid]],
  1751. uint3 tpitg[[thread_position_in_threadgroup]],
  1752. uint3 ntg[[threads_per_threadgroup]]) {
  1753. const int64_t i03 = tgpig[2];
  1754. const int64_t i02 = tgpig[1];
  1755. const int64_t i01 = tgpig[0];
  1756. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1757. const int64_t i3 = n / (ne2*ne1*ne0);
  1758. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1759. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1760. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1761. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1762. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1763. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1764. dst_data[i00] = src[0];
  1765. }
  1766. }
  1767. kernel void kernel_cpy_f16_f32(
  1768. device const half * src0,
  1769. device float * dst,
  1770. constant int64_t & ne00,
  1771. constant int64_t & ne01,
  1772. constant int64_t & ne02,
  1773. constant int64_t & ne03,
  1774. constant uint64_t & nb00,
  1775. constant uint64_t & nb01,
  1776. constant uint64_t & nb02,
  1777. constant uint64_t & nb03,
  1778. constant int64_t & ne0,
  1779. constant int64_t & ne1,
  1780. constant int64_t & ne2,
  1781. constant int64_t & ne3,
  1782. constant uint64_t & nb0,
  1783. constant uint64_t & nb1,
  1784. constant uint64_t & nb2,
  1785. constant uint64_t & nb3,
  1786. uint3 tgpig[[threadgroup_position_in_grid]],
  1787. uint3 tpitg[[thread_position_in_threadgroup]],
  1788. uint3 ntg[[threads_per_threadgroup]]) {
  1789. const int64_t i03 = tgpig[2];
  1790. const int64_t i02 = tgpig[1];
  1791. const int64_t i01 = tgpig[0];
  1792. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1793. const int64_t i3 = n / (ne2*ne1*ne0);
  1794. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1795. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1796. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1797. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1798. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1799. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1800. dst_data[i00] = src[0];
  1801. }
  1802. }
  1803. kernel void kernel_cpy_f32_f16(
  1804. device const float * src0,
  1805. device half * dst,
  1806. constant int64_t & ne00,
  1807. constant int64_t & ne01,
  1808. constant int64_t & ne02,
  1809. constant int64_t & ne03,
  1810. constant uint64_t & nb00,
  1811. constant uint64_t & nb01,
  1812. constant uint64_t & nb02,
  1813. constant uint64_t & nb03,
  1814. constant int64_t & ne0,
  1815. constant int64_t & ne1,
  1816. constant int64_t & ne2,
  1817. constant int64_t & ne3,
  1818. constant uint64_t & nb0,
  1819. constant uint64_t & nb1,
  1820. constant uint64_t & nb2,
  1821. constant uint64_t & nb3,
  1822. uint3 tgpig[[threadgroup_position_in_grid]],
  1823. uint3 tpitg[[thread_position_in_threadgroup]],
  1824. uint3 ntg[[threads_per_threadgroup]]) {
  1825. const int64_t i03 = tgpig[2];
  1826. const int64_t i02 = tgpig[1];
  1827. const int64_t i01 = tgpig[0];
  1828. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1829. const int64_t i3 = n / (ne2*ne1*ne0);
  1830. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1831. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1832. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1833. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1834. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1835. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1836. dst_data[i00] = src[0];
  1837. }
  1838. }
  1839. kernel void kernel_cpy_f32_f32(
  1840. device const float * src0,
  1841. device float * dst,
  1842. constant int64_t & ne00,
  1843. constant int64_t & ne01,
  1844. constant int64_t & ne02,
  1845. constant int64_t & ne03,
  1846. constant uint64_t & nb00,
  1847. constant uint64_t & nb01,
  1848. constant uint64_t & nb02,
  1849. constant uint64_t & nb03,
  1850. constant int64_t & ne0,
  1851. constant int64_t & ne1,
  1852. constant int64_t & ne2,
  1853. constant int64_t & ne3,
  1854. constant uint64_t & nb0,
  1855. constant uint64_t & nb1,
  1856. constant uint64_t & nb2,
  1857. constant uint64_t & nb3,
  1858. uint3 tgpig[[threadgroup_position_in_grid]],
  1859. uint3 tpitg[[thread_position_in_threadgroup]],
  1860. uint3 ntg[[threads_per_threadgroup]]) {
  1861. const int64_t i03 = tgpig[2];
  1862. const int64_t i02 = tgpig[1];
  1863. const int64_t i01 = tgpig[0];
  1864. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1865. const int64_t i3 = n / (ne2*ne1*ne0);
  1866. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1867. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1868. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  1869. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1870. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  1871. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1872. dst_data[i00] = src[0];
  1873. }
  1874. }
  1875. kernel void kernel_cpy_f32_q8_0(
  1876. device const float * src0,
  1877. device void * dst,
  1878. constant int64_t & ne00,
  1879. constant int64_t & ne01,
  1880. constant int64_t & ne02,
  1881. constant int64_t & ne03,
  1882. constant uint64_t & nb00,
  1883. constant uint64_t & nb01,
  1884. constant uint64_t & nb02,
  1885. constant uint64_t & nb03,
  1886. constant int64_t & ne0,
  1887. constant int64_t & ne1,
  1888. constant int64_t & ne2,
  1889. constant int64_t & ne3,
  1890. constant uint64_t & nb0,
  1891. constant uint64_t & nb1,
  1892. constant uint64_t & nb2,
  1893. constant uint64_t & nb3,
  1894. uint3 tgpig[[threadgroup_position_in_grid]],
  1895. uint3 tpitg[[thread_position_in_threadgroup]],
  1896. uint3 ntg[[threads_per_threadgroup]]) {
  1897. const int64_t i03 = tgpig[2];
  1898. const int64_t i02 = tgpig[1];
  1899. const int64_t i01 = tgpig[0];
  1900. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1901. const int64_t i3 = n / (ne2*ne1*ne0);
  1902. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1903. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1904. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK8_0;
  1905. device block_q8_0 * dst_data = (device block_q8_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1906. for (int64_t i00 = tpitg.x*QK8_0; i00 < ne00; i00 += ntg.x*QK8_0) {
  1907. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1908. float amax = 0.0f; // absolute max
  1909. for (int j = 0; j < QK8_0; j++) {
  1910. const float v = src[j];
  1911. amax = MAX(amax, fabs(v));
  1912. }
  1913. const float d = amax / ((1 << 7) - 1);
  1914. const float id = d ? 1.0f/d : 0.0f;
  1915. dst_data[i00/QK8_0].d = d;
  1916. for (int j = 0; j < QK8_0; ++j) {
  1917. const float x0 = src[j]*id;
  1918. dst_data[i00/QK8_0].qs[j] = round(x0);
  1919. }
  1920. }
  1921. }
  1922. kernel void kernel_cpy_f32_q4_0(
  1923. device const float * src0,
  1924. device void * dst,
  1925. constant int64_t & ne00,
  1926. constant int64_t & ne01,
  1927. constant int64_t & ne02,
  1928. constant int64_t & ne03,
  1929. constant uint64_t & nb00,
  1930. constant uint64_t & nb01,
  1931. constant uint64_t & nb02,
  1932. constant uint64_t & nb03,
  1933. constant int64_t & ne0,
  1934. constant int64_t & ne1,
  1935. constant int64_t & ne2,
  1936. constant int64_t & ne3,
  1937. constant uint64_t & nb0,
  1938. constant uint64_t & nb1,
  1939. constant uint64_t & nb2,
  1940. constant uint64_t & nb3,
  1941. uint3 tgpig[[threadgroup_position_in_grid]],
  1942. uint3 tpitg[[thread_position_in_threadgroup]],
  1943. uint3 ntg[[threads_per_threadgroup]]) {
  1944. const int64_t i03 = tgpig[2];
  1945. const int64_t i02 = tgpig[1];
  1946. const int64_t i01 = tgpig[0];
  1947. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  1948. const int64_t i3 = n / (ne2*ne1*ne0);
  1949. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  1950. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  1951. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_0;
  1952. device block_q4_0 * dst_data = (device block_q4_0 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  1953. for (int64_t i00 = tpitg.x*QK4_0; i00 < ne00; i00 += ntg.x*QK4_0) {
  1954. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  1955. float amax = 0.0f; // absolute max
  1956. float max = 0.0f;
  1957. for (int j = 0; j < QK4_0; j++) {
  1958. const float v = src[j];
  1959. if (amax < fabs(v)) {
  1960. amax = fabs(v);
  1961. max = v;
  1962. }
  1963. }
  1964. const float d = max / -8;
  1965. const float id = d ? 1.0f/d : 0.0f;
  1966. dst_data[i00/QK4_0].d = d;
  1967. for (int j = 0; j < QK4_0/2; ++j) {
  1968. const float x0 = src[0 + j]*id;
  1969. const float x1 = src[QK4_0/2 + j]*id;
  1970. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  1971. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  1972. dst_data[i00/QK4_0].qs[j] = xi0;
  1973. dst_data[i00/QK4_0].qs[j] |= xi1 << 4;
  1974. }
  1975. }
  1976. }
  1977. kernel void kernel_cpy_f32_q4_1(
  1978. device const float * src0,
  1979. device void * dst,
  1980. constant int64_t & ne00,
  1981. constant int64_t & ne01,
  1982. constant int64_t & ne02,
  1983. constant int64_t & ne03,
  1984. constant uint64_t & nb00,
  1985. constant uint64_t & nb01,
  1986. constant uint64_t & nb02,
  1987. constant uint64_t & nb03,
  1988. constant int64_t & ne0,
  1989. constant int64_t & ne1,
  1990. constant int64_t & ne2,
  1991. constant int64_t & ne3,
  1992. constant uint64_t & nb0,
  1993. constant uint64_t & nb1,
  1994. constant uint64_t & nb2,
  1995. constant uint64_t & nb3,
  1996. uint3 tgpig[[threadgroup_position_in_grid]],
  1997. uint3 tpitg[[thread_position_in_threadgroup]],
  1998. uint3 ntg[[threads_per_threadgroup]]) {
  1999. const int64_t i03 = tgpig[2];
  2000. const int64_t i02 = tgpig[1];
  2001. const int64_t i01 = tgpig[0];
  2002. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  2003. const int64_t i3 = n / (ne2*ne1*ne0);
  2004. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  2005. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  2006. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0)/QK4_1;
  2007. device block_q4_1 * dst_data = (device block_q4_1 *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  2008. for (int64_t i00 = tpitg.x*QK4_1; i00 < ne00; i00 += ntg.x*QK4_1) {
  2009. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  2010. float min = FLT_MAX;
  2011. float max = -FLT_MAX;
  2012. for (int j = 0; j < QK4_1; j++) {
  2013. const float v = src[j];
  2014. if (min > v) min = v;
  2015. if (max < v) max = v;
  2016. }
  2017. const float d = (max - min) / ((1 << 4) - 1);
  2018. const float id = d ? 1.0f/d : 0.0f;
  2019. dst_data[i00/QK4_1].d = d;
  2020. dst_data[i00/QK4_1].m = min;
  2021. for (int j = 0; j < QK4_1/2; ++j) {
  2022. const float x0 = (src[0 + j] - min)*id;
  2023. const float x1 = (src[QK4_1/2 + j] - min)*id;
  2024. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  2025. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  2026. dst_data[i00/QK4_1].qs[j] = xi0;
  2027. dst_data[i00/QK4_1].qs[j] |= xi1 << 4;
  2028. }
  2029. }
  2030. }
  2031. kernel void kernel_concat(
  2032. device const char * src0,
  2033. device const char * src1,
  2034. device char * dst,
  2035. constant int64_t & ne00,
  2036. constant int64_t & ne01,
  2037. constant int64_t & ne02,
  2038. constant int64_t & ne03,
  2039. constant uint64_t & nb00,
  2040. constant uint64_t & nb01,
  2041. constant uint64_t & nb02,
  2042. constant uint64_t & nb03,
  2043. constant int64_t & ne10,
  2044. constant int64_t & ne11,
  2045. constant int64_t & ne12,
  2046. constant int64_t & ne13,
  2047. constant uint64_t & nb10,
  2048. constant uint64_t & nb11,
  2049. constant uint64_t & nb12,
  2050. constant uint64_t & nb13,
  2051. constant int64_t & ne0,
  2052. constant int64_t & ne1,
  2053. constant int64_t & ne2,
  2054. constant int64_t & ne3,
  2055. constant uint64_t & nb0,
  2056. constant uint64_t & nb1,
  2057. constant uint64_t & nb2,
  2058. constant uint64_t & nb3,
  2059. uint3 tgpig[[threadgroup_position_in_grid]],
  2060. uint3 tpitg[[thread_position_in_threadgroup]],
  2061. uint3 ntg[[threads_per_threadgroup]]) {
  2062. const int64_t i03 = tgpig.z;
  2063. const int64_t i02 = tgpig.y;
  2064. const int64_t i01 = tgpig.x;
  2065. const int64_t i13 = i03 % ne13;
  2066. const int64_t i12 = i02 % ne12;
  2067. const int64_t i11 = i01 % ne11;
  2068. device const char * src0_ptr = src0 + i03*nb03 + i02*nb02 + i01*nb01 + tpitg.x*nb00;
  2069. device const char * src1_ptr = src1 + i13*nb13 + i12*nb12 + i11*nb11 + tpitg.x*nb10;
  2070. device char * dst_ptr = dst + i03*nb3 + i02*nb2 + i01*nb1 + tpitg.x*nb0;
  2071. for (int i0 = tpitg.x; i0 < ne0; i0 += ntg.x) {
  2072. if (i02 < ne02) {
  2073. ((device float *)dst_ptr)[0] = ((device float *)src0_ptr)[0];
  2074. src0_ptr += ntg.x*nb00;
  2075. } else {
  2076. ((device float *)dst_ptr)[0] = ((device float *)src1_ptr)[0];
  2077. src1_ptr += ntg.x*nb10;
  2078. }
  2079. dst_ptr += ntg.x*nb0;
  2080. }
  2081. }
  2082. //============================================ k-quants ======================================================
  2083. #ifndef QK_K
  2084. #define QK_K 256
  2085. #else
  2086. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  2087. #endif
  2088. #if QK_K == 256
  2089. #define K_SCALE_SIZE 12
  2090. #else
  2091. #define K_SCALE_SIZE 4
  2092. #endif
  2093. typedef struct {
  2094. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  2095. uint8_t qs[QK_K/4]; // quants
  2096. half d; // super-block scale for quantized scales
  2097. half dmin; // super-block scale for quantized mins
  2098. } block_q2_K;
  2099. // 84 bytes / block
  2100. typedef struct {
  2101. uint8_t hmask[QK_K/8]; // quants - high bit
  2102. uint8_t qs[QK_K/4]; // quants - low 2 bits
  2103. #if QK_K == 64
  2104. uint8_t scales[2];
  2105. #else
  2106. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  2107. #endif
  2108. half d; // super-block scale
  2109. } block_q3_K;
  2110. #if QK_K == 64
  2111. typedef struct {
  2112. half d[2]; // super-block scales/mins
  2113. uint8_t scales[2];
  2114. uint8_t qs[QK_K/2]; // 4-bit quants
  2115. } block_q4_K;
  2116. #else
  2117. typedef struct {
  2118. half d; // super-block scale for quantized scales
  2119. half dmin; // super-block scale for quantized mins
  2120. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  2121. uint8_t qs[QK_K/2]; // 4--bit quants
  2122. } block_q4_K;
  2123. #endif
  2124. #if QK_K == 64
  2125. typedef struct {
  2126. half d; // super-block scales/mins
  2127. int8_t scales[QK_K/16]; // 8-bit block scales
  2128. uint8_t qh[QK_K/8]; // quants, high bit
  2129. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2130. } block_q5_K;
  2131. #else
  2132. typedef struct {
  2133. half d; // super-block scale for quantized scales
  2134. half dmin; // super-block scale for quantized mins
  2135. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  2136. uint8_t qh[QK_K/8]; // quants, high bit
  2137. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2138. } block_q5_K;
  2139. // 176 bytes / block
  2140. #endif
  2141. typedef struct {
  2142. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  2143. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  2144. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  2145. half d; // super-block scale
  2146. } block_q6_K;
  2147. // 210 bytes / block
  2148. typedef struct {
  2149. half d;
  2150. uint16_t qs[QK_K/8];
  2151. } block_iq2_xxs;
  2152. // 66 bytes / block for QK_K = 256, so 2.0625 bpw
  2153. typedef struct {
  2154. half d;
  2155. uint16_t qs[QK_K/8];
  2156. uint8_t scales[QK_K/32];
  2157. } block_iq2_xs;
  2158. // 74 bytes / block for QK_K = 256, so 2.3125 bpw
  2159. // 2.5625 bpw quants
  2160. typedef struct {
  2161. half d;
  2162. uint8_t qs[QK_K/4];
  2163. uint8_t qh[QK_K/32];
  2164. uint8_t scales[QK_K/32];
  2165. } block_iq2_s;
  2166. typedef struct {
  2167. half d;
  2168. uint8_t qs[3*QK_K/8];
  2169. } block_iq3_xxs;
  2170. // 98 bytes / block for QK_K = 256, so 3.0625 bpw
  2171. // 3.4375 bpw
  2172. #if QK_K == 64
  2173. #define IQ3S_N_SCALE 2
  2174. #else
  2175. #define IQ3S_N_SCALE QK_K/64
  2176. #endif
  2177. typedef struct {
  2178. half d;
  2179. uint8_t qs[QK_K/4];
  2180. uint8_t qh[QK_K/32];
  2181. uint8_t signs[QK_K/8];
  2182. uint8_t scales[IQ3S_N_SCALE];
  2183. } block_iq3_s;
  2184. typedef struct {
  2185. half d;
  2186. uint8_t qs[QK_K/8];
  2187. uint8_t scales[QK_K/16];
  2188. } block_iq1_s;
  2189. // Non-linear quants
  2190. #define QK4_NL 32
  2191. typedef struct {
  2192. half d;
  2193. uint8_t qs[QK4_NL/2];
  2194. } block_iq4_nl;
  2195. #if QK_K == 64
  2196. #define block_iq4_xs block_iq4_nl
  2197. #else
  2198. typedef struct {
  2199. half d;
  2200. uint16_t scales_h;
  2201. uint8_t scales_l[QK_K/64];
  2202. uint8_t qs[QK_K/2];
  2203. } block_iq4_xs;
  2204. #endif
  2205. //====================================== dot products =========================
  2206. void kernel_mul_mv_q2_K_f32_impl(
  2207. device const void * src0,
  2208. device const float * src1,
  2209. device float * dst,
  2210. constant int64_t & ne00,
  2211. constant int64_t & ne01,
  2212. constant int64_t & ne02,
  2213. constant int64_t & ne10,
  2214. constant int64_t & ne12,
  2215. constant int64_t & ne0,
  2216. constant int64_t & ne1,
  2217. constant uint & r2,
  2218. constant uint & r3,
  2219. uint3 tgpig[[threadgroup_position_in_grid]],
  2220. uint tiisg[[thread_index_in_simdgroup]],
  2221. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2222. const int nb = ne00/QK_K;
  2223. const int r0 = tgpig.x;
  2224. const int r1 = tgpig.y;
  2225. const int im = tgpig.z;
  2226. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2227. const int ib_row = first_row * nb;
  2228. const uint i12 = im%ne12;
  2229. const uint i13 = im/ne12;
  2230. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2231. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  2232. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2233. float yl[32];
  2234. float sumf[N_DST]={0.f}, all_sum;
  2235. const int step = sizeof(block_q2_K) * nb;
  2236. #if QK_K == 256
  2237. const int ix = tiisg/8; // 0...3
  2238. const int it = tiisg%8; // 0...7
  2239. const int iq = it/4; // 0 or 1
  2240. const int ir = it%4; // 0...3
  2241. const int is = (8*ir)/16;// 0 or 1
  2242. device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir;
  2243. for (int ib = ix; ib < nb; ib += 4) {
  2244. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2245. for (int i = 0; i < 8; ++i) {
  2246. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2247. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  2248. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  2249. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  2250. }
  2251. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is;
  2252. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2253. device const half * dh = &x[ib].d;
  2254. for (int row = 0; row < N_DST; row++) {
  2255. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2256. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2257. for (int i = 0; i < 8; i += 2) {
  2258. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2259. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2260. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2261. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2262. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2263. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2264. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2265. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2266. }
  2267. float dall = dh[0];
  2268. float dmin = dh[1] * 1.f/16.f;
  2269. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2270. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  2271. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  2272. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  2273. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  2274. qs += step/2;
  2275. sc += step;
  2276. dh += step/2;
  2277. }
  2278. y4 += 4 * QK_K;
  2279. }
  2280. #else
  2281. const int ix = tiisg/2; // 0...15
  2282. const int it = tiisg%2; // 0...1
  2283. device const float * y4 = y + ix * QK_K + 8 * it;
  2284. for (int ib = ix; ib < nb; ib += 16) {
  2285. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2286. for (int i = 0; i < 8; ++i) {
  2287. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  2288. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  2289. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  2290. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  2291. }
  2292. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  2293. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2294. device const half * dh = &x[ib].d;
  2295. for (int row = 0; row < N_DST; row++) {
  2296. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2297. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2298. for (int i = 0; i < 8; i += 2) {
  2299. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  2300. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  2301. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  2302. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  2303. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  2304. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  2305. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  2306. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  2307. }
  2308. float dall = dh[0];
  2309. float dmin = dh[1];
  2310. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  2311. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  2312. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  2313. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  2314. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  2315. qs += step/2;
  2316. sc += step;
  2317. dh += step/2;
  2318. }
  2319. y4 += 16 * QK_K;
  2320. }
  2321. #endif
  2322. for (int row = 0; row < N_DST; ++row) {
  2323. all_sum = simd_sum(sumf[row]);
  2324. if (tiisg == 0) {
  2325. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2326. }
  2327. }
  2328. }
  2329. [[host_name("kernel_mul_mv_q2_K_f32")]]
  2330. kernel void kernel_mul_mv_q2_K_f32(
  2331. device const void * src0,
  2332. device const float * src1,
  2333. device float * dst,
  2334. constant int64_t & ne00,
  2335. constant int64_t & ne01,
  2336. constant int64_t & ne02,
  2337. constant uint64_t & nb00,
  2338. constant uint64_t & nb01,
  2339. constant uint64_t & nb02,
  2340. constant int64_t & ne10,
  2341. constant int64_t & ne11,
  2342. constant int64_t & ne12,
  2343. constant uint64_t & nb10,
  2344. constant uint64_t & nb11,
  2345. constant uint64_t & nb12,
  2346. constant int64_t & ne0,
  2347. constant int64_t & ne1,
  2348. constant uint & r2,
  2349. constant uint & r3,
  2350. uint3 tgpig[[threadgroup_position_in_grid]],
  2351. uint tiisg[[thread_index_in_simdgroup]],
  2352. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2353. kernel_mul_mv_q2_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2354. }
  2355. #if QK_K == 256
  2356. void kernel_mul_mv_q3_K_f32_impl(
  2357. device const void * src0,
  2358. device const float * src1,
  2359. device float * dst,
  2360. constant int64_t & ne00,
  2361. constant int64_t & ne01,
  2362. constant int64_t & ne02,
  2363. constant int64_t & ne10,
  2364. constant int64_t & ne12,
  2365. constant int64_t & ne0,
  2366. constant int64_t & ne1,
  2367. constant uint & r2,
  2368. constant uint & r3,
  2369. uint3 tgpig[[threadgroup_position_in_grid]],
  2370. uint tiisg[[thread_index_in_simdgroup]],
  2371. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2372. const int nb = ne00/QK_K;
  2373. const int64_t r0 = tgpig.x;
  2374. const int64_t r1 = tgpig.y;
  2375. const int64_t im = tgpig.z;
  2376. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2377. const uint i12 = im%ne12;
  2378. const uint i13 = im/ne12;
  2379. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2380. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  2381. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2382. float yl[32];
  2383. //const uint16_t kmask1 = 0x3030;
  2384. //const uint16_t kmask2 = 0x0f0f;
  2385. const int tid = tiisg/4;
  2386. const int ix = tiisg%4;
  2387. const int ip = tid/4; // 0 or 1
  2388. const int il = 2*((tid%4)/2); // 0 or 2
  2389. const int ir = tid%2;
  2390. const int n = 8;
  2391. const int l0 = n*ir;
  2392. // One would think that the Metal compiler would figure out that ip and il can only have
  2393. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  2394. // with these two tales.
  2395. //
  2396. // Possible masks for the high bit
  2397. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  2398. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  2399. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  2400. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  2401. // Possible masks for the low 2 bits
  2402. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  2403. const ushort4 hm = mm[2*ip + il/2];
  2404. const int shift = 2*il;
  2405. const float v1 = il == 0 ? 4.f : 64.f;
  2406. const float v2 = 4.f * v1;
  2407. const uint16_t s_shift1 = 4*ip;
  2408. const uint16_t s_shift2 = s_shift1 + il;
  2409. const int q_offset = 32*ip + l0;
  2410. const int y_offset = 128*ip + 32*il + l0;
  2411. const int step = sizeof(block_q3_K) * nb / 2;
  2412. device const float * y1 = yy + ix*QK_K + y_offset;
  2413. uint32_t scales32, aux32;
  2414. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  2415. thread const int8_t * scales = (thread const int8_t *)&scales32;
  2416. float sumf1[2] = {0.f};
  2417. float sumf2[2] = {0.f};
  2418. for (int i = ix; i < nb; i += 4) {
  2419. for (int l = 0; l < 8; ++l) {
  2420. yl[l+ 0] = y1[l+ 0];
  2421. yl[l+ 8] = y1[l+16];
  2422. yl[l+16] = y1[l+32];
  2423. yl[l+24] = y1[l+48];
  2424. }
  2425. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  2426. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  2427. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  2428. device const half * dh = &x[i].d;
  2429. for (int row = 0; row < 2; ++row) {
  2430. const float d_all = (float)dh[0];
  2431. scales16[0] = a[4];
  2432. scales16[1] = a[5];
  2433. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  2434. scales16[0] = a[il+0];
  2435. scales16[1] = a[il+1];
  2436. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  2437. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  2438. for (int l = 0; l < n; l += 2) {
  2439. const int32_t qs = q[l/2];
  2440. s1 += yl[l+0] * (qs & qm[il/2][0]);
  2441. s2 += yl[l+1] * (qs & qm[il/2][1]);
  2442. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  2443. s4 += yl[l+16] * (qs & qm[il/2][2]);
  2444. s5 += yl[l+17] * (qs & qm[il/2][3]);
  2445. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  2446. }
  2447. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2448. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2449. sumf1[row] += d1 * (scales[0] - 32);
  2450. sumf2[row] += d2 * (scales[2] - 32);
  2451. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  2452. for (int l = 0; l < n; l += 2) {
  2453. const int32_t qs = q[l/2+8];
  2454. s1 += yl[l+8] * (qs & qm[il/2][0]);
  2455. s2 += yl[l+9] * (qs & qm[il/2][1]);
  2456. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  2457. s4 += yl[l+24] * (qs & qm[il/2][2]);
  2458. s5 += yl[l+25] * (qs & qm[il/2][3]);
  2459. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  2460. }
  2461. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  2462. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  2463. sumf1[row] += d1 * (scales[1] - 32);
  2464. sumf2[row] += d2 * (scales[3] - 32);
  2465. q += step;
  2466. h += step;
  2467. a += step;
  2468. dh += step;
  2469. }
  2470. y1 += 4 * QK_K;
  2471. }
  2472. for (int row = 0; row < 2; ++row) {
  2473. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  2474. sumf1[row] = simd_sum(sumf);
  2475. }
  2476. if (tiisg == 0) {
  2477. for (int row = 0; row < 2; ++row) {
  2478. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = sumf1[row];
  2479. }
  2480. }
  2481. }
  2482. #else
  2483. void kernel_mul_mv_q3_K_f32_impl(
  2484. device const void * src0,
  2485. device const float * src1,
  2486. device float * dst,
  2487. constant int64_t & ne00,
  2488. constant int64_t & ne01,
  2489. constant int64_t & ne02,
  2490. constant int64_t & ne10,
  2491. constant int64_t & ne12,
  2492. constant int64_t & ne0,
  2493. constant int64_t & ne1,
  2494. constant uint & r2,
  2495. constant uint & r3,
  2496. uint3 tgpig[[threadgroup_position_in_grid]],
  2497. uint tiisg[[thread_index_in_simdgroup]],
  2498. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2499. const int nb = ne00/QK_K;
  2500. const int64_t r0 = tgpig.x;
  2501. const int64_t r1 = tgpig.y;
  2502. const int64_t im = tgpig.z;
  2503. const int row = 2 * r0 + sgitg;
  2504. const uint i12 = im%ne12;
  2505. const uint i13 = im/ne12;
  2506. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2507. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  2508. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2509. const int ix = tiisg/4;
  2510. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  2511. const int iq = il/8; // 0, 0, 1, 1
  2512. const int in = il%8; // 0, 4, 0, 4
  2513. float2 sum = {0.f, 0.f};
  2514. for (int i = ix; i < nb; i += 8) {
  2515. const float d_all = (float)(x[i].d);
  2516. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  2517. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  2518. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  2519. device const float * y = yy + i * QK_K + il;
  2520. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  2521. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  2522. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  2523. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  2524. for (int l = 0; l < 4; l += 2) {
  2525. const uint16_t hm = h[l/2] >> iq;
  2526. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  2527. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  2528. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  2529. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  2530. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  2531. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  2532. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  2533. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  2534. }
  2535. }
  2536. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  2537. const float tot = simd_sum(sumf);
  2538. if (tiisg == 0) {
  2539. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  2540. }
  2541. }
  2542. #endif
  2543. [[host_name("kernel_mul_mv_q3_K_f32")]]
  2544. kernel void kernel_mul_mv_q3_K_f32(
  2545. device const void * src0,
  2546. device const float * src1,
  2547. device float * dst,
  2548. constant int64_t & ne00,
  2549. constant int64_t & ne01,
  2550. constant int64_t & ne02,
  2551. constant uint64_t & nb00,
  2552. constant uint64_t & nb01,
  2553. constant uint64_t & nb02,
  2554. constant int64_t & ne10,
  2555. constant int64_t & ne11,
  2556. constant int64_t & ne12,
  2557. constant uint64_t & nb10,
  2558. constant uint64_t & nb11,
  2559. constant uint64_t & nb12,
  2560. constant int64_t & ne0,
  2561. constant int64_t & ne1,
  2562. constant uint & r2,
  2563. constant uint & r3,
  2564. uint3 tgpig[[threadgroup_position_in_grid]],
  2565. uint tiisg[[thread_index_in_simdgroup]],
  2566. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2567. kernel_mul_mv_q3_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2568. }
  2569. #if QK_K == 256
  2570. void kernel_mul_mv_q4_K_f32_impl(
  2571. device const void * src0,
  2572. device const float * src1,
  2573. device float * dst,
  2574. constant int64_t & ne00,
  2575. constant int64_t & ne01,
  2576. constant int64_t & ne02,
  2577. constant int64_t & ne10,
  2578. constant int64_t & ne12,
  2579. constant int64_t & ne0,
  2580. constant int64_t & ne1,
  2581. constant uint & r2,
  2582. constant uint & r3,
  2583. uint3 tgpig[[threadgroup_position_in_grid]],
  2584. uint tiisg[[thread_index_in_simdgroup]],
  2585. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2586. const uint16_t kmask1 = 0x3f3f;
  2587. const uint16_t kmask2 = 0x0f0f;
  2588. const uint16_t kmask3 = 0xc0c0;
  2589. const int ix = tiisg/8; // 0...3
  2590. const int it = tiisg%8; // 0...7
  2591. const int iq = it/4; // 0 or 1
  2592. const int ir = it%4; // 0...3
  2593. const int nb = ne00/QK_K;
  2594. const int r0 = tgpig.x;
  2595. const int r1 = tgpig.y;
  2596. const int im = tgpig.z;
  2597. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  2598. const int first_row = r0 * N_DST;
  2599. const int ib_row = first_row * nb;
  2600. const uint i12 = im%ne12;
  2601. const uint i13 = im/ne12;
  2602. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2603. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2604. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2605. float yl[16];
  2606. float yh[16];
  2607. float sumf[N_DST]={0.f}, all_sum;
  2608. const int step = sizeof(block_q4_K) * nb / 2;
  2609. device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
  2610. uint16_t sc16[4];
  2611. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2612. for (int ib = ix; ib < nb; ib += 4) {
  2613. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2614. for (int i = 0; i < 8; ++i) {
  2615. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  2616. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  2617. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  2618. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  2619. }
  2620. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq;
  2621. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir;
  2622. device const half * dh = &x[ib].d;
  2623. for (int row = 0; row < N_DST; row++) {
  2624. sc16[0] = sc[0] & kmask1;
  2625. sc16[1] = sc[2] & kmask1;
  2626. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  2627. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  2628. device const uint16_t * q2 = q1 + 32;
  2629. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  2630. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  2631. for (int i = 0; i < 8; i += 2) {
  2632. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  2633. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  2634. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  2635. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  2636. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  2637. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  2638. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  2639. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  2640. }
  2641. float dall = dh[0];
  2642. float dmin = dh[1];
  2643. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  2644. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  2645. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  2646. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  2647. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2648. q1 += step;
  2649. sc += step;
  2650. dh += step;
  2651. }
  2652. y4 += 4 * QK_K;
  2653. }
  2654. for (int row = 0; row < N_DST; ++row) {
  2655. all_sum = simd_sum(sumf[row]);
  2656. if (tiisg == 0) {
  2657. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2658. }
  2659. }
  2660. }
  2661. #else
  2662. void kernel_mul_mv_q4_K_f32_impl(
  2663. device const void * src0,
  2664. device const float * src1,
  2665. device float * dst,
  2666. constant int64_t & ne00,
  2667. constant int64_t & ne01,
  2668. constant int64_t & ne02,
  2669. constant int64_t & ne10,
  2670. constant int64_t & ne12,
  2671. constant int64_t & ne0,
  2672. constant int64_t & ne1,
  2673. constant uint & r2,
  2674. constant uint & r3,
  2675. uint3 tgpig[[threadgroup_position_in_grid]],
  2676. uint tiisg[[thread_index_in_simdgroup]],
  2677. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2678. const int ix = tiisg/4; // 0...7
  2679. const int it = tiisg%4; // 0...3
  2680. const int nb = ne00/QK_K;
  2681. const int r0 = tgpig.x;
  2682. const int r1 = tgpig.y;
  2683. const int im = tgpig.z;
  2684. const int first_row = r0 * N_DST;
  2685. const int ib_row = first_row * nb;
  2686. const uint i12 = im%ne12;
  2687. const uint i13 = im/ne12;
  2688. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2689. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  2690. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2691. float yl[8];
  2692. float yh[8];
  2693. float sumf[N_DST]={0.f}, all_sum;
  2694. const int step = sizeof(block_q4_K) * nb / 2;
  2695. device const float * y4 = y + ix * QK_K + 8 * it;
  2696. uint16_t sc16[4];
  2697. for (int ib = ix; ib < nb; ib += 8) {
  2698. float2 sumy = {0.f, 0.f};
  2699. for (int i = 0; i < 8; ++i) {
  2700. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  2701. yh[i] = y4[i+32]; sumy[1] += yh[i];
  2702. }
  2703. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  2704. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  2705. device const half * dh = x[ib].d;
  2706. for (int row = 0; row < N_DST; row++) {
  2707. sc16[0] = sc[0] & 0x000f;
  2708. sc16[1] = sc[0] & 0x0f00;
  2709. sc16[2] = sc[0] & 0x00f0;
  2710. sc16[3] = sc[0] & 0xf000;
  2711. float2 acc1 = {0.f, 0.f};
  2712. float2 acc2 = {0.f, 0.f};
  2713. for (int i = 0; i < 8; i += 2) {
  2714. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  2715. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  2716. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  2717. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  2718. }
  2719. float dall = dh[0];
  2720. float dmin = dh[1];
  2721. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  2722. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  2723. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  2724. qs += step;
  2725. sc += step;
  2726. dh += step;
  2727. }
  2728. y4 += 8 * QK_K;
  2729. }
  2730. for (int row = 0; row < N_DST; ++row) {
  2731. all_sum = simd_sum(sumf[row]);
  2732. if (tiisg == 0) {
  2733. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  2734. }
  2735. }
  2736. }
  2737. #endif
  2738. [[host_name("kernel_mul_mv_q4_K_f32")]]
  2739. kernel void kernel_mul_mv_q4_K_f32(
  2740. device const void * src0,
  2741. device const float * src1,
  2742. device float * dst,
  2743. constant int64_t & ne00,
  2744. constant int64_t & ne01,
  2745. constant int64_t & ne02,
  2746. constant uint64_t & nb00,
  2747. constant uint64_t & nb01,
  2748. constant uint64_t & nb02,
  2749. constant int64_t & ne10,
  2750. constant int64_t & ne11,
  2751. constant int64_t & ne12,
  2752. constant uint64_t & nb10,
  2753. constant uint64_t & nb11,
  2754. constant uint64_t & nb12,
  2755. constant int64_t & ne0,
  2756. constant int64_t & ne1,
  2757. constant uint & r2,
  2758. constant uint & r3,
  2759. uint3 tgpig[[threadgroup_position_in_grid]],
  2760. uint tiisg[[thread_index_in_simdgroup]],
  2761. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2762. kernel_mul_mv_q4_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2763. }
  2764. void kernel_mul_mv_q5_K_f32_impl(
  2765. device const void * src0,
  2766. device const float * src1,
  2767. device float * dst,
  2768. constant int64_t & ne00,
  2769. constant int64_t & ne01,
  2770. constant int64_t & ne02,
  2771. constant int64_t & ne10,
  2772. constant int64_t & ne12,
  2773. constant int64_t & ne0,
  2774. constant int64_t & ne1,
  2775. constant uint & r2,
  2776. constant uint & r3,
  2777. uint3 tgpig[[threadgroup_position_in_grid]],
  2778. uint tiisg[[thread_index_in_simdgroup]],
  2779. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2780. const int nb = ne00/QK_K;
  2781. const int64_t r0 = tgpig.x;
  2782. const int64_t r1 = tgpig.y;
  2783. const int im = tgpig.z;
  2784. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  2785. const uint i12 = im%ne12;
  2786. const uint i13 = im/ne12;
  2787. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2788. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  2789. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2790. float sumf[2]={0.f};
  2791. const int step = sizeof(block_q5_K) * nb;
  2792. #if QK_K == 256
  2793. #
  2794. float yl[16], yh[16];
  2795. const uint16_t kmask1 = 0x3f3f;
  2796. const uint16_t kmask2 = 0x0f0f;
  2797. const uint16_t kmask3 = 0xc0c0;
  2798. const int tid = tiisg/4;
  2799. const int ix = tiisg%4;
  2800. const int iq = tid/4;
  2801. const int ir = tid%4;
  2802. const int n = 8;
  2803. const int l0 = n*ir;
  2804. const int q_offset = 32*iq + l0;
  2805. const int y_offset = 64*iq + l0;
  2806. const uint8_t hm1 = 1u << (2*iq);
  2807. const uint8_t hm2 = hm1 << 1;
  2808. const uint8_t hm3 = hm1 << 4;
  2809. const uint8_t hm4 = hm2 << 4;
  2810. uint16_t sc16[4];
  2811. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  2812. device const float * y1 = yy + ix*QK_K + y_offset;
  2813. for (int i = ix; i < nb; i += 4) {
  2814. device const uint8_t * q1 = x[i].qs + q_offset;
  2815. device const uint8_t * qh = x[i].qh + l0;
  2816. device const half * dh = &x[i].d;
  2817. device const uint16_t * a = (device const uint16_t *)x[i].scales + iq;
  2818. device const float * y2 = y1 + 128;
  2819. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  2820. for (int l = 0; l < 8; ++l) {
  2821. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  2822. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  2823. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  2824. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  2825. }
  2826. for (int row = 0; row < 2; ++row) {
  2827. device const uint8_t * q2 = q1 + 64;
  2828. sc16[0] = a[0] & kmask1;
  2829. sc16[1] = a[2] & kmask1;
  2830. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  2831. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  2832. float4 acc1 = {0.f};
  2833. float4 acc2 = {0.f};
  2834. for (int l = 0; l < n; ++l) {
  2835. uint8_t h = qh[l];
  2836. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  2837. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  2838. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  2839. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  2840. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  2841. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  2842. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  2843. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  2844. }
  2845. const float dall = dh[0];
  2846. const float dmin = dh[1];
  2847. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  2848. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  2849. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  2850. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  2851. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  2852. q1 += step;
  2853. qh += step;
  2854. dh += step/2;
  2855. a += step/2;
  2856. }
  2857. y1 += 4 * QK_K;
  2858. }
  2859. #else
  2860. float yl[8], yh[8];
  2861. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  2862. const int ix = tiisg%8;
  2863. const int iq = il/8; // 0, 0, 1, 1
  2864. const int in = il%8; // 0, 4, 0, 4
  2865. device const float * y = yy + ix*QK_K + il;
  2866. for (int i = ix; i < nb; i += 8) {
  2867. for (int l = 0; l < 4; ++l) {
  2868. yl[l+0] = y[l+ 0];
  2869. yl[l+4] = y[l+16];
  2870. yh[l+0] = y[l+32];
  2871. yh[l+4] = y[l+48];
  2872. }
  2873. device const half * dh = &x[i].d;
  2874. device const uint8_t * q = x[i].qs + il;
  2875. device const uint8_t * h = x[i].qh + in;
  2876. device const int8_t * s = x[i].scales;
  2877. for (int row = 0; row < 2; ++row) {
  2878. const float d = dh[0];
  2879. float2 acc = {0.f, 0.f};
  2880. for (int l = 0; l < 4; ++l) {
  2881. const uint8_t hl = h[l] >> iq;
  2882. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  2883. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  2884. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  2885. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  2886. }
  2887. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  2888. q += step;
  2889. h += step;
  2890. s += step;
  2891. dh += step/2;
  2892. }
  2893. y += 8 * QK_K;
  2894. }
  2895. #endif
  2896. for (int row = 0; row < 2; ++row) {
  2897. const float tot = simd_sum(sumf[row]);
  2898. if (tiisg == 0) {
  2899. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  2900. }
  2901. }
  2902. }
  2903. [[host_name("kernel_mul_mv_q5_K_f32")]]
  2904. kernel void kernel_mul_mv_q5_K_f32(
  2905. device const void * src0,
  2906. device const float * src1,
  2907. device float * dst,
  2908. constant int64_t & ne00,
  2909. constant int64_t & ne01,
  2910. constant int64_t & ne02,
  2911. constant uint64_t & nb00,
  2912. constant uint64_t & nb01,
  2913. constant uint64_t & nb02,
  2914. constant int64_t & ne10,
  2915. constant int64_t & ne11,
  2916. constant int64_t & ne12,
  2917. constant uint64_t & nb10,
  2918. constant uint64_t & nb11,
  2919. constant uint64_t & nb12,
  2920. constant int64_t & ne0,
  2921. constant int64_t & ne1,
  2922. constant uint & r2,
  2923. constant uint & r3,
  2924. uint3 tgpig[[threadgroup_position_in_grid]],
  2925. uint tiisg[[thread_index_in_simdgroup]],
  2926. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2927. kernel_mul_mv_q5_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  2928. }
  2929. void kernel_mul_mv_q6_K_f32_impl(
  2930. device const void * src0,
  2931. device const float * src1,
  2932. device float * dst,
  2933. constant int64_t & ne00,
  2934. constant int64_t & ne01,
  2935. constant int64_t & ne02,
  2936. constant int64_t & ne10,
  2937. constant int64_t & ne12,
  2938. constant int64_t & ne0,
  2939. constant int64_t & ne1,
  2940. constant uint & r2,
  2941. constant uint & r3,
  2942. uint3 tgpig[[threadgroup_position_in_grid]],
  2943. uint tiisg[[thread_index_in_simdgroup]],
  2944. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  2945. const uint8_t kmask1 = 0x03;
  2946. const uint8_t kmask2 = 0x0C;
  2947. const uint8_t kmask3 = 0x30;
  2948. const uint8_t kmask4 = 0xC0;
  2949. const int nb = ne00/QK_K;
  2950. const int64_t r0 = tgpig.x;
  2951. const int64_t r1 = tgpig.y;
  2952. const int im = tgpig.z;
  2953. const int row = 2 * r0 + sgitg;
  2954. const uint i12 = im%ne12;
  2955. const uint i13 = im/ne12;
  2956. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  2957. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  2958. device const float * yy = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  2959. float sumf = 0;
  2960. #if QK_K == 256
  2961. const int tid = tiisg/2;
  2962. const int ix = tiisg%2;
  2963. const int ip = tid/8; // 0 or 1
  2964. const int il = tid%8;
  2965. const int n = 4;
  2966. const int l0 = n*il;
  2967. const int is = 8*ip + l0/16;
  2968. const int y_offset = 128*ip + l0;
  2969. const int q_offset_l = 64*ip + l0;
  2970. const int q_offset_h = 32*ip + l0;
  2971. for (int i = ix; i < nb; i += 2) {
  2972. device const uint8_t * q1 = x[i].ql + q_offset_l;
  2973. device const uint8_t * q2 = q1 + 32;
  2974. device const uint8_t * qh = x[i].qh + q_offset_h;
  2975. device const int8_t * sc = x[i].scales + is;
  2976. device const float * y = yy + i * QK_K + y_offset;
  2977. const float dall = x[i].d;
  2978. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2979. for (int l = 0; l < n; ++l) {
  2980. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2981. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  2982. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  2983. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  2984. }
  2985. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  2986. }
  2987. #else
  2988. const int ix = tiisg/4;
  2989. const int il = 4*(tiisg%4);
  2990. for (int i = ix; i < nb; i += 8) {
  2991. device const float * y = yy + i * QK_K + il;
  2992. device const uint8_t * ql = x[i].ql + il;
  2993. device const uint8_t * qh = x[i].qh + il;
  2994. device const int8_t * s = x[i].scales;
  2995. const float d = x[i].d;
  2996. float4 sums = {0.f, 0.f, 0.f, 0.f};
  2997. for (int l = 0; l < 4; ++l) {
  2998. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  2999. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  3000. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  3001. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  3002. }
  3003. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  3004. }
  3005. #endif
  3006. const float tot = simd_sum(sumf);
  3007. if (tiisg == 0) {
  3008. dst[r1*ne0 + im*ne0*ne1 + row] = tot;
  3009. }
  3010. }
  3011. [[host_name("kernel_mul_mv_q6_K_f32")]]
  3012. kernel void kernel_mul_mv_q6_K_f32(
  3013. device const void * src0,
  3014. device const float * src1,
  3015. device float * dst,
  3016. constant int64_t & ne00,
  3017. constant int64_t & ne01,
  3018. constant int64_t & ne02,
  3019. constant uint64_t & nb00,
  3020. constant uint64_t & nb01,
  3021. constant uint64_t & nb02,
  3022. constant int64_t & ne10,
  3023. constant int64_t & ne11,
  3024. constant int64_t & ne12,
  3025. constant uint64_t & nb10,
  3026. constant uint64_t & nb11,
  3027. constant uint64_t & nb12,
  3028. constant int64_t & ne0,
  3029. constant int64_t & ne1,
  3030. constant uint & r2,
  3031. constant uint & r3,
  3032. uint3 tgpig[[threadgroup_position_in_grid]],
  3033. uint tiisg[[thread_index_in_simdgroup]],
  3034. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3035. kernel_mul_mv_q6_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  3036. }
  3037. // ======================= "True" 2-bit
  3038. constexpr constant static uint64_t iq2xxs_grid[256] = {
  3039. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3040. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  3041. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  3042. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  3043. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  3044. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  3045. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  3046. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  3047. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  3048. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  3049. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  3050. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  3051. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  3052. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  3053. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  3054. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  3055. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  3056. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  3057. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  3058. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  3059. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  3060. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  3061. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  3062. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  3063. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  3064. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  3065. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  3066. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  3067. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  3068. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  3069. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  3070. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  3071. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  3072. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  3073. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  3074. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  3075. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  3076. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  3077. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  3078. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  3079. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  3080. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  3081. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  3082. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  3083. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  3084. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  3085. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  3086. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  3087. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  3088. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  3089. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  3090. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  3091. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  3092. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  3093. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  3094. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  3095. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  3096. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  3097. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  3098. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  3099. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  3100. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  3101. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  3102. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  3103. };
  3104. constexpr constant static uint64_t iq2xs_grid[512] = {
  3105. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3106. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3107. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3108. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3109. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3110. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  3111. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  3112. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  3113. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  3114. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  3115. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  3116. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  3117. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  3118. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  3119. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  3120. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  3121. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  3122. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  3123. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  3124. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  3125. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  3126. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  3127. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  3128. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  3129. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  3130. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  3131. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  3132. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  3133. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  3134. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  3135. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  3136. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  3137. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  3138. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  3139. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  3140. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  3141. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  3142. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  3143. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  3144. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  3145. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  3146. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  3147. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  3148. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  3149. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  3150. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  3151. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  3152. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  3153. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  3154. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  3155. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  3156. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  3157. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  3158. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  3159. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  3160. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  3161. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  3162. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  3163. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  3164. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  3165. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  3166. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  3167. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  3168. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  3169. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  3170. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  3171. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  3172. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  3173. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  3174. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  3175. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  3176. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  3177. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  3178. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  3179. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  3180. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  3181. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  3182. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  3183. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  3184. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  3185. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  3186. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  3187. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  3188. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  3189. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  3190. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  3191. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  3192. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  3193. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  3194. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  3195. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  3196. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  3197. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  3198. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  3199. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  3200. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  3201. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  3202. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  3203. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  3204. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  3205. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  3206. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  3207. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  3208. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  3209. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  3210. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  3211. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  3212. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  3213. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  3214. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  3215. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  3216. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  3217. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  3218. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  3219. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  3220. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  3221. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  3222. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  3223. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  3224. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  3225. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  3226. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  3227. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  3228. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  3229. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  3230. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  3231. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  3232. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  3233. };
  3234. constexpr constant static uint64_t iq2s_grid[1024] = {
  3235. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  3236. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  3237. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  3238. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  3239. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  3240. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
  3241. 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
  3242. 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
  3243. 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
  3244. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
  3245. 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
  3246. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
  3247. 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
  3248. 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
  3249. 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
  3250. 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
  3251. 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
  3252. 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
  3253. 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
  3254. 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
  3255. 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  3256. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
  3257. 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
  3258. 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
  3259. 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
  3260. 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
  3261. 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
  3262. 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
  3263. 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
  3264. 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
  3265. 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
  3266. 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
  3267. 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
  3268. 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
  3269. 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
  3270. 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
  3271. 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
  3272. 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
  3273. 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
  3274. 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
  3275. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
  3276. 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
  3277. 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
  3278. 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
  3279. 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
  3280. 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
  3281. 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
  3282. 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
  3283. 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
  3284. 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
  3285. 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
  3286. 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
  3287. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
  3288. 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
  3289. 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
  3290. 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
  3291. 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
  3292. 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
  3293. 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
  3294. 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
  3295. 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
  3296. 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
  3297. 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
  3298. 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
  3299. 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
  3300. 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
  3301. 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
  3302. 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
  3303. 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
  3304. 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
  3305. 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  3306. 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
  3307. 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
  3308. 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
  3309. 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
  3310. 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
  3311. 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
  3312. 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
  3313. 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
  3314. 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
  3315. 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
  3316. 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
  3317. 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
  3318. 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
  3319. 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
  3320. 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
  3321. 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
  3322. 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
  3323. 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
  3324. 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
  3325. 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
  3326. 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
  3327. 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
  3328. 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
  3329. 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
  3330. 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
  3331. 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
  3332. 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
  3333. 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
  3334. 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
  3335. 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
  3336. 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
  3337. 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
  3338. 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
  3339. 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
  3340. 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
  3341. 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
  3342. 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
  3343. 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
  3344. 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
  3345. 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
  3346. 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
  3347. 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
  3348. 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
  3349. 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
  3350. 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
  3351. 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
  3352. 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  3353. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
  3354. 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
  3355. 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
  3356. 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
  3357. 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
  3358. 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
  3359. 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
  3360. 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
  3361. 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  3362. 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
  3363. 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
  3364. 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
  3365. 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
  3366. 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
  3367. 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
  3368. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
  3369. 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
  3370. 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
  3371. 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
  3372. 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
  3373. 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
  3374. 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
  3375. 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
  3376. 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
  3377. 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
  3378. 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
  3379. 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
  3380. 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
  3381. 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
  3382. 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
  3383. 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
  3384. 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
  3385. 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
  3386. 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
  3387. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
  3388. 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
  3389. 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
  3390. 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
  3391. 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
  3392. 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
  3393. 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
  3394. 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
  3395. 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
  3396. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
  3397. 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
  3398. 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
  3399. 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
  3400. 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
  3401. 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
  3402. 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
  3403. 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
  3404. 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
  3405. 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
  3406. 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
  3407. 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
  3408. 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
  3409. 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
  3410. 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
  3411. 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
  3412. 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
  3413. 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
  3414. 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
  3415. 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
  3416. 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
  3417. 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
  3418. 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
  3419. 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
  3420. 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
  3421. 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
  3422. 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
  3423. 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
  3424. 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
  3425. 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
  3426. 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
  3427. 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
  3428. 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
  3429. 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
  3430. 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
  3431. 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
  3432. 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
  3433. 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
  3434. 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
  3435. 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
  3436. 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
  3437. 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
  3438. 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
  3439. 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
  3440. 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  3441. 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
  3442. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
  3443. 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
  3444. 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
  3445. 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
  3446. 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
  3447. 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
  3448. 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
  3449. 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
  3450. 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
  3451. 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
  3452. 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
  3453. 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
  3454. 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
  3455. 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
  3456. 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
  3457. 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
  3458. 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
  3459. 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
  3460. 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
  3461. 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
  3462. 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
  3463. 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
  3464. 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
  3465. 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
  3466. 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
  3467. 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
  3468. 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
  3469. 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
  3470. 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
  3471. 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
  3472. 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
  3473. 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
  3474. 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
  3475. 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
  3476. 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
  3477. 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
  3478. 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
  3479. 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
  3480. 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
  3481. 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
  3482. 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
  3483. 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
  3484. 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
  3485. 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
  3486. 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
  3487. 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
  3488. 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
  3489. 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
  3490. 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
  3491. };
  3492. constexpr constant static uint32_t iq3xxs_grid[256] = {
  3493. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  3494. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  3495. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  3496. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  3497. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  3498. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  3499. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  3500. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  3501. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  3502. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  3503. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  3504. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  3505. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  3506. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  3507. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  3508. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  3509. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  3510. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  3511. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  3512. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  3513. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  3514. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  3515. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  3516. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  3517. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  3518. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  3519. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  3520. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  3521. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  3522. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  3523. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  3524. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  3525. };
  3526. constexpr constant static uint32_t iq3xs_grid[512] = {
  3527. 0x04040404, 0x0404040c, 0x04040414, 0x0404042c, 0x0404043e, 0x04040c04, 0x04040c0c, 0x04040c14,
  3528. 0x04040c24, 0x04040c34, 0x04041404, 0x0404140c, 0x0404142c, 0x04041c1c, 0x04042404, 0x04042414,
  3529. 0x0404242c, 0x0404243e, 0x04042c0c, 0x04042c1c, 0x04043404, 0x04043414, 0x04043e0c, 0x04043e24,
  3530. 0x04043e3e, 0x040c0404, 0x040c040c, 0x040c0414, 0x040c0424, 0x040c0c04, 0x040c0c0c, 0x040c0c2c,
  3531. 0x040c1404, 0x040c141c, 0x040c143e, 0x040c1c0c, 0x040c1c2c, 0x040c2424, 0x040c340c, 0x040c342c,
  3532. 0x040c3e14, 0x04140404, 0x0414040c, 0x0414042c, 0x0414043e, 0x04140c04, 0x04140c1c, 0x04140c34,
  3533. 0x0414140c, 0x0414142c, 0x04141c04, 0x04141c24, 0x04142414, 0x0414242c, 0x0414243e, 0x04142c0c,
  3534. 0x04142c1c, 0x04143e04, 0x04143e1c, 0x041c041c, 0x041c0c0c, 0x041c0c2c, 0x041c1404, 0x041c1414,
  3535. 0x041c1c0c, 0x041c1c1c, 0x041c1c34, 0x041c2424, 0x041c2c04, 0x041c2c14, 0x041c343e, 0x041c3e0c,
  3536. 0x041c3e2c, 0x04240404, 0x04240c1c, 0x04240c3e, 0x0424140c, 0x04241424, 0x04241c14, 0x04242404,
  3537. 0x0424241c, 0x04242c0c, 0x04243e04, 0x042c0414, 0x042c0424, 0x042c1404, 0x042c1414, 0x042c1434,
  3538. 0x042c1c1c, 0x042c240c, 0x042c242c, 0x042c243e, 0x042c3434, 0x042c3e1c, 0x04340434, 0x04340c0c,
  3539. 0x04340c1c, 0x04341c0c, 0x04342c14, 0x04343e0c, 0x043e0404, 0x043e0414, 0x043e0424, 0x043e1404,
  3540. 0x043e1414, 0x043e1434, 0x043e1c1c, 0x043e2c04, 0x043e2c24, 0x0c040404, 0x0c04040c, 0x0c040414,
  3541. 0x0c040424, 0x0c040c04, 0x0c040c0c, 0x0c040c1c, 0x0c040c2c, 0x0c040c3e, 0x0c041404, 0x0c041414,
  3542. 0x0c041c0c, 0x0c041c24, 0x0c041c34, 0x0c042c24, 0x0c042c34, 0x0c04340c, 0x0c043e14, 0x0c0c0404,
  3543. 0x0c0c040c, 0x0c0c041c, 0x0c0c0434, 0x0c0c0c04, 0x0c0c0c24, 0x0c0c140c, 0x0c0c1c04, 0x0c0c1c1c,
  3544. 0x0c0c240c, 0x0c0c2c04, 0x0c0c2c14, 0x0c0c3e04, 0x0c0c3e34, 0x0c140404, 0x0c140c14, 0x0c140c2c,
  3545. 0x0c140c3e, 0x0c141404, 0x0c141424, 0x0c141c14, 0x0c142404, 0x0c14241c, 0x0c142c2c, 0x0c143404,
  3546. 0x0c143e14, 0x0c1c040c, 0x0c1c0424, 0x0c1c043e, 0x0c1c0c04, 0x0c1c0c1c, 0x0c1c140c, 0x0c1c143e,
  3547. 0x0c1c1c04, 0x0c1c1c24, 0x0c1c240c, 0x0c1c3414, 0x0c1c3e04, 0x0c24041c, 0x0c24042c, 0x0c240c14,
  3548. 0x0c240c24, 0x0c241c0c, 0x0c241c1c, 0x0c242414, 0x0c242434, 0x0c242c04, 0x0c242c24, 0x0c2c040c,
  3549. 0x0c2c0c04, 0x0c2c0c1c, 0x0c2c140c, 0x0c2c1c04, 0x0c2c1c14, 0x0c2c2c0c, 0x0c341404, 0x0c341424,
  3550. 0x0c34143e, 0x0c342424, 0x0c342434, 0x0c3e040c, 0x0c3e041c, 0x0c3e0c04, 0x0c3e0c14, 0x0c3e140c,
  3551. 0x0c3e1c2c, 0x0c3e240c, 0x0c3e3414, 0x0c3e3e04, 0x14040404, 0x1404040c, 0x1404041c, 0x1404042c,
  3552. 0x1404043e, 0x14040c04, 0x14040c14, 0x14040c24, 0x14040c34, 0x1404140c, 0x1404141c, 0x1404143e,
  3553. 0x14041c04, 0x14041c14, 0x1404240c, 0x1404241c, 0x1404242c, 0x14042c04, 0x14042c14, 0x1404343e,
  3554. 0x14043e04, 0x14043e1c, 0x14043e2c, 0x140c0404, 0x140c0414, 0x140c0c04, 0x140c0c1c, 0x140c0c3e,
  3555. 0x140c1414, 0x140c142c, 0x140c1c0c, 0x140c1c24, 0x140c2414, 0x140c2c0c, 0x1414040c, 0x14140424,
  3556. 0x1414043e, 0x1414140c, 0x1414141c, 0x14141c04, 0x14141c3e, 0x1414240c, 0x14142c1c, 0x14142c3e,
  3557. 0x14143e0c, 0x14143e24, 0x141c0404, 0x141c0414, 0x141c042c, 0x141c0c0c, 0x141c1414, 0x141c1424,
  3558. 0x141c1c0c, 0x141c1c1c, 0x141c2414, 0x141c2c04, 0x141c3434, 0x1424040c, 0x1424043e, 0x14241404,
  3559. 0x1424141c, 0x14241c14, 0x14241c2c, 0x1424240c, 0x14243e14, 0x14243e2c, 0x142c0424, 0x142c0c0c,
  3560. 0x142c1414, 0x142c1c3e, 0x142c2404, 0x142c2c1c, 0x142c3e04, 0x14340404, 0x14340414, 0x1434043e,
  3561. 0x1434140c, 0x14342c2c, 0x1434340c, 0x143e042c, 0x143e0c0c, 0x143e1434, 0x143e1c04, 0x143e241c,
  3562. 0x143e2c04, 0x1c040414, 0x1c040c0c, 0x1c040c1c, 0x1c040c2c, 0x1c040c3e, 0x1c041414, 0x1c041c0c,
  3563. 0x1c041c1c, 0x1c041c2c, 0x1c042414, 0x1c042424, 0x1c04243e, 0x1c042c0c, 0x1c04341c, 0x1c043e0c,
  3564. 0x1c0c040c, 0x1c0c041c, 0x1c0c042c, 0x1c0c0c24, 0x1c0c140c, 0x1c0c141c, 0x1c0c2404, 0x1c0c3404,
  3565. 0x1c0c3e14, 0x1c0c3e34, 0x1c140404, 0x1c140c14, 0x1c141404, 0x1c141c14, 0x1c141c24, 0x1c142c04,
  3566. 0x1c1c040c, 0x1c1c0c04, 0x1c1c0c24, 0x1c1c140c, 0x1c1c141c, 0x1c1c143e, 0x1c1c1c04, 0x1c1c240c,
  3567. 0x1c1c241c, 0x1c1c243e, 0x1c1c2c2c, 0x1c1c3e1c, 0x1c24041c, 0x1c240c0c, 0x1c240c34, 0x1c241414,
  3568. 0x1c241c0c, 0x1c242c14, 0x1c243404, 0x1c243424, 0x1c2c040c, 0x1c2c0c04, 0x1c2c0c14, 0x1c2c142c,
  3569. 0x1c2c1c14, 0x1c2c2424, 0x1c2c2c34, 0x1c2c3e1c, 0x1c340c34, 0x1c34240c, 0x1c3e040c, 0x1c3e041c,
  3570. 0x1c3e1404, 0x1c3e1414, 0x1c3e1c2c, 0x24040404, 0x24040424, 0x24040c14, 0x24041404, 0x24041424,
  3571. 0x2404143e, 0x24041c14, 0x2404240c, 0x24042c04, 0x24043e04, 0x240c0414, 0x240c043e, 0x240c0c0c,
  3572. 0x240c0c1c, 0x240c1414, 0x240c1c04, 0x240c1c2c, 0x240c241c, 0x240c2c0c, 0x240c2c2c, 0x2414040c,
  3573. 0x2414041c, 0x24140c04, 0x24140c2c, 0x2414140c, 0x24141c1c, 0x24142404, 0x24142c3e, 0x24143414,
  3574. 0x24143e04, 0x241c0424, 0x241c0c0c, 0x241c0c1c, 0x241c1404, 0x241c1414, 0x241c1c0c, 0x241c1c2c,
  3575. 0x24240404, 0x24240414, 0x24241424, 0x24241c3e, 0x24242404, 0x24243e0c, 0x242c042c, 0x242c043e,
  3576. 0x242c140c, 0x242c3414, 0x24340c1c, 0x24341c24, 0x24343404, 0x243e0c04, 0x243e0c2c, 0x243e1c04,
  3577. 0x243e241c, 0x243e2c0c, 0x2c040414, 0x2c040c04, 0x2c040c24, 0x2c041414, 0x2c042404, 0x2c042424,
  3578. 0x2c04243e, 0x2c042c14, 0x2c043434, 0x2c043e24, 0x2c0c040c, 0x2c0c041c, 0x2c0c042c, 0x2c0c0c14,
  3579. 0x2c0c140c, 0x2c0c1c14, 0x2c0c3e14, 0x2c140404, 0x2c140c0c, 0x2c14141c, 0x2c141c04, 0x2c141c34,
  3580. 0x2c142c1c, 0x2c1c0414, 0x2c1c043e, 0x2c1c0c04, 0x2c1c143e, 0x2c1c2424, 0x2c1c2c0c, 0x2c1c342c,
  3581. 0x2c1c3e1c, 0x2c24040c, 0x2c240424, 0x2c241404, 0x2c241c14, 0x2c242434, 0x2c2c0c14, 0x2c2c1434,
  3582. 0x2c2c2c0c, 0x2c2c2c1c, 0x2c342414, 0x2c3e0414, 0x2c3e0424, 0x2c3e1414, 0x34040c0c, 0x34040c1c,
  3583. 0x34040c2c, 0x34041c0c, 0x34041c1c, 0x34043404, 0x340c0404, 0x340c1404, 0x340c143e, 0x340c3424,
  3584. 0x34140c14, 0x34141c24, 0x34142414, 0x34142c2c, 0x34143414, 0x34143e04, 0x341c0404, 0x341c0c24,
  3585. 0x341c140c, 0x341c2404, 0x3424142c, 0x3424241c, 0x34243414, 0x342c0404, 0x342c041c, 0x342c1c24,
  3586. 0x342c3404, 0x3434042c, 0x34342404, 0x343e0c0c, 0x343e0c1c, 0x3e040404, 0x3e040424, 0x3e04043e,
  3587. 0x3e041404, 0x3e041414, 0x3e041c34, 0x3e042404, 0x3e042c24, 0x3e043414, 0x3e0c0414, 0x3e0c0c0c,
  3588. 0x3e0c1424, 0x3e0c241c, 0x3e0c242c, 0x3e14040c, 0x3e140424, 0x3e140c04, 0x3e140c34, 0x3e14140c,
  3589. 0x3e141c04, 0x3e142c0c, 0x3e1c0414, 0x3e1c1c14, 0x3e1c1c2c, 0x3e1c2c1c, 0x3e24040c, 0x3e24042c,
  3590. 0x3e240c1c, 0x3e241404, 0x3e242c04, 0x3e2c1414, 0x3e2c2414, 0x3e340414, 0x3e341c0c, 0x3e3e0404,
  3591. };
  3592. #define NGRID_IQ1S 512
  3593. constexpr constant static uint64_t iq1s_grid[NGRID_IQ1S] = {
  3594. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  3595. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  3596. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  3597. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  3598. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  3599. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  3600. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  3601. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  3602. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  3603. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  3604. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  3605. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  3606. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  3607. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  3608. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  3609. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  3610. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  3611. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  3612. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  3613. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  3614. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  3615. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  3616. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  3617. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  3618. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  3619. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  3620. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  3621. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  3622. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  3623. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  3624. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  3625. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  3626. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  3627. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  3628. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  3629. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  3630. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  3631. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  3632. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  3633. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  3634. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  3635. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  3636. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  3637. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  3638. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  3639. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  3640. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  3641. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  3642. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  3643. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  3644. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  3645. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  3646. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  3647. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  3648. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  3649. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  3650. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  3651. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  3652. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  3653. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  3654. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  3655. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  3656. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  3657. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  3658. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  3659. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  3660. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  3661. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  3662. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  3663. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  3664. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  3665. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  3666. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  3667. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  3668. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  3669. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  3670. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  3671. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  3672. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  3673. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  3674. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  3675. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  3676. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  3677. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  3678. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  3679. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  3680. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  3681. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  3682. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  3683. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  3684. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  3685. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  3686. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  3687. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  3688. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  3689. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  3690. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  3691. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  3692. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  3693. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  3694. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  3695. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  3696. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  3697. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  3698. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  3699. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  3700. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  3701. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  3702. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  3703. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  3704. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  3705. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  3706. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  3707. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  3708. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  3709. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  3710. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  3711. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  3712. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  3713. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  3714. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  3715. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  3716. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  3717. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  3718. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  3719. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  3720. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  3721. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  3722. };
  3723. constexpr constant static uint8_t ksigns_iq2xs[128] = {
  3724. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  3725. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  3726. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  3727. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  3728. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  3729. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  3730. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  3731. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  3732. };
  3733. constexpr constant static uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  3734. void kernel_mul_mv_iq2_xxs_f32_impl(
  3735. device const void * src0,
  3736. device const float * src1,
  3737. device float * dst,
  3738. constant int64_t & ne00,
  3739. constant int64_t & ne01,
  3740. constant int64_t & ne02,
  3741. constant int64_t & ne10,
  3742. constant int64_t & ne12,
  3743. constant int64_t & ne0,
  3744. constant int64_t & ne1,
  3745. constant uint & r2,
  3746. constant uint & r3,
  3747. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3748. uint3 tgpig[[threadgroup_position_in_grid]],
  3749. uint tiisg[[thread_index_in_simdgroup]],
  3750. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3751. const int nb = ne00/QK_K;
  3752. const int r0 = tgpig.x;
  3753. const int r1 = tgpig.y;
  3754. const int im = tgpig.z;
  3755. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3756. const int ib_row = first_row * nb;
  3757. const uint i12 = im%ne12;
  3758. const uint i13 = im/ne12;
  3759. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3760. device const block_iq2_xxs * x = (device const block_iq2_xxs *) src0 + ib_row + offset0;
  3761. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3762. float yl[32];
  3763. float sumf[N_DST]={0.f}, all_sum;
  3764. const int nb32 = nb * (QK_K / 32);
  3765. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3766. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3767. {
  3768. int nval = 4;
  3769. int pos = (32*sgitg + tiisg)*nval;
  3770. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xxs_grid[pos + i];
  3771. nval = 2;
  3772. pos = (32*sgitg + tiisg)*nval;
  3773. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3774. threadgroup_barrier(mem_flags::mem_threadgroup);
  3775. }
  3776. const int ix = tiisg;
  3777. device const float * y4 = y + 32 * ix;
  3778. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3779. for (int i = 0; i < 32; ++i) {
  3780. yl[i] = y4[i];
  3781. }
  3782. const int ibl = ib32 / (QK_K / 32);
  3783. const int ib = ib32 % (QK_K / 32);
  3784. device const block_iq2_xxs * xr = x + ibl;
  3785. device const uint16_t * q2 = xr->qs + 4 * ib;
  3786. device const half * dh = &xr->d;
  3787. for (int row = 0; row < N_DST; row++) {
  3788. const float db = dh[0];
  3789. device const uint8_t * aux8 = (device const uint8_t *)q2;
  3790. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3791. const float d = db * (0.5f + (aux32 >> 28));
  3792. float sum = 0;
  3793. for (int l = 0; l < 4; ++l) {
  3794. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + aux8[l]);
  3795. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  3796. for (int j = 0; j < 8; ++j) {
  3797. sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3798. }
  3799. }
  3800. sumf[row] += d * sum;
  3801. dh += nb*sizeof(block_iq2_xxs)/2;
  3802. q2 += nb*sizeof(block_iq2_xxs)/2;
  3803. }
  3804. y4 += 32 * 32;
  3805. }
  3806. for (int row = 0; row < N_DST; ++row) {
  3807. all_sum = simd_sum(sumf[row]);
  3808. if (tiisg == 0) {
  3809. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3810. }
  3811. }
  3812. }
  3813. [[host_name("kernel_mul_mv_iq2_xxs_f32")]]
  3814. kernel void kernel_mul_mv_iq2_xxs_f32(
  3815. device const void * src0,
  3816. device const float * src1,
  3817. device float * dst,
  3818. constant int64_t & ne00,
  3819. constant int64_t & ne01,
  3820. constant int64_t & ne02,
  3821. constant uint64_t & nb00,
  3822. constant uint64_t & nb01,
  3823. constant uint64_t & nb02,
  3824. constant int64_t & ne10,
  3825. constant int64_t & ne11,
  3826. constant int64_t & ne12,
  3827. constant uint64_t & nb10,
  3828. constant uint64_t & nb11,
  3829. constant uint64_t & nb12,
  3830. constant int64_t & ne0,
  3831. constant int64_t & ne1,
  3832. constant uint & r2,
  3833. constant uint & r3,
  3834. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3835. uint3 tgpig[[threadgroup_position_in_grid]],
  3836. uint tiisg[[thread_index_in_simdgroup]],
  3837. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3838. kernel_mul_mv_iq2_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3839. }
  3840. void kernel_mul_mv_iq2_xs_f32_impl(
  3841. device const void * src0,
  3842. device const float * src1,
  3843. device float * dst,
  3844. constant int64_t & ne00,
  3845. constant int64_t & ne01,
  3846. constant int64_t & ne02,
  3847. constant int64_t & ne10,
  3848. constant int64_t & ne12,
  3849. constant int64_t & ne0,
  3850. constant int64_t & ne1,
  3851. constant uint & r2,
  3852. constant uint & r3,
  3853. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3854. uint3 tgpig[[threadgroup_position_in_grid]],
  3855. uint tiisg[[thread_index_in_simdgroup]],
  3856. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3857. const int nb = ne00/QK_K;
  3858. const int r0 = tgpig.x;
  3859. const int r1 = tgpig.y;
  3860. const int im = tgpig.z;
  3861. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3862. const int ib_row = first_row * nb;
  3863. const uint i12 = im%ne12;
  3864. const uint i13 = im/ne12;
  3865. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3866. device const block_iq2_xs * x = (device const block_iq2_xs *) src0 + ib_row + offset0;
  3867. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3868. float yl[32];
  3869. float sumf[N_DST]={0.f}, all_sum;
  3870. const int nb32 = nb * (QK_K / 32);
  3871. threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  3872. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 512);
  3873. {
  3874. int nval = 8;
  3875. int pos = (32*sgitg + tiisg)*nval;
  3876. for (int i = 0; i < nval; ++i) values[pos + i] = iq2xs_grid[pos + i];
  3877. nval = 2;
  3878. pos = (32*sgitg + tiisg)*nval;
  3879. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3880. threadgroup_barrier(mem_flags::mem_threadgroup);
  3881. }
  3882. const int ix = tiisg;
  3883. device const float * y4 = y + 32 * ix;
  3884. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  3885. for (int i = 0; i < 32; ++i) {
  3886. yl[i] = y4[i];
  3887. }
  3888. const int ibl = ib32 / (QK_K / 32);
  3889. const int ib = ib32 % (QK_K / 32);
  3890. device const block_iq2_xs * xr = x + ibl;
  3891. device const uint16_t * q2 = xr->qs + 4 * ib;
  3892. device const uint8_t * sc = xr->scales + ib;
  3893. device const half * dh = &xr->d;
  3894. for (int row = 0; row < N_DST; row++) {
  3895. const float db = dh[0];
  3896. const uint8_t ls1 = sc[0] & 0xf;
  3897. const uint8_t ls2 = sc[0] >> 4;
  3898. const float d1 = db * (0.5f + ls1);
  3899. const float d2 = db * (0.5f + ls2);
  3900. float sum1 = 0, sum2 = 0;
  3901. for (int l = 0; l < 2; ++l) {
  3902. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3903. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3904. for (int j = 0; j < 8; ++j) {
  3905. sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3906. }
  3907. }
  3908. for (int l = 2; l < 4; ++l) {
  3909. const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + (q2[l] & 511));
  3910. const uint8_t signs = shared_signs[(q2[l] >> 9)];
  3911. for (int j = 0; j < 8; ++j) {
  3912. sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3913. }
  3914. }
  3915. sumf[row] += d1 * sum1 + d2 * sum2;
  3916. dh += nb*sizeof(block_iq2_xs)/2;
  3917. q2 += nb*sizeof(block_iq2_xs)/2;
  3918. sc += nb*sizeof(block_iq2_xs);
  3919. }
  3920. y4 += 32 * 32;
  3921. }
  3922. for (int row = 0; row < N_DST; ++row) {
  3923. all_sum = simd_sum(sumf[row]);
  3924. if (tiisg == 0) {
  3925. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  3926. }
  3927. }
  3928. }
  3929. [[host_name("kernel_mul_mv_iq2_xs_f32")]]
  3930. kernel void kernel_mul_mv_iq2_xs_f32(
  3931. device const void * src0,
  3932. device const float * src1,
  3933. device float * dst,
  3934. constant int64_t & ne00,
  3935. constant int64_t & ne01,
  3936. constant int64_t & ne02,
  3937. constant uint64_t & nb00,
  3938. constant uint64_t & nb01,
  3939. constant uint64_t & nb02,
  3940. constant int64_t & ne10,
  3941. constant int64_t & ne11,
  3942. constant int64_t & ne12,
  3943. constant uint64_t & nb10,
  3944. constant uint64_t & nb11,
  3945. constant uint64_t & nb12,
  3946. constant int64_t & ne0,
  3947. constant int64_t & ne1,
  3948. constant uint & r2,
  3949. constant uint & r3,
  3950. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3951. uint3 tgpig[[threadgroup_position_in_grid]],
  3952. uint tiisg[[thread_index_in_simdgroup]],
  3953. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3954. kernel_mul_mv_iq2_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  3955. }
  3956. void kernel_mul_mv_iq3_xxs_f32_impl(
  3957. device const void * src0,
  3958. device const float * src1,
  3959. device float * dst,
  3960. constant int64_t & ne00,
  3961. constant int64_t & ne01,
  3962. constant int64_t & ne02,
  3963. constant int64_t & ne10,
  3964. constant int64_t & ne12,
  3965. constant int64_t & ne0,
  3966. constant int64_t & ne1,
  3967. constant uint & r2,
  3968. constant uint & r3,
  3969. threadgroup int8_t * shared_values [[threadgroup(0)]],
  3970. uint3 tgpig[[threadgroup_position_in_grid]],
  3971. uint tiisg[[thread_index_in_simdgroup]],
  3972. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  3973. const int nb = ne00/QK_K;
  3974. const int r0 = tgpig.x;
  3975. const int r1 = tgpig.y;
  3976. const int im = tgpig.z;
  3977. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  3978. const int ib_row = first_row * nb;
  3979. const uint i12 = im%ne12;
  3980. const uint i13 = im/ne12;
  3981. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  3982. device const block_iq3_xxs * x = (device const block_iq3_xxs *) src0 + ib_row + offset0;
  3983. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  3984. float yl[32];
  3985. float sumf[N_DST]={0.f}, all_sum;
  3986. const int nb32 = nb * (QK_K / 32);
  3987. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  3988. threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);
  3989. {
  3990. int nval = 4;
  3991. int pos = (32*sgitg + tiisg)*nval;
  3992. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xxs_grid[pos + i];
  3993. nval = 2;
  3994. pos = (32*sgitg + tiisg)*nval;
  3995. for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];
  3996. threadgroup_barrier(mem_flags::mem_threadgroup);
  3997. }
  3998. const int ix = tiisg;
  3999. device const float * y4 = y + 32 * ix;
  4000. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4001. for (int i = 0; i < 32; ++i) {
  4002. yl[i] = y4[i];
  4003. }
  4004. const int ibl = ib32 / (QK_K / 32);
  4005. const int ib = ib32 % (QK_K / 32);
  4006. device const block_iq3_xxs * xr = x + ibl;
  4007. device const uint8_t * q3 = xr->qs + 8 * ib;
  4008. device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib;
  4009. device const half * dh = &xr->d;
  4010. for (int row = 0; row < N_DST; row++) {
  4011. const float db = dh[0];
  4012. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4013. const float d = db * (0.5f + (aux32 >> 28));
  4014. float2 sum = {0};
  4015. for (int l = 0; l < 4; ++l) {
  4016. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + q3[2*l+0]);
  4017. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + q3[2*l+1]);
  4018. const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];
  4019. for (int j = 0; j < 4; ++j) {
  4020. sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4021. sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4022. }
  4023. }
  4024. sumf[row] += d * (sum[0] + sum[1]);
  4025. dh += nb*sizeof(block_iq3_xxs)/2;
  4026. q3 += nb*sizeof(block_iq3_xxs);
  4027. gas += nb*sizeof(block_iq3_xxs)/2;
  4028. }
  4029. y4 += 32 * 32;
  4030. }
  4031. for (int row = 0; row < N_DST; ++row) {
  4032. all_sum = simd_sum(sumf[row]);
  4033. if (tiisg == 0) {
  4034. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  4035. }
  4036. }
  4037. }
  4038. [[host_name("kernel_mul_mv_iq3_xxs_f32")]]
  4039. kernel void kernel_mul_mv_iq3_xxs_f32(
  4040. device const void * src0,
  4041. device const float * src1,
  4042. device float * dst,
  4043. constant int64_t & ne00,
  4044. constant int64_t & ne01,
  4045. constant int64_t & ne02,
  4046. constant uint64_t & nb00,
  4047. constant uint64_t & nb01,
  4048. constant uint64_t & nb02,
  4049. constant int64_t & ne10,
  4050. constant int64_t & ne11,
  4051. constant int64_t & ne12,
  4052. constant uint64_t & nb10,
  4053. constant uint64_t & nb11,
  4054. constant uint64_t & nb12,
  4055. constant int64_t & ne0,
  4056. constant int64_t & ne1,
  4057. constant uint & r2,
  4058. constant uint & r3,
  4059. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4060. uint3 tgpig[[threadgroup_position_in_grid]],
  4061. uint tiisg[[thread_index_in_simdgroup]],
  4062. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4063. kernel_mul_mv_iq3_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4064. }
  4065. void kernel_mul_mv_iq3_s_f32_impl(
  4066. device const void * src0,
  4067. device const float * src1,
  4068. device float * dst,
  4069. constant int64_t & ne00,
  4070. constant int64_t & ne01,
  4071. constant int64_t & ne02,
  4072. constant int64_t & ne10,
  4073. constant int64_t & ne12,
  4074. constant int64_t & ne0,
  4075. constant int64_t & ne1,
  4076. constant uint & r2,
  4077. constant uint & r3,
  4078. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4079. uint3 tgpig[[threadgroup_position_in_grid]],
  4080. uint tiisg[[thread_index_in_simdgroup]],
  4081. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4082. const int nb = ne00/QK_K;
  4083. const int r0 = tgpig.x;
  4084. const int r1 = tgpig.y;
  4085. const int im = tgpig.z;
  4086. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4087. const int ib_row = first_row * nb;
  4088. const uint i12 = im%ne12;
  4089. const uint i13 = im/ne12;
  4090. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4091. device const block_iq3_s * x = (device const block_iq3_s *) src0 + ib_row + offset0;
  4092. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4093. float yl[32];
  4094. float sumf[N_DST]={0.f}, all_sum;
  4095. const int nb32 = nb * (QK_K / 32);
  4096. threadgroup uint32_t * values = (threadgroup uint32_t *)shared_values;
  4097. {
  4098. int nval = 8;
  4099. int pos = (32*sgitg + tiisg)*nval;
  4100. for (int i = 0; i < nval; ++i) values[pos + i] = iq3xs_grid[pos + i];
  4101. threadgroup_barrier(mem_flags::mem_threadgroup);
  4102. }
  4103. const int ix = tiisg;
  4104. device const float * y4 = y + 32 * ix;
  4105. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4106. for (int i = 0; i < 32; ++i) {
  4107. yl[i] = y4[i];
  4108. }
  4109. const int ibl = ib32 / (QK_K / 32);
  4110. const int ib = ib32 % (QK_K / 32);
  4111. device const block_iq3_s * xr = x + ibl;
  4112. device const uint8_t * qs = xr->qs + 8 * ib;
  4113. device const uint8_t * qh = xr->qh + ib;
  4114. device const uint8_t * sc = xr->scales + (ib/2);
  4115. device const uint8_t * signs = xr->signs + 4 * ib;
  4116. device const half * dh = &xr->d;
  4117. for (int row = 0; row < N_DST; row++) {
  4118. const float db = dh[0];
  4119. const float d = db * (0.5f + ((sc[0] >> 4*(ib%2)) & 0xf));
  4120. float2 sum = {0};
  4121. for (int l = 0; l < 4; ++l) {
  4122. const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  4123. const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  4124. for (int j = 0; j < 4; ++j) {
  4125. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]);
  4126. sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]);
  4127. }
  4128. }
  4129. sumf[row] += d * (sum[0] + sum[1]);
  4130. dh += nb*sizeof(block_iq3_s)/2;
  4131. qs += nb*sizeof(block_iq3_s);
  4132. qh += nb*sizeof(block_iq3_s);
  4133. sc += nb*sizeof(block_iq3_s);
  4134. signs += nb*sizeof(block_iq3_s);
  4135. }
  4136. y4 += 32 * 32;
  4137. }
  4138. for (int row = 0; row < N_DST; ++row) {
  4139. all_sum = simd_sum(sumf[row]);
  4140. if (tiisg == 0) {
  4141. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.5f;
  4142. }
  4143. }
  4144. }
  4145. [[host_name("kernel_mul_mv_iq3_s_f32")]]
  4146. kernel void kernel_mul_mv_iq3_s_f32(
  4147. device const void * src0,
  4148. device const float * src1,
  4149. device float * dst,
  4150. constant int64_t & ne00,
  4151. constant int64_t & ne01,
  4152. constant int64_t & ne02,
  4153. constant uint64_t & nb00,
  4154. constant uint64_t & nb01,
  4155. constant uint64_t & nb02,
  4156. constant int64_t & ne10,
  4157. constant int64_t & ne11,
  4158. constant int64_t & ne12,
  4159. constant uint64_t & nb10,
  4160. constant uint64_t & nb11,
  4161. constant uint64_t & nb12,
  4162. constant int64_t & ne0,
  4163. constant int64_t & ne1,
  4164. constant uint & r2,
  4165. constant uint & r3,
  4166. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4167. uint3 tgpig[[threadgroup_position_in_grid]],
  4168. uint tiisg[[thread_index_in_simdgroup]],
  4169. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4170. kernel_mul_mv_iq3_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4171. }
  4172. void kernel_mul_mv_iq2_s_f32_impl(
  4173. device const void * src0,
  4174. device const float * src1,
  4175. device float * dst,
  4176. constant int64_t & ne00,
  4177. constant int64_t & ne01,
  4178. constant int64_t & ne02,
  4179. constant int64_t & ne10,
  4180. constant int64_t & ne12,
  4181. constant int64_t & ne0,
  4182. constant int64_t & ne1,
  4183. constant uint & r2,
  4184. constant uint & r3,
  4185. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4186. uint3 tgpig[[threadgroup_position_in_grid]],
  4187. uint tiisg[[thread_index_in_simdgroup]],
  4188. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4189. const int nb = ne00/QK_K;
  4190. const int r0 = tgpig.x;
  4191. const int r1 = tgpig.y;
  4192. const int im = tgpig.z;
  4193. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4194. const int ib_row = first_row * nb;
  4195. const uint i12 = im%ne12;
  4196. const uint i13 = im/ne12;
  4197. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4198. device const block_iq2_s * x = (device const block_iq2_s *) src0 + ib_row + offset0;
  4199. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4200. float yl[32];
  4201. float sumf[N_DST]={0.f}, all_sum;
  4202. const int nb32 = nb * (QK_K / 32);
  4203. //threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;
  4204. //{
  4205. // int nval = 32;
  4206. // int pos = (32*sgitg + tiisg)*nval;
  4207. // for (int i = 0; i < nval; ++i) values[pos + i] = iq2s_grid[pos + i];
  4208. // threadgroup_barrier(mem_flags::mem_threadgroup);
  4209. //}
  4210. const int ix = tiisg;
  4211. device const float * y4 = y + 32 * ix;
  4212. for (int ib32 = ix; ib32 < nb32; ib32 += 32) {
  4213. for (int i = 0; i < 32; ++i) {
  4214. yl[i] = y4[i];
  4215. }
  4216. const int ibl = ib32 / (QK_K / 32);
  4217. const int ib = ib32 % (QK_K / 32);
  4218. device const block_iq2_s * xr = x + ibl;
  4219. device const uint8_t * qs = xr->qs + 4 * ib;
  4220. device const uint8_t * qh = xr->qh + ib;
  4221. device const uint8_t * sc = xr->scales + ib;
  4222. device const uint8_t * signs = qs + QK_K/8;
  4223. device const half * dh = &xr->d;
  4224. for (int row = 0; row < N_DST; row++) {
  4225. const float db = dh[0];
  4226. const float d1 = db * (0.5f + (sc[0] & 0xf));
  4227. const float d2 = db * (0.5f + (sc[0] >> 4));
  4228. float2 sum = {0};
  4229. for (int l = 0; l < 2; ++l) {
  4230. //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(values + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4231. //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(values + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4232. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300)));
  4233. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300)));
  4234. for (int j = 0; j < 8; ++j) {
  4235. sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]);
  4236. sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]);
  4237. }
  4238. }
  4239. sumf[row] += d1 * sum[0] + d2 * sum[1];
  4240. dh += nb*sizeof(block_iq2_s)/2;
  4241. qs += nb*sizeof(block_iq2_s);
  4242. qh += nb*sizeof(block_iq2_s);
  4243. sc += nb*sizeof(block_iq2_s);
  4244. signs += nb*sizeof(block_iq2_s);
  4245. }
  4246. y4 += 32 * 32;
  4247. }
  4248. for (int row = 0; row < N_DST; ++row) {
  4249. all_sum = simd_sum(sumf[row]);
  4250. if (tiisg == 0) {
  4251. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;
  4252. }
  4253. }
  4254. }
  4255. [[host_name("kernel_mul_mv_iq2_s_f32")]]
  4256. kernel void kernel_mul_mv_iq2_s_f32(
  4257. device const void * src0,
  4258. device const float * src1,
  4259. device float * dst,
  4260. constant int64_t & ne00,
  4261. constant int64_t & ne01,
  4262. constant int64_t & ne02,
  4263. constant uint64_t & nb00,
  4264. constant uint64_t & nb01,
  4265. constant uint64_t & nb02,
  4266. constant int64_t & ne10,
  4267. constant int64_t & ne11,
  4268. constant int64_t & ne12,
  4269. constant uint64_t & nb10,
  4270. constant uint64_t & nb11,
  4271. constant uint64_t & nb12,
  4272. constant int64_t & ne0,
  4273. constant int64_t & ne1,
  4274. constant uint & r2,
  4275. constant uint & r3,
  4276. threadgroup int8_t * shared_values [[threadgroup(0)]],
  4277. uint3 tgpig[[threadgroup_position_in_grid]],
  4278. uint tiisg[[thread_index_in_simdgroup]],
  4279. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4280. kernel_mul_mv_iq2_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4281. }
  4282. void kernel_mul_mv_iq1_s_f32_impl(
  4283. device const void * src0,
  4284. device const float * src1,
  4285. device float * dst,
  4286. constant int64_t & ne00,
  4287. constant int64_t & ne01,
  4288. constant int64_t & ne02,
  4289. constant int64_t & ne10,
  4290. constant int64_t & ne12,
  4291. constant int64_t & ne0,
  4292. constant int64_t & ne1,
  4293. constant uint & r2,
  4294. constant uint & r3,
  4295. uint3 tgpig[[threadgroup_position_in_grid]],
  4296. uint tiisg[[thread_index_in_simdgroup]],
  4297. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4298. const int nb = ne00/QK_K;
  4299. const int r0 = tgpig.x;
  4300. const int r1 = tgpig.y;
  4301. const int im = tgpig.z;
  4302. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  4303. const int ib_row = first_row * nb;
  4304. const uint i12 = im%ne12;
  4305. const uint i13 = im/ne12;
  4306. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4307. device const block_iq1_s * x = (device const block_iq1_s *) src0 + ib_row + offset0;
  4308. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4309. float yl[16];
  4310. float sumf[N_DST]={0.f}, all_sum;
  4311. const int nb32 = nb * (QK_K / 32);
  4312. const int ix = tiisg/2;
  4313. const int il = tiisg%2;
  4314. device const float * y4 = y + 32 * ix + 16 * il;
  4315. for (int ib32 = ix; ib32 < nb32; ib32 += 16) {
  4316. for (int i = 0; i < 16; ++i) {
  4317. yl[i] = y4[i];
  4318. }
  4319. const int ibl = ib32 / (QK_K / 32);
  4320. const int ib = ib32 % (QK_K / 32);
  4321. device const block_iq1_s * xr = x + ibl;
  4322. device const uint8_t * qs = xr->qs + 4 * ib + 2 * il;
  4323. device const uint8_t * sc = xr->scales + 2 * ib + il;
  4324. device const half * dh = &xr->d;
  4325. for (int row = 0; row < N_DST; row++) {
  4326. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4327. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4328. float2 sum = {0};
  4329. for (int j = 0; j < 8; ++j) {
  4330. sum[0] += yl[j+ 0] * grid1[j];
  4331. sum[1] += yl[j+ 8] * grid2[j];
  4332. }
  4333. sumf[row] += (float)dh[0] * (sum[0] * (2*(sc[0] & 7) + 1) + sum[1] * (2*((sc[0] >> 4) & 7) + 1));
  4334. dh += nb*sizeof(block_iq1_s)/2;
  4335. qs += nb*sizeof(block_iq1_s);
  4336. sc += nb*sizeof(block_iq1_s);
  4337. }
  4338. y4 += 16 * 32;
  4339. }
  4340. for (int row = 0; row < N_DST; ++row) {
  4341. all_sum = simd_sum(sumf[row]);
  4342. if (tiisg == 0) {
  4343. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4344. }
  4345. }
  4346. }
  4347. constexpr constant static float kvalues_iq4nl_f[16] = {
  4348. -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
  4349. };
  4350. void kernel_mul_mv_iq4_nl_f32_impl(
  4351. device const void * src0,
  4352. device const float * src1,
  4353. device float * dst,
  4354. constant int64_t & ne00,
  4355. constant int64_t & ne01,
  4356. constant int64_t & ne02,
  4357. constant int64_t & ne10,
  4358. constant int64_t & ne12,
  4359. constant int64_t & ne0,
  4360. constant int64_t & ne1,
  4361. constant uint & r2,
  4362. constant uint & r3,
  4363. threadgroup float * shared_values [[threadgroup(0)]],
  4364. uint3 tgpig[[threadgroup_position_in_grid]],
  4365. uint tiisg[[thread_index_in_simdgroup]],
  4366. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4367. const int nb = ne00/QK4_NL;
  4368. const int r0 = tgpig.x;
  4369. const int r1 = tgpig.y;
  4370. const int im = tgpig.z;
  4371. const int first_row = (r0 * 2 + sgitg) * 2;
  4372. const int ib_row = first_row * nb;
  4373. const uint i12 = im%ne12;
  4374. const uint i13 = im/ne12;
  4375. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4376. device const block_iq4_nl * x = (device const block_iq4_nl *) src0 + ib_row + offset0;
  4377. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4378. const int ix = tiisg/2; // 0...15
  4379. const int it = tiisg%2; // 0 or 1
  4380. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4381. threadgroup_barrier(mem_flags::mem_threadgroup);
  4382. float4 yl[4];
  4383. float sumf[2]={0.f}, all_sum;
  4384. device const float * yb = y + ix * QK4_NL + it * 8;
  4385. uint32_t aux32[2];
  4386. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4387. float4 qf1, qf2;
  4388. for (int ib = ix; ib < nb; ib += 16) {
  4389. device const float4 * y4 = (device const float4 *)yb;
  4390. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4391. for (int row = 0; row < 2; ++row) {
  4392. device const block_iq4_nl & xb = x[row*nb + ib];
  4393. device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it);
  4394. float4 acc1 = {0.f}, acc2 = {0.f};
  4395. aux32[0] = q4[0] | (q4[1] << 16);
  4396. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4397. aux32[0] &= 0x0f0f0f0f;
  4398. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4399. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4400. acc1 += yl[0] * qf1;
  4401. acc2 += yl[1] * qf2;
  4402. aux32[0] = q4[2] | (q4[3] << 16);
  4403. aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f;
  4404. aux32[0] &= 0x0f0f0f0f;
  4405. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4406. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4407. acc1 += yl[2] * qf1;
  4408. acc2 += yl[3] * qf2;
  4409. acc1 += acc2;
  4410. sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4411. }
  4412. yb += 16 * QK4_NL;
  4413. }
  4414. for (int row = 0; row < 2; ++row) {
  4415. all_sum = simd_sum(sumf[row]);
  4416. if (tiisg == 0) {
  4417. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4418. }
  4419. }
  4420. }
  4421. #if QK_K != 64
  4422. void kernel_mul_mv_iq4_xs_f32_impl(
  4423. device const void * src0,
  4424. device const float * src1,
  4425. device float * dst,
  4426. constant int64_t & ne00,
  4427. constant int64_t & ne01,
  4428. constant int64_t & ne02,
  4429. constant int64_t & ne10,
  4430. constant int64_t & ne12,
  4431. constant int64_t & ne0,
  4432. constant int64_t & ne1,
  4433. constant uint & r2,
  4434. constant uint & r3,
  4435. threadgroup float * shared_values [[threadgroup(0)]],
  4436. uint3 tgpig[[threadgroup_position_in_grid]],
  4437. uint tiisg[[thread_index_in_simdgroup]],
  4438. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4439. const int nb = ne00/QK_K;
  4440. const int r0 = tgpig.x;
  4441. const int r1 = tgpig.y;
  4442. const int im = tgpig.z;
  4443. const int first_row = (r0 * 2 + sgitg) * 2;
  4444. const int ib_row = first_row * nb;
  4445. const uint i12 = im%ne12;
  4446. const uint i13 = im/ne12;
  4447. const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
  4448. device const block_iq4_xs * x = (device const block_iq4_xs *) src0 + ib_row + offset0;
  4449. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  4450. const int ix = tiisg/16; // 0 or 1
  4451. const int it = tiisg%16; // 0...15
  4452. const int ib = it/2;
  4453. const int il = it%2;
  4454. shared_values[tiisg] = kvalues_iq4nl_f[tiisg%16];
  4455. threadgroup_barrier(mem_flags::mem_threadgroup);
  4456. float4 yl[4];
  4457. float sumf[2]={0.f}, all_sum;
  4458. device const float * yb = y + ix * QK_K + ib * 32 + il * 8;
  4459. uint32_t aux32[2];
  4460. thread const uint8_t * q8 = (thread const uint8_t *)aux32;
  4461. float4 qf1, qf2;
  4462. for (int ibl = ix; ibl < nb; ibl += 2) {
  4463. device const float4 * y4 = (device const float4 *)yb;
  4464. yl[0] = y4[0]; yl[1] = y4[4]; yl[2] = y4[1]; yl[3] = y4[5];
  4465. for (int row = 0; row < 2; ++row) {
  4466. device const block_iq4_xs & xb = x[row*nb + ibl];
  4467. device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il);
  4468. float4 acc1 = {0.f}, acc2 = {0.f};
  4469. aux32[0] = q4[0] & 0x0f0f0f0f;
  4470. aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f;
  4471. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4472. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4473. acc1 += yl[0] * qf1;
  4474. acc2 += yl[1] * qf2;
  4475. aux32[0] = q4[1] & 0x0f0f0f0f;
  4476. aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f;
  4477. qf1 = {shared_values[q8[0]], shared_values[q8[1]], shared_values[q8[2]], shared_values[q8[3]]};
  4478. qf2 = {shared_values[q8[4]], shared_values[q8[5]], shared_values[q8[6]], shared_values[q8[7]]};
  4479. acc1 += yl[2] * qf1;
  4480. acc2 += yl[3] * qf2;
  4481. acc1 += acc2;
  4482. const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32;
  4483. sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]);
  4484. }
  4485. yb += 2 * QK_K;
  4486. }
  4487. for (int row = 0; row < 2; ++row) {
  4488. all_sum = simd_sum(sumf[row]);
  4489. if (tiisg == 0) {
  4490. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum;
  4491. }
  4492. }
  4493. }
  4494. #endif
  4495. [[host_name("kernel_mul_mv_iq1_s_f32")]]
  4496. kernel void kernel_mul_mv_iq1_s_f32(
  4497. device const void * src0,
  4498. device const float * src1,
  4499. device float * dst,
  4500. constant int64_t & ne00,
  4501. constant int64_t & ne01,
  4502. constant int64_t & ne02,
  4503. constant uint64_t & nb00,
  4504. constant uint64_t & nb01,
  4505. constant uint64_t & nb02,
  4506. constant int64_t & ne10,
  4507. constant int64_t & ne11,
  4508. constant int64_t & ne12,
  4509. constant uint64_t & nb10,
  4510. constant uint64_t & nb11,
  4511. constant uint64_t & nb12,
  4512. constant int64_t & ne0,
  4513. constant int64_t & ne1,
  4514. constant uint & r2,
  4515. constant uint & r3,
  4516. uint3 tgpig[[threadgroup_position_in_grid]],
  4517. uint tiisg[[thread_index_in_simdgroup]],
  4518. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4519. kernel_mul_mv_iq1_s_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);
  4520. }
  4521. [[host_name("kernel_mul_mv_iq4_nl_f32")]]
  4522. kernel void kernel_mul_mv_iq4_nl_f32(
  4523. device const void * src0,
  4524. device const float * src1,
  4525. device float * dst,
  4526. constant int64_t & ne00,
  4527. constant int64_t & ne01,
  4528. constant int64_t & ne02,
  4529. constant uint64_t & nb00,
  4530. constant uint64_t & nb01,
  4531. constant uint64_t & nb02,
  4532. constant int64_t & ne10,
  4533. constant int64_t & ne11,
  4534. constant int64_t & ne12,
  4535. constant uint64_t & nb10,
  4536. constant uint64_t & nb11,
  4537. constant uint64_t & nb12,
  4538. constant int64_t & ne0,
  4539. constant int64_t & ne1,
  4540. constant uint & r2,
  4541. constant uint & r3,
  4542. threadgroup float * shared_values [[threadgroup(0)]],
  4543. uint3 tgpig[[threadgroup_position_in_grid]],
  4544. uint tiisg[[thread_index_in_simdgroup]],
  4545. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4546. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4547. }
  4548. [[host_name("kernel_mul_mv_iq4_xs_f32")]]
  4549. kernel void kernel_mul_mv_iq4_xs_f32(
  4550. device const void * src0,
  4551. device const float * src1,
  4552. device float * dst,
  4553. constant int64_t & ne00,
  4554. constant int64_t & ne01,
  4555. constant int64_t & ne02,
  4556. constant uint64_t & nb00,
  4557. constant uint64_t & nb01,
  4558. constant uint64_t & nb02,
  4559. constant int64_t & ne10,
  4560. constant int64_t & ne11,
  4561. constant int64_t & ne12,
  4562. constant uint64_t & nb10,
  4563. constant uint64_t & nb11,
  4564. constant uint64_t & nb12,
  4565. constant int64_t & ne0,
  4566. constant int64_t & ne1,
  4567. constant uint & r2,
  4568. constant uint & r3,
  4569. threadgroup float * shared_values [[threadgroup(0)]],
  4570. uint3 tgpig[[threadgroup_position_in_grid]],
  4571. uint tiisg[[thread_index_in_simdgroup]],
  4572. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  4573. #if QK_K == 64
  4574. kernel_mul_mv_iq4_nl_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4575. #else
  4576. kernel_mul_mv_iq4_xs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);
  4577. #endif
  4578. }
  4579. //============================= templates and their specializations =============================
  4580. // NOTE: this is not dequantizing - we are simply fitting the template
  4581. template <typename type4x4>
  4582. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  4583. float4x4 temp = *(((device float4x4 *)src));
  4584. for (int i = 0; i < 16; i++){
  4585. reg[i/4][i%4] = temp[i/4][i%4];
  4586. }
  4587. }
  4588. template <typename type4x4>
  4589. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  4590. half4x4 temp = *(((device half4x4 *)src));
  4591. for (int i = 0; i < 16; i++){
  4592. reg[i/4][i%4] = temp[i/4][i%4];
  4593. }
  4594. }
  4595. template <typename type4x4>
  4596. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  4597. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  4598. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4599. const float d2 = d1 / 256.f;
  4600. const float md = -8.h * xb->d;
  4601. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4602. const ushort mask1 = mask0 << 8;
  4603. for (int i=0;i<8;i++) {
  4604. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  4605. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  4606. }
  4607. }
  4608. template <typename type4x4>
  4609. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  4610. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  4611. const float d1 = il ? (xb->d / 16.h) : xb->d;
  4612. const float d2 = d1 / 256.f;
  4613. const float m = xb->m;
  4614. const ushort mask0 = il ? 0x00F0 : 0x000F;
  4615. const ushort mask1 = mask0 << 8;
  4616. for (int i=0;i<8;i++) {
  4617. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  4618. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  4619. }
  4620. }
  4621. template <typename type4x4>
  4622. void dequantize_q5_0(device const block_q5_0 *xb, short il, thread type4x4 & reg) {
  4623. device const uint16_t * qs = ((device const uint16_t *)xb + 3);
  4624. const float d = xb->d;
  4625. const float md = -16.h * xb->d;
  4626. const ushort mask = il ? 0x00F0 : 0x000F;
  4627. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4628. const int x_mv = il ? 4 : 0;
  4629. const int gh_mv = il ? 12 : 0;
  4630. const int gh_bk = il ? 0 : 4;
  4631. for (int i = 0; i < 8; i++) {
  4632. // extract the 5-th bits for x0 and x1
  4633. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4634. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4635. // combine the 4-bits from qs with the 5th bit
  4636. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4637. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4638. reg[i/2][2*(i%2)+0] = d * x0 + md;
  4639. reg[i/2][2*(i%2)+1] = d * x1 + md;
  4640. }
  4641. }
  4642. template <typename type4x4>
  4643. void dequantize_q5_1(device const block_q5_1 *xb, short il, thread type4x4 & reg) {
  4644. device const uint16_t * qs = ((device const uint16_t *)xb + 4);
  4645. const float d = xb->d;
  4646. const float m = xb->m;
  4647. const ushort mask = il ? 0x00F0 : 0x000F;
  4648. const uint32_t qh = *((device const uint32_t *)xb->qh);
  4649. const int x_mv = il ? 4 : 0;
  4650. const int gh_mv = il ? 12 : 0;
  4651. const int gh_bk = il ? 0 : 4;
  4652. for (int i = 0; i < 8; i++) {
  4653. // extract the 5-th bits for x0 and x1
  4654. const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10;
  4655. const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
  4656. // combine the 4-bits from qs with the 5th bit
  4657. const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0);
  4658. const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
  4659. reg[i/2][2*(i%2)+0] = d * x0 + m;
  4660. reg[i/2][2*(i%2)+1] = d * x1 + m;
  4661. }
  4662. }
  4663. template <typename type4x4>
  4664. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  4665. device const int8_t * qs = ((device const int8_t *)xb->qs);
  4666. const half d = xb->d;
  4667. for (int i = 0; i < 16; i++) {
  4668. reg[i/4][i%4] = (qs[i + 16*il] * d);
  4669. }
  4670. }
  4671. template <typename type4x4>
  4672. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  4673. const float d = xb->d;
  4674. const float min = xb->dmin;
  4675. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4676. float dl, ml;
  4677. uint8_t sc = xb->scales[il];
  4678. #if QK_K == 256
  4679. q = q + 32*(il/8) + 16*(il&1);
  4680. il = (il/2)%4;
  4681. #endif
  4682. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4683. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4684. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  4685. for (int i = 0; i < 16; ++i) {
  4686. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4687. }
  4688. }
  4689. template <typename type4x4>
  4690. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  4691. const half d_all = xb->d;
  4692. device const uint8_t * q = (device const uint8_t *)xb->qs;
  4693. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  4694. device const int8_t * scales = (device const int8_t *)xb->scales;
  4695. #if QK_K == 256
  4696. q = q + 32 * (il/8) + 16 * (il&1);
  4697. h = h + 16 * (il&1);
  4698. uint8_t m = 1 << (il/2);
  4699. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  4700. ((il/4)>0 ? 12 : 3);
  4701. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  4702. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  4703. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  4704. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  4705. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  4706. const float ml = 4.f * dl;
  4707. il = (il/2) & 3;
  4708. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4709. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4710. dl *= coef;
  4711. for (int i = 0; i < 16; ++i) {
  4712. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  4713. }
  4714. #else
  4715. float kcoef = il&1 ? 1.f/16.f : 1.f;
  4716. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  4717. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  4718. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  4719. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4720. uint8_t m = 1<<(il*2);
  4721. for (int i = 0; i < 16; ++i) {
  4722. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  4723. }
  4724. #endif
  4725. }
  4726. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  4727. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  4728. : 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))};
  4729. }
  4730. template <typename type4x4>
  4731. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  4732. device const uchar * q = xb->qs;
  4733. #if QK_K == 256
  4734. short is = (il/4) * 2;
  4735. q = q + (il/4) * 32 + 16 * (il&1);
  4736. il = il & 3;
  4737. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4738. const float d = il < 2 ? xb->d : xb->d / 16.h;
  4739. const float min = xb->dmin;
  4740. const float dl = d * sc[0];
  4741. const float ml = min * sc[1];
  4742. #else
  4743. (void) get_scale_min_k4_just2;
  4744. q = q + 16 * (il&1);
  4745. device const uint8_t * s = xb->scales;
  4746. device const half2 * dh = (device const half2 *)xb->d;
  4747. const float2 d = (float2)dh[0];
  4748. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  4749. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  4750. #endif
  4751. const ushort mask = il<2 ? 0x0F : 0xF0;
  4752. for (int i = 0; i < 16; ++i) {
  4753. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  4754. }
  4755. }
  4756. template <typename type4x4>
  4757. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  4758. device const uint8_t * q = xb->qs;
  4759. device const uint8_t * qh = xb->qh;
  4760. #if QK_K == 256
  4761. short is = (il/4) * 2;
  4762. q = q + 32 * (il/4) + 16 * (il&1);
  4763. qh = qh + 16 * (il&1);
  4764. uint8_t ul = 1 << (il/2);
  4765. il = il & 3;
  4766. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  4767. const float d = il < 2 ? xb->d : xb->d / 16.f;
  4768. const float min = xb->dmin;
  4769. const float dl = d * sc[0];
  4770. const float ml = min * sc[1];
  4771. const ushort mask = il<2 ? 0x0F : 0xF0;
  4772. const float qh_val = il<2 ? 16.f : 256.f;
  4773. for (int i = 0; i < 16; ++i) {
  4774. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  4775. }
  4776. #else
  4777. q = q + 16 * (il&1);
  4778. device const int8_t * s = xb->scales;
  4779. const float dl = xb->d * s[il];
  4780. uint8_t m = 1<<(il*2);
  4781. const float coef = il<2 ? 1.f : 1.f/16.f;
  4782. const ushort mask = il<2 ? 0x0F : 0xF0;
  4783. for (int i = 0; i < 16; ++i) {
  4784. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  4785. }
  4786. #endif
  4787. }
  4788. template <typename type4x4>
  4789. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  4790. const half d_all = xb->d;
  4791. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  4792. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  4793. device const int8_t * scales = (device const int8_t *)xb->scales;
  4794. #if QK_K == 256
  4795. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  4796. qh = qh + 32*(il/8) + 16*(il&1);
  4797. float sc = scales[(il%2) + 2 * ((il/2))];
  4798. il = (il/2) & 3;
  4799. #else
  4800. ql = ql + 16 * (il&1);
  4801. float sc = scales[il];
  4802. #endif
  4803. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  4804. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  4805. const float coef = il>1 ? 1.f/16.f : 1.f;
  4806. const float ml = d_all * sc * 32.f;
  4807. const float dl = d_all * sc * coef;
  4808. for (int i = 0; i < 16; ++i) {
  4809. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  4810. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  4811. reg[i/4][i%4] = dl * q - ml;
  4812. }
  4813. }
  4814. template <typename type4x4>
  4815. void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
  4816. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4817. const float d = xb->d;
  4818. const int ib32 = il/2;
  4819. il = il%2;
  4820. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4821. // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
  4822. device const uint16_t * q2 = xb->qs + 4*ib32;
  4823. const uint32_t aux32_g = q2[0] | (q2[1] << 16);
  4824. const uint32_t aux32_s = q2[2] | (q2[3] << 16);
  4825. thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
  4826. const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
  4827. constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  4828. uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
  4829. for (int i = 0; i < 8; ++i) {
  4830. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4831. }
  4832. grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  4833. signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
  4834. for (int i = 0; i < 8; ++i) {
  4835. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4836. }
  4837. }
  4838. template <typename type4x4>
  4839. void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
  4840. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4841. const float d = xb->d;
  4842. const int ib32 = il/2;
  4843. il = il%2;
  4844. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4845. device const uint16_t * q2 = xb->qs + 4*ib32;
  4846. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4847. constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
  4848. uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
  4849. for (int i = 0; i < 8; ++i) {
  4850. reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4851. }
  4852. grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
  4853. signs = ksigns_iq2xs[q2[2*il+1] >> 9];
  4854. for (int i = 0; i < 8; ++i) {
  4855. reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
  4856. }
  4857. }
  4858. template <typename type4x4>
  4859. void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
  4860. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4861. const float d = xb->d;
  4862. const int ib32 = il/2;
  4863. il = il%2;
  4864. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4865. device const uint8_t * q3 = xb->qs + 8*ib32;
  4866. device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
  4867. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4868. const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
  4869. constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
  4870. constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
  4871. uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  4872. for (int i = 0; i < 4; ++i) {
  4873. reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4874. reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4875. }
  4876. grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
  4877. grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
  4878. signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
  4879. for (int i = 0; i < 4; ++i) {
  4880. reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
  4881. reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
  4882. }
  4883. }
  4884. template <typename type4x4>
  4885. void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
  4886. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4887. const float d = xb->d;
  4888. const int ib32 = il/2;
  4889. il = il%2;
  4890. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4891. device const uint8_t * qs = xb->qs + 8*ib32;
  4892. device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
  4893. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4894. const float dl = d * (0.5f + ((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * 0.5f;
  4895. constant uint8_t * grid1 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+0] | ((qh << 8) & 256)));
  4896. constant uint8_t * grid2 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+1] | ((qh << 7) & 256)));
  4897. for (int i = 0; i < 4; ++i) {
  4898. reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
  4899. reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
  4900. }
  4901. grid1 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+2] | ((qh << 6) & 256)));
  4902. grid2 = (constant uint8_t *)(iq3xs_grid + (qs[4*il+3] | ((qh << 5) & 256)));
  4903. for (int i = 0; i < 4; ++i) {
  4904. reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
  4905. reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
  4906. }
  4907. }
  4908. template <typename type4x4>
  4909. void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
  4910. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4911. const float d = xb->d;
  4912. const int ib32 = il/2;
  4913. il = il%2;
  4914. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4915. device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
  4916. device const uint8_t * signs = qs + QK_K/8;
  4917. const uint8_t qh = xb->qh[ib32] >> 4*il;
  4918. const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
  4919. constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
  4920. constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
  4921. for (int i = 0; i < 8; ++i) {
  4922. reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
  4923. reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
  4924. }
  4925. }
  4926. template <typename type4x4>
  4927. void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
  4928. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4929. const float d = xb->d;
  4930. device const uint8_t * qs = xb->qs + 2*il;
  4931. device const uint8_t * sc = xb->scales + il;
  4932. const float dl1 = d * (2*(sc[0] & 7) + 1);
  4933. const float dl2 = d * (2*((sc[0] >> 4) & 7) + 1);
  4934. constant int8_t * grid1 = (constant int8_t *)(iq1s_grid + (qs[0] | ((sc[0] & 0x08) << 5)));
  4935. constant int8_t * grid2 = (constant int8_t *)(iq1s_grid + (qs[1] | ((sc[0] & 0x80) << 1)));
  4936. for (int i = 0; i < 8; ++i) {
  4937. reg[i/4+0][i%4] = dl1 * grid1[i];
  4938. reg[i/4+2][i%4] = dl2 * grid2[i];
  4939. }
  4940. }
  4941. template <typename type4x4>
  4942. void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
  4943. device const uint16_t * q4 = (device const uint16_t *)xb->qs;
  4944. const float d = xb->d;
  4945. uint32_t aux32;
  4946. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4947. for (int i = 0; i < 4; ++i) {
  4948. aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
  4949. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4950. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4951. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4952. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4953. }
  4954. }
  4955. template <typename type4x4>
  4956. void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
  4957. #if QK_K == 64
  4958. dequantize_iq4_nl(xb, il, reg);
  4959. #else
  4960. // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
  4961. const int ib32 = il/2;
  4962. il = il%2;
  4963. // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
  4964. device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
  4965. const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
  4966. const float d = (float)xb->d * (ls - 32);
  4967. uint32_t aux32;
  4968. thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
  4969. for (int i = 0; i < 4; ++i) {
  4970. aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
  4971. reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
  4972. reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
  4973. reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
  4974. reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
  4975. }
  4976. #endif
  4977. }
  4978. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  4979. kernel void kernel_get_rows(
  4980. device const void * src0,
  4981. device const char * src1,
  4982. device float * dst,
  4983. constant int64_t & ne00,
  4984. constant uint64_t & nb01,
  4985. constant uint64_t & nb02,
  4986. constant int64_t & ne10,
  4987. constant uint64_t & nb10,
  4988. constant uint64_t & nb11,
  4989. constant uint64_t & nb1,
  4990. constant uint64_t & nb2,
  4991. uint3 tgpig[[threadgroup_position_in_grid]],
  4992. uint tiitg[[thread_index_in_threadgroup]],
  4993. uint3 tptg [[threads_per_threadgroup]]) {
  4994. //const int64_t i = tgpig;
  4995. //const int64_t r = ((device int32_t *) src1)[i];
  4996. const int64_t i10 = tgpig.x;
  4997. const int64_t i11 = tgpig.y;
  4998. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  4999. const int64_t i02 = i11;
  5000. for (int64_t ind = tiitg; ind < ne00/16; ind += tptg.x) {
  5001. float4x4 temp;
  5002. dequantize_func(
  5003. ((device const block_q *) ((device char *) src0 + r*nb01 + i02*nb02)) + ind/nl, ind%nl, temp);
  5004. *(((device float4x4 *) ((device char *) dst + i11*nb2 + i10*nb1)) + ind) = temp;
  5005. }
  5006. }
  5007. kernel void kernel_get_rows_f32(
  5008. device const void * src0,
  5009. device const char * src1,
  5010. device float * dst,
  5011. constant int64_t & ne00,
  5012. constant uint64_t & nb01,
  5013. constant uint64_t & nb02,
  5014. constant int64_t & ne10,
  5015. constant uint64_t & nb10,
  5016. constant uint64_t & nb11,
  5017. constant uint64_t & nb1,
  5018. constant uint64_t & nb2,
  5019. uint3 tgpig[[threadgroup_position_in_grid]],
  5020. uint tiitg[[thread_index_in_threadgroup]],
  5021. uint3 tptg [[threads_per_threadgroup]]) {
  5022. const int64_t i10 = tgpig.x;
  5023. const int64_t i11 = tgpig.y;
  5024. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5025. const int64_t i02 = i11;
  5026. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5027. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5028. ((device float *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5029. }
  5030. }
  5031. kernel void kernel_get_rows_f16(
  5032. device const void * src0,
  5033. device const char * src1,
  5034. device float * dst,
  5035. constant int64_t & ne00,
  5036. constant uint64_t & nb01,
  5037. constant uint64_t & nb02,
  5038. constant int64_t & ne10,
  5039. constant uint64_t & nb10,
  5040. constant uint64_t & nb11,
  5041. constant uint64_t & nb1,
  5042. constant uint64_t & nb2,
  5043. uint3 tgpig[[threadgroup_position_in_grid]],
  5044. uint tiitg[[thread_index_in_threadgroup]],
  5045. uint3 tptg [[threads_per_threadgroup]]) {
  5046. const int64_t i10 = tgpig.x;
  5047. const int64_t i11 = tgpig.y;
  5048. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5049. const int64_t i02 = i11;
  5050. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5051. ((device float *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5052. ((device half *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5053. }
  5054. }
  5055. kernel void kernel_get_rows_i32(
  5056. device const void * src0,
  5057. device const char * src1,
  5058. device int32_t * dst,
  5059. constant int64_t & ne00,
  5060. constant uint64_t & nb01,
  5061. constant uint64_t & nb02,
  5062. constant int64_t & ne10,
  5063. constant uint64_t & nb10,
  5064. constant uint64_t & nb11,
  5065. constant uint64_t & nb1,
  5066. constant uint64_t & nb2,
  5067. uint3 tgpig[[threadgroup_position_in_grid]],
  5068. uint tiitg[[thread_index_in_threadgroup]],
  5069. uint3 tptg [[threads_per_threadgroup]]) {
  5070. const int64_t i10 = tgpig.x;
  5071. const int64_t i11 = tgpig.y;
  5072. const int64_t r = ((device int32_t *) ((device char *) src1 + i11*nb11 + i10*nb10))[0];
  5073. const int64_t i02 = i11;
  5074. for (int ind = tiitg; ind < ne00; ind += tptg.x) {
  5075. ((device int32_t *) ((device char *) dst + i11*nb2 + i10*nb1))[ind] =
  5076. ((device int32_t *) ((device char *) src0 + r*nb01 + i02*nb02))[ind];
  5077. }
  5078. }
  5079. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  5080. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix B
  5081. #define BLOCK_SIZE_K 32
  5082. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  5083. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  5084. #define THREAD_PER_BLOCK 128
  5085. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  5086. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  5087. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  5088. #define SG_MAT_ROW 8
  5089. // each block_q contains 16*nl weights
  5090. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5091. void kernel_mul_mm_impl(device const uchar * src0,
  5092. device const uchar * src1,
  5093. device float * dst,
  5094. constant int64_t & ne00,
  5095. constant int64_t & ne02,
  5096. constant uint64_t & nb01,
  5097. constant uint64_t & nb02,
  5098. constant int64_t & ne12,
  5099. constant uint64_t & nb10,
  5100. constant uint64_t & nb11,
  5101. constant uint64_t & nb12,
  5102. constant int64_t & ne0,
  5103. constant int64_t & ne1,
  5104. constant uint & r2,
  5105. constant uint & r3,
  5106. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5107. uint3 tgpig[[threadgroup_position_in_grid]],
  5108. uint tiitg[[thread_index_in_threadgroup]],
  5109. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5110. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5111. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5112. const uint r0 = tgpig.y;
  5113. const uint r1 = tgpig.x;
  5114. const uint im = tgpig.z;
  5115. // if this block is of 64x32 shape or smaller
  5116. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5117. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5118. // a thread shouldn't load data outside of the matrix
  5119. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5120. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5121. simdgroup_half8x8 ma[4];
  5122. simdgroup_float8x8 mb[2];
  5123. simdgroup_float8x8 c_res[8];
  5124. for (int i = 0; i < 8; i++){
  5125. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5126. }
  5127. short il = (tiitg % THREAD_PER_ROW);
  5128. const uint i12 = im%ne12;
  5129. const uint i13 = im/ne12;
  5130. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5131. ushort offset1 = il/nl;
  5132. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5133. device const float * y = (device const float *)(src1
  5134. + nb12 * im
  5135. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  5136. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5137. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5138. // load data and store to threadgroup memory
  5139. half4x4 temp_a;
  5140. dequantize_func(x, il, temp_a);
  5141. threadgroup_barrier(mem_flags::mem_threadgroup);
  5142. #pragma unroll(16)
  5143. for (int i = 0; i < 16; i++) {
  5144. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5145. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5146. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5147. }
  5148. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5149. il = (il + 2 < nl) ? il + 2 : il % 2;
  5150. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5151. y += BLOCK_SIZE_K;
  5152. threadgroup_barrier(mem_flags::mem_threadgroup);
  5153. // load matrices from threadgroup memory and conduct outer products
  5154. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5155. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5156. #pragma unroll(4)
  5157. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5158. #pragma unroll(4)
  5159. for (int i = 0; i < 4; i++) {
  5160. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5161. }
  5162. simdgroup_barrier(mem_flags::mem_none);
  5163. #pragma unroll(2)
  5164. for (int i = 0; i < 2; i++) {
  5165. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5166. }
  5167. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5168. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5169. #pragma unroll(8)
  5170. for (int i = 0; i < 8; i++){
  5171. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5172. }
  5173. }
  5174. }
  5175. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  5176. device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
  5177. + (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
  5178. for (int i = 0; i < 8; i++) {
  5179. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  5180. }
  5181. } else {
  5182. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  5183. threadgroup_barrier(mem_flags::mem_threadgroup);
  5184. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5185. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5186. for (int i = 0; i < 8; i++) {
  5187. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5188. }
  5189. threadgroup_barrier(mem_flags::mem_threadgroup);
  5190. device float * C = dst + (BLOCK_SIZE_M * r0) + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  5191. if (sgitg == 0) {
  5192. for (int i = 0; i < n_rows; i++) {
  5193. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5194. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5195. }
  5196. }
  5197. }
  5198. }
  5199. }
  5200. // same as kernel_mul_mm_impl, but src1 and dst are accessed via indices stored in src1ids
  5201. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5202. void kernel_mul_mm_id_impl(
  5203. device const uchar * src0,
  5204. device const uchar * src1,
  5205. thread short * src1ids,
  5206. device float * dst,
  5207. constant int64_t & ne00,
  5208. constant int64_t & ne02,
  5209. constant uint64_t & nb01,
  5210. constant uint64_t & nb02,
  5211. constant int64_t & ne12,
  5212. constant uint64_t & nb10,
  5213. constant uint64_t & nb11,
  5214. constant uint64_t & nb12,
  5215. constant int64_t & ne0,
  5216. int64_t ne1,
  5217. constant uint & r2,
  5218. constant uint & r3,
  5219. threadgroup uchar * shared_memory,
  5220. uint3 tgpig[[threadgroup_position_in_grid]],
  5221. uint tiitg[[thread_index_in_threadgroup]],
  5222. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5223. threadgroup half * sa = (threadgroup half *)(shared_memory);
  5224. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  5225. const uint r0 = tgpig.y;
  5226. const uint r1 = tgpig.x;
  5227. const uint im = tgpig.z;
  5228. if (r1 * BLOCK_SIZE_N >= ne1) return;
  5229. // if this block is of 64x32 shape or smaller
  5230. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  5231. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  5232. // a thread shouldn't load data outside of the matrix
  5233. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  5234. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  5235. simdgroup_half8x8 ma[4];
  5236. simdgroup_float8x8 mb[2];
  5237. simdgroup_float8x8 c_res[8];
  5238. for (int i = 0; i < 8; i++){
  5239. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  5240. }
  5241. short il = (tiitg % THREAD_PER_ROW);
  5242. const uint i12 = im%ne12;
  5243. const uint i13 = im/ne12;
  5244. uint offset0 = (i12/r2)*nb02 + (i13/r3)*(nb02*ne02);
  5245. ushort offset1 = il/nl;
  5246. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  5247. device const float * y = (device const float *)(src1
  5248. + nb12 * im
  5249. + nb11 * src1ids[r1 * BLOCK_SIZE_N + thread_col]
  5250. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  5251. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  5252. // load data and store to threadgroup memory
  5253. half4x4 temp_a;
  5254. dequantize_func(x, il, temp_a);
  5255. threadgroup_barrier(mem_flags::mem_threadgroup);
  5256. for (int i = 0; i < 16; i++) {
  5257. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  5258. + (tiitg % THREAD_PER_ROW) * 16 + (i / 8) * 8) \
  5259. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  5260. }
  5261. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) = *((device float2x4 *)y);
  5262. il = (il + 2 < nl) ? il + 2 : il % 2;
  5263. x = (il < 2) ? x + (2+nl-1)/nl : x;
  5264. y += BLOCK_SIZE_K;
  5265. threadgroup_barrier(mem_flags::mem_threadgroup);
  5266. // load matrices from threadgroup memory and conduct outer products
  5267. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  5268. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  5269. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  5270. for (int i = 0; i < 4; i++) {
  5271. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  5272. }
  5273. simdgroup_barrier(mem_flags::mem_none);
  5274. for (int i = 0; i < 2; i++) {
  5275. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  5276. }
  5277. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  5278. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  5279. for (int i = 0; i < 8; i++){
  5280. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  5281. }
  5282. }
  5283. }
  5284. {
  5285. threadgroup_barrier(mem_flags::mem_threadgroup);
  5286. threadgroup float * temp_str = ((threadgroup float *)shared_memory) \
  5287. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  5288. for (int i = 0; i < 8; i++) {
  5289. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  5290. }
  5291. threadgroup_barrier(mem_flags::mem_threadgroup);
  5292. device float * C = dst + (BLOCK_SIZE_M * r0) + im*ne1*ne0;
  5293. if (sgitg == 0) {
  5294. for (int i = 0; i < n_rows; i++) {
  5295. for (int j = tiitg; j < n_cols; j += BLOCK_SIZE_N) {
  5296. *(C + i + src1ids[j + r1*BLOCK_SIZE_N] * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  5297. }
  5298. }
  5299. }
  5300. }
  5301. }
  5302. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5303. kernel void kernel_mul_mm(device const uchar * src0,
  5304. device const uchar * src1,
  5305. device float * dst,
  5306. constant int64_t & ne00,
  5307. constant int64_t & ne02,
  5308. constant uint64_t & nb01,
  5309. constant uint64_t & nb02,
  5310. constant int64_t & ne12,
  5311. constant uint64_t & nb10,
  5312. constant uint64_t & nb11,
  5313. constant uint64_t & nb12,
  5314. constant int64_t & ne0,
  5315. constant int64_t & ne1,
  5316. constant uint & r2,
  5317. constant uint & r3,
  5318. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5319. uint3 tgpig[[threadgroup_position_in_grid]],
  5320. uint tiitg[[thread_index_in_threadgroup]],
  5321. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5322. kernel_mul_mm_impl<block_q, nl, dequantize_func>(
  5323. src0,
  5324. src1,
  5325. dst,
  5326. ne00,
  5327. ne02,
  5328. nb01,
  5329. nb02,
  5330. ne12,
  5331. nb10,
  5332. nb11,
  5333. nb12,
  5334. ne0,
  5335. ne1,
  5336. r2,
  5337. r3,
  5338. shared_memory,
  5339. tgpig,
  5340. tiitg,
  5341. sgitg);
  5342. }
  5343. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  5344. kernel void kernel_mul_mm_id(
  5345. device const uchar * ids,
  5346. device const uchar * src1,
  5347. device float * dst,
  5348. constant uint64_t & nbi1,
  5349. constant int64_t & ne00,
  5350. constant int64_t & ne02,
  5351. constant uint64_t & nb01,
  5352. constant uint64_t & nb02,
  5353. constant int64_t & ne12,
  5354. constant int64_t & ne13,
  5355. constant uint64_t & nb10,
  5356. constant uint64_t & nb11,
  5357. constant uint64_t & nb12,
  5358. constant int64_t & ne0,
  5359. constant int64_t & ne1,
  5360. constant uint64_t & nb1,
  5361. constant uint & r2,
  5362. constant uint & r3,
  5363. constant int & idx,
  5364. device const uchar * src00,
  5365. device const uchar * src01,
  5366. device const uchar * src02,
  5367. device const uchar * src03,
  5368. device const uchar * src04,
  5369. device const uchar * src05,
  5370. device const uchar * src06,
  5371. device const uchar * src07,
  5372. threadgroup uchar * shared_memory [[threadgroup(0)]],
  5373. uint3 tgpig[[threadgroup_position_in_grid]],
  5374. uint tiitg[[thread_index_in_threadgroup]],
  5375. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5376. device const uchar * src0s[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5377. // expert id
  5378. const int32_t id = tgpig.z/(ne12*ne13);
  5379. tgpig.z = tgpig.z%(ne12*ne13);
  5380. // row indices of src1 for expert id
  5381. int64_t _ne1 = 0;
  5382. short src1ids[512];
  5383. for (int64_t i1 = 0; i1 < ne1; i1++) {
  5384. if (((device int32_t *) (ids + i1*nbi1))[idx] == id) {
  5385. src1ids[_ne1++] = i1;
  5386. }
  5387. }
  5388. kernel_mul_mm_id_impl<block_q, nl, dequantize_func>(
  5389. src0s[id],
  5390. src1,
  5391. src1ids,
  5392. dst,
  5393. ne00,
  5394. ne02,
  5395. nb01,
  5396. nb02,
  5397. ne12,
  5398. nb10,
  5399. nb11,
  5400. nb12,
  5401. ne0,
  5402. _ne1,
  5403. r2,
  5404. r3,
  5405. shared_memory,
  5406. tgpig,
  5407. tiitg,
  5408. sgitg);
  5409. }
  5410. #if QK_K == 256
  5411. #define QK_NL 16
  5412. #else
  5413. #define QK_NL 4
  5414. #endif
  5415. //
  5416. // get rows
  5417. //
  5418. typedef void (get_rows_t)(
  5419. device const void * src0,
  5420. device const char * src1,
  5421. device float * dst,
  5422. constant int64_t & ne00,
  5423. constant uint64_t & nb01,
  5424. constant uint64_t & nb02,
  5425. constant int64_t & ne10,
  5426. constant uint64_t & nb10,
  5427. constant uint64_t & nb11,
  5428. constant uint64_t & nb1,
  5429. constant uint64_t & nb2,
  5430. uint3, uint, uint3);
  5431. //template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  5432. //template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  5433. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  5434. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  5435. template [[host_name("kernel_get_rows_q5_0")]] kernel get_rows_t kernel_get_rows<block_q5_0, 2, dequantize_q5_0>;
  5436. template [[host_name("kernel_get_rows_q5_1")]] kernel get_rows_t kernel_get_rows<block_q5_1, 2, dequantize_q5_1>;
  5437. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  5438. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  5439. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  5440. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  5441. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  5442. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  5443. template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_t kernel_get_rows<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5444. template [[host_name("kernel_get_rows_iq2_xs")]] kernel get_rows_t kernel_get_rows<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5445. template [[host_name("kernel_get_rows_iq3_xxs")]] kernel get_rows_t kernel_get_rows<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5446. template [[host_name("kernel_get_rows_iq3_s")]] kernel get_rows_t kernel_get_rows<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5447. template [[host_name("kernel_get_rows_iq2_s")]] kernel get_rows_t kernel_get_rows<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5448. template [[host_name("kernel_get_rows_iq1_s")]] kernel get_rows_t kernel_get_rows<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5449. template [[host_name("kernel_get_rows_iq4_nl")]] kernel get_rows_t kernel_get_rows<block_iq4_nl, 2, dequantize_iq4_nl>;
  5450. #if QK_K == 64
  5451. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_t kernel_get_rows<block_iq4_xs, 2, dequantize_iq4_xs>;
  5452. #else
  5453. template [[host_name("kernel_get_rows_iq4_xs")]] kernel get_rows_t kernel_get_rows<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5454. #endif
  5455. //
  5456. // matrix-matrix multiplication
  5457. //
  5458. typedef void (mat_mm_t)(
  5459. device const uchar * src0,
  5460. device const uchar * src1,
  5461. device float * dst,
  5462. constant int64_t & ne00,
  5463. constant int64_t & ne02,
  5464. constant uint64_t & nb01,
  5465. constant uint64_t & nb02,
  5466. constant int64_t & ne12,
  5467. constant uint64_t & nb10,
  5468. constant uint64_t & nb11,
  5469. constant uint64_t & nb12,
  5470. constant int64_t & ne0,
  5471. constant int64_t & ne1,
  5472. constant uint & r2,
  5473. constant uint & r3,
  5474. threadgroup uchar *,
  5475. uint3, uint, uint);
  5476. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  5477. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  5478. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  5479. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  5480. template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_0, 2, dequantize_q5_0>;
  5481. template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_1, 2, dequantize_q5_1>;
  5482. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  5483. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  5484. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  5485. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  5486. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  5487. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;
  5488. template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;
  5489. template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xs, QK_NL, dequantize_iq2_xs>;
  5490. template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_xxs, QK_NL, dequantize_iq3_xxs>;
  5491. template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq3_s, QK_NL, dequantize_iq3_s>;
  5492. template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_s, QK_NL, dequantize_iq2_s>;
  5493. template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq1_s, QK_NL, dequantize_iq1_s>;
  5494. template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_nl>;
  5495. #if QK_K == 64
  5496. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_nl, 2, dequantize_iq4_xs>;
  5497. #else
  5498. template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq4_xs, QK_NL, dequantize_iq4_xs>;
  5499. #endif
  5500. //
  5501. // indirect matrix-matrix multiplication
  5502. //
  5503. typedef void (mat_mm_id_t)(
  5504. device const uchar * ids,
  5505. device const uchar * src1,
  5506. device float * dst,
  5507. constant uint64_t & nbi1,
  5508. constant int64_t & ne00,
  5509. constant int64_t & ne02,
  5510. constant uint64_t & nb01,
  5511. constant uint64_t & nb02,
  5512. constant int64_t & ne12,
  5513. constant int64_t & ne13,
  5514. constant uint64_t & nb10,
  5515. constant uint64_t & nb11,
  5516. constant uint64_t & nb12,
  5517. constant int64_t & ne0,
  5518. constant int64_t & ne1,
  5519. constant uint64_t & nb1,
  5520. constant uint & r2,
  5521. constant uint & r3,
  5522. constant int & idx,
  5523. device const uchar * src00,
  5524. device const uchar * src01,
  5525. device const uchar * src02,
  5526. device const uchar * src03,
  5527. device const uchar * src04,
  5528. device const uchar * src05,
  5529. device const uchar * src06,
  5530. device const uchar * src07,
  5531. threadgroup uchar *,
  5532. uint3, uint, uint);
  5533. template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<float4x4, 1, dequantize_f32>;
  5534. template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<half4x4, 1, dequantize_f16>;
  5535. 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>;
  5536. 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>;
  5537. 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>;
  5538. 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>;
  5539. 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>;
  5540. 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>;
  5541. 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>;
  5542. 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>;
  5543. 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>;
  5544. 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>;
  5545. 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>;
  5546. 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>;
  5547. 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>;
  5548. 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>;
  5549. 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>;
  5550. 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>;
  5551. 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>;
  5552. #if QK_K == 64
  5553. 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>;
  5554. #else
  5555. 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>;
  5556. #endif
  5557. //
  5558. // matrix-vector multiplication
  5559. //
  5560. [[host_name("kernel_mul_mv_id_f32_f32")]]
  5561. kernel void kernel_mul_mv_id_f32_f32(
  5562. device const char * ids,
  5563. device const char * src1,
  5564. device float * dst,
  5565. constant uint64_t & nbi1,
  5566. constant int64_t & ne00,
  5567. constant int64_t & ne01,
  5568. constant int64_t & ne02,
  5569. constant uint64_t & nb00,
  5570. constant uint64_t & nb01,
  5571. constant uint64_t & nb02,
  5572. constant int64_t & ne10,
  5573. constant int64_t & ne11,
  5574. constant int64_t & ne12,
  5575. constant int64_t & ne13,
  5576. constant uint64_t & nb10,
  5577. constant uint64_t & nb11,
  5578. constant uint64_t & nb12,
  5579. constant int64_t & ne0,
  5580. constant int64_t & ne1,
  5581. constant uint64_t & nb1,
  5582. constant uint & r2,
  5583. constant uint & r3,
  5584. constant int & idx,
  5585. device const char * src00,
  5586. device const char * src01,
  5587. device const char * src02,
  5588. device const char * src03,
  5589. device const char * src04,
  5590. device const char * src05,
  5591. device const char * src06,
  5592. device const char * src07,
  5593. uint3 tgpig[[threadgroup_position_in_grid]],
  5594. uint tiitg[[thread_index_in_threadgroup]],
  5595. uint tiisg[[thread_index_in_simdgroup]],
  5596. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5597. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5598. const int64_t bid = tgpig.z/(ne12*ne13);
  5599. tgpig.z = tgpig.z%(ne12*ne13);
  5600. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5601. kernel_mul_mv_f32_f32_impl(
  5602. src0[id],
  5603. src1 + bid*nb11,
  5604. dst + bid*ne0,
  5605. ne00,
  5606. ne01,
  5607. ne02,
  5608. nb00,
  5609. nb01,
  5610. nb02,
  5611. ne10,
  5612. ne11,
  5613. ne12,
  5614. nb10,
  5615. nb11,
  5616. nb12,
  5617. ne0,
  5618. ne1,
  5619. r2,
  5620. r3,
  5621. tgpig,
  5622. tiisg);
  5623. }
  5624. [[host_name("kernel_mul_mv_id_f16_f32")]]
  5625. kernel void kernel_mul_mv_id_f16_f32(
  5626. device const char * ids,
  5627. device const char * src1,
  5628. device float * dst,
  5629. constant uint64_t & nbi1,
  5630. constant int64_t & ne00,
  5631. constant int64_t & ne01,
  5632. constant int64_t & ne02,
  5633. constant uint64_t & nb00,
  5634. constant uint64_t & nb01,
  5635. constant uint64_t & nb02,
  5636. constant int64_t & ne10,
  5637. constant int64_t & ne11,
  5638. constant int64_t & ne12,
  5639. constant int64_t & ne13,
  5640. constant uint64_t & nb10,
  5641. constant uint64_t & nb11,
  5642. constant uint64_t & nb12,
  5643. constant int64_t & ne0,
  5644. constant int64_t & ne1,
  5645. constant uint64_t & nb1,
  5646. constant uint & r2,
  5647. constant uint & r3,
  5648. constant int & idx,
  5649. device const char * src00,
  5650. device const char * src01,
  5651. device const char * src02,
  5652. device const char * src03,
  5653. device const char * src04,
  5654. device const char * src05,
  5655. device const char * src06,
  5656. device const char * src07,
  5657. uint3 tgpig[[threadgroup_position_in_grid]],
  5658. uint tiitg[[thread_index_in_threadgroup]],
  5659. uint tiisg[[thread_index_in_simdgroup]],
  5660. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5661. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5662. const int64_t bid = tgpig.z/(ne12*ne13);
  5663. tgpig.z = tgpig.z%(ne12*ne13);
  5664. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5665. kernel_mul_mv_f16_f32_impl(
  5666. src0[id],
  5667. src1 + bid*nb11,
  5668. dst + bid*ne0,
  5669. ne00,
  5670. ne01,
  5671. ne02,
  5672. nb00,
  5673. nb01,
  5674. nb02,
  5675. ne10,
  5676. ne11,
  5677. ne12,
  5678. nb10,
  5679. nb11,
  5680. nb12,
  5681. ne0,
  5682. ne1,
  5683. r2,
  5684. r3,
  5685. tgpig,
  5686. tiisg);
  5687. }
  5688. [[host_name("kernel_mul_mv_id_q8_0_f32")]]
  5689. kernel void kernel_mul_mv_id_q8_0_f32(
  5690. device const char * ids,
  5691. device const char * src1,
  5692. device float * dst,
  5693. constant uint64_t & nbi1,
  5694. constant int64_t & ne00,
  5695. constant int64_t & ne01,
  5696. constant int64_t & ne02,
  5697. constant uint64_t & nb00,
  5698. constant uint64_t & nb01,
  5699. constant uint64_t & nb02,
  5700. constant int64_t & ne10,
  5701. constant int64_t & ne11,
  5702. constant int64_t & ne12,
  5703. constant int64_t & ne13,
  5704. constant uint64_t & nb10,
  5705. constant uint64_t & nb11,
  5706. constant uint64_t & nb12,
  5707. constant int64_t & ne0,
  5708. constant int64_t & ne1,
  5709. constant uint64_t & nb1,
  5710. constant uint & r2,
  5711. constant uint & r3,
  5712. constant int & idx,
  5713. device const char * src00,
  5714. device const char * src01,
  5715. device const char * src02,
  5716. device const char * src03,
  5717. device const char * src04,
  5718. device const char * src05,
  5719. device const char * src06,
  5720. device const char * src07,
  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. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5726. const int64_t bid = tgpig.z/(ne12*ne13);
  5727. tgpig.z = tgpig.z%(ne12*ne13);
  5728. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5729. kernel_mul_mv_q8_0_f32_impl(
  5730. src0[id],
  5731. (device const float *) (src1 + bid*nb11),
  5732. dst + bid*ne0,
  5733. ne00,
  5734. ne01,
  5735. ne02,
  5736. ne10,
  5737. ne12,
  5738. ne0,
  5739. ne1,
  5740. r2,
  5741. r3,
  5742. tgpig,
  5743. tiisg,
  5744. sgitg);
  5745. }
  5746. [[host_name("kernel_mul_mv_id_q4_0_f32")]]
  5747. kernel void kernel_mul_mv_id_q4_0_f32(
  5748. device const char * ids,
  5749. device const char * src1,
  5750. device float * dst,
  5751. constant uint64_t & nbi1,
  5752. constant int64_t & ne00,
  5753. constant int64_t & ne01,
  5754. constant int64_t & ne02,
  5755. constant uint64_t & nb00,
  5756. constant uint64_t & nb01,
  5757. constant uint64_t & nb02,
  5758. constant int64_t & ne10,
  5759. constant int64_t & ne11,
  5760. constant int64_t & ne12,
  5761. constant int64_t & ne13,
  5762. constant uint64_t & nb10,
  5763. constant uint64_t & nb11,
  5764. constant uint64_t & nb12,
  5765. constant int64_t & ne0,
  5766. constant int64_t & ne1,
  5767. constant uint64_t & nb1,
  5768. constant uint & r2,
  5769. constant uint & r3,
  5770. constant int & idx,
  5771. device const char * src00,
  5772. device const char * src01,
  5773. device const char * src02,
  5774. device const char * src03,
  5775. device const char * src04,
  5776. device const char * src05,
  5777. device const char * src06,
  5778. device const char * src07,
  5779. uint3 tgpig[[threadgroup_position_in_grid]],
  5780. uint tiitg[[thread_index_in_threadgroup]],
  5781. uint tiisg[[thread_index_in_simdgroup]],
  5782. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5783. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5784. const int64_t bid = tgpig.z/(ne12*ne13);
  5785. tgpig.z = tgpig.z%(ne12*ne13);
  5786. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5787. mul_vec_q_n_f32_impl<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5788. src0[id],
  5789. (device const float *) (src1 + bid*nb11),
  5790. dst + bid*ne0,
  5791. ne00,
  5792. ne01,
  5793. ne02,
  5794. ne10,
  5795. ne12,
  5796. ne0,
  5797. ne1,
  5798. r2,
  5799. r3,
  5800. tgpig,
  5801. tiisg,
  5802. sgitg);
  5803. }
  5804. [[host_name("kernel_mul_mv_id_q4_1_f32")]]
  5805. kernel void kernel_mul_mv_id_q4_1_f32(
  5806. device const char * ids,
  5807. device const char * src1,
  5808. device float * dst,
  5809. constant uint64_t & nbi1,
  5810. constant int64_t & ne00,
  5811. constant int64_t & ne01,
  5812. constant int64_t & ne02,
  5813. constant uint64_t & nb00,
  5814. constant uint64_t & nb01,
  5815. constant uint64_t & nb02,
  5816. constant int64_t & ne10,
  5817. constant int64_t & ne11,
  5818. constant int64_t & ne12,
  5819. constant int64_t & ne13,
  5820. constant uint64_t & nb10,
  5821. constant uint64_t & nb11,
  5822. constant uint64_t & nb12,
  5823. constant int64_t & ne0,
  5824. constant int64_t & ne1,
  5825. constant uint64_t & nb1,
  5826. constant uint & r2,
  5827. constant uint & r3,
  5828. constant int & idx,
  5829. device const char * src00,
  5830. device const char * src01,
  5831. device const char * src02,
  5832. device const char * src03,
  5833. device const char * src04,
  5834. device const char * src05,
  5835. device const char * src06,
  5836. device const char * src07,
  5837. uint3 tgpig[[threadgroup_position_in_grid]],
  5838. uint tiitg[[thread_index_in_threadgroup]],
  5839. uint tiisg[[thread_index_in_simdgroup]],
  5840. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5841. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5842. const int64_t bid = tgpig.z/(ne12*ne13);
  5843. tgpig.z = tgpig.z%(ne12*ne13);
  5844. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5845. mul_vec_q_n_f32_impl<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5846. src0[id],
  5847. (device const float *) (src1 + bid*nb11),
  5848. dst + bid*ne0,
  5849. ne00,
  5850. ne01,
  5851. ne02,
  5852. ne10,
  5853. ne12,
  5854. ne0,
  5855. ne1,
  5856. r2,
  5857. r3,
  5858. tgpig,
  5859. tiisg,
  5860. sgitg);
  5861. }
  5862. [[host_name("kernel_mul_mv_id_q5_0_f32")]]
  5863. kernel void kernel_mul_mv_id_q5_0_f32(
  5864. device const char * ids,
  5865. device const char * src1,
  5866. device float * dst,
  5867. constant uint64_t & nbi1,
  5868. constant int64_t & ne00,
  5869. constant int64_t & ne01,
  5870. constant int64_t & ne02,
  5871. constant uint64_t & nb00,
  5872. constant uint64_t & nb01,
  5873. constant uint64_t & nb02,
  5874. constant int64_t & ne10,
  5875. constant int64_t & ne11,
  5876. constant int64_t & ne12,
  5877. constant int64_t & ne13,
  5878. constant uint64_t & nb10,
  5879. constant uint64_t & nb11,
  5880. constant uint64_t & nb12,
  5881. constant int64_t & ne0,
  5882. constant int64_t & ne1,
  5883. constant uint64_t & nb1,
  5884. constant uint & r2,
  5885. constant uint & r3,
  5886. constant int & idx,
  5887. device const char * src00,
  5888. device const char * src01,
  5889. device const char * src02,
  5890. device const char * src03,
  5891. device const char * src04,
  5892. device const char * src05,
  5893. device const char * src06,
  5894. device const char * src07,
  5895. uint3 tgpig[[threadgroup_position_in_grid]],
  5896. uint tiitg[[thread_index_in_threadgroup]],
  5897. uint tiisg[[thread_index_in_simdgroup]],
  5898. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5899. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5900. const int64_t bid = tgpig.z/(ne12*ne13);
  5901. tgpig.z = tgpig.z%(ne12*ne13);
  5902. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5903. mul_vec_q_n_f32_impl<block_q5_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5904. src0[id],
  5905. (device const float *) (src1 + bid*nb11),
  5906. dst + bid*ne0,
  5907. ne00,
  5908. ne01,
  5909. ne02,
  5910. ne10,
  5911. ne12,
  5912. ne0,
  5913. ne1,
  5914. r2,
  5915. r3,
  5916. tgpig,
  5917. tiisg,
  5918. sgitg);
  5919. }
  5920. [[host_name("kernel_mul_mv_id_q5_1_f32")]]
  5921. kernel void kernel_mul_mv_id_q5_1_f32(
  5922. device const char * ids,
  5923. device const char * src1,
  5924. device float * dst,
  5925. constant uint64_t & nbi1,
  5926. constant int64_t & ne00,
  5927. constant int64_t & ne01,
  5928. constant int64_t & ne02,
  5929. constant uint64_t & nb00,
  5930. constant uint64_t & nb01,
  5931. constant uint64_t & nb02,
  5932. constant int64_t & ne10,
  5933. constant int64_t & ne11,
  5934. constant int64_t & ne12,
  5935. constant int64_t & ne13,
  5936. constant uint64_t & nb10,
  5937. constant uint64_t & nb11,
  5938. constant uint64_t & nb12,
  5939. constant int64_t & ne0,
  5940. constant int64_t & ne1,
  5941. constant uint64_t & nb1,
  5942. constant uint & r2,
  5943. constant uint & r3,
  5944. constant int & idx,
  5945. device const char * src00,
  5946. device const char * src01,
  5947. device const char * src02,
  5948. device const char * src03,
  5949. device const char * src04,
  5950. device const char * src05,
  5951. device const char * src06,
  5952. device const char * src07,
  5953. uint3 tgpig[[threadgroup_position_in_grid]],
  5954. uint tiitg[[thread_index_in_threadgroup]],
  5955. uint tiisg[[thread_index_in_simdgroup]],
  5956. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  5957. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  5958. const int64_t bid = tgpig.z/(ne12*ne13);
  5959. tgpig.z = tgpig.z%(ne12*ne13);
  5960. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  5961. mul_vec_q_n_f32_impl<block_q5_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(
  5962. src0[id],
  5963. (device const float *) (src1 + bid*nb11),
  5964. dst + bid*ne0,
  5965. ne00,
  5966. ne01,
  5967. ne02,
  5968. ne10,
  5969. ne12,
  5970. ne0,
  5971. ne1,
  5972. r2,
  5973. r3,
  5974. tgpig,
  5975. tiisg,
  5976. sgitg);
  5977. }
  5978. [[host_name("kernel_mul_mv_id_q2_K_f32")]]
  5979. kernel void kernel_mul_mv_id_q2_K_f32(
  5980. device const char * ids,
  5981. device const char * src1,
  5982. device float * dst,
  5983. constant uint64_t & nbi1,
  5984. constant int64_t & ne00,
  5985. constant int64_t & ne01,
  5986. constant int64_t & ne02,
  5987. constant uint64_t & nb00,
  5988. constant uint64_t & nb01,
  5989. constant uint64_t & nb02,
  5990. constant int64_t & ne10,
  5991. constant int64_t & ne11,
  5992. constant int64_t & ne12,
  5993. constant int64_t & ne13,
  5994. constant uint64_t & nb10,
  5995. constant uint64_t & nb11,
  5996. constant uint64_t & nb12,
  5997. constant int64_t & ne0,
  5998. constant int64_t & ne1,
  5999. constant uint64_t & nb1,
  6000. constant uint & r2,
  6001. constant uint & r3,
  6002. constant int & idx,
  6003. device const char * src00,
  6004. device const char * src01,
  6005. device const char * src02,
  6006. device const char * src03,
  6007. device const char * src04,
  6008. device const char * src05,
  6009. device const char * src06,
  6010. device const char * src07,
  6011. uint3 tgpig[[threadgroup_position_in_grid]],
  6012. uint tiitg[[thread_index_in_threadgroup]],
  6013. uint tiisg[[thread_index_in_simdgroup]],
  6014. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6015. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6016. const int64_t bid = tgpig.z/(ne12*ne13);
  6017. tgpig.z = tgpig.z%(ne12*ne13);
  6018. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6019. kernel_mul_mv_q2_K_f32_impl(
  6020. src0[id],
  6021. (device const float *) (src1 + bid*nb11),
  6022. dst + bid*ne0,
  6023. ne00,
  6024. ne01,
  6025. ne02,
  6026. ne10,
  6027. ne12,
  6028. ne0,
  6029. ne1,
  6030. r2,
  6031. r3,
  6032. tgpig,
  6033. tiisg,
  6034. sgitg);
  6035. }
  6036. [[host_name("kernel_mul_mv_id_q3_K_f32")]]
  6037. kernel void kernel_mul_mv_id_q3_K_f32(
  6038. device const char * ids,
  6039. device const char * src1,
  6040. device float * dst,
  6041. constant uint64_t & nbi1,
  6042. constant int64_t & ne00,
  6043. constant int64_t & ne01,
  6044. constant int64_t & ne02,
  6045. constant uint64_t & nb00,
  6046. constant uint64_t & nb01,
  6047. constant uint64_t & nb02,
  6048. constant int64_t & ne10,
  6049. constant int64_t & ne11,
  6050. constant int64_t & ne12,
  6051. constant int64_t & ne13,
  6052. constant uint64_t & nb10,
  6053. constant uint64_t & nb11,
  6054. constant uint64_t & nb12,
  6055. constant int64_t & ne0,
  6056. constant int64_t & ne1,
  6057. constant uint64_t & nb1,
  6058. constant uint & r2,
  6059. constant uint & r3,
  6060. constant int & idx,
  6061. device const char * src00,
  6062. device const char * src01,
  6063. device const char * src02,
  6064. device const char * src03,
  6065. device const char * src04,
  6066. device const char * src05,
  6067. device const char * src06,
  6068. device const char * src07,
  6069. uint3 tgpig[[threadgroup_position_in_grid]],
  6070. uint tiitg[[thread_index_in_threadgroup]],
  6071. uint tiisg[[thread_index_in_simdgroup]],
  6072. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6073. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6074. const int64_t bid = tgpig.z/(ne12*ne13);
  6075. tgpig.z = tgpig.z%(ne12*ne13);
  6076. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6077. kernel_mul_mv_q3_K_f32_impl(
  6078. src0[id],
  6079. (device const float *) (src1 + bid*nb11),
  6080. dst + bid*ne0,
  6081. ne00,
  6082. ne01,
  6083. ne02,
  6084. ne10,
  6085. ne12,
  6086. ne0,
  6087. ne1,
  6088. r2,
  6089. r3,
  6090. tgpig,
  6091. tiisg,
  6092. sgitg);
  6093. }
  6094. [[host_name("kernel_mul_mv_id_q4_K_f32")]]
  6095. kernel void kernel_mul_mv_id_q4_K_f32(
  6096. device const char * ids,
  6097. device const char * src1,
  6098. device float * dst,
  6099. constant uint64_t & nbi1,
  6100. constant int64_t & ne00,
  6101. constant int64_t & ne01,
  6102. constant int64_t & ne02,
  6103. constant uint64_t & nb00,
  6104. constant uint64_t & nb01,
  6105. constant uint64_t & nb02,
  6106. constant int64_t & ne10,
  6107. constant int64_t & ne11,
  6108. constant int64_t & ne12,
  6109. constant int64_t & ne13,
  6110. constant uint64_t & nb10,
  6111. constant uint64_t & nb11,
  6112. constant uint64_t & nb12,
  6113. constant int64_t & ne0,
  6114. constant int64_t & ne1,
  6115. constant uint64_t & nb1,
  6116. constant uint & r2,
  6117. constant uint & r3,
  6118. constant int & idx,
  6119. device const char * src00,
  6120. device const char * src01,
  6121. device const char * src02,
  6122. device const char * src03,
  6123. device const char * src04,
  6124. device const char * src05,
  6125. device const char * src06,
  6126. device const char * src07,
  6127. uint3 tgpig[[threadgroup_position_in_grid]],
  6128. uint tiitg[[thread_index_in_threadgroup]],
  6129. uint tiisg[[thread_index_in_simdgroup]],
  6130. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6131. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6132. const int64_t bid = tgpig.z/(ne12*ne13);
  6133. tgpig.z = tgpig.z%(ne12*ne13);
  6134. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6135. kernel_mul_mv_q4_K_f32_impl(
  6136. src0[id],
  6137. (device const float *) (src1 + bid*nb11),
  6138. dst + bid*ne0,
  6139. ne00,
  6140. ne01,
  6141. ne02,
  6142. ne10,
  6143. ne12,
  6144. ne0,
  6145. ne1,
  6146. r2,
  6147. r3,
  6148. tgpig,
  6149. tiisg,
  6150. sgitg);
  6151. }
  6152. [[host_name("kernel_mul_mv_id_q5_K_f32")]]
  6153. kernel void kernel_mul_mv_id_q5_K_f32(
  6154. device const char * ids,
  6155. device const char * src1,
  6156. device float * dst,
  6157. constant uint64_t & nbi1,
  6158. constant int64_t & ne00,
  6159. constant int64_t & ne01,
  6160. constant int64_t & ne02,
  6161. constant uint64_t & nb00,
  6162. constant uint64_t & nb01,
  6163. constant uint64_t & nb02,
  6164. constant int64_t & ne10,
  6165. constant int64_t & ne11,
  6166. constant int64_t & ne12,
  6167. constant int64_t & ne13,
  6168. constant uint64_t & nb10,
  6169. constant uint64_t & nb11,
  6170. constant uint64_t & nb12,
  6171. constant int64_t & ne0,
  6172. constant int64_t & ne1,
  6173. constant uint64_t & nb1,
  6174. constant uint & r2,
  6175. constant uint & r3,
  6176. constant int & idx,
  6177. device const char * src00,
  6178. device const char * src01,
  6179. device const char * src02,
  6180. device const char * src03,
  6181. device const char * src04,
  6182. device const char * src05,
  6183. device const char * src06,
  6184. device const char * src07,
  6185. uint3 tgpig[[threadgroup_position_in_grid]],
  6186. uint tiitg[[thread_index_in_threadgroup]],
  6187. uint tiisg[[thread_index_in_simdgroup]],
  6188. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6189. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6190. const int64_t bid = tgpig.z/(ne12*ne13);
  6191. tgpig.z = tgpig.z%(ne12*ne13);
  6192. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6193. kernel_mul_mv_q5_K_f32_impl(
  6194. src0[id],
  6195. (device const float *) (src1 + bid*nb11),
  6196. dst + bid*ne0,
  6197. ne00,
  6198. ne01,
  6199. ne02,
  6200. ne10,
  6201. ne12,
  6202. ne0,
  6203. ne1,
  6204. r2,
  6205. r3,
  6206. tgpig,
  6207. tiisg,
  6208. sgitg);
  6209. }
  6210. [[host_name("kernel_mul_mv_id_q6_K_f32")]]
  6211. kernel void kernel_mul_mv_id_q6_K_f32(
  6212. device const char * ids,
  6213. device const char * src1,
  6214. device float * dst,
  6215. constant uint64_t & nbi1,
  6216. constant int64_t & ne00,
  6217. constant int64_t & ne01,
  6218. constant int64_t & ne02,
  6219. constant uint64_t & nb00,
  6220. constant uint64_t & nb01,
  6221. constant uint64_t & nb02,
  6222. constant int64_t & ne10,
  6223. constant int64_t & ne11,
  6224. constant int64_t & ne12,
  6225. constant int64_t & ne13,
  6226. constant uint64_t & nb10,
  6227. constant uint64_t & nb11,
  6228. constant uint64_t & nb12,
  6229. constant int64_t & ne0,
  6230. constant int64_t & ne1,
  6231. constant uint64_t & nb1,
  6232. constant uint & r2,
  6233. constant uint & r3,
  6234. constant int & idx,
  6235. device const char * src00,
  6236. device const char * src01,
  6237. device const char * src02,
  6238. device const char * src03,
  6239. device const char * src04,
  6240. device const char * src05,
  6241. device const char * src06,
  6242. device const char * src07,
  6243. uint3 tgpig[[threadgroup_position_in_grid]],
  6244. uint tiitg[[thread_index_in_threadgroup]],
  6245. uint tiisg[[thread_index_in_simdgroup]],
  6246. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6247. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6248. const int64_t bid = tgpig.z/(ne12*ne13);
  6249. tgpig.z = tgpig.z%(ne12*ne13);
  6250. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6251. kernel_mul_mv_q6_K_f32_impl(
  6252. src0[id],
  6253. (device const float *) (src1 + bid*nb11),
  6254. dst + bid*ne0,
  6255. ne00,
  6256. ne01,
  6257. ne02,
  6258. ne10,
  6259. ne12,
  6260. ne0,
  6261. ne1,
  6262. r2,
  6263. r3,
  6264. tgpig,
  6265. tiisg,
  6266. sgitg);
  6267. }
  6268. [[host_name("kernel_mul_mv_id_iq2_xxs_f32")]]
  6269. kernel void kernel_mul_mv_id_iq2_xxs_f32(
  6270. device const char * ids,
  6271. device const char * src1,
  6272. device float * dst,
  6273. constant uint64_t & nbi1,
  6274. constant int64_t & ne00,
  6275. constant int64_t & ne01,
  6276. constant int64_t & ne02,
  6277. constant uint64_t & nb00,
  6278. constant uint64_t & nb01,
  6279. constant uint64_t & nb02,
  6280. constant int64_t & ne10,
  6281. constant int64_t & ne11,
  6282. constant int64_t & ne12,
  6283. constant int64_t & ne13,
  6284. constant uint64_t & nb10,
  6285. constant uint64_t & nb11,
  6286. constant uint64_t & nb12,
  6287. constant int64_t & ne0,
  6288. constant int64_t & ne1,
  6289. constant uint64_t & nb1,
  6290. constant uint & r2,
  6291. constant uint & r3,
  6292. constant int & idx,
  6293. device const char * src00,
  6294. device const char * src01,
  6295. device const char * src02,
  6296. device const char * src03,
  6297. device const char * src04,
  6298. device const char * src05,
  6299. device const char * src06,
  6300. device const char * src07,
  6301. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6302. uint3 tgpig[[threadgroup_position_in_grid]],
  6303. uint tiitg[[thread_index_in_threadgroup]],
  6304. uint tiisg[[thread_index_in_simdgroup]],
  6305. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6306. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6307. const int64_t bid = tgpig.z/(ne12*ne13);
  6308. tgpig.z = tgpig.z%(ne12*ne13);
  6309. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6310. kernel_mul_mv_iq2_xxs_f32_impl(
  6311. src0[id],
  6312. (device const float *) (src1 + bid*nb11),
  6313. dst + bid*ne0,
  6314. ne00,
  6315. ne01,
  6316. ne02,
  6317. ne10,
  6318. ne12,
  6319. ne0,
  6320. ne1,
  6321. r2,
  6322. r3,
  6323. shared_values,
  6324. tgpig,
  6325. tiisg,
  6326. sgitg);
  6327. }
  6328. [[host_name("kernel_mul_mv_id_iq2_xs_f32")]]
  6329. kernel void kernel_mul_mv_id_iq2_xs_f32(
  6330. device const char * ids,
  6331. device const char * src1,
  6332. device float * dst,
  6333. constant uint64_t & nbi1,
  6334. constant int64_t & ne00,
  6335. constant int64_t & ne01,
  6336. constant int64_t & ne02,
  6337. constant uint64_t & nb00,
  6338. constant uint64_t & nb01,
  6339. constant uint64_t & nb02,
  6340. constant int64_t & ne10,
  6341. constant int64_t & ne11,
  6342. constant int64_t & ne12,
  6343. constant int64_t & ne13,
  6344. constant uint64_t & nb10,
  6345. constant uint64_t & nb11,
  6346. constant uint64_t & nb12,
  6347. constant int64_t & ne0,
  6348. constant int64_t & ne1,
  6349. constant uint64_t & nb1,
  6350. constant uint & r2,
  6351. constant uint & r3,
  6352. constant int & idx,
  6353. device const char * src00,
  6354. device const char * src01,
  6355. device const char * src02,
  6356. device const char * src03,
  6357. device const char * src04,
  6358. device const char * src05,
  6359. device const char * src06,
  6360. device const char * src07,
  6361. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6362. uint3 tgpig[[threadgroup_position_in_grid]],
  6363. uint tiitg[[thread_index_in_threadgroup]],
  6364. uint tiisg[[thread_index_in_simdgroup]],
  6365. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6366. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6367. const int64_t bid = tgpig.z/(ne12*ne13);
  6368. tgpig.z = tgpig.z%(ne12*ne13);
  6369. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6370. kernel_mul_mv_iq2_xs_f32_impl(
  6371. src0[id],
  6372. (device const float *) (src1 + bid*nb11),
  6373. dst + bid*ne0,
  6374. ne00,
  6375. ne01,
  6376. ne02,
  6377. ne10,
  6378. ne12,
  6379. ne0,
  6380. ne1,
  6381. r2,
  6382. r3,
  6383. shared_values,
  6384. tgpig,
  6385. tiisg,
  6386. sgitg);
  6387. }
  6388. [[host_name("kernel_mul_mv_id_iq3_xxs_f32")]]
  6389. kernel void kernel_mul_mv_id_iq3_xxs_f32(
  6390. device const char * ids,
  6391. device const char * src1,
  6392. device float * dst,
  6393. constant uint64_t & nbi1,
  6394. constant int64_t & ne00,
  6395. constant int64_t & ne01,
  6396. constant int64_t & ne02,
  6397. constant uint64_t & nb00,
  6398. constant uint64_t & nb01,
  6399. constant uint64_t & nb02,
  6400. constant int64_t & ne10,
  6401. constant int64_t & ne11,
  6402. constant int64_t & ne12,
  6403. constant int64_t & ne13,
  6404. constant uint64_t & nb10,
  6405. constant uint64_t & nb11,
  6406. constant uint64_t & nb12,
  6407. constant int64_t & ne0,
  6408. constant int64_t & ne1,
  6409. constant uint64_t & nb1,
  6410. constant uint & r2,
  6411. constant uint & r3,
  6412. constant int & idx,
  6413. device const char * src00,
  6414. device const char * src01,
  6415. device const char * src02,
  6416. device const char * src03,
  6417. device const char * src04,
  6418. device const char * src05,
  6419. device const char * src06,
  6420. device const char * src07,
  6421. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6422. uint3 tgpig[[threadgroup_position_in_grid]],
  6423. uint tiitg[[thread_index_in_threadgroup]],
  6424. uint tiisg[[thread_index_in_simdgroup]],
  6425. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6426. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6427. const int64_t bid = tgpig.z/(ne12*ne13);
  6428. tgpig.z = tgpig.z%(ne12*ne13);
  6429. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6430. kernel_mul_mv_iq3_xxs_f32_impl(
  6431. src0[id],
  6432. (device const float *) (src1 + bid*nb11),
  6433. dst + bid*ne0,
  6434. ne00,
  6435. ne01,
  6436. ne02,
  6437. ne10,
  6438. ne12,
  6439. ne0,
  6440. ne1,
  6441. r2,
  6442. r3,
  6443. shared_values,
  6444. tgpig,
  6445. tiisg,
  6446. sgitg);
  6447. }
  6448. [[host_name("kernel_mul_mv_id_iq3_s_f32")]]
  6449. kernel void kernel_mul_mv_id_iq3_s_f32(
  6450. device const char * ids,
  6451. device const char * src1,
  6452. device float * dst,
  6453. constant uint64_t & nbi1,
  6454. constant int64_t & ne00,
  6455. constant int64_t & ne01,
  6456. constant int64_t & ne02,
  6457. constant uint64_t & nb00,
  6458. constant uint64_t & nb01,
  6459. constant uint64_t & nb02,
  6460. constant int64_t & ne10,
  6461. constant int64_t & ne11,
  6462. constant int64_t & ne12,
  6463. constant int64_t & ne13,
  6464. constant uint64_t & nb10,
  6465. constant uint64_t & nb11,
  6466. constant uint64_t & nb12,
  6467. constant int64_t & ne0,
  6468. constant int64_t & ne1,
  6469. constant uint64_t & nb1,
  6470. constant uint & r2,
  6471. constant uint & r3,
  6472. constant int & idx,
  6473. device const char * src00,
  6474. device const char * src01,
  6475. device const char * src02,
  6476. device const char * src03,
  6477. device const char * src04,
  6478. device const char * src05,
  6479. device const char * src06,
  6480. device const char * src07,
  6481. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6482. uint3 tgpig[[threadgroup_position_in_grid]],
  6483. uint tiitg[[thread_index_in_threadgroup]],
  6484. uint tiisg[[thread_index_in_simdgroup]],
  6485. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6486. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6487. const int64_t bid = tgpig.z/(ne12*ne13);
  6488. tgpig.z = tgpig.z%(ne12*ne13);
  6489. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6490. kernel_mul_mv_iq3_s_f32_impl(
  6491. src0[id],
  6492. (device const float *) (src1 + bid*nb11),
  6493. dst + bid*ne0,
  6494. ne00,
  6495. ne01,
  6496. ne02,
  6497. ne10,
  6498. ne12,
  6499. ne0,
  6500. ne1,
  6501. r2,
  6502. r3,
  6503. shared_values,
  6504. tgpig,
  6505. tiisg,
  6506. sgitg);
  6507. }
  6508. [[host_name("kernel_mul_mv_id_iq2_s_f32")]]
  6509. kernel void kernel_mul_mv_id_iq2_s_f32(
  6510. device const char * ids,
  6511. device const char * src1,
  6512. device float * dst,
  6513. constant uint64_t & nbi1,
  6514. constant int64_t & ne00,
  6515. constant int64_t & ne01,
  6516. constant int64_t & ne02,
  6517. constant uint64_t & nb00,
  6518. constant uint64_t & nb01,
  6519. constant uint64_t & nb02,
  6520. constant int64_t & ne10,
  6521. constant int64_t & ne11,
  6522. constant int64_t & ne12,
  6523. constant int64_t & ne13,
  6524. constant uint64_t & nb10,
  6525. constant uint64_t & nb11,
  6526. constant uint64_t & nb12,
  6527. constant int64_t & ne0,
  6528. constant int64_t & ne1,
  6529. constant uint64_t & nb1,
  6530. constant uint & r2,
  6531. constant uint & r3,
  6532. constant int & idx,
  6533. device const char * src00,
  6534. device const char * src01,
  6535. device const char * src02,
  6536. device const char * src03,
  6537. device const char * src04,
  6538. device const char * src05,
  6539. device const char * src06,
  6540. device const char * src07,
  6541. threadgroup int8_t * shared_values [[threadgroup(0)]],
  6542. uint3 tgpig[[threadgroup_position_in_grid]],
  6543. uint tiitg[[thread_index_in_threadgroup]],
  6544. uint tiisg[[thread_index_in_simdgroup]],
  6545. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6546. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6547. const int64_t bid = tgpig.z/(ne12*ne13);
  6548. tgpig.z = tgpig.z%(ne12*ne13);
  6549. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6550. kernel_mul_mv_iq2_s_f32_impl(
  6551. src0[id],
  6552. (device const float *) (src1 + bid*nb11),
  6553. dst + bid*ne0,
  6554. ne00,
  6555. ne01,
  6556. ne02,
  6557. ne10,
  6558. ne12,
  6559. ne0,
  6560. ne1,
  6561. r2,
  6562. r3,
  6563. shared_values,
  6564. tgpig,
  6565. tiisg,
  6566. sgitg);
  6567. }
  6568. [[host_name("kernel_mul_mv_id_iq1_s_f32")]]
  6569. kernel void kernel_mul_mv_id_iq1_s_f32(
  6570. device const char * ids,
  6571. device const char * src1,
  6572. device float * dst,
  6573. constant uint64_t & nbi1,
  6574. constant int64_t & ne00,
  6575. constant int64_t & ne01,
  6576. constant int64_t & ne02,
  6577. constant uint64_t & nb00,
  6578. constant uint64_t & nb01,
  6579. constant uint64_t & nb02,
  6580. constant int64_t & ne10,
  6581. constant int64_t & ne11,
  6582. constant int64_t & ne12,
  6583. constant int64_t & ne13,
  6584. constant uint64_t & nb10,
  6585. constant uint64_t & nb11,
  6586. constant uint64_t & nb12,
  6587. constant int64_t & ne0,
  6588. constant int64_t & ne1,
  6589. constant uint64_t & nb1,
  6590. constant uint & r2,
  6591. constant uint & r3,
  6592. constant int & idx,
  6593. device const char * src00,
  6594. device const char * src01,
  6595. device const char * src02,
  6596. device const char * src03,
  6597. device const char * src04,
  6598. device const char * src05,
  6599. device const char * src06,
  6600. device const char * src07,
  6601. uint3 tgpig[[threadgroup_position_in_grid]],
  6602. uint tiitg[[thread_index_in_threadgroup]],
  6603. uint tiisg[[thread_index_in_simdgroup]],
  6604. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6605. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6606. const int64_t bid = tgpig.z/(ne12*ne13);
  6607. tgpig.z = tgpig.z%(ne12*ne13);
  6608. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6609. kernel_mul_mv_iq1_s_f32_impl(
  6610. src0[id],
  6611. (device const float *) (src1 + bid*nb11),
  6612. dst + bid*ne0,
  6613. ne00,
  6614. ne01,
  6615. ne02,
  6616. ne10,
  6617. ne12,
  6618. ne0,
  6619. ne1,
  6620. r2,
  6621. r3,
  6622. tgpig,
  6623. tiisg,
  6624. sgitg);
  6625. }
  6626. [[host_name("kernel_mul_mv_id_iq4_nl_f32")]]
  6627. kernel void kernel_mul_mv_id_iq4_nl_f32(
  6628. device const char * ids,
  6629. device const char * src1,
  6630. device float * dst,
  6631. constant uint64_t & nbi1,
  6632. constant int64_t & ne00,
  6633. constant int64_t & ne01,
  6634. constant int64_t & ne02,
  6635. constant uint64_t & nb00,
  6636. constant uint64_t & nb01,
  6637. constant uint64_t & nb02,
  6638. constant int64_t & ne10,
  6639. constant int64_t & ne11,
  6640. constant int64_t & ne12,
  6641. constant int64_t & ne13,
  6642. constant uint64_t & nb10,
  6643. constant uint64_t & nb11,
  6644. constant uint64_t & nb12,
  6645. constant int64_t & ne0,
  6646. constant int64_t & ne1,
  6647. constant uint64_t & nb1,
  6648. constant uint & r2,
  6649. constant uint & r3,
  6650. constant int & idx,
  6651. device const char * src00,
  6652. device const char * src01,
  6653. device const char * src02,
  6654. device const char * src03,
  6655. device const char * src04,
  6656. device const char * src05,
  6657. device const char * src06,
  6658. device const char * src07,
  6659. threadgroup float * shared_values [[threadgroup(0)]],
  6660. uint3 tgpig[[threadgroup_position_in_grid]],
  6661. uint tiitg[[thread_index_in_threadgroup]],
  6662. uint tiisg[[thread_index_in_simdgroup]],
  6663. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6664. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6665. const int64_t bid = tgpig.z/(ne12*ne13);
  6666. tgpig.z = tgpig.z%(ne12*ne13);
  6667. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6668. kernel_mul_mv_iq4_nl_f32_impl(
  6669. src0[id],
  6670. (device const float *) (src1 + bid*nb11),
  6671. dst + bid*ne0,
  6672. ne00,
  6673. ne01,
  6674. ne02,
  6675. ne10,
  6676. ne12,
  6677. ne0,
  6678. ne1,
  6679. r2,
  6680. r3,
  6681. shared_values,
  6682. tgpig,
  6683. tiisg,
  6684. sgitg);
  6685. }
  6686. [[host_name("kernel_mul_mv_id_iq4_xs_f32")]]
  6687. kernel void kernel_mul_mv_id_iq4_xs_f32(
  6688. device const char * ids,
  6689. device const char * src1,
  6690. device float * dst,
  6691. constant uint64_t & nbi1,
  6692. constant int64_t & ne00,
  6693. constant int64_t & ne01,
  6694. constant int64_t & ne02,
  6695. constant uint64_t & nb00,
  6696. constant uint64_t & nb01,
  6697. constant uint64_t & nb02,
  6698. constant int64_t & ne10,
  6699. constant int64_t & ne11,
  6700. constant int64_t & ne12,
  6701. constant int64_t & ne13,
  6702. constant uint64_t & nb10,
  6703. constant uint64_t & nb11,
  6704. constant uint64_t & nb12,
  6705. constant int64_t & ne0,
  6706. constant int64_t & ne1,
  6707. constant uint64_t & nb1,
  6708. constant uint & r2,
  6709. constant uint & r3,
  6710. constant int & idx,
  6711. device const char * src00,
  6712. device const char * src01,
  6713. device const char * src02,
  6714. device const char * src03,
  6715. device const char * src04,
  6716. device const char * src05,
  6717. device const char * src06,
  6718. device const char * src07,
  6719. threadgroup float * shared_values [[threadgroup(0)]],
  6720. uint3 tgpig[[threadgroup_position_in_grid]],
  6721. uint tiitg[[thread_index_in_threadgroup]],
  6722. uint tiisg[[thread_index_in_simdgroup]],
  6723. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  6724. device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};
  6725. const int64_t bid = tgpig.z/(ne12*ne13);
  6726. tgpig.z = tgpig.z%(ne12*ne13);
  6727. const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];
  6728. #if QK_K == 64
  6729. kernel_mul_mv_iq4_nl_f32_impl(
  6730. #else
  6731. kernel_mul_mv_iq4_xs_f32_impl(
  6732. #endif
  6733. src0[id],
  6734. (device const float *) (src1 + bid*nb11),
  6735. dst + bid*ne0,
  6736. ne00,
  6737. ne01,
  6738. ne02,
  6739. ne10,
  6740. ne12,
  6741. ne0,
  6742. ne1,
  6743. r2,
  6744. r3,
  6745. shared_values,
  6746. tgpig,
  6747. tiisg,
  6748. sgitg);
  6749. }