ggml-quants.c 485 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #define GGML_COMMON_IMPL_C
  4. #include "ggml-common.h"
  5. #include <math.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <float.h>
  9. #include <stdlib.h> // for qsort
  10. #include <stdio.h> // for GGML_ASSERT
  11. #ifdef __ARM_NEON
  12. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  13. //
  14. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  15. //
  16. #include <arm_neon.h>
  17. #else
  18. #ifdef __wasm_simd128__
  19. #include <wasm_simd128.h>
  20. #else
  21. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  22. #include <altivec.h>
  23. #undef bool
  24. #define bool _Bool
  25. #else
  26. #if defined(_MSC_VER) || defined(__MINGW32__)
  27. #include <intrin.h>
  28. #else
  29. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  30. #if !defined(__riscv)
  31. #include <immintrin.h>
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #endif
  37. #endif
  38. #ifdef __riscv_v_intrinsic
  39. #include <riscv_vector.h>
  40. #endif
  41. #undef MIN
  42. #undef MAX
  43. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  44. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  45. #define UNUSED GGML_UNUSED
  46. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  47. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  48. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  49. // multiply int8_t, add results pairwise twice
  50. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  51. // Get absolute values of x vectors
  52. const __m128i ax = _mm_sign_epi8(x, x);
  53. // Sign the values of the y vectors
  54. const __m128i sy = _mm_sign_epi8(y, x);
  55. // Perform multiplication and create 16-bit values
  56. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  57. const __m128i ones = _mm_set1_epi16(1);
  58. return _mm_madd_epi16(ones, dot);
  59. }
  60. #if __AVX__ || __AVX2__ || __AVX512F__
  61. // horizontally add 8 floats
  62. static inline float hsum_float_8(const __m256 x) {
  63. __m128 res = _mm256_extractf128_ps(x, 1);
  64. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  65. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  66. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  67. return _mm_cvtss_f32(res);
  68. }
  69. // horizontally add 8 int32_t
  70. static inline int hsum_i32_8(const __m256i a) {
  71. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  72. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  73. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  74. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  75. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  76. }
  77. // horizontally add 4 int32_t
  78. static inline int hsum_i32_4(const __m128i a) {
  79. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  80. const __m128i sum64 = _mm_add_epi32(hi64, a);
  81. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  82. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  83. }
  84. #if defined(__AVX2__) || defined(__AVX512F__)
  85. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  86. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  87. uint32_t x32;
  88. memcpy(&x32, x, sizeof(uint32_t));
  89. const __m256i shuf_mask = _mm256_set_epi64x(
  90. 0x0303030303030303, 0x0202020202020202,
  91. 0x0101010101010101, 0x0000000000000000);
  92. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  93. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  94. bytes = _mm256_or_si256(bytes, bit_mask);
  95. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  96. }
  97. // Unpack 32 4-bit fields into 32 bytes
  98. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  99. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  100. {
  101. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  102. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  103. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  104. return _mm256_and_si256(lowMask, bytes);
  105. }
  106. // add int16_t pairwise and return as float vector
  107. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  108. const __m256i ones = _mm256_set1_epi16(1);
  109. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  110. return _mm256_cvtepi32_ps(summed_pairs);
  111. }
  112. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  113. #if __AVXVNNI__
  114. const __m256i zero = _mm256_setzero_si256();
  115. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  116. return _mm256_cvtepi32_ps(summed_pairs);
  117. #else
  118. // Perform multiplication and create 16-bit values
  119. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  120. return sum_i16_pairs_float(dot);
  121. #endif
  122. }
  123. // multiply int8_t, add results pairwise twice and return as float vector
  124. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  125. #if __AVXVNNIINT8__
  126. const __m256i zero = _mm256_setzero_si256();
  127. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  128. return _mm256_cvtepi32_ps(summed_pairs);
  129. #else
  130. // Get absolute values of x vectors
  131. const __m256i ax = _mm256_sign_epi8(x, x);
  132. // Sign the values of the y vectors
  133. const __m256i sy = _mm256_sign_epi8(y, x);
  134. return mul_sum_us8_pairs_float(ax, sy);
  135. #endif
  136. }
  137. static inline __m128i packNibbles( __m256i bytes )
  138. {
  139. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  140. #if __AVX512F__
  141. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  142. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  143. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  144. #else
  145. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  146. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  147. __m256i low = _mm256_and_si256( lowByte, bytes );
  148. high = _mm256_srli_epi16( high, 4 );
  149. bytes = _mm256_or_si256( low, high );
  150. // Compress uint16_t lanes into bytes
  151. __m128i r0 = _mm256_castsi256_si128( bytes );
  152. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  153. return _mm_packus_epi16( r0, r1 );
  154. #endif
  155. }
  156. #elif defined(__AVX__)
  157. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  158. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  159. uint32_t x32;
  160. memcpy(&x32, x, sizeof(uint32_t));
  161. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  162. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  163. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  164. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  165. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  166. bytesl = _mm_or_si128(bytesl, bit_mask);
  167. bytesh = _mm_or_si128(bytesh, bit_mask);
  168. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  169. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  170. return MM256_SET_M128I(bytesh, bytesl);
  171. }
  172. // Unpack 32 4-bit fields into 32 bytes
  173. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  174. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  175. {
  176. // Load 16 bytes from memory
  177. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  178. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  179. const __m128i lowMask = _mm_set1_epi8(0xF);
  180. tmpl = _mm_and_si128(lowMask, tmpl);
  181. tmph = _mm_and_si128(lowMask, tmph);
  182. return MM256_SET_M128I(tmph, tmpl);
  183. }
  184. // add int16_t pairwise and return as float vector
  185. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  186. const __m128i ones = _mm_set1_epi16(1);
  187. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  188. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  189. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  190. return _mm256_cvtepi32_ps(summed_pairs);
  191. }
  192. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  193. const __m128i axl = _mm256_castsi256_si128(ax);
  194. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  195. const __m128i syl = _mm256_castsi256_si128(sy);
  196. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  197. // Perform multiplication and create 16-bit values
  198. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  199. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  200. return sum_i16_pairs_float(doth, dotl);
  201. }
  202. // multiply int8_t, add results pairwise twice and return as float vector
  203. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  204. const __m128i xl = _mm256_castsi256_si128(x);
  205. const __m128i xh = _mm256_extractf128_si256(x, 1);
  206. const __m128i yl = _mm256_castsi256_si128(y);
  207. const __m128i yh = _mm256_extractf128_si256(y, 1);
  208. // Get absolute values of x vectors
  209. const __m128i axl = _mm_sign_epi8(xl, xl);
  210. const __m128i axh = _mm_sign_epi8(xh, xh);
  211. // Sign the values of the y vectors
  212. const __m128i syl = _mm_sign_epi8(yl, xl);
  213. const __m128i syh = _mm_sign_epi8(yh, xh);
  214. // Perform multiplication and create 16-bit values
  215. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  216. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  217. return sum_i16_pairs_float(doth, dotl);
  218. }
  219. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  220. {
  221. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  222. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  223. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  224. __m128i low = _mm_and_si128( lowByte, bytes1 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes1 = _mm_or_si128( low, high );
  227. high = _mm_andnot_si128( lowByte, bytes2 );
  228. low = _mm_and_si128( lowByte, bytes2 );
  229. high = _mm_srli_epi16( high, 4 );
  230. bytes2 = _mm_or_si128( low, high );
  231. return _mm_packus_epi16( bytes1, bytes2);
  232. }
  233. #endif
  234. #elif defined(__SSSE3__)
  235. // horizontally add 4x4 floats
  236. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  237. __m128 res_0 =_mm_hadd_ps(a, b);
  238. __m128 res_1 =_mm_hadd_ps(c, d);
  239. __m128 res =_mm_hadd_ps(res_0, res_1);
  240. res =_mm_hadd_ps(res, res);
  241. res =_mm_hadd_ps(res, res);
  242. return _mm_cvtss_f32(res);
  243. }
  244. #endif // __AVX__ || __AVX2__ || __AVX512F__
  245. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  246. #if defined(__ARM_NEON)
  247. #ifdef _MSC_VER
  248. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  249. #else
  250. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  251. #endif
  252. #if !defined(__aarch64__)
  253. // 64-bit compatibility
  254. // vaddvq_s16
  255. // vpaddq_s16
  256. // vpaddq_s32
  257. // vaddvq_s32
  258. // vaddvq_f32
  259. // vmaxvq_f32
  260. // vcvtnq_s32_f32
  261. // vzip1_u8
  262. // vzip2_u8
  263. inline static int32_t vaddvq_s16(int16x8_t v) {
  264. return
  265. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  266. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  267. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  268. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  269. }
  270. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  271. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  272. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  273. return vcombine_s16(a0, b0);
  274. }
  275. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  276. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  277. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  278. return vcombine_s32(a0, b0);
  279. }
  280. inline static int32_t vaddvq_s32(int32x4_t v) {
  281. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  282. }
  283. inline static float vaddvq_f32(float32x4_t v) {
  284. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  285. }
  286. inline static float vmaxvq_f32(float32x4_t v) {
  287. return
  288. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  289. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  290. }
  291. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  292. int32x4_t res;
  293. res[0] = roundf(vgetq_lane_f32(v, 0));
  294. res[1] = roundf(vgetq_lane_f32(v, 1));
  295. res[2] = roundf(vgetq_lane_f32(v, 2));
  296. res[3] = roundf(vgetq_lane_f32(v, 3));
  297. return res;
  298. }
  299. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  300. uint8x8_t res;
  301. res[0] = a[0]; res[1] = b[0];
  302. res[2] = a[1]; res[3] = b[1];
  303. res[4] = a[2]; res[5] = b[2];
  304. res[6] = a[3]; res[7] = b[3];
  305. return res;
  306. }
  307. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  308. uint8x8_t res;
  309. res[0] = a[4]; res[1] = b[4];
  310. res[2] = a[5]; res[3] = b[5];
  311. res[4] = a[6]; res[5] = b[6];
  312. res[6] = a[7]; res[7] = b[7];
  313. return res;
  314. }
  315. // vld1q_s16_x2
  316. // vld1q_u8_x2
  317. // vld1q_u8_x4
  318. // vld1q_s8_x2
  319. // vld1q_s8_x4
  320. // TODO: double-check these work correctly
  321. typedef struct ggml_int16x8x2_t {
  322. int16x8_t val[2];
  323. } ggml_int16x8x2_t;
  324. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  325. ggml_int16x8x2_t res;
  326. res.val[0] = vld1q_s16(ptr + 0);
  327. res.val[1] = vld1q_s16(ptr + 8);
  328. return res;
  329. }
  330. typedef struct ggml_uint8x16x2_t {
  331. uint8x16_t val[2];
  332. } ggml_uint8x16x2_t;
  333. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  334. ggml_uint8x16x2_t res;
  335. res.val[0] = vld1q_u8(ptr + 0);
  336. res.val[1] = vld1q_u8(ptr + 16);
  337. return res;
  338. }
  339. typedef struct ggml_uint8x16x4_t {
  340. uint8x16_t val[4];
  341. } ggml_uint8x16x4_t;
  342. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  343. ggml_uint8x16x4_t res;
  344. res.val[0] = vld1q_u8(ptr + 0);
  345. res.val[1] = vld1q_u8(ptr + 16);
  346. res.val[2] = vld1q_u8(ptr + 32);
  347. res.val[3] = vld1q_u8(ptr + 48);
  348. return res;
  349. }
  350. typedef struct ggml_int8x16x2_t {
  351. int8x16_t val[2];
  352. } ggml_int8x16x2_t;
  353. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  354. ggml_int8x16x2_t res;
  355. res.val[0] = vld1q_s8(ptr + 0);
  356. res.val[1] = vld1q_s8(ptr + 16);
  357. return res;
  358. }
  359. typedef struct ggml_int8x16x4_t {
  360. int8x16_t val[4];
  361. } ggml_int8x16x4_t;
  362. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  363. ggml_int8x16x4_t res;
  364. res.val[0] = vld1q_s8(ptr + 0);
  365. res.val[1] = vld1q_s8(ptr + 16);
  366. res.val[2] = vld1q_s8(ptr + 32);
  367. res.val[3] = vld1q_s8(ptr + 48);
  368. return res;
  369. }
  370. // NOTE: not tested
  371. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  372. int8x16_t res;
  373. res[ 0] = a[b[ 0]];
  374. res[ 1] = a[b[ 1]];
  375. res[ 2] = a[b[ 2]];
  376. res[ 3] = a[b[ 3]];
  377. res[ 4] = a[b[ 4]];
  378. res[ 5] = a[b[ 5]];
  379. res[ 6] = a[b[ 6]];
  380. res[ 7] = a[b[ 7]];
  381. res[ 8] = a[b[ 8]];
  382. res[ 9] = a[b[ 9]];
  383. res[10] = a[b[10]];
  384. res[11] = a[b[11]];
  385. res[12] = a[b[12]];
  386. res[13] = a[b[13]];
  387. res[14] = a[b[14]];
  388. res[15] = a[b[15]];
  389. return res;
  390. }
  391. // NOTE: not tested
  392. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  393. uint8x16_t res;
  394. res[ 0] = a[b[ 0]];
  395. res[ 1] = a[b[ 1]];
  396. res[ 2] = a[b[ 2]];
  397. res[ 3] = a[b[ 3]];
  398. res[ 4] = a[b[ 4]];
  399. res[ 5] = a[b[ 5]];
  400. res[ 6] = a[b[ 6]];
  401. res[ 7] = a[b[ 7]];
  402. res[ 8] = a[b[ 8]];
  403. res[ 9] = a[b[ 9]];
  404. res[10] = a[b[10]];
  405. res[11] = a[b[11]];
  406. res[12] = a[b[12]];
  407. res[13] = a[b[13]];
  408. res[14] = a[b[14]];
  409. res[15] = a[b[15]];
  410. return res;
  411. }
  412. #else
  413. #define ggml_int16x8x2_t int16x8x2_t
  414. #define ggml_uint8x16x2_t uint8x16x2_t
  415. #define ggml_uint8x16x4_t uint8x16x4_t
  416. #define ggml_int8x16x2_t int8x16x2_t
  417. #define ggml_int8x16x4_t int8x16x4_t
  418. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  419. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  420. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  421. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  422. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  423. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  424. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  425. #endif
  426. #if !defined(__ARM_FEATURE_DOTPROD)
  427. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  428. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  429. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  430. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  431. }
  432. #else
  433. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  434. #endif
  435. #endif
  436. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  437. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  438. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  439. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  440. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  441. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  442. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  443. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  444. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  445. // precomputed tables for expanding 8bits to 8 bytes:
  446. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  447. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  448. #endif
  449. // reference implementation for deterministic creation of model files
  450. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  451. static const int qk = QK4_0;
  452. assert(k % qk == 0);
  453. const int nb = k / qk;
  454. for (int i = 0; i < nb; i++) {
  455. float amax = 0.0f; // absolute max
  456. float max = 0.0f;
  457. for (int j = 0; j < qk; j++) {
  458. const float v = x[i*qk + j];
  459. if (amax < fabsf(v)) {
  460. amax = fabsf(v);
  461. max = v;
  462. }
  463. }
  464. const float d = max / -8;
  465. const float id = d ? 1.0f/d : 0.0f;
  466. y[i].d = GGML_FP32_TO_FP16(d);
  467. for (int j = 0; j < qk/2; ++j) {
  468. const float x0 = x[i*qk + 0 + j]*id;
  469. const float x1 = x[i*qk + qk/2 + j]*id;
  470. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  471. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  472. y[i].qs[j] = xi0;
  473. y[i].qs[j] |= xi1 << 4;
  474. }
  475. }
  476. }
  477. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  478. quantize_row_q4_0_reference(x, y, k);
  479. }
  480. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  481. const int qk = QK4_1;
  482. assert(k % qk == 0);
  483. const int nb = k / qk;
  484. for (int i = 0; i < nb; i++) {
  485. float min = FLT_MAX;
  486. float max = -FLT_MAX;
  487. for (int j = 0; j < qk; j++) {
  488. const float v = x[i*qk + j];
  489. if (v < min) min = v;
  490. if (v > max) max = v;
  491. }
  492. const float d = (max - min) / ((1 << 4) - 1);
  493. const float id = d ? 1.0f/d : 0.0f;
  494. y[i].d = GGML_FP32_TO_FP16(d);
  495. y[i].m = GGML_FP32_TO_FP16(min);
  496. for (int j = 0; j < qk/2; ++j) {
  497. const float x0 = (x[i*qk + 0 + j] - min)*id;
  498. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  499. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  500. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  501. y[i].qs[j] = xi0;
  502. y[i].qs[j] |= xi1 << 4;
  503. }
  504. }
  505. }
  506. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  507. quantize_row_q4_1_reference(x, y, k);
  508. }
  509. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  510. static const int qk = QK5_0;
  511. assert(k % qk == 0);
  512. const int nb = k / qk;
  513. for (int i = 0; i < nb; i++) {
  514. float amax = 0.0f; // absolute max
  515. float max = 0.0f;
  516. for (int j = 0; j < qk; j++) {
  517. const float v = x[i*qk + j];
  518. if (amax < fabsf(v)) {
  519. amax = fabsf(v);
  520. max = v;
  521. }
  522. }
  523. const float d = max / -16;
  524. const float id = d ? 1.0f/d : 0.0f;
  525. y[i].d = GGML_FP32_TO_FP16(d);
  526. uint32_t qh = 0;
  527. for (int j = 0; j < qk/2; ++j) {
  528. const float x0 = x[i*qk + 0 + j]*id;
  529. const float x1 = x[i*qk + qk/2 + j]*id;
  530. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  531. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  532. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  533. // get the 5-th bit and store it in qh at the right position
  534. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  535. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  536. }
  537. memcpy(&y[i].qh, &qh, sizeof(qh));
  538. }
  539. }
  540. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  541. quantize_row_q5_0_reference(x, y, k);
  542. }
  543. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  544. const int qk = QK5_1;
  545. assert(k % qk == 0);
  546. const int nb = k / qk;
  547. for (int i = 0; i < nb; i++) {
  548. float min = FLT_MAX;
  549. float max = -FLT_MAX;
  550. for (int j = 0; j < qk; j++) {
  551. const float v = x[i*qk + j];
  552. if (v < min) min = v;
  553. if (v > max) max = v;
  554. }
  555. const float d = (max - min) / ((1 << 5) - 1);
  556. const float id = d ? 1.0f/d : 0.0f;
  557. y[i].d = GGML_FP32_TO_FP16(d);
  558. y[i].m = GGML_FP32_TO_FP16(min);
  559. uint32_t qh = 0;
  560. for (int j = 0; j < qk/2; ++j) {
  561. const float x0 = (x[i*qk + 0 + j] - min)*id;
  562. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  563. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  564. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  565. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  566. // get the 5-th bit and store it in qh at the right position
  567. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  568. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  569. }
  570. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  571. }
  572. }
  573. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  574. quantize_row_q5_1_reference(x, y, k);
  575. }
  576. // reference implementation for deterministic creation of model files
  577. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  578. assert(k % QK8_0 == 0);
  579. const int nb = k / QK8_0;
  580. for (int i = 0; i < nb; i++) {
  581. float amax = 0.0f; // absolute max
  582. for (int j = 0; j < QK8_0; j++) {
  583. const float v = x[i*QK8_0 + j];
  584. amax = MAX(amax, fabsf(v));
  585. }
  586. const float d = amax / ((1 << 7) - 1);
  587. const float id = d ? 1.0f/d : 0.0f;
  588. y[i].d = GGML_FP32_TO_FP16(d);
  589. for (int j = 0; j < QK8_0; ++j) {
  590. const float x0 = x[i*QK8_0 + j]*id;
  591. y[i].qs[j] = roundf(x0);
  592. }
  593. }
  594. }
  595. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  596. assert(QK8_0 == 32);
  597. assert(k % QK8_0 == 0);
  598. const int nb = k / QK8_0;
  599. block_q8_0 * restrict y = vy;
  600. #if defined(__ARM_NEON)
  601. for (int i = 0; i < nb; i++) {
  602. float32x4_t srcv [8];
  603. float32x4_t asrcv[8];
  604. float32x4_t amaxv[8];
  605. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  606. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  607. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  608. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  609. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  610. const float amax = vmaxvq_f32(amaxv[0]);
  611. const float d = amax / ((1 << 7) - 1);
  612. const float id = d ? 1.0f/d : 0.0f;
  613. y[i].d = GGML_FP32_TO_FP16(d);
  614. for (int j = 0; j < 8; j++) {
  615. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  616. const int32x4_t vi = vcvtnq_s32_f32(v);
  617. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  618. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  619. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  620. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  621. }
  622. }
  623. #elif defined(__wasm_simd128__)
  624. for (int i = 0; i < nb; i++) {
  625. v128_t srcv [8];
  626. v128_t asrcv[8];
  627. v128_t amaxv[8];
  628. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  629. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  630. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  631. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  632. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  633. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  634. wasm_f32x4_extract_lane(amaxv[0], 1)),
  635. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  636. wasm_f32x4_extract_lane(amaxv[0], 3)));
  637. const float d = amax / ((1 << 7) - 1);
  638. const float id = d ? 1.0f/d : 0.0f;
  639. y[i].d = GGML_FP32_TO_FP16(d);
  640. for (int j = 0; j < 8; j++) {
  641. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  642. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  643. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  644. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  645. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  646. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  647. }
  648. }
  649. #elif defined(__AVX2__) || defined(__AVX__)
  650. for (int i = 0; i < nb; i++) {
  651. // Load elements into 4 AVX vectors
  652. __m256 v0 = _mm256_loadu_ps( x );
  653. __m256 v1 = _mm256_loadu_ps( x + 8 );
  654. __m256 v2 = _mm256_loadu_ps( x + 16 );
  655. __m256 v3 = _mm256_loadu_ps( x + 24 );
  656. x += 32;
  657. // Compute max(abs(e)) for the block
  658. const __m256 signBit = _mm256_set1_ps( -0.0f );
  659. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  660. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  661. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  662. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  663. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  664. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  665. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  666. const float maxScalar = _mm_cvtss_f32( max4 );
  667. // Quantize these floats
  668. const float d = maxScalar / 127.f;
  669. y[i].d = GGML_FP32_TO_FP16(d);
  670. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  671. const __m256 mul = _mm256_set1_ps( id );
  672. // Apply the multiplier
  673. v0 = _mm256_mul_ps( v0, mul );
  674. v1 = _mm256_mul_ps( v1, mul );
  675. v2 = _mm256_mul_ps( v2, mul );
  676. v3 = _mm256_mul_ps( v3, mul );
  677. // Round to nearest integer
  678. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  679. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  680. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  681. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  682. // Convert floats to integers
  683. __m256i i0 = _mm256_cvtps_epi32( v0 );
  684. __m256i i1 = _mm256_cvtps_epi32( v1 );
  685. __m256i i2 = _mm256_cvtps_epi32( v2 );
  686. __m256i i3 = _mm256_cvtps_epi32( v3 );
  687. #if defined(__AVX2__)
  688. // Convert int32 to int16
  689. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  690. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  691. // Convert int16 to int8
  692. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  693. // We got our precious signed bytes, but the order is now wrong
  694. // These AVX2 pack instructions process 16-byte pieces independently
  695. // The following instruction is fixing the order
  696. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  697. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  698. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  699. #else
  700. // Since we don't have in AVX some necessary functions,
  701. // we split the registers in half and call AVX2 analogs from SSE
  702. __m128i ni0 = _mm256_castsi256_si128( i0 );
  703. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  704. __m128i ni2 = _mm256_castsi256_si128( i1 );
  705. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  706. __m128i ni4 = _mm256_castsi256_si128( i2 );
  707. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  708. __m128i ni6 = _mm256_castsi256_si128( i3 );
  709. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  710. // Convert int32 to int16
  711. ni0 = _mm_packs_epi32( ni0, ni1 );
  712. ni2 = _mm_packs_epi32( ni2, ni3 );
  713. ni4 = _mm_packs_epi32( ni4, ni5 );
  714. ni6 = _mm_packs_epi32( ni6, ni7 );
  715. // Convert int16 to int8
  716. ni0 = _mm_packs_epi16( ni0, ni2 );
  717. ni4 = _mm_packs_epi16( ni4, ni6 );
  718. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  719. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  720. #endif
  721. }
  722. #elif defined(__riscv_v_intrinsic)
  723. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  724. for (int i = 0; i < nb; i++) {
  725. // load elements
  726. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  727. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  728. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  729. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  730. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  731. const float d = amax / ((1 << 7) - 1);
  732. const float id = d ? 1.0f/d : 0.0f;
  733. y[i].d = GGML_FP32_TO_FP16(d);
  734. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  735. // convert to integer
  736. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  737. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  738. // store result
  739. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  740. }
  741. #else
  742. GGML_UNUSED(nb);
  743. // scalar
  744. quantize_row_q8_0_reference(x, y, k);
  745. #endif
  746. }
  747. // reference implementation for deterministic creation of model files
  748. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  749. assert(QK8_1 == 32);
  750. assert(k % QK8_1 == 0);
  751. const int nb = k / QK8_1;
  752. for (int i = 0; i < nb; i++) {
  753. float amax = 0.0f; // absolute max
  754. for (int j = 0; j < QK8_1; j++) {
  755. const float v = x[i*QK8_1 + j];
  756. amax = MAX(amax, fabsf(v));
  757. }
  758. const float d = amax / ((1 << 7) - 1);
  759. const float id = d ? 1.0f/d : 0.0f;
  760. y[i].d = d;
  761. int sum = 0;
  762. for (int j = 0; j < QK8_1/2; ++j) {
  763. const float v0 = x[i*QK8_1 + j]*id;
  764. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  765. y[i].qs[ j] = roundf(v0);
  766. y[i].qs[QK8_1/2 + j] = roundf(v1);
  767. sum += y[i].qs[ j];
  768. sum += y[i].qs[QK8_1/2 + j];
  769. }
  770. y[i].s = sum*d;
  771. }
  772. }
  773. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  774. assert(k % QK8_1 == 0);
  775. const int nb = k / QK8_1;
  776. block_q8_1 * restrict y = vy;
  777. #if defined(__ARM_NEON)
  778. for (int i = 0; i < nb; i++) {
  779. float32x4_t srcv [8];
  780. float32x4_t asrcv[8];
  781. float32x4_t amaxv[8];
  782. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  783. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  784. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  785. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  786. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  787. const float amax = vmaxvq_f32(amaxv[0]);
  788. const float d = amax / ((1 << 7) - 1);
  789. const float id = d ? 1.0f/d : 0.0f;
  790. y[i].d = d;
  791. int32x4_t accv = vdupq_n_s32(0);
  792. for (int j = 0; j < 8; j++) {
  793. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  794. const int32x4_t vi = vcvtnq_s32_f32(v);
  795. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  796. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  797. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  798. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  799. accv = vaddq_s32(accv, vi);
  800. }
  801. y[i].s = d * vaddvq_s32(accv);
  802. }
  803. #elif defined(__wasm_simd128__)
  804. for (int i = 0; i < nb; i++) {
  805. v128_t srcv [8];
  806. v128_t asrcv[8];
  807. v128_t amaxv[8];
  808. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  809. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  810. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  811. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  812. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  813. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  814. wasm_f32x4_extract_lane(amaxv[0], 1)),
  815. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  816. wasm_f32x4_extract_lane(amaxv[0], 3)));
  817. const float d = amax / ((1 << 7) - 1);
  818. const float id = d ? 1.0f/d : 0.0f;
  819. y[i].d = d;
  820. v128_t accv = wasm_i32x4_splat(0);
  821. for (int j = 0; j < 8; j++) {
  822. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  823. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  824. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  825. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  826. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  827. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  828. accv = wasm_i32x4_add(accv, vi);
  829. }
  830. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  831. wasm_i32x4_extract_lane(accv, 1) +
  832. wasm_i32x4_extract_lane(accv, 2) +
  833. wasm_i32x4_extract_lane(accv, 3));
  834. }
  835. #elif defined(__AVX2__) || defined(__AVX__)
  836. for (int i = 0; i < nb; i++) {
  837. // Load elements into 4 AVX vectors
  838. __m256 v0 = _mm256_loadu_ps( x );
  839. __m256 v1 = _mm256_loadu_ps( x + 8 );
  840. __m256 v2 = _mm256_loadu_ps( x + 16 );
  841. __m256 v3 = _mm256_loadu_ps( x + 24 );
  842. x += 32;
  843. // Compute max(abs(e)) for the block
  844. const __m256 signBit = _mm256_set1_ps( -0.0f );
  845. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  846. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  847. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  848. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  849. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  850. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  851. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  852. const float maxScalar = _mm_cvtss_f32( max4 );
  853. // Quantize these floats
  854. const float d = maxScalar / 127.f;
  855. y[i].d = d;
  856. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  857. const __m256 mul = _mm256_set1_ps( id );
  858. // Apply the multiplier
  859. v0 = _mm256_mul_ps( v0, mul );
  860. v1 = _mm256_mul_ps( v1, mul );
  861. v2 = _mm256_mul_ps( v2, mul );
  862. v3 = _mm256_mul_ps( v3, mul );
  863. // Round to nearest integer
  864. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  865. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  866. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  867. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  868. // Convert floats to integers
  869. __m256i i0 = _mm256_cvtps_epi32( v0 );
  870. __m256i i1 = _mm256_cvtps_epi32( v1 );
  871. __m256i i2 = _mm256_cvtps_epi32( v2 );
  872. __m256i i3 = _mm256_cvtps_epi32( v3 );
  873. #if defined(__AVX2__)
  874. // Compute the sum of the quants and set y[i].s
  875. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  876. // Convert int32 to int16
  877. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  878. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  879. // Convert int16 to int8
  880. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  881. // We got our precious signed bytes, but the order is now wrong
  882. // These AVX2 pack instructions process 16-byte pieces independently
  883. // The following instruction is fixing the order
  884. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  885. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  886. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  887. #else
  888. // Since we don't have in AVX some necessary functions,
  889. // we split the registers in half and call AVX2 analogs from SSE
  890. __m128i ni0 = _mm256_castsi256_si128( i0 );
  891. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  892. __m128i ni2 = _mm256_castsi256_si128( i1 );
  893. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  894. __m128i ni4 = _mm256_castsi256_si128( i2 );
  895. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  896. __m128i ni6 = _mm256_castsi256_si128( i3 );
  897. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  898. // Compute the sum of the quants and set y[i].s
  899. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  900. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  901. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  902. // Convert int32 to int16
  903. ni0 = _mm_packs_epi32( ni0, ni1 );
  904. ni2 = _mm_packs_epi32( ni2, ni3 );
  905. ni4 = _mm_packs_epi32( ni4, ni5 );
  906. ni6 = _mm_packs_epi32( ni6, ni7 );
  907. // Convert int16 to int8
  908. ni0 = _mm_packs_epi16( ni0, ni2 );
  909. ni4 = _mm_packs_epi16( ni4, ni6 );
  910. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  911. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  912. #endif
  913. }
  914. #elif defined(__riscv_v_intrinsic)
  915. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  916. for (int i = 0; i < nb; i++) {
  917. // load elements
  918. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  919. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  920. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  921. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  922. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  923. const float d = amax / ((1 << 7) - 1);
  924. const float id = d ? 1.0f/d : 0.0f;
  925. y[i].d = d;
  926. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  927. // convert to integer
  928. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  929. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  930. // store result
  931. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  932. // compute sum for y[i].s
  933. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  934. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  935. // set y[i].s
  936. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  937. y[i].s = sum*d;
  938. }
  939. #else
  940. GGML_UNUSED(nb);
  941. // scalar
  942. quantize_row_q8_1_reference(x, y, k);
  943. #endif
  944. }
  945. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  946. static const int qk = QK4_0;
  947. assert(k % qk == 0);
  948. const int nb = k / qk;
  949. for (int i = 0; i < nb; i++) {
  950. const float d = GGML_FP16_TO_FP32(x[i].d);
  951. for (int j = 0; j < qk/2; ++j) {
  952. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  953. const int x1 = (x[i].qs[j] >> 4) - 8;
  954. y[i*qk + j + 0 ] = x0*d;
  955. y[i*qk + j + qk/2] = x1*d;
  956. }
  957. }
  958. }
  959. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  960. static const int qk = QK4_1;
  961. assert(k % qk == 0);
  962. const int nb = k / qk;
  963. for (int i = 0; i < nb; i++) {
  964. const float d = GGML_FP16_TO_FP32(x[i].d);
  965. const float m = GGML_FP16_TO_FP32(x[i].m);
  966. for (int j = 0; j < qk/2; ++j) {
  967. const int x0 = (x[i].qs[j] & 0x0F);
  968. const int x1 = (x[i].qs[j] >> 4);
  969. y[i*qk + j + 0 ] = x0*d + m;
  970. y[i*qk + j + qk/2] = x1*d + m;
  971. }
  972. }
  973. }
  974. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  975. static const int qk = QK5_0;
  976. assert(k % qk == 0);
  977. const int nb = k / qk;
  978. for (int i = 0; i < nb; i++) {
  979. const float d = GGML_FP16_TO_FP32(x[i].d);
  980. uint32_t qh;
  981. memcpy(&qh, x[i].qh, sizeof(qh));
  982. for (int j = 0; j < qk/2; ++j) {
  983. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  984. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  985. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  986. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  987. y[i*qk + j + 0 ] = x0*d;
  988. y[i*qk + j + qk/2] = x1*d;
  989. }
  990. }
  991. }
  992. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  993. static const int qk = QK5_1;
  994. assert(k % qk == 0);
  995. const int nb = k / qk;
  996. for (int i = 0; i < nb; i++) {
  997. const float d = GGML_FP16_TO_FP32(x[i].d);
  998. const float m = GGML_FP16_TO_FP32(x[i].m);
  999. uint32_t qh;
  1000. memcpy(&qh, x[i].qh, sizeof(qh));
  1001. for (int j = 0; j < qk/2; ++j) {
  1002. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1003. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1004. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  1005. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  1006. y[i*qk + j + 0 ] = x0*d + m;
  1007. y[i*qk + j + qk/2] = x1*d + m;
  1008. }
  1009. }
  1010. }
  1011. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  1012. static const int qk = QK8_0;
  1013. assert(k % qk == 0);
  1014. const int nb = k / qk;
  1015. for (int i = 0; i < nb; i++) {
  1016. const float d = GGML_FP16_TO_FP32(x[i].d);
  1017. for (int j = 0; j < qk; ++j) {
  1018. y[i*qk + j] = x[i].qs[j]*d;
  1019. }
  1020. }
  1021. }
  1022. //
  1023. // 2-6 bit quantization in super-blocks
  1024. //
  1025. //
  1026. // ===================== Helper functions
  1027. //
  1028. static inline int nearest_int(float fval) {
  1029. assert(fval <= 4194303.f);
  1030. float val = fval + 12582912.f;
  1031. int i; memcpy(&i, &val, sizeof(int));
  1032. return (i & 0x007fffff) - 0x00400000;
  1033. }
  1034. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  1035. const float * restrict qw) {
  1036. float max = 0;
  1037. float amax = 0;
  1038. for (int i = 0; i < n; ++i) {
  1039. float ax = fabsf(x[i]);
  1040. if (ax > amax) { amax = ax; max = x[i]; }
  1041. }
  1042. if (amax < 1e-30f) { // all zero
  1043. for (int i = 0; i < n; ++i) {
  1044. L[i] = 0;
  1045. }
  1046. return 0.f;
  1047. }
  1048. float iscale = -nmax / max;
  1049. if (rmse_type == 0) {
  1050. for (int i = 0; i < n; ++i) {
  1051. int l = nearest_int(iscale * x[i]);
  1052. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1053. }
  1054. return 1/iscale;
  1055. }
  1056. bool return_early = false;
  1057. if (rmse_type < 0) {
  1058. rmse_type = -rmse_type;
  1059. return_early = true;
  1060. }
  1061. float sumlx = 0;
  1062. float suml2 = 0;
  1063. #ifdef HAVE_BUGGY_APPLE_LINKER
  1064. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1065. for (volatile int i = 0; i < n; ++i) {
  1066. #else
  1067. for (int i = 0; i < n; ++i) {
  1068. #endif
  1069. int l = nearest_int(iscale * x[i]);
  1070. l = MAX(-nmax, MIN(nmax-1, l));
  1071. L[i] = l + nmax;
  1072. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1073. sumlx += w*x[i]*l;
  1074. suml2 += w*l*l;
  1075. }
  1076. float scale = sumlx/suml2;
  1077. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1078. float best = scale * sumlx;
  1079. for (int is = -9; is <= 9; ++is) {
  1080. if (is == 0) {
  1081. continue;
  1082. }
  1083. iscale = -(nmax + 0.1f*is) / max;
  1084. sumlx = suml2 = 0;
  1085. for (int i = 0; i < n; ++i) {
  1086. int l = nearest_int(iscale * x[i]);
  1087. l = MAX(-nmax, MIN(nmax-1, l));
  1088. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1089. sumlx += w*x[i]*l;
  1090. suml2 += w*l*l;
  1091. }
  1092. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1093. for (int i = 0; i < n; ++i) {
  1094. int l = nearest_int(iscale * x[i]);
  1095. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1096. }
  1097. scale = sumlx/suml2; best = scale*sumlx;
  1098. }
  1099. }
  1100. return scale;
  1101. }
  1102. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1103. float max = 0;
  1104. float amax = 0;
  1105. for (int i = 0; i < n; ++i) {
  1106. float ax = fabsf(x[i]);
  1107. if (ax > amax) { amax = ax; max = x[i]; }
  1108. }
  1109. if (!amax) { // all zero
  1110. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1111. return 0.f;
  1112. }
  1113. float iscale = -nmax / max;
  1114. if (do_rmse) {
  1115. float sumlx = 0;
  1116. float suml2 = 0;
  1117. for (int i = 0; i < n; ++i) {
  1118. int l = nearest_int(iscale * x[i]);
  1119. l = MAX(-nmax, MIN(nmax-1, l));
  1120. L[i] = l;
  1121. float w = x[i]*x[i];
  1122. sumlx += w*x[i]*l;
  1123. suml2 += w*l*l;
  1124. }
  1125. for (int itry = 0; itry < 5; ++itry) {
  1126. int n_changed = 0;
  1127. for (int i = 0; i < n; ++i) {
  1128. float w = x[i]*x[i];
  1129. float slx = sumlx - w*x[i]*L[i];
  1130. if (slx > 0) {
  1131. float sl2 = suml2 - w*L[i]*L[i];
  1132. int new_l = nearest_int(x[i] * sl2 / slx);
  1133. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1134. if (new_l != L[i]) {
  1135. slx += w*x[i]*new_l;
  1136. sl2 += w*new_l*new_l;
  1137. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1138. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1139. ++n_changed;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (!n_changed) {
  1145. break;
  1146. }
  1147. }
  1148. for (int i = 0; i < n; ++i) {
  1149. L[i] += nmax;
  1150. }
  1151. return sumlx / suml2;
  1152. }
  1153. for (int i = 0; i < n; ++i) {
  1154. int l = nearest_int(iscale * x[i]);
  1155. l = MAX(-nmax, MIN(nmax-1, l));
  1156. L[i] = l + nmax;
  1157. }
  1158. return 1/iscale;
  1159. }
  1160. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1161. int ntry, float alpha) {
  1162. float min = x[0];
  1163. float max = x[0];
  1164. for (int i = 1; i < n; ++i) {
  1165. if (x[i] < min) min = x[i];
  1166. if (x[i] > max) max = x[i];
  1167. }
  1168. if (max == min) {
  1169. for (int i = 0; i < n; ++i) L[i] = 0;
  1170. *the_min = 0;
  1171. return 0.f;
  1172. }
  1173. if (min > 0) min = 0;
  1174. float iscale = nmax/(max - min);
  1175. float scale = 1/iscale;
  1176. for (int itry = 0; itry < ntry; ++itry) {
  1177. float sumlx = 0; int suml2 = 0;
  1178. bool did_change = false;
  1179. for (int i = 0; i < n; ++i) {
  1180. int l = nearest_int(iscale*(x[i] - min));
  1181. l = MAX(0, MIN(nmax, l));
  1182. if (l != L[i]) {
  1183. L[i] = l;
  1184. did_change = true;
  1185. }
  1186. sumlx += (x[i] - min)*l;
  1187. suml2 += l*l;
  1188. }
  1189. scale = sumlx/suml2;
  1190. float sum = 0;
  1191. for (int i = 0; i < n; ++i) {
  1192. sum += x[i] - scale*L[i];
  1193. }
  1194. min = alpha*min + (1 - alpha)*sum/n;
  1195. if (min > 0) min = 0;
  1196. iscale = 1/scale;
  1197. if (!did_change) break;
  1198. }
  1199. *the_min = -min;
  1200. return scale;
  1201. }
  1202. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1203. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1204. float rmin, float rdelta, int nstep, bool use_mad) {
  1205. float min = x[0];
  1206. float max = x[0];
  1207. float sum_w = weights[0];
  1208. float sum_x = sum_w * x[0];
  1209. #ifdef HAVE_BUGGY_APPLE_LINKER
  1210. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1211. for (volatile int i = 1; i < n; ++i) {
  1212. #else
  1213. for (int i = 1; i < n; ++i) {
  1214. #endif
  1215. if (x[i] < min) min = x[i];
  1216. if (x[i] > max) max = x[i];
  1217. float w = weights[i];
  1218. sum_w += w;
  1219. sum_x += w * x[i];
  1220. }
  1221. if (min > 0) min = 0;
  1222. if (max == min) {
  1223. for (int i = 0; i < n; ++i) L[i] = 0;
  1224. *the_min = -min;
  1225. return 0.f;
  1226. }
  1227. float iscale = nmax/(max - min);
  1228. float scale = 1/iscale;
  1229. float best_mad = 0;
  1230. for (int i = 0; i < n; ++i) {
  1231. int l = nearest_int(iscale*(x[i] - min));
  1232. L[i] = MAX(0, MIN(nmax, l));
  1233. float diff = scale * L[i] + min - x[i];
  1234. diff = use_mad ? fabsf(diff) : diff * diff;
  1235. float w = weights[i];
  1236. best_mad += w * diff;
  1237. }
  1238. if (nstep < 1) {
  1239. *the_min = -min;
  1240. return scale;
  1241. }
  1242. for (int is = 0; is <= nstep; ++is) {
  1243. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1244. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1245. for (int i = 0; i < n; ++i) {
  1246. int l = nearest_int(iscale*(x[i] - min));
  1247. l = MAX(0, MIN(nmax, l));
  1248. Laux[i] = l;
  1249. float w = weights[i];
  1250. sum_l += w*l;
  1251. sum_l2 += w*l*l;
  1252. sum_xl += w*l*x[i];
  1253. }
  1254. float D = sum_w * sum_l2 - sum_l * sum_l;
  1255. if (D > 0) {
  1256. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1257. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1258. if (this_min > 0) {
  1259. this_min = 0;
  1260. this_scale = sum_xl / sum_l2;
  1261. }
  1262. float mad = 0;
  1263. for (int i = 0; i < n; ++i) {
  1264. float diff = this_scale * Laux[i] + this_min - x[i];
  1265. diff = use_mad ? fabsf(diff) : diff * diff;
  1266. float w = weights[i];
  1267. mad += w * diff;
  1268. }
  1269. if (mad < best_mad) {
  1270. for (int i = 0; i < n; ++i) {
  1271. L[i] = Laux[i];
  1272. }
  1273. best_mad = mad;
  1274. scale = this_scale;
  1275. min = this_min;
  1276. }
  1277. }
  1278. }
  1279. *the_min = -min;
  1280. return scale;
  1281. }
  1282. #if QK_K == 256
  1283. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1284. if (j < 4) {
  1285. *d = q[j] & 63; *m = q[j + 4] & 63;
  1286. } else {
  1287. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1288. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1289. }
  1290. }
  1291. #endif
  1292. //========================- 2-bit (de)-quantization
  1293. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1294. assert(k % QK_K == 0);
  1295. const int nb = k / QK_K;
  1296. uint8_t L[QK_K];
  1297. uint8_t Laux[16];
  1298. float weights[16];
  1299. float mins[QK_K/16];
  1300. float scales[QK_K/16];
  1301. const float q4scale = 15.f;
  1302. for (int i = 0; i < nb; i++) {
  1303. float max_scale = 0; // as we are deducting the min, scales are always positive
  1304. float max_min = 0;
  1305. for (int j = 0; j < QK_K/16; ++j) {
  1306. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1307. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1308. float scale = scales[j];
  1309. if (scale > max_scale) {
  1310. max_scale = scale;
  1311. }
  1312. float min = mins[j];
  1313. if (min > max_min) {
  1314. max_min = min;
  1315. }
  1316. }
  1317. if (max_scale > 0) {
  1318. float iscale = q4scale/max_scale;
  1319. for (int j = 0; j < QK_K/16; ++j) {
  1320. int l = nearest_int(iscale*scales[j]);
  1321. y[i].scales[j] = l;
  1322. }
  1323. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1324. } else {
  1325. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1326. y[i].d = GGML_FP32_TO_FP16(0.f);
  1327. }
  1328. if (max_min > 0) {
  1329. float iscale = q4scale/max_min;
  1330. for (int j = 0; j < QK_K/16; ++j) {
  1331. int l = nearest_int(iscale*mins[j]);
  1332. y[i].scales[j] |= (l << 4);
  1333. }
  1334. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1335. } else {
  1336. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1337. }
  1338. for (int j = 0; j < QK_K/16; ++j) {
  1339. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1340. if (!d) continue;
  1341. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1342. for (int ii = 0; ii < 16; ++ii) {
  1343. int l = nearest_int((x[16*j + ii] + dm)/d);
  1344. l = MAX(0, MIN(3, l));
  1345. L[16*j + ii] = l;
  1346. }
  1347. }
  1348. #if QK_K == 256
  1349. for (int j = 0; j < QK_K; j += 128) {
  1350. for (int l = 0; l < 32; ++l) {
  1351. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1352. }
  1353. }
  1354. #else
  1355. for (int l = 0; l < 16; ++l) {
  1356. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1357. }
  1358. #endif
  1359. x += QK_K;
  1360. }
  1361. }
  1362. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1363. assert(k % QK_K == 0);
  1364. const int nb = k / QK_K;
  1365. for (int i = 0; i < nb; i++) {
  1366. const float d = GGML_FP16_TO_FP32(x[i].d);
  1367. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1368. const uint8_t * q = x[i].qs;
  1369. #if QK_K == 256
  1370. int is = 0;
  1371. float dl, ml;
  1372. for (int n = 0; n < QK_K; n += 128) {
  1373. int shift = 0;
  1374. for (int j = 0; j < 4; ++j) {
  1375. uint8_t sc = x[i].scales[is++];
  1376. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1377. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1378. sc = x[i].scales[is++];
  1379. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1380. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1381. shift += 2;
  1382. }
  1383. q += 32;
  1384. }
  1385. #else
  1386. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1387. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1388. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1389. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1390. for (int l = 0; l < 16; ++l) {
  1391. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1392. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1393. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1394. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1395. }
  1396. y += QK_K;
  1397. #endif
  1398. }
  1399. }
  1400. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1401. quantize_row_q2_K_reference(x, vy, k);
  1402. }
  1403. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1404. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1405. float rmin, float rdelta, int nstep, bool use_mad) {
  1406. float min = x[0];
  1407. float max = x[0];
  1408. float sum_w = weights ? weights[0] : x[0]*x[0];
  1409. float sum_x = sum_w * x[0];
  1410. #ifdef HAVE_BUGGY_APPLE_LINKER
  1411. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1412. for (volatile int i = 1; i < n; ++i) {
  1413. #else
  1414. for (int i = 1; i < n; ++i) {
  1415. #endif
  1416. if (x[i] < min) min = x[i];
  1417. if (x[i] > max) max = x[i];
  1418. float w = weights ? weights[i] : x[i]*x[i];
  1419. sum_w += w;
  1420. sum_x += w * x[i];
  1421. }
  1422. if (min > 0) {
  1423. min = 0;
  1424. }
  1425. if (max <= min) {
  1426. memset(L, 0, n);
  1427. *the_min = -min;
  1428. return 0.f;
  1429. }
  1430. float iscale = nmax/(max - min);
  1431. float scale = 1/iscale;
  1432. float best_mad = 0;
  1433. for (int i = 0; i < n; ++i) {
  1434. int l = nearest_int(iscale*(x[i] - min));
  1435. L[i] = MAX(0, MIN(nmax, l));
  1436. float diff = scale * L[i] + min - x[i];
  1437. diff = use_mad ? fabsf(diff) : diff*diff;
  1438. float w = weights ? weights[i] : x[i]*x[i];
  1439. best_mad += w * diff;
  1440. }
  1441. if (nstep < 1) {
  1442. *the_min = -min;
  1443. return scale;
  1444. }
  1445. for (int is = 0; is <= nstep; ++is) {
  1446. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1447. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1448. for (int i = 0; i < n; ++i) {
  1449. int l = nearest_int(iscale*(x[i] - min));
  1450. l = MAX(0, MIN(nmax, l));
  1451. Laux[i] = l;
  1452. float w = weights ? weights[i] : x[i]*x[i];
  1453. sum_l += w*l;
  1454. sum_l2 += w*l*l;
  1455. sum_xl += w*l*x[i];
  1456. }
  1457. float D = sum_w * sum_l2 - sum_l * sum_l;
  1458. if (D > 0) {
  1459. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1460. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1461. if (this_min > 0) {
  1462. this_min = 0;
  1463. this_scale = sum_xl / sum_l2;
  1464. }
  1465. float mad = 0;
  1466. for (int i = 0; i < n; ++i) {
  1467. float diff = this_scale * Laux[i] + this_min - x[i];
  1468. diff = use_mad ? fabsf(diff) : diff*diff;
  1469. float w = weights ? weights[i] : x[i]*x[i];
  1470. mad += w * diff;
  1471. }
  1472. if (mad < best_mad) {
  1473. for (int i = 0; i < n; ++i) {
  1474. L[i] = Laux[i];
  1475. }
  1476. best_mad = mad;
  1477. scale = this_scale;
  1478. min = this_min;
  1479. }
  1480. }
  1481. }
  1482. *the_min = -min;
  1483. return scale;
  1484. }
  1485. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1486. float max = 0;
  1487. for (int i = 0; i < n; ++i) {
  1488. max = MAX(max, x[i]);
  1489. }
  1490. if (!max) { // all zero
  1491. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1492. return 0.f;
  1493. }
  1494. float iscale = nmax / max;
  1495. for (int i = 0; i < n; ++i) {
  1496. L[i] = nearest_int(iscale * x[i]);
  1497. }
  1498. float scale = 1/iscale;
  1499. float best_mse = 0;
  1500. for (int i = 0; i < n; ++i) {
  1501. float diff = x[i] - scale*L[i];
  1502. float w = quant_weights[i];
  1503. best_mse += w*diff*diff;
  1504. }
  1505. for (int is = -4; is <= 4; ++is) {
  1506. if (is == 0) continue;
  1507. float iscale_is = (0.1f*is + nmax)/max;
  1508. float scale_is = 1/iscale_is;
  1509. float mse = 0;
  1510. for (int i = 0; i < n; ++i) {
  1511. int l = nearest_int(iscale_is*x[i]);
  1512. l = MIN(nmax, l);
  1513. float diff = x[i] - scale_is*l;
  1514. float w = quant_weights[i];
  1515. mse += w*diff*diff;
  1516. }
  1517. if (mse < best_mse) {
  1518. best_mse = mse;
  1519. iscale = iscale_is;
  1520. }
  1521. }
  1522. float sumlx = 0;
  1523. float suml2 = 0;
  1524. for (int i = 0; i < n; ++i) {
  1525. int l = nearest_int(iscale * x[i]);
  1526. l = MIN(nmax, l);
  1527. L[i] = l;
  1528. float w = quant_weights[i];
  1529. sumlx += w*x[i]*l;
  1530. suml2 += w*l*l;
  1531. }
  1532. for (int itry = 0; itry < 5; ++itry) {
  1533. int n_changed = 0;
  1534. for (int i = 0; i < n; ++i) {
  1535. float w = quant_weights[i];
  1536. float slx = sumlx - w*x[i]*L[i];
  1537. float sl2 = suml2 - w*L[i]*L[i];
  1538. if (slx > 0 && sl2 > 0) {
  1539. int new_l = nearest_int(x[i] * sl2 / slx);
  1540. new_l = MIN(nmax, new_l);
  1541. if (new_l != L[i]) {
  1542. slx += w*x[i]*new_l;
  1543. sl2 += w*new_l*new_l;
  1544. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1545. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1546. ++n_changed;
  1547. }
  1548. }
  1549. }
  1550. }
  1551. if (!n_changed) {
  1552. break;
  1553. }
  1554. }
  1555. return sumlx / suml2;
  1556. }
  1557. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1558. GGML_ASSERT(quant_weights);
  1559. assert(k % QK_K == 0);
  1560. const int nb = k / QK_K;
  1561. const bool requantize = true;
  1562. uint8_t L[QK_K];
  1563. uint8_t Laux[16];
  1564. float mins[QK_K/16];
  1565. float scales[QK_K/16];
  1566. float sw[QK_K/16];
  1567. float weight[16];
  1568. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1569. for (int i = 0; i < nb; i++) {
  1570. memset(sw, 0, QK_K/16*sizeof(float));
  1571. float sumx2 = 0;
  1572. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1573. float sigma2 = sumx2/QK_K;
  1574. for (int j = 0; j < QK_K/16; ++j) {
  1575. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1576. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1577. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1578. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1579. }
  1580. float dm, mm;
  1581. #if QK_K == 64
  1582. float max_scale = 0, max_min = 0;
  1583. for (int j = 0; j < QK_K/16; ++j) {
  1584. max_scale = MAX(max_scale, scales[j]);
  1585. max_min = MAX(max_min, mins[j]);
  1586. }
  1587. dm = max_scale/15;
  1588. mm = max_min/15;
  1589. if (max_scale) {
  1590. float id = 1/dm;
  1591. for (int j = 0; j < QK_K/16; ++j) {
  1592. int l = nearest_int(id*scales[j]);
  1593. Ls[j] = MAX(0, MIN(15, l));
  1594. }
  1595. } else {
  1596. memset(Ls, 0, QK_K/16);
  1597. }
  1598. if (max_min) {
  1599. float id = 1/mm;
  1600. for (int j = 0; j < QK_K/16; ++j) {
  1601. int l = nearest_int(id*mins[j]);
  1602. Lm[j] = MAX(0, MIN(15, l));
  1603. }
  1604. } else {
  1605. memset(Lm, 0, QK_K/16);
  1606. }
  1607. #else
  1608. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1609. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1610. #endif
  1611. y[i].d = GGML_FP32_TO_FP16(dm);
  1612. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1613. dm = GGML_FP16_TO_FP32(y[i].d);
  1614. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1615. for (int j = 0; j < QK_K/16; ++j) {
  1616. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1617. }
  1618. if (requantize) {
  1619. for (int j = 0; j < QK_K/16; ++j) {
  1620. const float d = dm * (y[i].scales[j] & 0xF);
  1621. if (!d) continue;
  1622. const float m = mm * (y[i].scales[j] >> 4);
  1623. for (int ii = 0; ii < 16; ++ii) {
  1624. int l = nearest_int((x[16*j + ii] + m)/d);
  1625. l = MAX(0, MIN(3, l));
  1626. L[16*j + ii] = l;
  1627. }
  1628. }
  1629. }
  1630. #if QK_K == 256
  1631. for (int j = 0; j < QK_K; j += 128) {
  1632. for (int l = 0; l < 32; ++l) {
  1633. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1634. }
  1635. }
  1636. #else
  1637. for (int l = 0; l < 16; ++l) {
  1638. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1639. }
  1640. #endif
  1641. x += QK_K;
  1642. }
  1643. }
  1644. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1645. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1646. if (!quant_weights) {
  1647. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1648. }
  1649. else {
  1650. char * qrow = (char *)dst;
  1651. for (int row = 0; row < nrow; ++row) {
  1652. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1653. src += n_per_row;
  1654. qrow += row_size;
  1655. }
  1656. }
  1657. return nrow * row_size;
  1658. }
  1659. //========================= 3-bit (de)-quantization
  1660. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1661. assert(k % QK_K == 0);
  1662. const int nb = k / QK_K;
  1663. int8_t L[QK_K];
  1664. float scales[QK_K / 16];
  1665. for (int i = 0; i < nb; i++) {
  1666. float max_scale = 0;
  1667. float amax = 0;
  1668. for (int j = 0; j < QK_K/16; ++j) {
  1669. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1670. float scale = fabsf(scales[j]);
  1671. if (scale > amax) {
  1672. amax = scale; max_scale = scales[j];
  1673. }
  1674. }
  1675. #if QK_K == 256
  1676. memset(y[i].scales, 0, 12);
  1677. if (max_scale) {
  1678. float iscale = -32.f/max_scale;
  1679. for (int j = 0; j < QK_K/16; ++j) {
  1680. int8_t l = nearest_int(iscale*scales[j]);
  1681. l = MAX(-32, MIN(31, l)) + 32;
  1682. if (j < 8) {
  1683. y[i].scales[j] = l & 0xF;
  1684. } else {
  1685. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1686. }
  1687. l >>= 4;
  1688. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1689. }
  1690. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1691. } else {
  1692. y[i].d = GGML_FP32_TO_FP16(0.f);
  1693. }
  1694. int8_t sc;
  1695. for (int j = 0; j < QK_K/16; ++j) {
  1696. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1697. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1698. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1699. if (!d) {
  1700. continue;
  1701. }
  1702. for (int ii = 0; ii < 16; ++ii) {
  1703. int l = nearest_int(x[16*j + ii]/d);
  1704. l = MAX(-4, MIN(3, l));
  1705. L[16*j + ii] = l + 4;
  1706. }
  1707. }
  1708. #else
  1709. if (max_scale) {
  1710. float iscale = -8.f/max_scale;
  1711. for (int j = 0; j < QK_K/16; j+=2) {
  1712. int l1 = nearest_int(iscale*scales[j]);
  1713. l1 = 8 + MAX(-8, MIN(7, l1));
  1714. int l2 = nearest_int(iscale*scales[j+1]);
  1715. l2 = 8 + MAX(-8, MIN(7, l2));
  1716. y[i].scales[j/2] = l1 | (l2 << 4);
  1717. }
  1718. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1719. } else {
  1720. for (int j = 0; j < QK_K/16; j+=2) {
  1721. y[i].scales[j/2] = 0;
  1722. }
  1723. y[i].d = GGML_FP32_TO_FP16(0.f);
  1724. }
  1725. for (int j = 0; j < QK_K/16; ++j) {
  1726. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1727. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1728. if (!d) {
  1729. continue;
  1730. }
  1731. for (int ii = 0; ii < 16; ++ii) {
  1732. int l = nearest_int(x[16*j + ii]/d);
  1733. l = MAX(-4, MIN(3, l));
  1734. L[16*j + ii] = l + 4;
  1735. }
  1736. }
  1737. #endif
  1738. memset(y[i].hmask, 0, QK_K/8);
  1739. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1740. int m = 0;
  1741. uint8_t hm = 1;
  1742. for (int j = 0; j < QK_K; ++j) {
  1743. if (L[j] > 3) {
  1744. y[i].hmask[m] |= hm;
  1745. L[j] -= 4;
  1746. }
  1747. if (++m == QK_K/8) {
  1748. m = 0; hm <<= 1;
  1749. }
  1750. }
  1751. #if QK_K == 256
  1752. for (int j = 0; j < QK_K; j += 128) {
  1753. for (int l = 0; l < 32; ++l) {
  1754. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1755. }
  1756. }
  1757. #else
  1758. for (int l = 0; l < 16; ++l) {
  1759. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1760. }
  1761. #endif
  1762. x += QK_K;
  1763. }
  1764. }
  1765. #if QK_K == 256
  1766. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1767. assert(k % QK_K == 0);
  1768. const int nb = k / QK_K;
  1769. const uint32_t kmask1 = 0x03030303;
  1770. const uint32_t kmask2 = 0x0f0f0f0f;
  1771. uint32_t aux[4];
  1772. const int8_t * scales = (const int8_t*)aux;
  1773. for (int i = 0; i < nb; i++) {
  1774. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1775. const uint8_t * restrict q = x[i].qs;
  1776. const uint8_t * restrict hm = x[i].hmask;
  1777. uint8_t m = 1;
  1778. memcpy(aux, x[i].scales, 12);
  1779. uint32_t tmp = aux[2];
  1780. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1781. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1782. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1783. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1784. int is = 0;
  1785. float dl;
  1786. for (int n = 0; n < QK_K; n += 128) {
  1787. int shift = 0;
  1788. for (int j = 0; j < 4; ++j) {
  1789. dl = d_all * (scales[is++] - 32);
  1790. for (int l = 0; l < 16; ++l) {
  1791. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1792. }
  1793. dl = d_all * (scales[is++] - 32);
  1794. for (int l = 0; l < 16; ++l) {
  1795. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1796. }
  1797. shift += 2;
  1798. m <<= 1;
  1799. }
  1800. q += 32;
  1801. }
  1802. }
  1803. }
  1804. #else
  1805. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1806. assert(k % QK_K == 0);
  1807. assert(QK_K == 64);
  1808. const int nb = k / QK_K;
  1809. for (int i = 0; i < nb; i++) {
  1810. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1811. const uint8_t * restrict q = x[i].qs;
  1812. const uint8_t * restrict hm = x[i].hmask;
  1813. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1814. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1815. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1816. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1817. for (int l=0; l<8; ++l) {
  1818. uint8_t h = hm[l];
  1819. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1820. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1821. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1822. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1823. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1824. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1825. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1826. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1827. }
  1828. y += QK_K;
  1829. }
  1830. }
  1831. #endif
  1832. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1833. quantize_row_q3_K_reference(x, vy, k);
  1834. }
  1835. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1836. #if QK_K != 256
  1837. (void)quant_weights;
  1838. quantize_row_q3_K_reference(x, y, n_per_row);
  1839. #else
  1840. assert(n_per_row % QK_K == 0);
  1841. const int nb = n_per_row / QK_K;
  1842. int8_t L[QK_K];
  1843. float scales[QK_K / 16];
  1844. float weight[16];
  1845. float sw[QK_K / 16];
  1846. int8_t Ls[QK_K / 16];
  1847. for (int i = 0; i < nb; i++) {
  1848. float sumx2 = 0;
  1849. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1850. float sigma2 = 2*sumx2/QK_K;
  1851. for (int j = 0; j < QK_K/16; ++j) {
  1852. if (quant_weights) {
  1853. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1854. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1855. } else {
  1856. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1857. }
  1858. float sumw = 0;
  1859. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1860. sw[j] = sumw;
  1861. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1862. }
  1863. memset(y[i].scales, 0, 12);
  1864. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1865. for (int j = 0; j < QK_K/16; ++j) {
  1866. int l = Ls[j];
  1867. if (j < 8) {
  1868. y[i].scales[j] = l & 0xF;
  1869. } else {
  1870. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1871. }
  1872. l >>= 4;
  1873. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1874. }
  1875. y[i].d = GGML_FP32_TO_FP16(d_block);
  1876. int8_t sc;
  1877. for (int j = 0; j < QK_K/16; ++j) {
  1878. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1879. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1880. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1881. if (!d) {
  1882. continue;
  1883. }
  1884. for (int ii = 0; ii < 16; ++ii) {
  1885. int l = nearest_int(x[16*j + ii]/d);
  1886. l = MAX(-4, MIN(3, l));
  1887. L[16*j + ii] = l + 4;
  1888. }
  1889. }
  1890. memset(y[i].hmask, 0, QK_K/8);
  1891. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1892. int m = 0;
  1893. uint8_t hm = 1;
  1894. for (int j = 0; j < QK_K; ++j) {
  1895. if (L[j] > 3) {
  1896. y[i].hmask[m] |= hm;
  1897. L[j] -= 4;
  1898. }
  1899. if (++m == QK_K/8) {
  1900. m = 0; hm <<= 1;
  1901. }
  1902. }
  1903. for (int j = 0; j < QK_K; j += 128) {
  1904. for (int l = 0; l < 32; ++l) {
  1905. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1906. }
  1907. }
  1908. x += QK_K;
  1909. }
  1910. #endif
  1911. }
  1912. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1913. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1914. if (!quant_weights) {
  1915. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1916. }
  1917. else {
  1918. char * qrow = (char *)dst;
  1919. for (int row = 0; row < nrow; ++row) {
  1920. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1921. src += n_per_row;
  1922. qrow += row_size;
  1923. }
  1924. }
  1925. return nrow * row_size;
  1926. }
  1927. // ====================== 4-bit (de)-quantization
  1928. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1929. assert(k % QK_K == 0);
  1930. const int nb = k / QK_K;
  1931. uint8_t L[QK_K];
  1932. uint8_t Laux[32];
  1933. float weights[32];
  1934. float mins[QK_K/32];
  1935. float scales[QK_K/32];
  1936. for (int i = 0; i < nb; i++) {
  1937. float max_scale = 0; // as we are deducting the min, scales are always positive
  1938. float max_min = 0;
  1939. for (int j = 0; j < QK_K/32; ++j) {
  1940. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1941. float sum_x2 = 0;
  1942. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1943. float av_x = sqrtf(sum_x2/32);
  1944. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1945. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1946. float scale = scales[j];
  1947. if (scale > max_scale) {
  1948. max_scale = scale;
  1949. }
  1950. float min = mins[j];
  1951. if (min > max_min) {
  1952. max_min = min;
  1953. }
  1954. }
  1955. #if QK_K == 256
  1956. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1957. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1958. for (int j = 0; j < QK_K/32; ++j) {
  1959. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1960. uint8_t lm = nearest_int(inv_min*mins[j]);
  1961. ls = MIN(63, ls);
  1962. lm = MIN(63, lm);
  1963. if (j < 4) {
  1964. y[i].scales[j] = ls;
  1965. y[i].scales[j+4] = lm;
  1966. } else {
  1967. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1968. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1969. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1970. }
  1971. }
  1972. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1973. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1974. uint8_t sc, m;
  1975. for (int j = 0; j < QK_K/32; ++j) {
  1976. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1977. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1978. if (!d) continue;
  1979. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1980. for (int ii = 0; ii < 32; ++ii) {
  1981. int l = nearest_int((x[32*j + ii] + dm)/d);
  1982. l = MAX(0, MIN(15, l));
  1983. L[32*j + ii] = l;
  1984. }
  1985. }
  1986. #else
  1987. const float s_factor = 15.f;
  1988. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1989. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1990. int d1 = nearest_int(inv_scale*scales[0]);
  1991. int m1 = nearest_int(inv_min*mins[0]);
  1992. int d2 = nearest_int(inv_scale*scales[1]);
  1993. int m2 = nearest_int(inv_min*mins[1]);
  1994. y[i].scales[0] = d1 | (m1 << 4);
  1995. y[i].scales[1] = d2 | (m2 << 4);
  1996. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1997. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1998. float sumlx = 0;
  1999. int suml2 = 0;
  2000. for (int j = 0; j < QK_K/32; ++j) {
  2001. const uint8_t sd = y[i].scales[j] & 0xF;
  2002. const uint8_t sm = y[i].scales[j] >> 4;
  2003. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  2004. if (!d) continue;
  2005. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  2006. for (int ii = 0; ii < 32; ++ii) {
  2007. int l = nearest_int((x[32*j + ii] + m)/d);
  2008. l = MAX(0, MIN(15, l));
  2009. L[32*j + ii] = l;
  2010. sumlx += (x[32*j + ii] + m)*l*sd;
  2011. suml2 += l*l*sd*sd;
  2012. }
  2013. }
  2014. if (suml2) {
  2015. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  2016. }
  2017. #endif
  2018. uint8_t * q = y[i].qs;
  2019. for (int j = 0; j < QK_K; j += 64) {
  2020. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2021. q += 32;
  2022. }
  2023. x += QK_K;
  2024. }
  2025. }
  2026. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  2027. assert(k % QK_K == 0);
  2028. const int nb = k / QK_K;
  2029. for (int i = 0; i < nb; i++) {
  2030. const uint8_t * q = x[i].qs;
  2031. #if QK_K == 256
  2032. const float d = GGML_FP16_TO_FP32(x[i].d);
  2033. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2034. int is = 0;
  2035. uint8_t sc, m;
  2036. for (int j = 0; j < QK_K; j += 64) {
  2037. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2038. const float d1 = d * sc; const float m1 = min * m;
  2039. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2040. const float d2 = d * sc; const float m2 = min * m;
  2041. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  2042. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  2043. q += 32; is += 2;
  2044. }
  2045. #else
  2046. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  2047. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  2048. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  2049. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  2050. for (int l = 0; l < 32; ++l) {
  2051. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  2052. y[l+32] = d2 * (q[l] >> 4) - m2;
  2053. }
  2054. y += QK_K;
  2055. #endif
  2056. }
  2057. }
  2058. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  2059. assert(k % QK_K == 0);
  2060. block_q4_K * restrict y = vy;
  2061. quantize_row_q4_K_reference(x, y, k);
  2062. }
  2063. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2064. #if QK_K != 256
  2065. (void)quant_weights;
  2066. quantize_row_q4_K_reference(x, y, n_per_row);
  2067. #else
  2068. assert(n_per_row % QK_K == 0);
  2069. const int nb = n_per_row / QK_K;
  2070. uint8_t L[QK_K];
  2071. uint8_t Laux[32];
  2072. uint8_t Ls[QK_K/32];
  2073. uint8_t Lm[QK_K/32];
  2074. float weights[32];
  2075. float sw[QK_K/32];
  2076. float mins[QK_K/32];
  2077. float scales[QK_K/32];
  2078. for (int i = 0; i < nb; i++) {
  2079. float sum_x2 = 0;
  2080. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2081. float sigma2 = 2*sum_x2/QK_K;
  2082. float av_x = sqrtf(sigma2);
  2083. for (int j = 0; j < QK_K/32; ++j) {
  2084. if (quant_weights) {
  2085. const float * qw = quant_weights + QK_K*i + 32*j;
  2086. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2087. } else {
  2088. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2089. }
  2090. float sumw = 0;
  2091. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2092. sw[j] = sumw;
  2093. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2094. }
  2095. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2096. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2097. for (int j = 0; j < QK_K/32; ++j) {
  2098. uint8_t ls = Ls[j];
  2099. uint8_t lm = Lm[j];
  2100. if (j < 4) {
  2101. y[i].scales[j] = ls;
  2102. y[i].scales[j+4] = lm;
  2103. } else {
  2104. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2105. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2106. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2107. }
  2108. }
  2109. y[i].d = GGML_FP32_TO_FP16(d_block);
  2110. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2111. uint8_t sc, m;
  2112. for (int j = 0; j < QK_K/32; ++j) {
  2113. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2114. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2115. if (!d) continue;
  2116. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2117. for (int ii = 0; ii < 32; ++ii) {
  2118. int l = nearest_int((x[32*j + ii] + dm)/d);
  2119. l = MAX(0, MIN(15, l));
  2120. L[32*j + ii] = l;
  2121. }
  2122. }
  2123. uint8_t * q = y[i].qs;
  2124. for (int j = 0; j < QK_K; j += 64) {
  2125. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2126. q += 32;
  2127. }
  2128. x += QK_K;
  2129. }
  2130. #endif
  2131. }
  2132. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2133. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2134. if (!quant_weights) {
  2135. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2136. }
  2137. else {
  2138. char * qrow = (char *)dst;
  2139. for (int row = 0; row < nrow; ++row) {
  2140. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2141. src += n_per_row;
  2142. qrow += row_size;
  2143. }
  2144. }
  2145. return nrow * row_size;
  2146. }
  2147. // ====================== 5-bit (de)-quantization
  2148. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2149. assert(k % QK_K == 0);
  2150. const int nb = k / QK_K;
  2151. #if QK_K == 256
  2152. uint8_t L[QK_K];
  2153. float mins[QK_K/32];
  2154. float scales[QK_K/32];
  2155. float weights[32];
  2156. uint8_t Laux[32];
  2157. #else
  2158. int8_t L[QK_K];
  2159. float scales[QK_K/16];
  2160. #endif
  2161. for (int i = 0; i < nb; i++) {
  2162. #if QK_K == 256
  2163. float max_scale = 0; // as we are deducting the min, scales are always positive
  2164. float max_min = 0;
  2165. for (int j = 0; j < QK_K/32; ++j) {
  2166. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2167. float sum_x2 = 0;
  2168. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2169. float av_x = sqrtf(sum_x2/32);
  2170. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2171. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2172. float scale = scales[j];
  2173. if (scale > max_scale) {
  2174. max_scale = scale;
  2175. }
  2176. float min = mins[j];
  2177. if (min > max_min) {
  2178. max_min = min;
  2179. }
  2180. }
  2181. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2182. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2183. for (int j = 0; j < QK_K/32; ++j) {
  2184. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2185. uint8_t lm = nearest_int(inv_min*mins[j]);
  2186. ls = MIN(63, ls);
  2187. lm = MIN(63, lm);
  2188. if (j < 4) {
  2189. y[i].scales[j] = ls;
  2190. y[i].scales[j+4] = lm;
  2191. } else {
  2192. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2193. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2194. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2195. }
  2196. }
  2197. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2198. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2199. uint8_t sc, m;
  2200. for (int j = 0; j < QK_K/32; ++j) {
  2201. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2202. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2203. if (!d) continue;
  2204. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2205. for (int ii = 0; ii < 32; ++ii) {
  2206. int l = nearest_int((x[32*j + ii] + dm)/d);
  2207. l = MAX(0, MIN(31, l));
  2208. L[32*j + ii] = l;
  2209. }
  2210. }
  2211. uint8_t * restrict qh = y[i].qh;
  2212. uint8_t * restrict ql = y[i].qs;
  2213. memset(qh, 0, QK_K/8);
  2214. uint8_t m1 = 1, m2 = 2;
  2215. for (int n = 0; n < QK_K; n += 64) {
  2216. for (int j = 0; j < 32; ++j) {
  2217. int l1 = L[n + j];
  2218. if (l1 > 15) {
  2219. l1 -= 16; qh[j] |= m1;
  2220. }
  2221. int l2 = L[n + j + 32];
  2222. if (l2 > 15) {
  2223. l2 -= 16; qh[j] |= m2;
  2224. }
  2225. ql[j] = l1 | (l2 << 4);
  2226. }
  2227. m1 <<= 2; m2 <<= 2;
  2228. ql += 32;
  2229. }
  2230. #else
  2231. float max_scale = 0, amax = 0;
  2232. for (int j = 0; j < QK_K/16; ++j) {
  2233. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2234. float abs_scale = fabsf(scales[j]);
  2235. if (abs_scale > amax) {
  2236. amax = abs_scale;
  2237. max_scale = scales[j];
  2238. }
  2239. }
  2240. float iscale = -128.f/max_scale;
  2241. for (int j = 0; j < QK_K/16; ++j) {
  2242. int l = nearest_int(iscale*scales[j]);
  2243. y[i].scales[j] = MAX(-128, MIN(127, l));
  2244. }
  2245. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2246. for (int j = 0; j < QK_K/16; ++j) {
  2247. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2248. if (!d) continue;
  2249. for (int ii = 0; ii < 16; ++ii) {
  2250. int l = nearest_int(x[16*j + ii]/d);
  2251. l = MAX(-16, MIN(15, l));
  2252. L[16*j + ii] = l + 16;
  2253. }
  2254. }
  2255. uint8_t * restrict qh = y[i].qh;
  2256. uint8_t * restrict ql = y[i].qs;
  2257. memset(qh, 0, QK_K/8);
  2258. for (int j = 0; j < 32; ++j) {
  2259. int jm = j%8;
  2260. int is = j/8;
  2261. int l1 = L[j];
  2262. if (l1 > 15) {
  2263. l1 -= 16; qh[jm] |= (1 << is);
  2264. }
  2265. int l2 = L[j + 32];
  2266. if (l2 > 15) {
  2267. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2268. }
  2269. ql[j] = l1 | (l2 << 4);
  2270. }
  2271. #endif
  2272. x += QK_K;
  2273. }
  2274. }
  2275. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2276. assert(k % QK_K == 0);
  2277. const int nb = k / QK_K;
  2278. for (int i = 0; i < nb; i++) {
  2279. const uint8_t * ql = x[i].qs;
  2280. const uint8_t * qh = x[i].qh;
  2281. #if QK_K == 256
  2282. const float d = GGML_FP16_TO_FP32(x[i].d);
  2283. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2284. int is = 0;
  2285. uint8_t sc, m;
  2286. uint8_t u1 = 1, u2 = 2;
  2287. for (int j = 0; j < QK_K; j += 64) {
  2288. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2289. const float d1 = d * sc; const float m1 = min * m;
  2290. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2291. const float d2 = d * sc; const float m2 = min * m;
  2292. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2293. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2294. ql += 32; is += 2;
  2295. u1 <<= 2; u2 <<= 2;
  2296. }
  2297. #else
  2298. float d = GGML_FP16_TO_FP32(x[i].d);
  2299. const int8_t * restrict s = x[i].scales;
  2300. for (int l = 0; l < 8; ++l) {
  2301. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2302. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2303. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2304. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2305. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2306. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2307. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2308. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2309. }
  2310. y += QK_K;
  2311. #endif
  2312. }
  2313. }
  2314. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2315. assert(k % QK_K == 0);
  2316. block_q5_K * restrict y = vy;
  2317. quantize_row_q5_K_reference(x, y, k);
  2318. }
  2319. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2320. #if QK_K != 256
  2321. (void)quant_weights;
  2322. quantize_row_q5_K_reference(x, y, n_per_row);
  2323. #else
  2324. assert(n_per_row % QK_K == 0);
  2325. const int nb = n_per_row / QK_K;
  2326. uint8_t L[QK_K];
  2327. uint8_t Laux[32];
  2328. uint8_t Ls[QK_K/32];
  2329. uint8_t Lm[QK_K/32];
  2330. float mins[QK_K/32];
  2331. float scales[QK_K/32];
  2332. float sw[QK_K/32];
  2333. float weights[32];
  2334. for (int i = 0; i < nb; i++) {
  2335. float sum_x2 = 0;
  2336. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2337. float sigma2 = 2*sum_x2/QK_K;
  2338. float av_x = sqrtf(sigma2);
  2339. for (int j = 0; j < QK_K/32; ++j) {
  2340. if (quant_weights) {
  2341. const float * qw = quant_weights + QK_K*i + 32*j;
  2342. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2343. } else {
  2344. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2345. }
  2346. float sumw = 0;
  2347. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2348. sw[j] = sumw;
  2349. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2350. }
  2351. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2352. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2353. for (int j = 0; j < QK_K/32; ++j) {
  2354. uint8_t ls = Ls[j];
  2355. uint8_t lm = Lm[j];
  2356. ls = MIN(63, ls);
  2357. lm = MIN(63, lm);
  2358. if (j < 4) {
  2359. y[i].scales[j] = ls;
  2360. y[i].scales[j+4] = lm;
  2361. } else {
  2362. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2363. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2364. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2365. }
  2366. }
  2367. y[i].d = GGML_FP32_TO_FP16(d_block);
  2368. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2369. uint8_t sc, m;
  2370. for (int j = 0; j < QK_K/32; ++j) {
  2371. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2372. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2373. if (!d) continue;
  2374. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2375. for (int ii = 0; ii < 32; ++ii) {
  2376. int l = nearest_int((x[32*j + ii] + dm)/d);
  2377. l = MAX(0, MIN(31, l));
  2378. L[32*j + ii] = l;
  2379. }
  2380. }
  2381. uint8_t * restrict qh = y[i].qh;
  2382. uint8_t * restrict ql = y[i].qs;
  2383. memset(qh, 0, QK_K/8);
  2384. uint8_t m1 = 1, m2 = 2;
  2385. for (int n = 0; n < QK_K; n += 64) {
  2386. for (int j = 0; j < 32; ++j) {
  2387. int l1 = L[n + j];
  2388. if (l1 > 15) {
  2389. l1 -= 16; qh[j] |= m1;
  2390. }
  2391. int l2 = L[n + j + 32];
  2392. if (l2 > 15) {
  2393. l2 -= 16; qh[j] |= m2;
  2394. }
  2395. ql[j] = l1 | (l2 << 4);
  2396. }
  2397. m1 <<= 2; m2 <<= 2;
  2398. ql += 32;
  2399. }
  2400. x += QK_K;
  2401. }
  2402. #endif
  2403. }
  2404. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2405. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2406. if (!quant_weights) {
  2407. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2408. }
  2409. else {
  2410. char * qrow = (char *)dst;
  2411. for (int row = 0; row < nrow; ++row) {
  2412. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2413. src += n_per_row;
  2414. qrow += row_size;
  2415. }
  2416. }
  2417. return nrow * row_size;
  2418. }
  2419. // ====================== 6-bit (de)-quantization
  2420. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2421. assert(k % QK_K == 0);
  2422. const int nb = k / QK_K;
  2423. int8_t L[QK_K];
  2424. float scales[QK_K/16];
  2425. for (int i = 0; i < nb; i++) {
  2426. float max_scale = 0;
  2427. float max_abs_scale = 0;
  2428. for (int ib = 0; ib < QK_K/16; ++ib) {
  2429. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2430. scales[ib] = scale;
  2431. const float abs_scale = fabsf(scale);
  2432. if (abs_scale > max_abs_scale) {
  2433. max_abs_scale = abs_scale;
  2434. max_scale = scale;
  2435. }
  2436. }
  2437. if (!max_abs_scale) {
  2438. memset(&y[i], 0, sizeof(block_q6_K));
  2439. y[i].d = GGML_FP32_TO_FP16(0.f);
  2440. x += QK_K;
  2441. continue;
  2442. }
  2443. float iscale = -128.f/max_scale;
  2444. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2445. for (int ib = 0; ib < QK_K/16; ++ib) {
  2446. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2447. }
  2448. for (int j = 0; j < QK_K/16; ++j) {
  2449. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2450. if (!d) {
  2451. continue;
  2452. }
  2453. for (int ii = 0; ii < 16; ++ii) {
  2454. int l = nearest_int(x[16*j + ii]/d);
  2455. l = MAX(-32, MIN(31, l));
  2456. L[16*j + ii] = l + 32;
  2457. }
  2458. }
  2459. uint8_t * restrict ql = y[i].ql;
  2460. uint8_t * restrict qh = y[i].qh;
  2461. #if QK_K == 256
  2462. for (int j = 0; j < QK_K; j += 128) {
  2463. for (int l = 0; l < 32; ++l) {
  2464. const uint8_t q1 = L[j + l + 0] & 0xF;
  2465. const uint8_t q2 = L[j + l + 32] & 0xF;
  2466. const uint8_t q3 = L[j + l + 64] & 0xF;
  2467. const uint8_t q4 = L[j + l + 96] & 0xF;
  2468. ql[l+ 0] = q1 | (q3 << 4);
  2469. ql[l+32] = q2 | (q4 << 4);
  2470. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2471. }
  2472. ql += 64;
  2473. qh += 32;
  2474. }
  2475. #else
  2476. for (int l = 0; l < 32; ++l) {
  2477. const uint8_t q1 = L[l + 0] & 0xF;
  2478. const uint8_t q2 = L[l + 32] & 0xF;
  2479. ql[l] = q1 | (q2 << 4);
  2480. }
  2481. for (int l = 0; l < 16; ++l) {
  2482. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2483. }
  2484. #endif
  2485. x += QK_K;
  2486. }
  2487. }
  2488. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2489. assert(k % QK_K == 0);
  2490. const int nb = k / QK_K;
  2491. for (int i = 0; i < nb; i++) {
  2492. const float d = GGML_FP16_TO_FP32(x[i].d);
  2493. const uint8_t * restrict ql = x[i].ql;
  2494. const uint8_t * restrict qh = x[i].qh;
  2495. const int8_t * restrict sc = x[i].scales;
  2496. #if QK_K == 256
  2497. for (int n = 0; n < QK_K; n += 128) {
  2498. for (int l = 0; l < 32; ++l) {
  2499. int is = l/16;
  2500. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2501. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2502. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2503. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2504. y[l + 0] = d * sc[is + 0] * q1;
  2505. y[l + 32] = d * sc[is + 2] * q2;
  2506. y[l + 64] = d * sc[is + 4] * q3;
  2507. y[l + 96] = d * sc[is + 6] * q4;
  2508. }
  2509. y += 128;
  2510. ql += 64;
  2511. qh += 32;
  2512. sc += 8;
  2513. }
  2514. #else
  2515. for (int l = 0; l < 16; ++l) {
  2516. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2517. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2518. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2519. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2520. y[l+ 0] = d * sc[0] * q1;
  2521. y[l+16] = d * sc[1] * q2;
  2522. y[l+32] = d * sc[2] * q3;
  2523. y[l+48] = d * sc[3] * q4;
  2524. }
  2525. y += 64;
  2526. #endif
  2527. }
  2528. }
  2529. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2530. assert(k % QK_K == 0);
  2531. block_q6_K * restrict y = vy;
  2532. quantize_row_q6_K_reference(x, y, k);
  2533. }
  2534. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2535. #if QK_K != 256
  2536. (void)quant_weights;
  2537. quantize_row_q6_K_reference(x, y, n_per_row);
  2538. #else
  2539. assert(n_per_row % QK_K == 0);
  2540. const int nb = n_per_row / QK_K;
  2541. int8_t L[QK_K];
  2542. float scales[QK_K/16];
  2543. //float weights[16];
  2544. for (int i = 0; i < nb; i++) {
  2545. //float sum_x2 = 0;
  2546. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2547. //float sigma2 = sum_x2/QK_K;
  2548. float max_scale = 0;
  2549. float max_abs_scale = 0;
  2550. for (int ib = 0; ib < QK_K/16; ++ib) {
  2551. float scale;
  2552. if (quant_weights) {
  2553. const float * qw = quant_weights + QK_K*i + 16*ib;
  2554. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2555. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2556. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2557. } else {
  2558. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2559. }
  2560. scales[ib] = scale;
  2561. const float abs_scale = fabsf(scale);
  2562. if (abs_scale > max_abs_scale) {
  2563. max_abs_scale = abs_scale;
  2564. max_scale = scale;
  2565. }
  2566. }
  2567. if (!max_abs_scale) {
  2568. memset(&y[i], 0, sizeof(block_q6_K));
  2569. y[i].d = GGML_FP32_TO_FP16(0.f);
  2570. x += QK_K;
  2571. continue;
  2572. }
  2573. float iscale = -128.f/max_scale;
  2574. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2575. for (int ib = 0; ib < QK_K/16; ++ib) {
  2576. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2577. }
  2578. for (int j = 0; j < QK_K/16; ++j) {
  2579. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2580. if (!d) {
  2581. continue;
  2582. }
  2583. for (int ii = 0; ii < 16; ++ii) {
  2584. int l = nearest_int(x[16*j + ii]/d);
  2585. l = MAX(-32, MIN(31, l));
  2586. L[16*j + ii] = l + 32;
  2587. }
  2588. }
  2589. uint8_t * restrict ql = y[i].ql;
  2590. uint8_t * restrict qh = y[i].qh;
  2591. for (int j = 0; j < QK_K; j += 128) {
  2592. for (int l = 0; l < 32; ++l) {
  2593. const uint8_t q1 = L[j + l + 0] & 0xF;
  2594. const uint8_t q2 = L[j + l + 32] & 0xF;
  2595. const uint8_t q3 = L[j + l + 64] & 0xF;
  2596. const uint8_t q4 = L[j + l + 96] & 0xF;
  2597. ql[l+ 0] = q1 | (q3 << 4);
  2598. ql[l+32] = q2 | (q4 << 4);
  2599. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2600. }
  2601. ql += 64;
  2602. qh += 32;
  2603. }
  2604. x += QK_K;
  2605. }
  2606. #endif
  2607. }
  2608. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2609. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2610. if (!quant_weights) {
  2611. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2612. }
  2613. else {
  2614. char * qrow = (char *)dst;
  2615. for (int row = 0; row < nrow; ++row) {
  2616. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2617. src += n_per_row;
  2618. qrow += row_size;
  2619. }
  2620. }
  2621. return nrow * row_size;
  2622. }
  2623. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2624. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2625. if (!quant_weights) {
  2626. quantize_row_q4_0_reference(x, y, n_per_row);
  2627. return;
  2628. }
  2629. float weight[QK4_0];
  2630. int8_t L[QK4_0];
  2631. float sum_x2 = 0;
  2632. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2633. float sigma2 = sum_x2/n_per_row;
  2634. const int nb = n_per_row/QK4_0;
  2635. for (int ib = 0; ib < nb; ++ib) {
  2636. const float * xb = x + QK4_0 * ib;
  2637. const float * qw = quant_weights + QK4_0 * ib;
  2638. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2639. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2640. y[ib].d = GGML_FP32_TO_FP16(d);
  2641. for (int j = 0; j < 16; ++j) {
  2642. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2643. }
  2644. }
  2645. }
  2646. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2647. if (!quant_weights) {
  2648. quantize_row_q4_0_reference(src, dst, nrow*n_per_row);
  2649. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2650. }
  2651. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2652. char * qrow = (char *)dst;
  2653. for (int row = 0; row < nrow; ++row) {
  2654. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2655. src += n_per_row;
  2656. qrow += row_size;
  2657. }
  2658. return nrow * row_size;
  2659. }
  2660. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2661. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2662. if (!quant_weights) {
  2663. quantize_row_q4_1_reference(x, y, n_per_row);
  2664. return;
  2665. }
  2666. float weight[QK4_1];
  2667. uint8_t L[QK4_1], Laux[QK4_1];
  2668. float sum_x2 = 0;
  2669. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2670. float sigma2 = sum_x2/n_per_row;
  2671. const int nb = n_per_row/QK4_1;
  2672. for (int ib = 0; ib < nb; ++ib) {
  2673. const float * xb = x + QK4_1 * ib;
  2674. const float * qw = quant_weights + QK4_1 * ib;
  2675. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2676. float min;
  2677. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2678. y[ib].d = GGML_FP32_TO_FP16(d);
  2679. y[ib].m = GGML_FP32_TO_FP16(-min);
  2680. for (int j = 0; j < 16; ++j) {
  2681. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2682. }
  2683. }
  2684. }
  2685. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2686. if (!quant_weights) {
  2687. quantize_row_q4_1_reference(src, dst, nrow*n_per_row);
  2688. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2689. }
  2690. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2691. char * qrow = (char *)dst;
  2692. for (int row = 0; row < nrow; ++row) {
  2693. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2694. src += n_per_row;
  2695. qrow += row_size;
  2696. }
  2697. return nrow * row_size;
  2698. }
  2699. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2700. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2701. if (!quant_weights) {
  2702. quantize_row_q5_0_reference(x, y, n_per_row);
  2703. return;
  2704. }
  2705. float weight[QK5_0];
  2706. int8_t L[QK5_0];
  2707. float sum_x2 = 0;
  2708. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2709. float sigma2 = sum_x2/n_per_row;
  2710. const int nb = n_per_row/QK5_0;
  2711. for (int ib = 0; ib < nb; ++ib) {
  2712. const float * xb = x + QK5_0 * ib;
  2713. const float * qw = quant_weights + QK5_0 * ib;
  2714. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2715. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2716. y[ib].d = GGML_FP32_TO_FP16(d);
  2717. uint32_t qh = 0;
  2718. for (int j = 0; j < 16; ++j) {
  2719. const uint8_t xi0 = L[j];
  2720. const uint8_t xi1 = L[j+16];
  2721. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2722. // get the 5-th bit and store it in qh at the right position
  2723. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2724. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2725. }
  2726. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2727. }
  2728. }
  2729. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2730. if (!quant_weights) {
  2731. quantize_row_q5_0_reference(src, dst, nrow*n_per_row);
  2732. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2733. }
  2734. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2735. char * qrow = (char *)dst;
  2736. for (int row = 0; row < nrow; ++row) {
  2737. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2738. src += n_per_row;
  2739. qrow += row_size;
  2740. }
  2741. return nrow * row_size;
  2742. }
  2743. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2744. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2745. if (!quant_weights) {
  2746. quantize_row_q5_1_reference(x, y, n_per_row);
  2747. return;
  2748. }
  2749. float weight[QK5_1];
  2750. uint8_t L[QK5_1], Laux[QK5_1];
  2751. float sum_x2 = 0;
  2752. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2753. float sigma2 = sum_x2/n_per_row;
  2754. const int nb = n_per_row/QK5_1;
  2755. for (int ib = 0; ib < nb; ++ib) {
  2756. const float * xb = x + QK5_1 * ib;
  2757. const float * qw = quant_weights + QK5_1 * ib;
  2758. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2759. float min;
  2760. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2761. y[ib].d = GGML_FP32_TO_FP16(d);
  2762. y[ib].m = GGML_FP32_TO_FP16(-min);
  2763. uint32_t qh = 0;
  2764. for (int j = 0; j < 16; ++j) {
  2765. const uint8_t xi0 = L[j];
  2766. const uint8_t xi1 = L[j+16];
  2767. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2768. // get the 5-th bit and store it in qh at the right position
  2769. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2770. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2771. }
  2772. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2773. }
  2774. }
  2775. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2776. if (!quant_weights) {
  2777. quantize_row_q5_1_reference(src, dst, nrow*n_per_row);
  2778. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2779. }
  2780. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2781. char * qrow = (char *)dst;
  2782. for (int row = 0; row < nrow; ++row) {
  2783. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2784. src += n_per_row;
  2785. qrow += row_size;
  2786. }
  2787. return nrow * row_size;
  2788. }
  2789. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2790. (void)quant_weights; // not used
  2791. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2792. quantize_row_q8_0_reference(src, dst, nrow*n_per_row);
  2793. return nrow * row_size;
  2794. }
  2795. // ====================== "True" 2-bit (de)-quantization
  2796. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2797. assert(k % QK_K == 0);
  2798. const int nb = k / QK_K;
  2799. uint32_t aux32[2];
  2800. const uint8_t * aux8 = (const uint8_t *)aux32;
  2801. for (int i = 0; i < nb; i++) {
  2802. const float d = GGML_FP16_TO_FP32(x[i].d);
  2803. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2804. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2805. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2806. for (int l = 0; l < 4; ++l) {
  2807. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2808. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2809. for (int j = 0; j < 8; ++j) {
  2810. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2811. }
  2812. y += 8;
  2813. }
  2814. }
  2815. }
  2816. }
  2817. // ====================== 2.3125 bpw (de)-quantization
  2818. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2819. assert(k % QK_K == 0);
  2820. const int nb = k / QK_K;
  2821. float db[2];
  2822. for (int i = 0; i < nb; i++) {
  2823. const float d = GGML_FP16_TO_FP32(x[i].d);
  2824. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2825. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2826. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2827. for (int l = 0; l < 4; ++l) {
  2828. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2829. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2830. for (int j = 0; j < 8; ++j) {
  2831. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2832. }
  2833. y += 8;
  2834. }
  2835. }
  2836. }
  2837. }
  2838. // ====================== 2.5625 bpw (de)-quantization
  2839. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int k) {
  2840. assert(k % QK_K == 0);
  2841. const int nb = k / QK_K;
  2842. float db[2];
  2843. for (int i = 0; i < nb; i++) {
  2844. const float d = GGML_FP16_TO_FP32(x[i].d);
  2845. const uint8_t * qs = x[i].qs;
  2846. const uint8_t * qh = x[i].qh;
  2847. const uint8_t * signs = qs + QK_K/8;
  2848. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2849. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2850. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2851. for (int l = 0; l < 4; ++l) {
  2852. const float dl = db[l/2];
  2853. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2854. for (int j = 0; j < 8; ++j) {
  2855. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2856. }
  2857. y += 8;
  2858. }
  2859. qs += 4;
  2860. signs += 4;
  2861. }
  2862. }
  2863. }
  2864. // ====================== 3.0625 bpw (de)-quantization
  2865. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  2866. assert(k % QK_K == 0);
  2867. const int nb = k / QK_K;
  2868. uint32_t aux32;
  2869. for (int i = 0; i < nb; i++) {
  2870. const float d = GGML_FP16_TO_FP32(x[i].d);
  2871. const uint8_t * qs = x[i].qs;
  2872. const uint8_t * scales_and_signs = qs + QK_K/4;
  2873. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2874. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2875. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2876. for (int l = 0; l < 4; ++l) {
  2877. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2878. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2879. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2880. for (int j = 0; j < 4; ++j) {
  2881. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2882. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2883. }
  2884. y += 8;
  2885. }
  2886. qs += 8;
  2887. }
  2888. }
  2889. }
  2890. // ====================== 3.3125 bpw (de)-quantization
  2891. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int k) {
  2892. assert(k % QK_K == 0);
  2893. const int nb = k / QK_K;
  2894. for (int i = 0; i < nb; i++) {
  2895. const float d = GGML_FP16_TO_FP32(x[i].d);
  2896. const uint8_t * qs = x[i].qs;
  2897. const uint8_t * qh = x[i].qh;
  2898. const uint8_t * signs = x[i].signs;
  2899. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2900. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2901. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2902. for (int l = 0; l < 4; ++l) {
  2903. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2904. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2905. for (int j = 0; j < 4; ++j) {
  2906. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2907. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2908. }
  2909. y += 8;
  2910. }
  2911. qs += 8;
  2912. signs += 4;
  2913. for (int l = 0; l < 4; ++l) {
  2914. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2915. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2916. for (int j = 0; j < 4; ++j) {
  2917. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2918. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2919. }
  2920. y += 8;
  2921. }
  2922. qh += 2;
  2923. qs += 8;
  2924. signs += 4;
  2925. }
  2926. }
  2927. }
  2928. // ====================== 1.5625 bpw (de)-quantization
  2929. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int k) {
  2930. assert(k % QK_K == 0);
  2931. const int nb = k / QK_K;
  2932. for (int i = 0; i < nb; i++) {
  2933. const float d = GGML_FP16_TO_FP32(x[i].d);
  2934. const uint8_t * qs = x[i].qs;
  2935. const uint16_t * qh = x[i].qh;
  2936. for (int ib = 0; ib < QK_K/32; ++ib) {
  2937. const float dl = d * (2*(qh[ib] >> 12) + 1);
  2938. for (int l = 0; l < 4; ++l) {
  2939. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2940. for (int j = 0; j < 8; ++j) {
  2941. y[j] = dl * grid[j];
  2942. }
  2943. y += 8;
  2944. }
  2945. qs += 4;
  2946. }
  2947. }
  2948. }
  2949. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2950. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int k) {
  2951. assert(k % QK4_NL == 0);
  2952. const int nb = k / QK4_NL;
  2953. for (int i = 0; i < nb; i++) {
  2954. const uint8_t * qs = x[i].qs;
  2955. const float d = GGML_FP16_TO_FP32(x[i].d);
  2956. for (int j = 0; j < QK4_NL/2; ++j) {
  2957. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2958. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2959. }
  2960. y += QK4_NL;
  2961. qs += QK4_NL/2;
  2962. }
  2963. }
  2964. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int k) {
  2965. assert(k % QK_K == 0);
  2966. #if QK_K == 64
  2967. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2968. #else
  2969. const int nb = k / QK_K;
  2970. for (int i = 0; i < nb; i++) {
  2971. const uint8_t * qs = x[i].qs;
  2972. const float d = GGML_FP16_TO_FP32(x[i].d);
  2973. for (int ib = 0; ib < QK_K/32; ++ib) {
  2974. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2975. const float dl = d * (ls - 32);
  2976. for (int j = 0; j < 16; ++j) {
  2977. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2978. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2979. }
  2980. y += 32;
  2981. qs += 16;
  2982. }
  2983. }
  2984. #endif
  2985. }
  2986. //===================================== Q8_K ==============================================
  2987. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  2988. assert(k % QK_K == 0);
  2989. const int nb = k / QK_K;
  2990. for (int i = 0; i < nb; i++) {
  2991. float max = 0;
  2992. float amax = 0;
  2993. for (int j = 0; j < QK_K; ++j) {
  2994. float ax = fabsf(x[j]);
  2995. if (ax > amax) {
  2996. amax = ax; max = x[j];
  2997. }
  2998. }
  2999. if (!amax) {
  3000. y[i].d = 0;
  3001. memset(y[i].qs, 0, QK_K);
  3002. x += QK_K;
  3003. continue;
  3004. }
  3005. //const float iscale = -128.f/max;
  3006. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3007. const float iscale = -127.f/max;
  3008. for (int j = 0; j < QK_K; ++j) {
  3009. int v = nearest_int(iscale*x[j]);
  3010. y[i].qs[j] = MIN(127, v);
  3011. }
  3012. for (int j = 0; j < QK_K/16; ++j) {
  3013. int sum = 0;
  3014. for (int ii = 0; ii < 16; ++ii) {
  3015. sum += y[i].qs[j*16 + ii];
  3016. }
  3017. y[i].bsums[j] = sum;
  3018. }
  3019. y[i].d = 1/iscale;
  3020. x += QK_K;
  3021. }
  3022. }
  3023. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3024. assert(k % QK_K == 0);
  3025. const int nb = k / QK_K;
  3026. for (int i = 0; i < nb; i++) {
  3027. for (int j = 0; j < QK_K; ++j) {
  3028. *y++ = x[i].d * x[i].qs[j];
  3029. }
  3030. }
  3031. }
  3032. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3033. quantize_row_q8_K_reference(x, y, k);
  3034. }
  3035. //===================================== Dot ptoducts =================================
  3036. //
  3037. // Helper functions
  3038. //
  3039. #if __AVX__ || __AVX2__ || __AVX512F__
  3040. // shuffles to pick the required scales in dot products
  3041. static inline __m256i get_scale_shuffle_q3k(int i) {
  3042. static const uint8_t k_shuffle[128] = {
  3043. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3044. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3045. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3046. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
  3047. };
  3048. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3049. }
  3050. static inline __m256i get_scale_shuffle_k4(int i) {
  3051. static const uint8_t k_shuffle[256] = {
  3052. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  3053. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3054. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
  3055. 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3056. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
  3057. 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3058. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
  3059. 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15
  3060. };
  3061. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3062. }
  3063. static inline __m128i get_scale_shuffle(int i) {
  3064. static const uint8_t k_shuffle[128] = {
  3065. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3066. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3067. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3068. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3069. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3070. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3071. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3072. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3073. };
  3074. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3075. }
  3076. #endif
  3077. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3078. const int qk = QK8_0;
  3079. const int nb = n / qk;
  3080. assert(n % qk == 0);
  3081. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3082. assert((nrc == 2) || (nrc == 1));
  3083. #else
  3084. assert(nrc == 1);
  3085. #endif
  3086. UNUSED(nrc);
  3087. UNUSED(bx);
  3088. UNUSED(by);
  3089. UNUSED(bs);
  3090. const block_q4_0 * restrict x = vx;
  3091. const block_q8_0 * restrict y = vy;
  3092. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3093. if (nrc == 2) {
  3094. const block_q4_0 * restrict vx0 = vx;
  3095. const block_q4_0 * restrict vx1 = vx + bx;
  3096. const block_q8_0 * restrict vy0 = vy;
  3097. const block_q8_0 * restrict vy1 = vy + by;
  3098. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3099. for (int i = 0; i < nb; i++) {
  3100. const block_q4_0 * restrict b_x0 = &vx0[i];
  3101. const block_q4_0 * restrict b_x1 = &vx1[i];
  3102. const block_q8_0 * restrict b_y0 = &vy0[i];
  3103. const block_q8_0 * restrict b_y1 = &vy1[i];
  3104. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3105. const int8x16_t s8b = vdupq_n_s8(0x8);
  3106. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3107. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3108. // 4-bit -> 8-bit
  3109. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3110. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3111. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3112. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3113. // sub 8
  3114. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  3115. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  3116. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  3117. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  3118. // load y
  3119. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3120. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3121. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3122. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3123. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3124. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3125. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3126. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3127. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3128. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3129. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3130. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3131. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3132. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3133. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3134. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3135. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3136. l1, r1)), l2, r2)), l3, r3))), scale);
  3137. }
  3138. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3139. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3140. vst1_f32(s, vget_low_f32(sumv2));
  3141. vst1_f32(s + bs, vget_high_f32(sumv2));
  3142. return;
  3143. }
  3144. #endif
  3145. #if defined(__ARM_NEON)
  3146. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3147. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3148. assert(nb % 2 == 0); // TODO: handle odd nb
  3149. for (int i = 0; i < nb; i += 2) {
  3150. const block_q4_0 * restrict x0 = &x[i + 0];
  3151. const block_q4_0 * restrict x1 = &x[i + 1];
  3152. const block_q8_0 * restrict y0 = &y[i + 0];
  3153. const block_q8_0 * restrict y1 = &y[i + 1];
  3154. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3155. const int8x16_t s8b = vdupq_n_s8(0x8);
  3156. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3157. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3158. // 4-bit -> 8-bit
  3159. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3160. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3161. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3162. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3163. // sub 8
  3164. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3165. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3166. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3167. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3168. // load y
  3169. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3170. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3171. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3172. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3173. // dot product into int32x4_t
  3174. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3175. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3176. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3177. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3178. }
  3179. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3180. #elif defined(__AVX2__)
  3181. // Initialize accumulator with zeros
  3182. __m256 acc = _mm256_setzero_ps();
  3183. // Main loop
  3184. for (int i = 0; i < nb; ++i) {
  3185. /* Compute combined scale for the block */
  3186. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3187. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3188. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3189. const __m256i off = _mm256_set1_epi8( 8 );
  3190. qx = _mm256_sub_epi8( qx, off );
  3191. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3192. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3193. /* Multiply q with scale and accumulate */
  3194. acc = _mm256_fmadd_ps( d, q, acc );
  3195. }
  3196. *s = hsum_float_8(acc);
  3197. #elif defined(__AVX__)
  3198. // Initialize accumulator with zeros
  3199. __m256 acc = _mm256_setzero_ps();
  3200. // Main loop
  3201. for (int i = 0; i < nb; ++i) {
  3202. // Compute combined scale for the block
  3203. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3204. const __m128i lowMask = _mm_set1_epi8(0xF);
  3205. const __m128i off = _mm_set1_epi8(8);
  3206. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3207. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3208. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3209. bx_0 = _mm_sub_epi8(bx_0, off);
  3210. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3211. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3212. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3213. bx_0 = _mm_sub_epi8(bx_0, off);
  3214. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3215. // Convert int32_t to float
  3216. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3217. // Apply the scale, and accumulate
  3218. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3219. }
  3220. *s = hsum_float_8(acc);
  3221. #elif defined(__SSSE3__)
  3222. // set constants
  3223. const __m128i lowMask = _mm_set1_epi8(0xF);
  3224. const __m128i off = _mm_set1_epi8(8);
  3225. // Initialize accumulator with zeros
  3226. __m128 acc_0 = _mm_setzero_ps();
  3227. __m128 acc_1 = _mm_setzero_ps();
  3228. __m128 acc_2 = _mm_setzero_ps();
  3229. __m128 acc_3 = _mm_setzero_ps();
  3230. // First round without accumulation
  3231. {
  3232. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3233. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3234. // Compute combined scale for the block 0 and 1
  3235. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3236. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3237. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3238. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3239. bx_0 = _mm_sub_epi8(bx_0, off);
  3240. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3241. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3242. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3243. bx_1 = _mm_sub_epi8(bx_1, off);
  3244. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3245. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3246. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3247. // Compute combined scale for the block 2 and 3
  3248. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3249. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3250. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3251. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3252. bx_2 = _mm_sub_epi8(bx_2, off);
  3253. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3254. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3255. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3256. bx_3 = _mm_sub_epi8(bx_3, off);
  3257. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3258. // Convert int32_t to float
  3259. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3260. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3261. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3262. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3263. // Apply the scale
  3264. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3265. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3266. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3267. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3268. }
  3269. assert(nb % 2 == 0); // TODO: handle odd nb
  3270. // Main loop
  3271. for (int i = 2; i < nb; i+=2) {
  3272. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3273. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3274. // Compute combined scale for the block 0 and 1
  3275. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3276. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3277. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3278. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3279. bx_0 = _mm_sub_epi8(bx_0, off);
  3280. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3281. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3282. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3283. bx_1 = _mm_sub_epi8(bx_1, off);
  3284. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3285. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3286. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3287. // Compute combined scale for the block 2 and 3
  3288. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3289. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3290. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3291. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3292. bx_2 = _mm_sub_epi8(bx_2, off);
  3293. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3294. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3295. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3296. bx_3 = _mm_sub_epi8(bx_3, off);
  3297. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3298. // Convert int32_t to float
  3299. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3300. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3301. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3302. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3303. // Apply the scale
  3304. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3305. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3306. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3307. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3308. // Acummulate
  3309. acc_0 = _mm_add_ps(p0_d, acc_0);
  3310. acc_1 = _mm_add_ps(p1_d, acc_1);
  3311. acc_2 = _mm_add_ps(p2_d, acc_2);
  3312. acc_3 = _mm_add_ps(p3_d, acc_3);
  3313. }
  3314. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3315. #elif defined(__riscv_v_intrinsic)
  3316. float sumf = 0.0;
  3317. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3318. for (int i = 0; i < nb; i++) {
  3319. // load elements
  3320. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3321. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3322. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3323. // mask and store lower part of x, and then upper part
  3324. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3325. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3326. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3327. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3328. // subtract offset
  3329. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3330. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3331. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3332. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3333. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3334. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3335. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3336. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3337. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3338. }
  3339. *s = sumf;
  3340. #else
  3341. // scalar
  3342. float sumf = 0.0;
  3343. for (int i = 0; i < nb; i++) {
  3344. int sumi = 0;
  3345. for (int j = 0; j < qk/2; ++j) {
  3346. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3347. const int v1 = (x[i].qs[j] >> 4) - 8;
  3348. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3349. }
  3350. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3351. }
  3352. *s = sumf;
  3353. #endif
  3354. }
  3355. void ggml_vec_dot_q4_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3356. const int qk = QK8_1;
  3357. const int nb = n / qk;
  3358. assert(n % qk == 0);
  3359. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3360. assert((nrc == 2) || (nrc == 1));
  3361. #else
  3362. assert(nrc == 1);
  3363. #endif
  3364. UNUSED(nrc);
  3365. UNUSED(bx);
  3366. UNUSED(by);
  3367. UNUSED(bs);
  3368. const block_q4_1 * restrict x = vx;
  3369. const block_q8_1 * restrict y = vy;
  3370. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3371. if (nrc == 2) {
  3372. const block_q4_1 * restrict vx0 = vx;
  3373. const block_q4_1 * restrict vx1 = vx + bx;
  3374. const block_q8_1 * restrict vy0 = vy;
  3375. const block_q8_1 * restrict vy1 = vy + by;
  3376. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3377. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3378. for (int i = 0; i < nb; i++) {
  3379. const block_q4_1 * restrict b_x0 = &vx0[i];
  3380. const block_q4_1 * restrict b_x1 = &vx1[i];
  3381. const block_q8_1 * restrict b_y0 = &vy0[i];
  3382. const block_q8_1 * restrict b_y1 = &vy1[i];
  3383. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * b_y0->s,
  3384. GGML_FP16_TO_FP32(b_x1->m) * b_y0->s,
  3385. GGML_FP16_TO_FP32(b_x0->m) * b_y1->s,
  3386. GGML_FP16_TO_FP32(b_x1->m) * b_y1->s};
  3387. summs0 += summs_t;
  3388. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3389. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3390. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3391. // 4-bit -> 8-bit
  3392. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3393. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3394. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3395. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3396. // load y
  3397. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3398. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3399. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3400. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3401. // mmla into int32x4_t
  3402. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3403. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3404. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3405. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3406. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3407. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3408. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3409. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3410. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3411. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3412. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3413. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3414. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3415. l1, r1)), l2, r2)), l3, r3))), scale);
  3416. }
  3417. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3418. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3419. sumv2 = sumv2 + summs0;
  3420. vst1_f32(s, vget_low_f32(sumv2));
  3421. vst1_f32(s + bs, vget_high_f32(sumv2));
  3422. return;
  3423. }
  3424. #endif
  3425. // TODO: add WASM SIMD
  3426. #if defined(__ARM_NEON)
  3427. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3428. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3429. float summs = 0;
  3430. assert(nb % 2 == 0); // TODO: handle odd nb
  3431. for (int i = 0; i < nb; i += 2) {
  3432. const block_q4_1 * restrict x0 = &x[i + 0];
  3433. const block_q4_1 * restrict x1 = &x[i + 1];
  3434. const block_q8_1 * restrict y0 = &y[i + 0];
  3435. const block_q8_1 * restrict y1 = &y[i + 1];
  3436. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3437. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3438. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3439. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3440. // 4-bit -> 8-bit
  3441. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3442. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3443. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3444. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3445. // load y
  3446. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3447. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3448. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3449. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3450. // dot product into int32x4_t
  3451. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3452. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3453. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3454. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3455. }
  3456. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3457. #elif defined(__AVX2__) || defined(__AVX__)
  3458. // Initialize accumulator with zeros
  3459. __m256 acc = _mm256_setzero_ps();
  3460. float summs = 0;
  3461. // Main loop
  3462. for (int i = 0; i < nb; ++i) {
  3463. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3464. const float d1 = y[i].d;
  3465. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3466. const __m256 d0v = _mm256_set1_ps( d0 );
  3467. const __m256 d1v = _mm256_set1_ps( d1 );
  3468. // Compute combined scales
  3469. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3470. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3471. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3472. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3473. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3474. // Accumulate d0*d1*x*y
  3475. #if defined(__AVX2__)
  3476. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3477. #else
  3478. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3479. #endif
  3480. }
  3481. *s = hsum_float_8(acc) + summs;
  3482. #elif defined(__riscv_v_intrinsic)
  3483. float sumf = 0.0;
  3484. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3485. for (int i = 0; i < nb; i++) {
  3486. // load elements
  3487. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3488. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3489. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3490. // mask and store lower part of x, and then upper part
  3491. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3492. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3493. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3494. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3495. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3496. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3497. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3498. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3499. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3500. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3501. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3502. }
  3503. *s = sumf;
  3504. #else
  3505. // scalar
  3506. float sumf = 0.0;
  3507. for (int i = 0; i < nb; i++) {
  3508. int sumi = 0;
  3509. for (int j = 0; j < qk/2; ++j) {
  3510. const int v0 = (x[i].qs[j] & 0x0F);
  3511. const int v1 = (x[i].qs[j] >> 4);
  3512. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3513. }
  3514. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3515. }
  3516. *s = sumf;
  3517. #endif
  3518. }
  3519. void ggml_vec_dot_q5_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3520. const int qk = QK8_0;
  3521. const int nb = n / qk;
  3522. assert(n % qk == 0);
  3523. assert(qk == QK5_0);
  3524. assert(nrc == 1);
  3525. UNUSED(nrc);
  3526. UNUSED(bx);
  3527. UNUSED(by);
  3528. UNUSED(bs);
  3529. const block_q5_0 * restrict x = vx;
  3530. const block_q8_0 * restrict y = vy;
  3531. #if defined(__ARM_NEON)
  3532. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3533. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3534. uint32_t qh0;
  3535. uint32_t qh1;
  3536. uint64_t tmp0[4];
  3537. uint64_t tmp1[4];
  3538. assert(nb % 2 == 0); // TODO: handle odd nb
  3539. for (int i = 0; i < nb; i += 2) {
  3540. const block_q5_0 * restrict x0 = &x[i];
  3541. const block_q5_0 * restrict x1 = &x[i + 1];
  3542. const block_q8_0 * restrict y0 = &y[i];
  3543. const block_q8_0 * restrict y1 = &y[i + 1];
  3544. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3545. // extract the 5th bit via lookup table ((!b) << 4)
  3546. memcpy(&qh0, x0->qh, sizeof(qh0));
  3547. memcpy(&qh1, x1->qh, sizeof(qh1));
  3548. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3549. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3550. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3551. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3552. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3553. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3554. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3555. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3556. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3557. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3558. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3559. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3560. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3561. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3562. // 4-bit -> 8-bit
  3563. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3564. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3565. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3566. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3567. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3568. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3569. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3570. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3571. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3572. // load y
  3573. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3574. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3575. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3576. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3577. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3578. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3579. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3580. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3581. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3582. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3583. }
  3584. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3585. #elif defined(__wasm_simd128__)
  3586. v128_t sumv = wasm_f32x4_splat(0.0f);
  3587. uint32_t qh;
  3588. uint64_t tmp[4];
  3589. // TODO: check if unrolling this is better
  3590. for (int i = 0; i < nb; ++i) {
  3591. const block_q5_0 * restrict x0 = &x[i];
  3592. const block_q8_0 * restrict y0 = &y[i];
  3593. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3594. // extract the 5th bit
  3595. memcpy(&qh, x0->qh, sizeof(qh));
  3596. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3597. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3598. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3599. tmp[3] = table_b2b_1[(qh >> 24) ];
  3600. const v128_t qhl = wasm_v128_load(tmp + 0);
  3601. const v128_t qhh = wasm_v128_load(tmp + 2);
  3602. const v128_t v0 = wasm_v128_load(x0->qs);
  3603. // 4-bit -> 8-bit
  3604. const v128_t v0l = wasm_v128_and (v0, m4b);
  3605. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3606. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3607. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3608. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3609. // load y
  3610. const v128_t v1l = wasm_v128_load(y0->qs);
  3611. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3612. // int8x16 -> int16x8
  3613. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3614. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3615. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3616. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3617. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3618. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3619. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3620. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3621. // dot product
  3622. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3623. wasm_i32x4_add(
  3624. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3625. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3626. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3627. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3628. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3629. }
  3630. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3631. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3632. #elif defined(__AVX2__)
  3633. // Initialize accumulator with zeros
  3634. __m256 acc = _mm256_setzero_ps();
  3635. // Main loop
  3636. for (int i = 0; i < nb; i++) {
  3637. /* Compute combined scale for the block */
  3638. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3639. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3640. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3641. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3642. qx = _mm256_or_si256(qx, bxhi);
  3643. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3644. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3645. /* Multiply q with scale and accumulate */
  3646. acc = _mm256_fmadd_ps(d, q, acc);
  3647. }
  3648. *s = hsum_float_8(acc);
  3649. #elif defined(__AVX__)
  3650. // Initialize accumulator with zeros
  3651. __m256 acc = _mm256_setzero_ps();
  3652. __m128i mask = _mm_set1_epi8((char)0xF0);
  3653. // Main loop
  3654. for (int i = 0; i < nb; i++) {
  3655. /* Compute combined scale for the block */
  3656. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3657. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3658. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3659. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3660. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3661. bxhil = _mm_andnot_si128(bxhil, mask);
  3662. bxhih = _mm_andnot_si128(bxhih, mask);
  3663. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3664. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3665. bxl = _mm_or_si128(bxl, bxhil);
  3666. bxh = _mm_or_si128(bxh, bxhih);
  3667. bx_0 = MM256_SET_M128I(bxh, bxl);
  3668. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3669. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3670. /* Multiply q with scale and accumulate */
  3671. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3672. }
  3673. *s = hsum_float_8(acc);
  3674. #elif defined(__riscv_v_intrinsic)
  3675. float sumf = 0.0;
  3676. uint32_t qh;
  3677. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3678. // These temporary registers are for masking and shift operations
  3679. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3680. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3681. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3682. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3683. for (int i = 0; i < nb; i++) {
  3684. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3685. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3686. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3687. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3688. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3689. // ((qh & (1u << (j + 16))) >> (j + 12));
  3690. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3691. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3692. // narrowing
  3693. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3694. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3695. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3696. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3697. // load
  3698. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3699. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3700. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3701. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3702. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3703. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3704. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3705. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3706. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3707. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3708. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3709. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3710. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3711. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3712. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3713. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3714. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3715. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3716. }
  3717. *s = sumf;
  3718. #else
  3719. // scalar
  3720. float sumf = 0.0;
  3721. for (int i = 0; i < nb; i++) {
  3722. uint32_t qh;
  3723. memcpy(&qh, x[i].qh, sizeof(qh));
  3724. int sumi = 0;
  3725. for (int j = 0; j < qk/2; ++j) {
  3726. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3727. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3728. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3729. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3730. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3731. }
  3732. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3733. }
  3734. *s = sumf;
  3735. #endif
  3736. }
  3737. void ggml_vec_dot_q5_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3738. const int qk = QK8_1;
  3739. const int nb = n / qk;
  3740. assert(n % qk == 0);
  3741. assert(qk == QK5_1);
  3742. assert(nrc == 1);
  3743. UNUSED(nrc);
  3744. UNUSED(bx);
  3745. UNUSED(by);
  3746. UNUSED(bs);
  3747. const block_q5_1 * restrict x = vx;
  3748. const block_q8_1 * restrict y = vy;
  3749. #if defined(__ARM_NEON)
  3750. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3751. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3752. float summs0 = 0.0f;
  3753. float summs1 = 0.0f;
  3754. uint32_t qh0;
  3755. uint32_t qh1;
  3756. uint64_t tmp0[4];
  3757. uint64_t tmp1[4];
  3758. assert(nb % 2 == 0); // TODO: handle odd nb
  3759. for (int i = 0; i < nb; i += 2) {
  3760. const block_q5_1 * restrict x0 = &x[i];
  3761. const block_q5_1 * restrict x1 = &x[i + 1];
  3762. const block_q8_1 * restrict y0 = &y[i];
  3763. const block_q8_1 * restrict y1 = &y[i + 1];
  3764. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3765. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3766. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3767. // extract the 5th bit via lookup table ((b) << 4)
  3768. memcpy(&qh0, x0->qh, sizeof(qh0));
  3769. memcpy(&qh1, x1->qh, sizeof(qh1));
  3770. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3771. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3772. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3773. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3774. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3775. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3776. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3777. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3778. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3779. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3780. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3781. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3782. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3783. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3784. // 4-bit -> 8-bit
  3785. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3786. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3787. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3788. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3789. // add high bit
  3790. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3791. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3792. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3793. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3794. // load y
  3795. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3796. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3797. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3798. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3799. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3800. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3801. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3802. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3803. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3804. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3805. }
  3806. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3807. #elif defined(__wasm_simd128__)
  3808. v128_t sumv = wasm_f32x4_splat(0.0f);
  3809. float summs = 0.0f;
  3810. uint32_t qh;
  3811. uint64_t tmp[4];
  3812. // TODO: check if unrolling this is better
  3813. for (int i = 0; i < nb; ++i) {
  3814. const block_q5_1 * restrict x0 = &x[i];
  3815. const block_q8_1 * restrict y0 = &y[i];
  3816. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3817. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3818. // extract the 5th bit
  3819. memcpy(&qh, x0->qh, sizeof(qh));
  3820. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3821. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3822. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3823. tmp[3] = table_b2b_0[(qh >> 24) ];
  3824. const v128_t qhl = wasm_v128_load(tmp + 0);
  3825. const v128_t qhh = wasm_v128_load(tmp + 2);
  3826. const v128_t v0 = wasm_v128_load(x0->qs);
  3827. // 4-bit -> 8-bit
  3828. const v128_t v0l = wasm_v128_and (v0, m4b);
  3829. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3830. // add high bit
  3831. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3832. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3833. // load y
  3834. const v128_t v1l = wasm_v128_load(y0->qs);
  3835. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3836. // int8x16 -> int16x8
  3837. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3838. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3839. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3840. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3841. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3842. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3843. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3844. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3845. // dot product
  3846. sumv = wasm_f32x4_add(sumv,
  3847. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3848. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3849. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3850. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3851. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3852. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3853. }
  3854. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3855. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3856. #elif defined(__AVX2__)
  3857. // Initialize accumulator with zeros
  3858. __m256 acc = _mm256_setzero_ps();
  3859. float summs = 0.0f;
  3860. // Main loop
  3861. for (int i = 0; i < nb; i++) {
  3862. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3863. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3864. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3865. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3866. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3867. qx = _mm256_or_si256(qx, bxhi);
  3868. const __m256 dy = _mm256_set1_ps(y[i].d);
  3869. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3870. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3871. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3872. }
  3873. *s = hsum_float_8(acc) + summs;
  3874. #elif defined(__AVX__)
  3875. // Initialize accumulator with zeros
  3876. __m256 acc = _mm256_setzero_ps();
  3877. __m128i mask = _mm_set1_epi8(0x10);
  3878. float summs = 0.0f;
  3879. // Main loop
  3880. for (int i = 0; i < nb; i++) {
  3881. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3882. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3883. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3884. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3885. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3886. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3887. bxhil = _mm_and_si128(bxhil, mask);
  3888. bxhih = _mm_and_si128(bxhih, mask);
  3889. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3890. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3891. bxl = _mm_or_si128(bxl, bxhil);
  3892. bxh = _mm_or_si128(bxh, bxhih);
  3893. bx_0 = MM256_SET_M128I(bxh, bxl);
  3894. const __m256 dy = _mm256_set1_ps(y[i].d);
  3895. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3896. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3897. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3898. }
  3899. *s = hsum_float_8(acc) + summs;
  3900. #elif defined(__riscv_v_intrinsic)
  3901. float sumf = 0.0;
  3902. uint32_t qh;
  3903. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3904. // temporary registers for shift operations
  3905. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3906. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3907. for (int i = 0; i < nb; i++) {
  3908. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3909. // load qh
  3910. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3911. // ((qh >> (j + 0)) << 4) & 0x10;
  3912. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3913. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3914. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3915. // ((qh >> (j + 12)) ) & 0x10;
  3916. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3917. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3918. // narrowing
  3919. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3920. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3921. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3922. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3923. // load
  3924. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3925. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3926. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3927. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3928. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3929. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3930. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3931. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3932. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3933. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3934. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3935. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3936. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3937. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3938. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3939. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3940. }
  3941. *s = sumf;
  3942. #else
  3943. // scalar
  3944. float sumf = 0.0;
  3945. for (int i = 0; i < nb; i++) {
  3946. uint32_t qh;
  3947. memcpy(&qh, x[i].qh, sizeof(qh));
  3948. int sumi = 0;
  3949. for (int j = 0; j < qk/2; ++j) {
  3950. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3951. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3952. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3953. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3954. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3955. }
  3956. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3957. }
  3958. *s = sumf;
  3959. #endif
  3960. }
  3961. void ggml_vec_dot_q8_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3962. const int qk = QK8_0;
  3963. const int nb = n / qk;
  3964. assert(n % qk == 0);
  3965. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3966. assert((nrc == 2) || (nrc == 1));
  3967. #else
  3968. assert(nrc == 1);
  3969. #endif
  3970. UNUSED(nrc);
  3971. UNUSED(bx);
  3972. UNUSED(by);
  3973. UNUSED(bs);
  3974. const block_q8_0 * restrict x = vx;
  3975. const block_q8_0 * restrict y = vy;
  3976. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3977. if (nrc == 2) {
  3978. const block_q8_0 * restrict vx0 = vx;
  3979. const block_q8_0 * restrict vx1 = vx + bx;
  3980. const block_q8_0 * restrict vy0 = vy;
  3981. const block_q8_0 * restrict vy1 = vy + by;
  3982. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3983. for (int i = 0; i < nb; i++) {
  3984. const block_q8_0 * restrict b_x0 = &vx0[i];
  3985. const block_q8_0 * restrict b_y0 = &vy0[i];
  3986. const block_q8_0 * restrict b_x1 = &vx1[i];
  3987. const block_q8_0 * restrict b_y1 = &vy1[i];
  3988. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3989. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3990. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3991. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3992. // load y
  3993. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3994. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3995. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3996. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3997. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3998. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3999. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  4000. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  4001. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4002. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4003. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4004. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4005. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4006. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4007. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4008. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4009. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  4010. l1, r1)), l2, r2)), l3, r3))), scale);
  4011. }
  4012. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  4013. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  4014. vst1_f32(s, vget_low_f32(sumv2));
  4015. vst1_f32(s + bs, vget_high_f32(sumv2));
  4016. return;
  4017. }
  4018. #endif
  4019. #if defined(__ARM_NEON)
  4020. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4021. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4022. assert(nb % 2 == 0); // TODO: handle odd nb
  4023. for (int i = 0; i < nb; i += 2) {
  4024. const block_q8_0 * restrict x0 = &x[i + 0];
  4025. const block_q8_0 * restrict x1 = &x[i + 1];
  4026. const block_q8_0 * restrict y0 = &y[i + 0];
  4027. const block_q8_0 * restrict y1 = &y[i + 1];
  4028. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  4029. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  4030. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  4031. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  4032. // load y
  4033. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  4034. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  4035. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  4036. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  4037. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4038. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  4039. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4040. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4041. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  4042. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4043. }
  4044. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  4045. #elif defined(__AVX2__) || defined(__AVX__)
  4046. // Initialize accumulator with zeros
  4047. __m256 acc = _mm256_setzero_ps();
  4048. // Main loop
  4049. for (int i = 0; i < nb; ++i) {
  4050. // Compute combined scale for the block
  4051. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4052. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  4053. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4054. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4055. // Multiply q with scale and accumulate
  4056. #if defined(__AVX2__)
  4057. acc = _mm256_fmadd_ps( d, q, acc );
  4058. #else
  4059. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  4060. #endif
  4061. }
  4062. *s = hsum_float_8(acc);
  4063. #elif defined(__riscv_v_intrinsic)
  4064. float sumf = 0.0;
  4065. size_t vl = __riscv_vsetvl_e8m1(qk);
  4066. for (int i = 0; i < nb; i++) {
  4067. // load elements
  4068. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  4069. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  4070. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  4071. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4072. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  4073. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  4074. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4075. }
  4076. *s = sumf;
  4077. #else
  4078. // scalar
  4079. float sumf = 0.0;
  4080. for (int i = 0; i < nb; i++) {
  4081. int sumi = 0;
  4082. for (int j = 0; j < qk; j++) {
  4083. sumi += x[i].qs[j]*y[i].qs[j];
  4084. }
  4085. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4086. }
  4087. *s = sumf;
  4088. #endif
  4089. }
  4090. #if QK_K == 256
  4091. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4092. assert(nrc == 1);
  4093. UNUSED(nrc);
  4094. UNUSED(bx);
  4095. UNUSED(by);
  4096. UNUSED(bs);
  4097. const block_q2_K * restrict x = vx;
  4098. const block_q8_K * restrict y = vy;
  4099. const int nb = n / QK_K;
  4100. #ifdef __ARM_NEON
  4101. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4102. const uint8x16_t m4 = vdupq_n_u8(0xF);
  4103. const int32x4_t vzero = vdupq_n_s32(0);
  4104. ggml_int8x16x2_t q2bytes;
  4105. uint8_t aux[16];
  4106. float sum = 0;
  4107. for (int i = 0; i < nb; ++i) {
  4108. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4109. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4110. const uint8_t * restrict q2 = x[i].qs;
  4111. const int8_t * restrict q8 = y[i].qs;
  4112. const uint8_t * restrict sc = x[i].scales;
  4113. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4114. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4115. vst1q_u8(aux, scales);
  4116. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4117. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4118. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4119. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4120. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4121. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4122. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4123. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4124. int isum = 0;
  4125. int is = 0;
  4126. // We use this macro instead of a function call because for some reason
  4127. // the code runs 2-3% slower, even if the function is declared inline
  4128. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4129. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4130. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4131. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4132. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4133. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4134. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4135. MULTIPLY_ACCUM_WITH_SCALE((index));
  4136. for (int j = 0; j < QK_K/128; ++j) {
  4137. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4138. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4139. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4140. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4141. MULTIPLY_ACCUM_WITH_SCALE(0);
  4142. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4143. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4144. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4145. is += 8;
  4146. }
  4147. sum += d * isum;
  4148. }
  4149. *s = sum;
  4150. #elif defined __AVX2__
  4151. const __m256i m3 = _mm256_set1_epi8(3);
  4152. const __m128i m4 = _mm_set1_epi8(0xF);
  4153. __m256 acc = _mm256_setzero_ps();
  4154. for (int i = 0; i < nb; ++i) {
  4155. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4156. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4157. const uint8_t * restrict q2 = x[i].qs;
  4158. const int8_t * restrict q8 = y[i].qs;
  4159. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4160. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4161. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4162. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4163. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4164. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4165. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4166. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4167. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4168. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4169. __m256i sumi = _mm256_setzero_si256();
  4170. for (int j = 0; j < QK_K/128; ++j) {
  4171. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4172. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4173. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4174. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4175. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4176. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4177. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4178. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4179. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4180. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4181. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4182. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4183. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4184. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4185. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4186. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4187. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4188. p0 = _mm256_add_epi32(p0, p1);
  4189. p2 = _mm256_add_epi32(p2, p3);
  4190. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4191. }
  4192. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4193. }
  4194. *s = hsum_float_8(acc);
  4195. #elif defined __AVX__
  4196. const __m128i m3 = _mm_set1_epi8(0x3);
  4197. const __m128i m4 = _mm_set1_epi8(0xF);
  4198. const __m128i m2 = _mm_set1_epi8(0x2);
  4199. __m256 acc = _mm256_setzero_ps();
  4200. for (int i = 0; i < nb; ++i) {
  4201. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4202. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4203. const uint8_t * restrict q2 = x[i].qs;
  4204. const int8_t * restrict q8 = y[i].qs;
  4205. // load mins and scales from block_q2_K.scales[QK_K/16]
  4206. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4207. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4208. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4209. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4210. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4211. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4212. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4213. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4214. // sumf += -dmin * summs in 32bits*8
  4215. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4216. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4217. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4218. const __m128i scales[2] = { scales_0, scales_1 };
  4219. __m128i sumi_0 = _mm_setzero_si128();
  4220. __m128i sumi_1 = _mm_setzero_si128();
  4221. for (int j = 0; j < QK_K/128; ++j) {
  4222. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4223. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4224. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4225. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4226. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4227. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4228. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4229. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4230. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4231. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4232. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4233. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4234. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4235. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4236. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4237. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4238. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4239. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4240. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4241. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4242. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4243. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4244. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4245. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4246. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4247. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4248. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4249. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4250. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4251. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4252. __m128i shuffle = _mm_set1_epi16(0x0100);
  4253. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4254. shuffle = _mm_add_epi16(shuffle, m2);
  4255. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4256. shuffle = _mm_add_epi16(shuffle, m2);
  4257. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4258. shuffle = _mm_add_epi16(shuffle, m2);
  4259. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4260. shuffle = _mm_add_epi16(shuffle, m2);
  4261. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4262. shuffle = _mm_add_epi16(shuffle, m2);
  4263. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4264. shuffle = _mm_add_epi16(shuffle, m2);
  4265. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4266. shuffle = _mm_add_epi16(shuffle, m2);
  4267. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4268. p0 = _mm_add_epi32(p0, p1);
  4269. p2 = _mm_add_epi32(p2, p3);
  4270. p4 = _mm_add_epi32(p4, p5);
  4271. p6 = _mm_add_epi32(p6, p7);
  4272. // isum in 32bits*4*2
  4273. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4274. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4275. }
  4276. // sumf += dall * isum - dmin * summs in 32bits
  4277. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4278. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4279. }
  4280. *s = hsum_float_8(acc);
  4281. #elif defined __riscv_v_intrinsic
  4282. float sumf = 0;
  4283. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4284. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4285. for (int i = 0; i < nb; ++i) {
  4286. const uint8_t * q2 = x[i].qs;
  4287. const int8_t * q8 = y[i].qs;
  4288. const uint8_t * sc = x[i].scales;
  4289. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4290. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4291. size_t vl = 16;
  4292. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4293. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4294. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4295. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4296. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4297. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4298. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4299. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4300. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4301. vl = 32;
  4302. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4303. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4304. uint8_t is=0;
  4305. int isum=0;
  4306. for (int j = 0; j < QK_K/128; ++j) {
  4307. // load Q2
  4308. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4309. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4310. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4311. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4312. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4313. // duplicate scale elements for product
  4314. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4315. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4316. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4317. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4318. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4319. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4320. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4321. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4322. // load Q8
  4323. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4324. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4325. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4326. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4327. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4328. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4329. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4330. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4331. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4332. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4333. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4334. q2+=32; q8+=128; is=8;
  4335. }
  4336. sumf += dall * isum;
  4337. }
  4338. *s = sumf;
  4339. #else
  4340. float sumf = 0;
  4341. for (int i = 0; i < nb; ++i) {
  4342. const uint8_t * q2 = x[i].qs;
  4343. const int8_t * q8 = y[i].qs;
  4344. const uint8_t * sc = x[i].scales;
  4345. int summs = 0;
  4346. for (int j = 0; j < 16; ++j) {
  4347. summs += y[i].bsums[j] * (sc[j] >> 4);
  4348. }
  4349. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4350. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4351. int isum = 0;
  4352. int is = 0;
  4353. int d;
  4354. for (int k = 0; k < QK_K/128; ++k) {
  4355. int shift = 0;
  4356. for (int j = 0; j < 4; ++j) {
  4357. d = sc[is++] & 0xF;
  4358. int isuml = 0;
  4359. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4360. isum += d * isuml;
  4361. d = sc[is++] & 0xF;
  4362. isuml = 0;
  4363. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4364. isum += d * isuml;
  4365. shift += 2;
  4366. q8 += 32;
  4367. }
  4368. q2 += 32;
  4369. }
  4370. sumf += dall * isum - dmin * summs;
  4371. }
  4372. *s = sumf;
  4373. #endif
  4374. }
  4375. #else
  4376. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4377. assert(nrc == 1);
  4378. UNUSED(nrc);
  4379. UNUSED(bx);
  4380. UNUSED(by);
  4381. UNUSED(bs);
  4382. const block_q2_K * restrict x = vx;
  4383. const block_q8_K * restrict y = vy;
  4384. const int nb = n / QK_K;
  4385. #ifdef __ARM_NEON
  4386. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4387. const int32x4_t vzero = vdupq_n_s32(0);
  4388. ggml_int8x16x4_t q2bytes;
  4389. uint32_t aux32[2];
  4390. const uint8_t * scales = (const uint8_t *)aux32;
  4391. float sum = 0;
  4392. for (int i = 0; i < nb; ++i) {
  4393. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4394. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4395. const uint8_t * restrict q2 = x[i].qs;
  4396. const int8_t * restrict q8 = y[i].qs;
  4397. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4398. aux32[0] = sc[0] & 0x0f0f0f0f;
  4399. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4400. sum += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4401. int isum1 = 0, isum2 = 0;
  4402. const uint8x16_t q2bits = vld1q_u8(q2);
  4403. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4404. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4405. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4406. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4407. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4408. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4409. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4410. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4411. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4412. sum += d * (isum1 + isum2);
  4413. }
  4414. *s = sum;
  4415. #elif defined __AVX2__
  4416. const __m256i m3 = _mm256_set1_epi8(3);
  4417. __m256 acc = _mm256_setzero_ps();
  4418. uint32_t ud, um;
  4419. const uint8_t * restrict db = (const uint8_t *)&ud;
  4420. const uint8_t * restrict mb = (const uint8_t *)&um;
  4421. float summs = 0;
  4422. // TODO: optimize this
  4423. for (int i = 0; i < nb; ++i) {
  4424. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4425. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4426. const uint8_t * restrict q2 = x[i].qs;
  4427. const int8_t * restrict q8 = y[i].qs;
  4428. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4429. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4430. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4431. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4432. summs += dmin * smin;
  4433. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4434. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4435. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4436. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4437. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4438. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4439. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4440. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4441. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4442. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4443. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4444. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4445. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4446. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4447. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4448. }
  4449. *s = hsum_float_8(acc) + summs;
  4450. #elif defined __AVX__
  4451. const __m128i m3 = _mm_set1_epi8(3);
  4452. __m256 acc = _mm256_setzero_ps();
  4453. uint32_t ud, um;
  4454. const uint8_t * restrict db = (const uint8_t *)&ud;
  4455. const uint8_t * restrict mb = (const uint8_t *)&um;
  4456. float summs = 0;
  4457. // TODO: optimize this
  4458. for (int i = 0; i < nb; ++i) {
  4459. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4460. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4461. const uint8_t * restrict q2 = x[i].qs;
  4462. const int8_t * restrict q8 = y[i].qs;
  4463. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4464. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4465. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4466. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4467. summs += dmin * smin;
  4468. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4469. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4470. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4471. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4472. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4473. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4474. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4475. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4476. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4477. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4478. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4479. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4480. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4481. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4482. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4483. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4484. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4485. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4486. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4487. }
  4488. *s = hsum_float_8(acc) + summs;
  4489. #elif defined __riscv_v_intrinsic
  4490. uint32_t aux32[2];
  4491. const uint8_t * scales = (const uint8_t *)aux32;
  4492. float sumf = 0;
  4493. for (int i = 0; i < nb; ++i) {
  4494. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4495. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4496. const uint8_t * restrict q2 = x[i].qs;
  4497. const int8_t * restrict q8 = y[i].qs;
  4498. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4499. aux32[0] = sc[0] & 0x0f0f0f0f;
  4500. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4501. sumf += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4502. int isum1 = 0;
  4503. int isum2 = 0;
  4504. size_t vl = 16;
  4505. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4506. // load Q2
  4507. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4508. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4509. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4510. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4511. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4512. // load Q8, and take product with Q2
  4513. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4514. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4515. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4516. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4517. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4518. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4519. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4520. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4521. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4522. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4523. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4524. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4525. sumf += d * (isum1 + isum2);
  4526. }
  4527. *s = sumf;
  4528. #else
  4529. float sumf = 0;
  4530. int isum[QK_K/16];
  4531. for (int i = 0; i < nb; ++i) {
  4532. const uint8_t * q2 = x[i].qs;
  4533. const int8_t * q8 = y[i].qs;
  4534. const uint8_t * sc = x[i].scales;
  4535. int summs = 0;
  4536. for (int j = 0; j < QK_K/16; ++j) {
  4537. summs += y[i].bsums[j] * (sc[j] >> 4);
  4538. }
  4539. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4540. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4541. memset(isum, 0, (QK_K/16)*sizeof(int));
  4542. for (int l = 0; l < 16; ++l) {
  4543. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4544. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4545. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4546. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4547. }
  4548. for (int l = 0; l < QK_K/16; ++l) {
  4549. isum[l] *= (sc[l] & 0xF);
  4550. }
  4551. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4552. }
  4553. *s = sumf;
  4554. #endif
  4555. }
  4556. #endif
  4557. #if QK_K == 256
  4558. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4559. assert(n % QK_K == 0);
  4560. assert(nrc == 1);
  4561. UNUSED(nrc);
  4562. UNUSED(bx);
  4563. UNUSED(by);
  4564. UNUSED(bs);
  4565. const uint32_t kmask1 = 0x03030303;
  4566. const uint32_t kmask2 = 0x0f0f0f0f;
  4567. const block_q3_K * restrict x = vx;
  4568. const block_q8_K * restrict y = vy;
  4569. const int nb = n / QK_K;
  4570. #ifdef __ARM_NEON
  4571. uint32_t aux[3];
  4572. uint32_t utmp[4];
  4573. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4574. const int32x4_t vzero = vdupq_n_s32(0);
  4575. const uint8x16_t m0 = vdupq_n_u8(1);
  4576. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4577. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4578. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4579. const int8_t m32 = 32;
  4580. ggml_int8x16x4_t q3bytes;
  4581. float sum = 0;
  4582. for (int i = 0; i < nb; ++i) {
  4583. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4584. const uint8_t * restrict q3 = x[i].qs;
  4585. const uint8_t * restrict qh = x[i].hmask;
  4586. const int8_t * restrict q8 = y[i].qs;
  4587. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4588. ggml_uint8x16x4_t q3h;
  4589. int32_t isum = 0;
  4590. // Set up scales
  4591. memcpy(aux, x[i].scales, 12);
  4592. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4593. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4594. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4595. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4596. int8_t * scale = (int8_t *)utmp;
  4597. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4598. for (int j = 0; j < QK_K/128; ++j) {
  4599. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4600. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4601. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4602. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4603. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4604. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4605. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4606. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4607. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4608. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4609. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4610. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4611. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4612. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4613. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4614. scale += 4;
  4615. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4616. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4617. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4618. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4619. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4620. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4621. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4622. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4623. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4624. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4625. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4626. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4627. scale += 4;
  4628. if (j == 0) {
  4629. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4630. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4631. }
  4632. }
  4633. sum += d * isum;
  4634. }
  4635. *s = sum;
  4636. #elif defined __AVX2__
  4637. const __m256i m3 = _mm256_set1_epi8(3);
  4638. const __m256i mone = _mm256_set1_epi8(1);
  4639. const __m128i m32 = _mm_set1_epi8(32);
  4640. __m256 acc = _mm256_setzero_ps();
  4641. uint32_t aux[3];
  4642. for (int i = 0; i < nb; ++i) {
  4643. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4644. const uint8_t * restrict q3 = x[i].qs;
  4645. const int8_t * restrict q8 = y[i].qs;
  4646. // Set up scales
  4647. memcpy(aux, x[i].scales, 12);
  4648. __m128i scales128 = _mm_set_epi32(
  4649. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4650. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4651. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4652. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4653. scales128 = _mm_sub_epi8(scales128, m32);
  4654. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4655. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4656. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4657. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4658. // high bit
  4659. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4660. // integer accumulator
  4661. __m256i sumi = _mm256_setzero_si256();
  4662. int bit = 0;
  4663. int is = 0;
  4664. for (int j = 0; j < QK_K/128; ++j) {
  4665. // load low 2 bits
  4666. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4667. // prepare low and high bits
  4668. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4669. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4670. ++bit;
  4671. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4672. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4673. ++bit;
  4674. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4675. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4676. ++bit;
  4677. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4678. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4679. ++bit;
  4680. // load Q8 quants
  4681. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4682. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4683. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4684. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4685. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4686. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4687. // and 2 if the high bit was set)
  4688. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4689. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4690. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4691. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4692. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4693. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4694. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4695. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4696. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4697. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4698. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4699. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4700. // multiply with scales
  4701. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4702. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4703. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4704. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4705. // accumulate
  4706. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4707. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4708. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4709. }
  4710. // multiply with block scale and accumulate
  4711. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4712. }
  4713. *s = hsum_float_8(acc);
  4714. #elif defined __AVX__
  4715. const __m128i m3 = _mm_set1_epi8(3);
  4716. const __m128i mone = _mm_set1_epi8(1);
  4717. const __m128i m32 = _mm_set1_epi8(32);
  4718. const __m128i m2 = _mm_set1_epi8(2);
  4719. __m256 acc = _mm256_setzero_ps();
  4720. const uint32_t *aux;
  4721. for (int i = 0; i < nb; ++i) {
  4722. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4723. const uint8_t * restrict q3 = x[i].qs;
  4724. const int8_t * restrict q8 = y[i].qs;
  4725. // Set up scales
  4726. aux = (const uint32_t *)x[i].scales;
  4727. __m128i scales128 = _mm_set_epi32(
  4728. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4729. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4730. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4731. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4732. scales128 = _mm_sub_epi8(scales128, m32);
  4733. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4734. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4735. const __m128i scales[2] = { scales_0, scales_1 };
  4736. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4737. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4738. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4739. // integer accumulator
  4740. __m128i sumi_0 = _mm_setzero_si128();
  4741. __m128i sumi_1 = _mm_setzero_si128();
  4742. for (int j = 0; j < QK_K/128; ++j) {
  4743. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4744. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4745. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4746. // prepare low and high bits
  4747. const int bit = j << 2;
  4748. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4749. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4750. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4751. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4752. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4753. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4754. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4755. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4756. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4757. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4758. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4759. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4760. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4761. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4762. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4763. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4764. // load Q8 quants from block_q8_K.qs[QK_K]
  4765. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4766. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4767. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4768. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4769. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4770. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4771. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4772. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4773. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4774. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4775. // and 2 if the high bit was set)
  4776. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4777. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4778. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4779. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4780. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4781. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4782. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4783. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4784. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4785. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4786. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4787. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4788. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4789. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4790. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4791. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4792. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4793. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4794. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4795. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4796. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4797. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4798. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4799. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4800. // multiply with scales
  4801. __m128i shuffle = _mm_set1_epi16(0x0100);
  4802. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4803. shuffle = _mm_add_epi16(shuffle, m2);
  4804. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4805. shuffle = _mm_add_epi16(shuffle, m2);
  4806. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4807. shuffle = _mm_add_epi16(shuffle, m2);
  4808. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4809. shuffle = _mm_add_epi16(shuffle, m2);
  4810. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4811. shuffle = _mm_add_epi16(shuffle, m2);
  4812. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4813. shuffle = _mm_add_epi16(shuffle, m2);
  4814. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4815. shuffle = _mm_add_epi16(shuffle, m2);
  4816. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4817. // accumulate
  4818. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4819. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4820. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4821. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4822. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4823. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4824. }
  4825. // multiply with block scale and accumulate
  4826. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4827. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4828. }
  4829. *s = hsum_float_8(acc);
  4830. #elif defined __riscv_v_intrinsic
  4831. uint32_t aux[3];
  4832. uint32_t utmp[4];
  4833. float sumf = 0;
  4834. for (int i = 0; i < nb; ++i) {
  4835. const uint8_t * restrict q3 = x[i].qs;
  4836. const uint8_t * restrict qh = x[i].hmask;
  4837. const int8_t * restrict q8 = y[i].qs;
  4838. memcpy(aux, x[i].scales, 12);
  4839. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4840. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4841. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4842. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4843. int8_t * scale = (int8_t *)utmp;
  4844. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4845. size_t vl = 32;
  4846. uint8_t m = 1;
  4847. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4848. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4849. int sum_t = 0;
  4850. for (int j = 0; j < QK_K; j += 128) {
  4851. vl = 32;
  4852. // load Q3
  4853. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4854. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4855. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4856. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4857. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4858. // compute mask for subtraction
  4859. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4860. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4861. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4862. m <<= 1;
  4863. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4864. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4865. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4866. m <<= 1;
  4867. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4868. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4869. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4870. m <<= 1;
  4871. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4872. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4873. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4874. m <<= 1;
  4875. // load Q8 and take product with Q3
  4876. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4877. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4878. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4879. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4880. vl = 16;
  4881. // retrieve lane to multiply with scale
  4882. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4883. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4884. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4885. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4886. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4887. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4888. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4889. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4890. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4891. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4892. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4893. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4894. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4895. q3 += 32; q8 += 128; scale += 8;
  4896. }
  4897. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4898. sumf += d*sum_t;
  4899. }
  4900. *s = sumf;
  4901. #else
  4902. // scalar version
  4903. // This function is written like this so the compiler can manage to vectorize most of it
  4904. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4905. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4906. // The ideal situation would be if we could just write the code once, and the compiler would
  4907. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4908. // write vectorized versions for AVX, ARM_NEON, etc.
  4909. int8_t aux8[QK_K];
  4910. int16_t aux16[8];
  4911. float sums [8];
  4912. int32_t aux32[8];
  4913. memset(sums, 0, 8*sizeof(float));
  4914. uint32_t auxs[4];
  4915. const int8_t * scales = (const int8_t*)auxs;
  4916. float sumf = 0;
  4917. for (int i = 0; i < nb; ++i) {
  4918. const uint8_t * restrict q3 = x[i].qs;
  4919. const uint8_t * restrict hm = x[i].hmask;
  4920. const int8_t * restrict q8 = y[i].qs;
  4921. memset(aux32, 0, 8*sizeof(int32_t));
  4922. int8_t * restrict a = aux8;
  4923. uint8_t m = 1;
  4924. for (int j = 0; j < QK_K; j += 128) {
  4925. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4926. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4927. a += 32; m <<= 1;
  4928. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4929. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4930. a += 32; m <<= 1;
  4931. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4932. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4933. a += 32; m <<= 1;
  4934. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4935. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4936. a += 32; m <<= 1;
  4937. q3 += 32;
  4938. }
  4939. a = aux8;
  4940. memcpy(auxs, x[i].scales, 12);
  4941. uint32_t tmp = auxs[2];
  4942. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4943. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4944. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4945. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4946. for (int j = 0; j < QK_K/16; ++j) {
  4947. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4948. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4949. q8 += 8; a += 8;
  4950. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4951. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4952. q8 += 8; a += 8;
  4953. }
  4954. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4955. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4956. }
  4957. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4958. *s = sumf;
  4959. #endif
  4960. }
  4961. #else
  4962. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4963. assert(n % QK_K == 0);
  4964. assert(nrc == 1);
  4965. UNUSED(nrc);
  4966. UNUSED(bx);
  4967. UNUSED(by);
  4968. UNUSED(bs);
  4969. const block_q3_K * restrict x = vx;
  4970. const block_q8_K * restrict y = vy;
  4971. const int nb = n / QK_K;
  4972. #ifdef __ARM_NEON
  4973. const int32x4_t vzero = vdupq_n_s32(0);
  4974. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4975. const uint8x16_t mh = vdupq_n_u8(4);
  4976. ggml_int8x16x4_t q3bytes;
  4977. uint16_t aux16[2];
  4978. int8_t * scales = (int8_t *)aux16;
  4979. float sum = 0;
  4980. for (int i = 0; i < nb; ++i) {
  4981. ggml_uint8x16x4_t q3h;
  4982. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4983. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4984. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4985. const uint16_t a = *(const uint16_t *)x[i].scales;
  4986. aux16[0] = a & 0x0f0f;
  4987. aux16[1] = (a >> 4) & 0x0f0f;
  4988. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4989. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  4990. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4991. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4992. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4993. q3h.val[1] = vandq_u8(mh, htmp);
  4994. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4995. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4996. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4997. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4998. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4999. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  5000. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  5001. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  5002. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  5003. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  5004. sum += d * isum;
  5005. }
  5006. *s = sum;
  5007. #elif defined __AVX2__
  5008. const __m256i m3 = _mm256_set1_epi8(3);
  5009. const __m256i m1 = _mm256_set1_epi8(1);
  5010. __m256 acc = _mm256_setzero_ps();
  5011. uint64_t aux64;
  5012. uint16_t aux16[2];
  5013. const int8_t * aux8 = (const int8_t *)aux16;
  5014. for (int i = 0; i < nb; ++i) {
  5015. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5016. const uint8_t * restrict q3 = x[i].qs;
  5017. const int8_t * restrict q8 = y[i].qs;
  5018. const uint16_t a = *(const uint16_t *)x[i].scales;
  5019. aux16[0] = a & 0x0f0f;
  5020. aux16[1] = (a >> 4) & 0x0f0f;
  5021. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  5022. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  5023. memcpy(&aux64, x[i].hmask, 8);
  5024. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5025. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  5026. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  5027. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  5028. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  5029. // load low 2 bits
  5030. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5031. // prepare low and high bits
  5032. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  5033. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  5034. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  5035. // load Q8 quants
  5036. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5037. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5038. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5039. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5040. // and 2 if the high bit was set)
  5041. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  5042. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  5043. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  5044. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  5045. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5046. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5047. // multiply with scales
  5048. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5049. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5050. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  5051. // multiply with block scale and accumulate
  5052. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  5053. }
  5054. *s = hsum_float_8(acc);
  5055. #elif defined __AVX__
  5056. const __m128i m3 = _mm_set1_epi8(3);
  5057. const __m128i m1 = _mm_set1_epi8(1);
  5058. __m256 acc = _mm256_setzero_ps();
  5059. uint64_t aux64;
  5060. uint16_t aux16[2];
  5061. const int8_t * aux8 = (const int8_t *)aux16;
  5062. for (int i = 0; i < nb; ++i) {
  5063. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5064. const uint8_t * restrict q3 = x[i].qs;
  5065. const int8_t * restrict q8 = y[i].qs;
  5066. const uint16_t a = *(const uint16_t *)x[i].scales;
  5067. aux16[0] = a & 0x0f0f;
  5068. aux16[1] = (a >> 4) & 0x0f0f;
  5069. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  5070. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  5071. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  5072. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  5073. memcpy(&aux64, x[i].hmask, 8);
  5074. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5075. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  5076. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  5077. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  5078. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  5079. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  5080. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  5081. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  5082. // load low 2 bits
  5083. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5084. // prepare low and high bits
  5085. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  5086. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  5087. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  5088. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  5089. // load Q8 quants
  5090. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5091. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5092. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  5093. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5094. // and 2 if the high bit was set)
  5095. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  5096. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  5097. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  5098. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  5099. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  5100. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  5101. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  5102. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  5103. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5104. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5105. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5106. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5107. // multiply with scales
  5108. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5109. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  5110. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  5111. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  5112. p16_0 = _mm_add_epi32(p16_0, p16_2);
  5113. p16_1 = _mm_add_epi32(p16_1, p16_3);
  5114. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  5115. // multiply with block scale and accumulate
  5116. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  5117. }
  5118. *s = hsum_float_8(acc);
  5119. #elif defined __riscv_v_intrinsic
  5120. uint16_t aux16[2];
  5121. int8_t * scales = (int8_t *)aux16;
  5122. float sumf = 0;
  5123. for (int i = 0; i < nb; ++i) {
  5124. const uint8_t * restrict q3 = x[i].qs;
  5125. const int8_t * restrict q8 = y[i].qs;
  5126. const uint16_t a = *(const uint16_t *)x[i].scales;
  5127. aux16[0] = a & 0x0f0f;
  5128. aux16[1] = (a >> 4) & 0x0f0f;
  5129. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5130. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  5131. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5132. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5133. // load qh
  5134. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5135. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5136. size_t vl = 16;
  5137. // extend and combine both qh_x1 and qh_x2
  5138. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5139. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5140. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5141. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5142. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5143. // load Q3
  5144. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5145. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5146. vuint8mf2_t q3h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 2, vl), 0x3, vl), qh_1, vl);
  5147. vuint8mf2_t q3h_2 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 4, vl), 0x3, vl), qh_2, vl);
  5148. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5149. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5150. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5151. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5152. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5153. // load Q8 and take product with Q3
  5154. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5155. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5156. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5157. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5158. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5159. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5160. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5161. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5162. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5163. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5164. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5165. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5166. sumf += d * isum;
  5167. }
  5168. *s = sumf;
  5169. #else
  5170. int8_t aux8[QK_K];
  5171. int16_t aux16[8];
  5172. float sums [8];
  5173. int32_t aux32[8];
  5174. int32_t scales[4];
  5175. memset(sums, 0, 8*sizeof(float));
  5176. float sumf = 0;
  5177. for (int i = 0; i < nb; ++i) {
  5178. const uint8_t * restrict q3 = x[i].qs;
  5179. const uint8_t * restrict hm = x[i].hmask;
  5180. const int8_t * restrict q8 = y[i].qs;
  5181. int8_t * restrict a = aux8;
  5182. for (int l = 0; l < 8; ++l) {
  5183. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5184. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5185. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5186. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5187. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5188. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5189. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5190. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5191. }
  5192. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5193. scales[1] = (x[i].scales[0] >> 4) - 8;
  5194. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5195. scales[3] = (x[i].scales[1] >> 4) - 8;
  5196. memset(aux32, 0, 8*sizeof(int32_t));
  5197. for (int j = 0; j < QK_K/16; ++j) {
  5198. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5199. q8 += 8; a += 8;
  5200. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5201. q8 += 8; a += 8;
  5202. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5203. }
  5204. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5205. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5206. }
  5207. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5208. *s = sumf;
  5209. #endif
  5210. }
  5211. #endif
  5212. #if QK_K == 256
  5213. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5214. assert(n % QK_K == 0);
  5215. assert(nrc == 1);
  5216. UNUSED(nrc);
  5217. UNUSED(bx);
  5218. UNUSED(by);
  5219. UNUSED(bs);
  5220. const block_q4_K * restrict x = vx;
  5221. const block_q8_K * restrict y = vy;
  5222. const int nb = n / QK_K;
  5223. static const uint32_t kmask1 = 0x3f3f3f3f;
  5224. static const uint32_t kmask2 = 0x0f0f0f0f;
  5225. static const uint32_t kmask3 = 0x03030303;
  5226. uint32_t utmp[4];
  5227. #ifdef __ARM_NEON
  5228. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5229. const int32x4_t mzero = vdupq_n_s32(0);
  5230. ggml_int8x16x2_t q4bytes;
  5231. ggml_int8x16x2_t q8bytes;
  5232. float sumf = 0;
  5233. for (int i = 0; i < nb; ++i) {
  5234. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5235. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5236. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5237. memcpy(utmp, x[i].scales, 12);
  5238. uint32x2_t mins8 = { 0 };
  5239. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5240. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5241. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5242. utmp[0] &= kmask1;
  5243. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5244. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5245. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5246. sumf -= dmin * vaddvq_s32(prod);
  5247. const uint8_t * scales = (const uint8_t *)utmp;
  5248. const uint8_t * restrict q4 = x[i].qs;
  5249. const int8_t * restrict q8 = y[i].qs;
  5250. int32_t sumi1 = 0;
  5251. int32_t sumi2 = 0;
  5252. for (int j = 0; j < QK_K/64; ++j) {
  5253. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5254. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5255. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5256. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5257. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5258. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5259. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5260. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5261. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5262. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5263. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5264. }
  5265. sumf += d * (sumi1 + sumi2);
  5266. }
  5267. *s = sumf;
  5268. #elif defined __AVX2__
  5269. const __m256i m4 = _mm256_set1_epi8(0xF);
  5270. __m256 acc = _mm256_setzero_ps();
  5271. __m128 acc_m = _mm_setzero_ps();
  5272. for (int i = 0; i < nb; ++i) {
  5273. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5274. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5275. memcpy(utmp, x[i].scales, 12);
  5276. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5277. const uint32_t uaux = utmp[1] & kmask1;
  5278. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5279. utmp[2] = uaux;
  5280. utmp[0] &= kmask1;
  5281. const uint8_t * restrict q4 = x[i].qs;
  5282. const int8_t * restrict q8 = y[i].qs;
  5283. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5284. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5285. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5286. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5287. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5288. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5289. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5290. __m256i sumi = _mm256_setzero_si256();
  5291. for (int j = 0; j < QK_K/64; ++j) {
  5292. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5293. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5294. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5295. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5296. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5297. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5298. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5299. p16l = _mm256_madd_epi16(scale_l, p16l);
  5300. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5301. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5302. p16h = _mm256_madd_epi16(scale_h, p16h);
  5303. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5304. sumi = _mm256_add_epi32(sumi, sumj);
  5305. }
  5306. __m256 vd = _mm256_set1_ps(d);
  5307. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5308. }
  5309. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5310. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5311. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5312. #elif defined __AVX__
  5313. const __m128i m4 = _mm_set1_epi8(0xF);
  5314. const __m128i m2 = _mm_set1_epi8(0x2);
  5315. __m256 acc = _mm256_setzero_ps();
  5316. __m128 acc_m = _mm_setzero_ps();
  5317. for (int i = 0; i < nb; ++i) {
  5318. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5319. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5320. const uint8_t * restrict q4 = x[i].qs;
  5321. const int8_t * restrict q8 = y[i].qs;
  5322. memcpy(utmp, x[i].scales, 12);
  5323. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5324. const uint32_t uaux = utmp[1] & kmask1;
  5325. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5326. utmp[2] = uaux;
  5327. utmp[0] &= kmask1;
  5328. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5329. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5330. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5331. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5332. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5333. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5334. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5335. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5336. __m128i sumi_0 = _mm_setzero_si128();
  5337. __m128i sumi_1 = _mm_setzero_si128();
  5338. __m128i shuffle = _mm_set1_epi16(0x0100);
  5339. for (int j = 0; j < QK_K/64; ++j) {
  5340. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5341. shuffle = _mm_add_epi16(shuffle, m2);
  5342. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5343. shuffle = _mm_add_epi16(shuffle, m2);
  5344. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5345. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5346. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5347. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5348. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5349. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5350. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5351. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5352. p16l = _mm_madd_epi16(scale_l, p16l);
  5353. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5354. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5355. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5356. p16l = _mm_madd_epi16(scale_l, p16l);
  5357. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5358. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5359. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5360. p16h = _mm_madd_epi16(scale_h, p16h);
  5361. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5362. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5363. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5364. p16h = _mm_madd_epi16(scale_h, p16h);
  5365. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5366. }
  5367. __m256 vd = _mm256_set1_ps(d);
  5368. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5369. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5370. }
  5371. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5372. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5373. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5374. #elif defined __riscv_v_intrinsic
  5375. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5376. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5377. float sumf = 0;
  5378. for (int i = 0; i < nb; ++i) {
  5379. size_t vl = 8;
  5380. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5381. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5382. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5383. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5384. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5385. memcpy(utmp, x[i].scales, 12);
  5386. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5387. const uint32_t uaux = utmp[1] & kmask1;
  5388. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5389. utmp[2] = uaux;
  5390. utmp[0] &= kmask1;
  5391. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5392. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5393. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5394. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5395. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5396. const uint8_t * restrict q4 = x[i].qs;
  5397. const int8_t * restrict q8 = y[i].qs;
  5398. vl = 32;
  5399. int32_t sum_1 = 0;
  5400. int32_t sum_2 = 0;
  5401. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5402. for (int j = 0; j < QK_K/64; ++j) {
  5403. // load Q4
  5404. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5405. // load Q8 and multiply it with lower Q4 nibble
  5406. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5407. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5408. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5409. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5410. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5411. // load Q8 and multiply it with upper Q4 nibble
  5412. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5413. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5414. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5415. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5416. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5417. q4 += 32; q8 += 64;
  5418. }
  5419. sumf += d*(sum_1 + sum_2);
  5420. }
  5421. *s = sumf;
  5422. #else
  5423. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5424. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5425. int8_t aux8[QK_K];
  5426. int16_t aux16[8];
  5427. float sums [8];
  5428. int32_t aux32[8];
  5429. memset(sums, 0, 8*sizeof(float));
  5430. float sumf = 0;
  5431. for (int i = 0; i < nb; ++i) {
  5432. const uint8_t * restrict q4 = x[i].qs;
  5433. const int8_t * restrict q8 = y[i].qs;
  5434. memset(aux32, 0, 8*sizeof(int32_t));
  5435. int8_t * restrict a = aux8;
  5436. for (int j = 0; j < QK_K/64; ++j) {
  5437. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5438. a += 32;
  5439. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5440. a += 32; q4 += 32;
  5441. }
  5442. memcpy(utmp, x[i].scales, 12);
  5443. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5444. const uint32_t uaux = utmp[1] & kmask1;
  5445. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5446. utmp[2] = uaux;
  5447. utmp[0] &= kmask1;
  5448. int sumi = 0;
  5449. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5450. a = aux8;
  5451. int is = 0;
  5452. for (int j = 0; j < QK_K/32; ++j) {
  5453. int32_t scale = scales[is++];
  5454. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5455. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5456. q8 += 8; a += 8;
  5457. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5458. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5459. q8 += 8; a += 8;
  5460. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5461. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5462. q8 += 8; a += 8;
  5463. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5464. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5465. q8 += 8; a += 8;
  5466. }
  5467. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5468. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5469. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5470. sumf -= dmin * sumi;
  5471. }
  5472. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5473. *s = sumf;
  5474. #endif
  5475. }
  5476. #else
  5477. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5478. assert(n % QK_K == 0);
  5479. assert(nrc == 1);
  5480. UNUSED(nrc);
  5481. UNUSED(bx);
  5482. UNUSED(by);
  5483. UNUSED(bs);
  5484. const block_q4_K * restrict x = vx;
  5485. const block_q8_K * restrict y = vy;
  5486. const int nb = n / QK_K;
  5487. #ifdef __ARM_NEON
  5488. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5489. const int32x4_t mzero = vdupq_n_s32(0);
  5490. float sumf = 0;
  5491. ggml_int8x16x2_t q4bytes;
  5492. ggml_int8x16x4_t q8bytes;
  5493. float sum_mins = 0.f;
  5494. uint16_t aux16[2];
  5495. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5496. for (int i = 0; i < nb; ++i) {
  5497. const uint8_t * restrict q4 = x[i].qs;
  5498. const int8_t * restrict q8 = y[i].qs;
  5499. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5500. aux16[0] = a[0] & 0x0f0f;
  5501. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5502. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5503. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5504. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5505. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5506. q8bytes = ggml_vld1q_s8_x4(q8);
  5507. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5508. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5509. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5510. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5511. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5512. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5513. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5514. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5515. sumf += d * (sumi1 + sumi2);
  5516. }
  5517. *s = sumf - sum_mins;
  5518. #elif defined __AVX2__
  5519. const __m256i m4 = _mm256_set1_epi8(0xF);
  5520. __m256 acc = _mm256_setzero_ps();
  5521. float summs = 0;
  5522. uint16_t aux16[2];
  5523. const uint8_t * scales = (const uint8_t *)aux16;
  5524. for (int i = 0; i < nb; ++i) {
  5525. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5526. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5527. const __m256 vd = _mm256_set1_ps(d);
  5528. const uint16_t * a = (const uint16_t *)x[i].scales;
  5529. aux16[0] = a[0] & 0x0f0f;
  5530. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5531. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5532. const uint8_t * restrict q4 = x[i].qs;
  5533. const int8_t * restrict q8 = y[i].qs;
  5534. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5535. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5536. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5537. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5538. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5539. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5540. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5541. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5542. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5543. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5544. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5545. }
  5546. *s = hsum_float_8(acc) - summs;
  5547. #elif defined __AVX__
  5548. const __m128i m4 = _mm_set1_epi8(0xF);
  5549. __m256 acc = _mm256_setzero_ps();
  5550. float summs = 0;
  5551. uint16_t aux16[2];
  5552. const uint8_t * scales = (const uint8_t *)aux16;
  5553. for (int i = 0; i < nb; ++i) {
  5554. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5555. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5556. const __m256 vd = _mm256_set1_ps(d);
  5557. const uint16_t * a = (const uint16_t *)x[i].scales;
  5558. aux16[0] = a[0] & 0x0f0f;
  5559. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5560. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5561. const uint8_t * restrict q4 = x[i].qs;
  5562. const int8_t * restrict q8 = y[i].qs;
  5563. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5564. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5565. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5566. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5567. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5568. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5569. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5570. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5571. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5572. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5573. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5574. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5575. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5576. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5577. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5578. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5579. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5580. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5581. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5582. }
  5583. *s = hsum_float_8(acc) - summs;
  5584. #elif defined __riscv_v_intrinsic
  5585. uint16_t s16[2];
  5586. const uint8_t * restrict scales = (const uint8_t *)s16;
  5587. float sumf = 0;
  5588. for (int i = 0; i < nb; ++i) {
  5589. const uint8_t * restrict q4 = x[i].qs;
  5590. const int8_t * restrict q8 = y[i].qs;
  5591. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5592. s16[0] = b[0] & 0x0f0f;
  5593. s16[1] = (b[0] >> 4) & 0x0f0f;
  5594. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5595. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5596. size_t vl = 32;
  5597. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5598. // load Q4
  5599. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5600. // load Q8 and multiply it with lower Q4 nibble
  5601. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5602. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5603. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5604. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5605. // load Q8 and multiply it with upper Q4 nibble
  5606. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5607. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5608. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5609. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5610. }
  5611. *s = sumf;
  5612. #else
  5613. uint8_t aux8[QK_K];
  5614. int16_t aux16[16];
  5615. float sums [8];
  5616. memset(sums, 0, 8*sizeof(float));
  5617. uint16_t s16[2];
  5618. const uint8_t * restrict scales = (const uint8_t *)s16;
  5619. float sumf = 0;
  5620. for (int i = 0; i < nb; ++i) {
  5621. const uint8_t * restrict q4 = x[i].qs;
  5622. const int8_t * restrict q8 = y[i].qs;
  5623. uint8_t * restrict a = aux8;
  5624. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5625. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5626. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5627. s16[0] = b[0] & 0x0f0f;
  5628. s16[1] = (b[0] >> 4) & 0x0f0f;
  5629. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5630. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5631. for (int j = 0; j < QK_K/32; ++j) {
  5632. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5633. q8 += 16; a += 16;
  5634. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5635. q8 += 16; a += 16;
  5636. const float dl = d * scales[j];
  5637. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5638. }
  5639. }
  5640. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5641. *s = sumf;
  5642. #endif
  5643. }
  5644. #endif
  5645. #if QK_K == 256
  5646. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5647. assert(n % QK_K == 0);
  5648. assert(nrc == 1);
  5649. UNUSED(nrc);
  5650. UNUSED(bx);
  5651. UNUSED(by);
  5652. UNUSED(bs);
  5653. const block_q5_K * restrict x = vx;
  5654. const block_q8_K * restrict y = vy;
  5655. const int nb = n / QK_K;
  5656. static const uint32_t kmask1 = 0x3f3f3f3f;
  5657. static const uint32_t kmask2 = 0x0f0f0f0f;
  5658. static const uint32_t kmask3 = 0x03030303;
  5659. uint32_t utmp[4];
  5660. #ifdef __ARM_NEON
  5661. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5662. const uint8x16_t mone = vdupq_n_u8(1);
  5663. const uint8x16_t mtwo = vdupq_n_u8(2);
  5664. const int32x4_t mzero = vdupq_n_s32(0);
  5665. ggml_int8x16x4_t q5bytes;
  5666. float sumf = 0;
  5667. for (int i = 0; i < nb; ++i) {
  5668. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5669. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5670. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5671. memcpy(utmp, x[i].scales, 12);
  5672. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5673. const uint32_t uaux = utmp[1] & kmask1;
  5674. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5675. utmp[2] = uaux;
  5676. utmp[0] &= kmask1;
  5677. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5678. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5679. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5680. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5681. int32_t sumi_mins = vaddvq_s32(prod);
  5682. const uint8_t * scales = (const uint8_t *)utmp;
  5683. const uint8_t * restrict q5 = x[i].qs;
  5684. const uint8_t * restrict qh = x[i].qh;
  5685. const int8_t * restrict q8 = y[i].qs;
  5686. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5687. ggml_uint8x16x4_t q5h;
  5688. int32_t sumi = 0;
  5689. for (int j = 0; j < QK_K/64; ++j) {
  5690. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5691. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5692. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5693. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5694. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5695. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5696. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5697. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5698. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5699. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5700. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5701. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5702. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5703. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5704. }
  5705. sumf += d * sumi - dmin * sumi_mins;
  5706. }
  5707. *s = sumf;
  5708. #elif defined __AVX2__
  5709. const __m256i m4 = _mm256_set1_epi8(0xF);
  5710. const __m128i mzero = _mm_setzero_si128();
  5711. const __m256i mone = _mm256_set1_epi8(1);
  5712. __m256 acc = _mm256_setzero_ps();
  5713. float summs = 0.f;
  5714. for (int i = 0; i < nb; ++i) {
  5715. const uint8_t * restrict q5 = x[i].qs;
  5716. const int8_t * restrict q8 = y[i].qs;
  5717. #if QK_K == 256
  5718. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5719. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5720. memcpy(utmp, x[i].scales, 12);
  5721. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5722. const uint32_t uaux = utmp[1] & kmask1;
  5723. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5724. utmp[2] = uaux;
  5725. utmp[0] &= kmask1;
  5726. #else
  5727. // TODO
  5728. const float d = 0, dmin = 0;
  5729. #endif
  5730. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5731. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5732. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5733. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5734. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5735. summs += dmin * _mm_extract_epi32(hsum, 0);
  5736. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5737. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5738. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5739. __m256i hmask = mone;
  5740. __m256i sumi = _mm256_setzero_si256();
  5741. int bit = 0;
  5742. for (int j = 0; j < QK_K/64; ++j) {
  5743. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5744. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5745. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5746. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5747. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5748. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5749. hmask = _mm256_slli_epi16(hmask, 1);
  5750. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5751. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5752. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5753. hmask = _mm256_slli_epi16(hmask, 1);
  5754. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5755. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5756. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5757. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5758. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5759. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5760. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5761. }
  5762. __m256 vd = _mm256_set1_ps(d);
  5763. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5764. }
  5765. *s = hsum_float_8(acc) + summs;
  5766. #elif defined __AVX__
  5767. const __m128i m4 = _mm_set1_epi8(0xF);
  5768. const __m128i mzero = _mm_setzero_si128();
  5769. const __m128i mone = _mm_set1_epi8(1);
  5770. const __m128i m2 = _mm_set1_epi8(2);
  5771. __m256 acc = _mm256_setzero_ps();
  5772. float summs = 0.f;
  5773. for (int i = 0; i < nb; ++i) {
  5774. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5775. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5776. const uint8_t * restrict q5 = x[i].qs;
  5777. const int8_t * restrict q8 = y[i].qs;
  5778. memcpy(utmp, x[i].scales, 12);
  5779. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5780. const uint32_t uaux = utmp[1] & kmask1;
  5781. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5782. utmp[2] = uaux;
  5783. utmp[0] &= kmask1;
  5784. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5785. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5786. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5787. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5788. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5789. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5790. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5791. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5792. summs += dmin * _mm_extract_epi32(hsum, 0);
  5793. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5794. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5795. __m128i hmask = mone;
  5796. __m128i sumi_0 = _mm_setzero_si128();
  5797. __m128i sumi_1 = _mm_setzero_si128();
  5798. int bit = 0;
  5799. __m128i shuffle = _mm_set1_epi16(0x0100);
  5800. for (int j = 0; j < QK_K/64; ++j) {
  5801. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5802. shuffle = _mm_add_epi16(shuffle, m2);
  5803. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5804. shuffle = _mm_add_epi16(shuffle, m2);
  5805. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5806. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5807. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5808. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5809. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5810. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5811. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5812. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5813. hmask = _mm_slli_epi16(hmask, 1);
  5814. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5815. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5816. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5817. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5818. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5819. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5820. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5821. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5822. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5823. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5824. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5825. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5826. hmask = _mm_slli_epi16(hmask, 1);
  5827. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5828. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5829. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5830. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5831. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5832. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5833. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5834. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5835. }
  5836. __m256 vd = _mm256_set1_ps(d);
  5837. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5838. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5839. }
  5840. *s = hsum_float_8(acc) + summs;
  5841. #elif defined __riscv_v_intrinsic
  5842. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5843. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5844. float sumf = 0;
  5845. float sums = 0.0;
  5846. size_t vl;
  5847. for (int i = 0; i < nb; ++i) {
  5848. vl = 8;
  5849. const uint8_t * restrict q5 = x[i].qs;
  5850. const uint8_t * restrict hm = x[i].qh;
  5851. const int8_t * restrict q8 = y[i].qs;
  5852. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5853. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5854. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5855. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5856. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5857. memcpy(utmp, x[i].scales, 12);
  5858. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5859. const uint32_t uaux = utmp[1] & kmask1;
  5860. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5861. utmp[2] = uaux;
  5862. utmp[0] &= kmask1;
  5863. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5864. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5865. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5866. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5867. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5868. vl = 32;
  5869. int32_t aux32 = 0;
  5870. int is = 0;
  5871. uint8_t m = 1;
  5872. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5873. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5874. for (int j = 0; j < QK_K/64; ++j) {
  5875. // load Q5 and Q8
  5876. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5877. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5878. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5879. // compute mask for addition
  5880. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5881. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5882. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5883. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5884. m <<= 1;
  5885. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5886. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5887. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5888. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5889. m <<= 1;
  5890. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5891. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5892. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5893. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5894. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5895. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5896. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5897. q5 += 32; q8 += 64;
  5898. }
  5899. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5900. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5901. }
  5902. *s = sumf+sums;
  5903. #else
  5904. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5905. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5906. int8_t aux8[QK_K];
  5907. int16_t aux16[8];
  5908. float sums [8];
  5909. int32_t aux32[8];
  5910. memset(sums, 0, 8*sizeof(float));
  5911. float sumf = 0;
  5912. for (int i = 0; i < nb; ++i) {
  5913. const uint8_t * restrict q4 = x[i].qs;
  5914. const uint8_t * restrict hm = x[i].qh;
  5915. const int8_t * restrict q8 = y[i].qs;
  5916. memset(aux32, 0, 8*sizeof(int32_t));
  5917. int8_t * restrict a = aux8;
  5918. uint8_t m = 1;
  5919. for (int j = 0; j < QK_K/64; ++j) {
  5920. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5921. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5922. a += 32; m <<= 1;
  5923. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5924. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5925. a += 32; m <<= 1;
  5926. q4 += 32;
  5927. }
  5928. memcpy(utmp, x[i].scales, 12);
  5929. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5930. const uint32_t uaux = utmp[1] & kmask1;
  5931. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5932. utmp[2] = uaux;
  5933. utmp[0] &= kmask1;
  5934. int sumi = 0;
  5935. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5936. a = aux8;
  5937. int is = 0;
  5938. for (int j = 0; j < QK_K/32; ++j) {
  5939. int32_t scale = scales[is++];
  5940. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5941. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5942. q8 += 8; a += 8;
  5943. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5944. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5945. q8 += 8; a += 8;
  5946. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5947. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5948. q8 += 8; a += 8;
  5949. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5950. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5951. q8 += 8; a += 8;
  5952. }
  5953. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5954. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5955. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5956. sumf -= dmin * sumi;
  5957. }
  5958. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5959. *s = sumf;
  5960. #endif
  5961. }
  5962. #else
  5963. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5964. assert(n % QK_K == 0);
  5965. assert(nrc == 1);
  5966. UNUSED(nrc);
  5967. UNUSED(bx);
  5968. UNUSED(by);
  5969. UNUSED(bs);
  5970. const block_q5_K * restrict x = vx;
  5971. const block_q8_K * restrict y = vy;
  5972. const int nb = n / QK_K;
  5973. #ifdef __ARM_NEON
  5974. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5975. const uint8x16_t mh = vdupq_n_u8(16);
  5976. const int32x4_t mzero = vdupq_n_s32(0);
  5977. ggml_int8x16x4_t q5bytes;
  5978. ggml_uint8x16x4_t q5h;
  5979. float sumf = 0;
  5980. for (int i = 0; i < nb; ++i) {
  5981. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5982. const int8_t * sc = x[i].scales;
  5983. const uint8_t * restrict q5 = x[i].qs;
  5984. const uint8_t * restrict qh = x[i].qh;
  5985. const int8_t * restrict q8 = y[i].qs;
  5986. const uint8x8_t qhbits = vld1_u8(qh);
  5987. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5988. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5989. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5990. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5991. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5992. q5h.val[2] = vbicq_u8(mh, htmp);
  5993. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5994. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5995. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5996. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5997. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5998. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5999. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  6000. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  6001. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  6002. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6003. }
  6004. *s = sumf;
  6005. #elif defined __AVX2__
  6006. const __m256i m4 = _mm256_set1_epi8(0xF);
  6007. const __m256i mone = _mm256_set1_epi8(1);
  6008. __m256 acc = _mm256_setzero_ps();
  6009. for (int i = 0; i < nb; ++i) {
  6010. const uint8_t * restrict q5 = x[i].qs;
  6011. const int8_t * restrict q8 = y[i].qs;
  6012. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6013. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6014. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  6015. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  6016. int64_t aux64;
  6017. memcpy(&aux64, x[i].qh, 8);
  6018. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  6019. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  6020. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  6021. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  6022. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  6023. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  6024. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6025. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6026. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  6027. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  6028. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  6029. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  6030. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  6031. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  6032. }
  6033. *s = hsum_float_8(acc);
  6034. #elif defined __AVX__
  6035. const __m128i m4 = _mm_set1_epi8(0xF);
  6036. const __m128i mone = _mm_set1_epi8(1);
  6037. __m256 acc = _mm256_setzero_ps();
  6038. for (int i = 0; i < nb; ++i) {
  6039. const uint8_t * restrict q5 = x[i].qs;
  6040. const int8_t * restrict q8 = y[i].qs;
  6041. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6042. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6043. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  6044. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  6045. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  6046. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  6047. int64_t aux64;
  6048. memcpy(&aux64, x[i].qh, 8);
  6049. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  6050. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  6051. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  6052. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  6053. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  6054. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  6055. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  6056. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  6057. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  6058. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  6059. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6060. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6061. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  6062. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  6063. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  6064. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  6065. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  6066. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  6067. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  6068. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  6069. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  6070. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  6071. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  6072. }
  6073. *s = hsum_float_8(acc);
  6074. #elif defined __riscv_v_intrinsic
  6075. float sumf = 0;
  6076. for (int i = 0; i < nb; ++i) {
  6077. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6078. const int8_t * sc = x[i].scales;
  6079. const uint8_t * restrict q5 = x[i].qs;
  6080. const uint8_t * restrict qh = x[i].qh;
  6081. const int8_t * restrict q8 = y[i].qs;
  6082. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6083. // load qh
  6084. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  6085. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  6086. size_t vl = 16;
  6087. // combine both qh_1 and qh_2
  6088. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  6089. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6090. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  6091. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  6092. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6093. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  6094. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  6095. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  6096. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  6097. // load q5
  6098. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  6099. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  6100. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  6101. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  6102. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  6103. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  6104. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  6105. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  6106. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  6107. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  6108. // load Q8 and multiply it with Q5
  6109. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6110. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6111. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6112. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6113. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6114. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6115. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6116. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6117. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  6118. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  6119. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  6120. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  6121. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6122. }
  6123. *s = sumf;
  6124. #else
  6125. int8_t aux8[QK_K];
  6126. int16_t aux16[16];
  6127. float sums [8];
  6128. memset(sums, 0, 8*sizeof(float));
  6129. float sumf = 0;
  6130. for (int i = 0; i < nb; ++i) {
  6131. const uint8_t * restrict q4 = x[i].qs;
  6132. const uint8_t * restrict hm = x[i].qh;
  6133. const int8_t * restrict q8 = y[i].qs;
  6134. int8_t * restrict a = aux8;
  6135. for (int l = 0; l < 32; ++l) {
  6136. a[l+ 0] = q4[l] & 0xF;
  6137. a[l+32] = q4[l] >> 4;
  6138. }
  6139. for (int is = 0; is < 8; ++is) {
  6140. uint8_t m = 1 << is;
  6141. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  6142. }
  6143. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6144. const int8_t * restrict sc = x[i].scales;
  6145. for (int j = 0; j < QK_K/16; ++j) {
  6146. const float dl = d * sc[j];
  6147. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  6148. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  6149. q8 += 16; a += 16;
  6150. }
  6151. }
  6152. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6153. *s = sumf;
  6154. #endif
  6155. }
  6156. #endif
  6157. #if QK_K == 256
  6158. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6159. assert(n % QK_K == 0);
  6160. assert(nrc == 1);
  6161. UNUSED(nrc);
  6162. UNUSED(bx);
  6163. UNUSED(by);
  6164. UNUSED(bs);
  6165. const block_q6_K * restrict x = vx;
  6166. const block_q8_K * restrict y = vy;
  6167. const int nb = n / QK_K;
  6168. #ifdef __ARM_NEON
  6169. float sum = 0;
  6170. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6171. const int32x4_t vzero = vdupq_n_s32(0);
  6172. //const int8x16_t m32s = vdupq_n_s8(32);
  6173. const uint8x16_t mone = vdupq_n_u8(3);
  6174. ggml_int8x16x4_t q6bytes;
  6175. ggml_uint8x16x4_t q6h;
  6176. for (int i = 0; i < nb; ++i) {
  6177. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6178. const uint8_t * restrict q6 = x[i].ql;
  6179. const uint8_t * restrict qh = x[i].qh;
  6180. const int8_t * restrict q8 = y[i].qs;
  6181. const int8_t * restrict scale = x[i].scales;
  6182. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6183. const int8x16_t scales = vld1q_s8(scale);
  6184. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6185. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6186. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6187. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6188. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6189. int32_t isum_mins = vaddvq_s32(prod);
  6190. int32_t isum = 0;
  6191. for (int j = 0; j < QK_K/128; ++j) {
  6192. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6193. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6194. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6195. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6196. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6197. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6198. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6199. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6200. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6201. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6202. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6203. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6204. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6205. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6206. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6207. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6208. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6209. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6210. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6211. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6212. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6213. scale += 4;
  6214. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6215. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6216. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6217. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6218. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6219. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6220. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6221. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6222. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6223. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6224. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6225. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6226. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6227. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6228. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6229. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6230. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6231. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6232. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6233. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6234. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6235. scale += 4;
  6236. }
  6237. //sum += isum * d_all * y[i].d;
  6238. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6239. }
  6240. *s = sum;
  6241. #elif defined __AVX2__
  6242. const __m256i m4 = _mm256_set1_epi8(0xF);
  6243. const __m256i m2 = _mm256_set1_epi8(3);
  6244. const __m256i m32s = _mm256_set1_epi8(32);
  6245. __m256 acc = _mm256_setzero_ps();
  6246. for (int i = 0; i < nb; ++i) {
  6247. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6248. const uint8_t * restrict q4 = x[i].ql;
  6249. const uint8_t * restrict qh = x[i].qh;
  6250. const int8_t * restrict q8 = y[i].qs;
  6251. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6252. __m256i sumi = _mm256_setzero_si256();
  6253. int is = 0;
  6254. for (int j = 0; j < QK_K/128; ++j) {
  6255. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6256. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6257. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6258. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6259. is += 4;
  6260. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6261. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6262. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6263. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6264. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6265. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6266. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6267. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6268. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6269. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6270. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6271. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6272. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6273. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6274. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6275. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6276. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6277. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6278. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6279. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6280. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6281. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6282. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6283. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6284. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6285. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6286. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6287. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6288. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6289. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6290. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6291. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6292. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6293. }
  6294. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6295. }
  6296. *s = hsum_float_8(acc);
  6297. #elif defined __AVX__
  6298. const __m128i m4 = _mm_set1_epi8(0xF);
  6299. const __m128i m3 = _mm_set1_epi8(3);
  6300. const __m128i m32s = _mm_set1_epi8(32);
  6301. const __m128i m2 = _mm_set1_epi8(2);
  6302. __m256 acc = _mm256_setzero_ps();
  6303. for (int i = 0; i < nb; ++i) {
  6304. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6305. const uint8_t * restrict q4 = x[i].ql;
  6306. const uint8_t * restrict qh = x[i].qh;
  6307. const int8_t * restrict q8 = y[i].qs;
  6308. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6309. __m128i sumi_0 = _mm_setzero_si128();
  6310. __m128i sumi_1 = _mm_setzero_si128();
  6311. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6312. for (int j = 0; j < QK_K/128; ++j) {
  6313. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6314. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6315. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6316. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6317. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6318. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6319. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6320. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6321. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6322. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6323. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6324. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6325. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6326. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6327. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6328. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6329. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6330. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6331. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6332. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6333. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6334. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6335. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6336. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6337. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6338. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6339. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6340. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6341. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6342. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6343. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6344. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6345. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6346. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6347. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6348. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6349. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6350. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6351. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6352. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6353. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6354. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6355. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6356. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6357. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6358. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6359. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6360. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6361. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6362. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6363. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6364. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6365. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6366. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6367. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6368. shuffle = _mm_add_epi8(shuffle, m2);
  6369. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6370. shuffle = _mm_add_epi8(shuffle, m2);
  6371. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6372. shuffle = _mm_add_epi8(shuffle, m2);
  6373. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6374. shuffle = _mm_add_epi8(shuffle, m2);
  6375. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6376. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6377. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6378. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6379. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6380. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6381. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6382. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6383. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6384. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6385. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6386. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6387. }
  6388. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6389. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6390. }
  6391. *s = hsum_float_8(acc);
  6392. #elif defined __riscv_v_intrinsic
  6393. float sumf = 0;
  6394. for (int i = 0; i < nb; ++i) {
  6395. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6396. const uint8_t * restrict q6 = x[i].ql;
  6397. const uint8_t * restrict qh = x[i].qh;
  6398. const int8_t * restrict q8 = y[i].qs;
  6399. const int8_t * restrict scale = x[i].scales;
  6400. size_t vl;
  6401. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6402. int sum_t = 0;
  6403. int is = 0;
  6404. for (int j = 0; j < QK_K/128; ++j) {
  6405. vl = 32;
  6406. // load qh
  6407. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6408. // load Q6
  6409. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6410. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6411. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6412. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6413. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6414. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6415. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6416. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6417. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6418. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6419. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6420. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6421. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6422. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6423. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6424. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6425. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6426. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6427. // load Q8 and take product
  6428. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6429. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6430. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6431. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6432. vl = 16;
  6433. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6434. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6435. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6436. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6437. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6438. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6439. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6440. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6441. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6442. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6443. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6444. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6445. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6446. q6 += 64; qh += 32; q8 += 128; is=8;
  6447. }
  6448. sumf += d * sum_t;
  6449. }
  6450. *s = sumf;
  6451. #else
  6452. int8_t aux8[QK_K];
  6453. int16_t aux16[8];
  6454. float sums [8];
  6455. int32_t aux32[8];
  6456. memset(sums, 0, 8*sizeof(float));
  6457. float sumf = 0;
  6458. for (int i = 0; i < nb; ++i) {
  6459. const uint8_t * restrict q4 = x[i].ql;
  6460. const uint8_t * restrict qh = x[i].qh;
  6461. const int8_t * restrict q8 = y[i].qs;
  6462. memset(aux32, 0, 8*sizeof(int32_t));
  6463. int8_t * restrict a = aux8;
  6464. for (int j = 0; j < QK_K; j += 128) {
  6465. for (int l = 0; l < 32; ++l) {
  6466. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6467. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6468. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6469. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6470. }
  6471. a += 128;
  6472. q4 += 64;
  6473. qh += 32;
  6474. }
  6475. a = aux8;
  6476. int is = 0;
  6477. for (int j = 0; j < QK_K/16; ++j) {
  6478. int scale = x[i].scales[is++];
  6479. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6480. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6481. q8 += 8; a += 8;
  6482. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6483. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6484. q8 += 8; a += 8;
  6485. }
  6486. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6487. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6488. }
  6489. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6490. *s = sumf;
  6491. #endif
  6492. }
  6493. #else
  6494. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6495. assert(n % QK_K == 0);
  6496. assert(nrc == 1);
  6497. UNUSED(nrc);
  6498. UNUSED(bx);
  6499. UNUSED(by);
  6500. UNUSED(bs);
  6501. const block_q6_K * restrict x = vx;
  6502. const block_q8_K * restrict y = vy;
  6503. const int nb = n / QK_K;
  6504. #ifdef __ARM_NEON
  6505. float sum = 0;
  6506. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6507. const int8x16_t m32s = vdupq_n_s8(32);
  6508. const int32x4_t vzero = vdupq_n_s32(0);
  6509. const uint8x16_t mone = vdupq_n_u8(3);
  6510. ggml_int8x16x4_t q6bytes;
  6511. ggml_uint8x16x4_t q6h;
  6512. for (int i = 0; i < nb; ++i) {
  6513. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6514. const uint8_t * restrict q6 = x[i].ql;
  6515. const uint8_t * restrict qh = x[i].qh;
  6516. const int8_t * restrict q8 = y[i].qs;
  6517. const int8_t * restrict scale = x[i].scales;
  6518. int32_t isum = 0;
  6519. uint8x16_t qhbits = vld1q_u8(qh);
  6520. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6521. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6522. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6523. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6524. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6525. shifted = vshrq_n_u8(qhbits, 4);
  6526. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6527. shifted = vshrq_n_u8(qhbits, 6);
  6528. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6529. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6530. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6531. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6532. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6533. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6534. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6535. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6536. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6537. sum += isum * d_all * y[i].d;
  6538. }
  6539. *s = sum;
  6540. #elif defined __AVX2__
  6541. const __m256i m4 = _mm256_set1_epi8(0xF);
  6542. const __m256i m2 = _mm256_set1_epi8(3);
  6543. const __m256i m32s = _mm256_set1_epi8(32);
  6544. __m256 acc = _mm256_setzero_ps();
  6545. for (int i = 0; i < nb; ++i) {
  6546. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6547. const uint8_t * restrict q4 = x[i].ql;
  6548. const uint8_t * restrict qh = x[i].qh;
  6549. const int8_t * restrict q8 = y[i].qs;
  6550. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6551. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6552. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6553. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6554. __m256i sumi = _mm256_setzero_si256();
  6555. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6556. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6557. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6558. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6559. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6560. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 6), _mm_srli_epi16(q4bitsH, 4)), m2), 4);
  6561. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6562. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6563. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6564. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6565. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6566. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6567. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6568. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6569. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6570. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6571. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6572. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6573. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6574. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6575. }
  6576. *s = hsum_float_8(acc);
  6577. #elif defined __AVX__
  6578. const __m128i m4 = _mm_set1_epi8(0xF);
  6579. const __m128i m2 = _mm_set1_epi8(3);
  6580. const __m128i m32s = _mm_set1_epi8(32);
  6581. __m256 acc = _mm256_setzero_ps();
  6582. for (int i = 0; i < nb; ++i) {
  6583. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6584. const uint8_t * restrict q4 = x[i].ql;
  6585. const uint8_t * restrict qh = x[i].qh;
  6586. const int8_t * restrict q8 = y[i].qs;
  6587. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6588. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6589. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6590. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6591. __m128i sumi_0 = _mm_setzero_si128();
  6592. __m128i sumi_1 = _mm_setzero_si128();
  6593. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6594. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6595. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6596. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6597. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6598. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6599. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6600. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6601. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6602. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6603. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6604. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6605. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6606. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6607. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6608. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6609. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6610. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6611. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6612. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6613. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6614. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6615. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6616. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6617. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6618. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6619. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6620. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6621. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6622. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6623. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6624. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6625. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6626. }
  6627. *s = hsum_float_8(acc);
  6628. #elif defined __riscv_v_intrinsic
  6629. float sumf = 0;
  6630. for (int i = 0; i < nb; ++i) {
  6631. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6632. const uint8_t * restrict q6 = x[i].ql;
  6633. const uint8_t * restrict qh = x[i].qh;
  6634. const int8_t * restrict q8 = y[i].qs;
  6635. const int8_t * restrict scale = x[i].scales;
  6636. int32_t isum = 0;
  6637. size_t vl = 16;
  6638. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6639. // load Q6
  6640. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6641. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6642. // load qh
  6643. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6644. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6645. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6646. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6647. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6648. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6649. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6650. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6651. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6652. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6653. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6654. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6655. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6656. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6657. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6658. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6659. // load Q8 and take product
  6660. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6661. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6662. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6663. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6664. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6665. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6666. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6667. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6668. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6669. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6670. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6671. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6672. sumf += isum * d_all * y[i].d;
  6673. }
  6674. *s = sumf;
  6675. #else
  6676. int8_t aux8[QK_K];
  6677. int16_t aux16[8];
  6678. float sums [8];
  6679. int32_t aux32[8];
  6680. memset(sums, 0, 8*sizeof(float));
  6681. float sumf = 0;
  6682. for (int i = 0; i < nb; ++i) {
  6683. const uint8_t * restrict q4 = x[i].ql;
  6684. const uint8_t * restrict qh = x[i].qh;
  6685. const int8_t * restrict q8 = y[i].qs;
  6686. memset(aux32, 0, 8*sizeof(int32_t));
  6687. int8_t * restrict a = aux8;
  6688. for (int l = 0; l < 16; ++l) {
  6689. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6690. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6691. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6692. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6693. }
  6694. int is = 0;
  6695. for (int j = 0; j < QK_K/16; ++j) {
  6696. int scale = x[i].scales[is++];
  6697. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6698. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6699. q8 += 8; a += 8;
  6700. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6701. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6702. q8 += 8; a += 8;
  6703. }
  6704. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6705. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6706. }
  6707. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6708. *s = sumf;
  6709. #endif
  6710. }
  6711. #endif
  6712. #if defined (__AVX2__) || defined (__ARM_NEON)
  6713. static const int8_t keven_signs_q2xs[1024] = {
  6714. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6715. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6716. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6717. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6718. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6719. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6720. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6721. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6722. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6723. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6724. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6725. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6726. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6727. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6728. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6729. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6730. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6731. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6732. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6733. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6734. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6735. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6736. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6737. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6738. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6739. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6740. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6741. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6742. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6743. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6744. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6745. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6746. };
  6747. #endif
  6748. void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6749. assert(n % QK_K == 0);
  6750. assert(nrc == 1);
  6751. UNUSED(nrc);
  6752. UNUSED(bx);
  6753. UNUSED(by);
  6754. UNUSED(bs);
  6755. const block_iq2_xxs * restrict x = vx;
  6756. const block_q8_K * restrict y = vy;
  6757. const int nb = n / QK_K;
  6758. #if defined(__ARM_NEON)
  6759. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6760. uint32_t aux32[4];
  6761. const uint8_t * aux8 = (const uint8_t *)aux32;
  6762. ggml_int8x16x4_t q2u;
  6763. ggml_int8x16x4_t q2s;
  6764. ggml_int8x16x4_t q8b;
  6765. float sumf = 0;
  6766. for (int i = 0; i < nb; ++i) {
  6767. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6768. const uint16_t * restrict q2 = x[i].qs;
  6769. const int8_t * restrict q8 = y[i].qs;
  6770. float sumf1 = 0, sumf2 = 0;
  6771. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6772. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6773. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6774. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6775. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6776. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6777. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6778. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6779. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6780. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6781. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6782. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6783. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6784. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6785. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6786. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);
  6787. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);
  6788. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6789. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6790. }
  6791. sumf += d*(sumf1 + sumf2);
  6792. }
  6793. *s = 0.25f * sumf;
  6794. #elif defined(__AVX2__)
  6795. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6796. uint32_t aux32[4];
  6797. const uint8_t * aux8 = (const uint8_t *)aux32;
  6798. __m256 accumf = _mm256_setzero_ps();
  6799. for (int i = 0; i < nb; ++i) {
  6800. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6801. const uint16_t * restrict q2 = x[i].qs;
  6802. const int8_t * restrict q8 = y[i].qs;
  6803. __m256i sumi1 = _mm256_setzero_si256();
  6804. __m256i sumi2 = _mm256_setzero_si256();
  6805. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6806. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6807. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6808. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6809. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6810. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6811. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6812. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6813. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6814. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6815. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6816. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6817. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6818. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6819. const uint16_t ls1 = aux32[1] >> 28;
  6820. const uint16_t ls2 = aux32[3] >> 28;
  6821. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6822. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6823. sumi1 = _mm256_add_epi32(sumi1, p1);
  6824. sumi2 = _mm256_add_epi32(sumi2, p2);
  6825. }
  6826. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6827. }
  6828. *s = 0.125f * hsum_float_8(accumf);
  6829. #else
  6830. uint32_t aux32[2];
  6831. const uint8_t * aux8 = (const uint8_t *)aux32;
  6832. float sumf = 0.f;
  6833. for (int i = 0; i < nb; ++i) {
  6834. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6835. const uint16_t * restrict q2 = x[i].qs;
  6836. const int8_t * restrict q8 = y[i].qs;
  6837. int32_t bsum = 0;
  6838. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6839. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6840. q2 += 4;
  6841. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6842. int32_t sumi = 0;
  6843. for (int l = 0; l < 4; ++l) {
  6844. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6845. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6846. for (int j = 0; j < 8; ++j) {
  6847. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6848. }
  6849. q8 += 8;
  6850. }
  6851. bsum += sumi * ls;
  6852. }
  6853. sumf += d * bsum;
  6854. }
  6855. *s = 0.125f * sumf;
  6856. #endif
  6857. }
  6858. void ggml_vec_dot_iq2_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6859. assert(n % QK_K == 0);
  6860. assert(nrc == 1);
  6861. UNUSED(nrc);
  6862. UNUSED(bx);
  6863. UNUSED(by);
  6864. UNUSED(bs);
  6865. const block_iq2_xs * restrict x = vx;
  6866. const block_q8_K * restrict y = vy;
  6867. const int nb = n / QK_K;
  6868. #if defined(__ARM_NEON)
  6869. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6870. ggml_int8x16x4_t q2u;
  6871. ggml_int8x16x4_t q2s;
  6872. ggml_int8x16x4_t q8b;
  6873. int32x4x4_t scales32;
  6874. float sumf = 0;
  6875. for (int i = 0; i < nb; ++i) {
  6876. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6877. const uint16_t * restrict q2 = x[i].qs;
  6878. const int8_t * restrict q8 = y[i].qs;
  6879. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6880. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6881. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6882. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6883. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6884. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6885. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6886. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6887. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6888. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6889. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6890. int32x4_t sumi = vdupq_n_s32(0);
  6891. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6892. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6893. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6894. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6895. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6896. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6897. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6898. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6899. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6900. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6901. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6902. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6903. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6904. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6905. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6906. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6907. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6908. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6909. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6910. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6911. q2 += 8;
  6912. }
  6913. sumf += d*vaddvq_s32(sumi);
  6914. }
  6915. *s = 0.125f * sumf;
  6916. #elif defined(__AVX2__)
  6917. const __m256i mone = _mm256_set1_epi8(1);
  6918. static const char block_sign_shuffle_mask_1[32] = {
  6919. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6920. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6921. };
  6922. static const char block_sign_shuffle_mask_2[32] = {
  6923. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6924. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6925. };
  6926. static const uint8_t bit_selector_mask_bytes[32] = {
  6927. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6928. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6929. };
  6930. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6931. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6932. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6933. #if QK_K == 64
  6934. static const uint8_t k_bit_helper[16] = {
  6935. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6936. };
  6937. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6938. const __m128i m511 = _mm_set1_epi16(511);
  6939. typedef union {
  6940. __m128i vec_index;
  6941. uint16_t index[8];
  6942. } index_t;
  6943. index_t idx;
  6944. __m256 accumf = _mm256_setzero_ps();
  6945. for (int i = 0; i < nb; ++i) {
  6946. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6947. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6948. idx.vec_index = _mm_and_si128(q2_data, m511);
  6949. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6950. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6951. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6952. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6953. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6954. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6955. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6956. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6957. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6958. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6959. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6960. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6961. __m256i signs;
  6962. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6963. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6964. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6965. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6966. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6967. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6968. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6969. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6970. const __m256i sc1 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[0] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[0] & 0xf)+1));
  6971. const __m256i sc2 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[1] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[1] & 0xf)+1));
  6972. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6973. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6974. }
  6975. *s = 0.125f * hsum_float_8(accumf);
  6976. #else
  6977. static const uint8_t k_bit_helper[32] = {
  6978. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6979. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6980. };
  6981. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6982. const __m256i m511 = _mm256_set1_epi16(511);
  6983. const __m128i m4 = _mm_set1_epi8(0xf);
  6984. const __m128i m1 = _mm_set1_epi8(1);
  6985. uint64_t aux64;
  6986. // somewhat hacky, but gives a significant boost in performance
  6987. __m256i aux_gindex;
  6988. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6989. __m256 accumf = _mm256_setzero_ps();
  6990. for (int i = 0; i < nb; ++i) {
  6991. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6992. const uint16_t * restrict q2 = x[i].qs;
  6993. const int8_t * restrict q8 = y[i].qs;
  6994. memcpy(&aux64, x[i].scales, 8);
  6995. __m128i stmp = _mm_set1_epi64x(aux64);
  6996. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6997. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6998. __m256i sumi1 = _mm256_setzero_si256();
  6999. __m256i sumi2 = _mm256_setzero_si256();
  7000. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7001. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  7002. aux_gindex = _mm256_and_si256(q2_data, m511);
  7003. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  7004. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  7005. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  7006. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7007. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  7008. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7009. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7010. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7011. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7012. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7013. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7014. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7015. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7016. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7017. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7018. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7019. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7020. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  7021. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  7022. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  7023. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  7024. __m256i signs;
  7025. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  7026. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7027. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7028. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  7029. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7030. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7031. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  7032. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7033. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  7034. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  7035. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7036. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  7037. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7038. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7039. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  7040. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  7041. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  7042. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  7043. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  7044. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  7045. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  7046. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  7047. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  7048. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  7049. }
  7050. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7051. }
  7052. *s = 0.125f * hsum_float_8(accumf);
  7053. #endif
  7054. #else
  7055. float sumf = 0.f;
  7056. for (int i = 0; i < nb; ++i) {
  7057. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7058. const uint16_t * restrict q2 = x[i].qs;
  7059. const uint8_t * restrict sc = x[i].scales;
  7060. const int8_t * restrict q8 = y[i].qs;
  7061. int32_t bsum = 0;
  7062. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7063. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  7064. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  7065. int32_t sumi = 0;
  7066. for (int l = 0; l < 2; ++l) {
  7067. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7068. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7069. for (int j = 0; j < 8; ++j) {
  7070. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7071. }
  7072. q8 += 8;
  7073. }
  7074. bsum += sumi * ls1;
  7075. sumi = 0;
  7076. for (int l = 2; l < 4; ++l) {
  7077. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7078. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7079. for (int j = 0; j < 8; ++j) {
  7080. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7081. }
  7082. q8 += 8;
  7083. }
  7084. bsum += sumi * ls2;
  7085. q2 += 4;
  7086. }
  7087. sumf += d * bsum;
  7088. }
  7089. *s = 0.125f * sumf;
  7090. #endif
  7091. }
  7092. void ggml_vec_dot_iq2_s_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7093. assert(n % QK_K == 0);
  7094. assert(nrc == 1);
  7095. UNUSED(nrc);
  7096. UNUSED(bx);
  7097. UNUSED(by);
  7098. UNUSED(bs);
  7099. const block_iq2_s * restrict x = vx;
  7100. const block_q8_K * restrict y = vy;
  7101. const int nb = n / QK_K;
  7102. #if defined(__ARM_NEON)
  7103. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7104. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7105. };
  7106. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7107. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7108. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7109. const uint8x16_t m1 = vdupq_n_u8(1);
  7110. const int32x4_t vzero = vdupq_n_s32(0);
  7111. uint8x16x2_t vs;
  7112. ggml_int8x16x4_t q2s;
  7113. ggml_int8x16x4_t q8b;
  7114. float sumf = 0;
  7115. for (int i = 0; i < nb; ++i) {
  7116. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7117. const uint8_t * restrict qs = x[i].qs;
  7118. const uint8_t * restrict qh = x[i].qh;
  7119. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7120. const int8_t * restrict q8 = y[i].qs;
  7121. int sumi1 = 0, sumi2 = 0;
  7122. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7123. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7124. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  7125. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  7126. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  7127. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  7128. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  7129. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  7130. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  7131. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  7132. qs += 8;
  7133. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | (signs[1] << 16)));
  7134. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7135. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7136. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7137. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7138. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  7139. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  7140. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | (signs[3] << 16)));
  7141. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7142. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7143. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7144. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7145. signs += 4;
  7146. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  7147. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  7148. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  7149. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  7150. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  7151. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  7152. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  7153. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  7154. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  7155. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  7156. }
  7157. sumf += d*(sumi1 + sumi2);
  7158. }
  7159. *s = 0.125f * sumf;
  7160. #elif defined(__AVX2__)
  7161. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7162. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7163. };
  7164. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7165. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7166. };
  7167. const __m128i m4 = _mm_set1_epi8(0xf);
  7168. const __m128i m1 = _mm_set1_epi8(1);
  7169. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7170. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7171. uint64_t aux64;
  7172. __m256 accumf = _mm256_setzero_ps();
  7173. for (int i = 0; i < nb; ++i) {
  7174. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7175. const uint8_t * restrict qs = x[i].qs;
  7176. const uint8_t * restrict qh = x[i].qh;
  7177. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7178. const int8_t * restrict q8 = y[i].qs;
  7179. memcpy(&aux64, x[i].scales, 8);
  7180. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7181. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7182. __m256i sumi1 = _mm256_setzero_si256();
  7183. __m256i sumi2 = _mm256_setzero_si256();
  7184. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7185. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7186. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7187. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7188. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7189. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7190. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7191. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7192. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7193. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7194. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7195. qs += 8;
  7196. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7197. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7198. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7199. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7200. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7201. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7202. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7203. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7204. signs += 4;
  7205. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7206. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7207. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7208. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7209. sumi1 = _mm256_add_epi32(sumi1, p1);
  7210. sumi2 = _mm256_add_epi32(sumi2, p2);
  7211. }
  7212. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7213. }
  7214. *s = 0.125f * hsum_float_8(accumf);
  7215. #else
  7216. float sumf = 0;
  7217. for (int i = 0; i < nb; i++) {
  7218. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7219. const int8_t * q8 = y[i].qs;
  7220. const uint8_t * qs = x[i].qs;
  7221. const uint8_t * qh = x[i].qh;
  7222. const uint8_t * signs = qs + QK_K/8;
  7223. int bsum = 0;
  7224. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7225. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7226. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7227. int sumi1 = 0, sumi2 = 0;
  7228. for (int l = 0; l < 2; ++l) {
  7229. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7230. for (int j = 0; j < 8; ++j) {
  7231. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7232. }
  7233. q8 += 8;
  7234. }
  7235. for (int l = 2; l < 4; ++l) {
  7236. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7237. for (int j = 0; j < 8; ++j) {
  7238. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7239. }
  7240. q8 += 8;
  7241. }
  7242. bsum += ls1 * sumi1 + ls2 * sumi2;
  7243. qs += 4;
  7244. signs += 4;
  7245. }
  7246. sumf += d * bsum;
  7247. }
  7248. *s = 0.125f * sumf;
  7249. #endif
  7250. }
  7251. void ggml_vec_dot_iq3_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7252. assert(n % QK_K == 0);
  7253. assert(nrc == 1);
  7254. UNUSED(nrc);
  7255. UNUSED(bx);
  7256. UNUSED(by);
  7257. UNUSED(bs);
  7258. const block_iq3_xxs * restrict x = vx;
  7259. const block_q8_K * restrict y = vy;
  7260. const int nb = n / QK_K;
  7261. #if defined(__ARM_NEON)
  7262. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7263. uint32_t aux32[2];
  7264. ggml_int8x16x4_t q3s;
  7265. ggml_int8x16x4_t q8b;
  7266. float sumf = 0;
  7267. for (int i = 0; i < nb; ++i) {
  7268. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7269. const uint8_t * restrict q3 = x[i].qs;
  7270. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7271. const int8_t * restrict q8 = y[i].qs;
  7272. float sumf1 = 0, sumf2 = 0;
  7273. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7274. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7275. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7276. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7277. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7278. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7279. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7280. q3 += 16;
  7281. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7282. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7283. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7284. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7285. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7286. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7287. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7288. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7289. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7290. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7291. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7292. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7293. }
  7294. sumf += d*(sumf1 + sumf2);
  7295. }
  7296. *s = 0.5f * sumf;
  7297. #elif defined(__AVX2__)
  7298. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7299. uint32_t aux32[2];
  7300. __m256 accumf = _mm256_setzero_ps();
  7301. for (int i = 0; i < nb; ++i) {
  7302. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7303. const uint8_t * restrict q3 = x[i].qs;
  7304. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7305. const int8_t * restrict q8 = y[i].qs;
  7306. __m256i sumi1 = _mm256_setzero_si256();
  7307. __m256i sumi2 = _mm256_setzero_si256();
  7308. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7309. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7310. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7311. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7312. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7313. q3 += 8;
  7314. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7315. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7316. q3 += 8;
  7317. memcpy(aux32, gas, 8); gas += 8;
  7318. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7319. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7320. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7321. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7322. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7323. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7324. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7325. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7326. const uint16_t ls1 = aux32[0] >> 28;
  7327. const uint16_t ls2 = aux32[1] >> 28;
  7328. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7329. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7330. sumi1 = _mm256_add_epi32(sumi1, p1);
  7331. sumi2 = _mm256_add_epi32(sumi2, p2);
  7332. }
  7333. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7334. }
  7335. *s = 0.25f * hsum_float_8(accumf);
  7336. #else
  7337. uint32_t aux32;
  7338. float sumf = 0.f;
  7339. for (int i = 0; i < nb; ++i) {
  7340. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7341. const uint8_t * restrict q3 = x[i].qs;
  7342. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7343. const int8_t * restrict q8 = y[i].qs;
  7344. int32_t bsum = 0;
  7345. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7346. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7347. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7348. int32_t sumi = 0;
  7349. for (int l = 0; l < 4; ++l) {
  7350. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7351. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7352. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7353. for (int j = 0; j < 4; ++j) {
  7354. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7355. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7356. }
  7357. q8 += 8;
  7358. }
  7359. q3 += 8;
  7360. bsum += sumi * ls;
  7361. }
  7362. sumf += d * bsum;
  7363. }
  7364. *s = 0.25f * sumf;
  7365. #endif
  7366. }
  7367. void ggml_vec_dot_iq3_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7368. assert(n % QK_K == 0);
  7369. assert(nrc == 1);
  7370. UNUSED(nrc);
  7371. UNUSED(bx);
  7372. UNUSED(by);
  7373. UNUSED(bs);
  7374. const block_iq3_s * restrict x = vx;
  7375. const block_q8_K * restrict y = vy;
  7376. const int nb = n / QK_K;
  7377. #if defined(__ARM_NEON)
  7378. typedef union {
  7379. uint16x8_t vec_index;
  7380. uint16_t index[8];
  7381. } vec_index_t;
  7382. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7383. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7384. };
  7385. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7386. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7387. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7388. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7389. const int16x8_t hshift = vld1q_s16(k_shift);
  7390. const uint16x8_t m256 = vdupq_n_u16(256);
  7391. const uint8x16_t m1 = vdupq_n_u8(1);
  7392. uint8x16x2_t vs;
  7393. ggml_int8x16x4_t q3s;
  7394. ggml_int8x16x4_t q8b;
  7395. vec_index_t idx;
  7396. #if QK_K == 256
  7397. uint32_t scales32[2];
  7398. const uint8_t * scales8 = (const uint8_t *)scales32;
  7399. #endif
  7400. float sumf = 0;
  7401. for (int i = 0; i < nb; ++i) {
  7402. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7403. const uint8_t * restrict qs = x[i].qs;
  7404. const uint8_t * restrict qh = x[i].qh;
  7405. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7406. const int8_t * restrict q8 = y[i].qs;
  7407. #if QK_K == 256
  7408. memcpy(scales32, x[i].scales, 4);
  7409. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7410. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7411. #endif
  7412. int sumi1 = 0, sumi2 = 0;
  7413. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7414. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7415. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7416. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7417. const uint32x4_t aux32x4_0 = {iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7418. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]};
  7419. const uint32x4_t aux32x4_1 = {iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7420. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]};
  7421. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7422. const uint32x4_t aux32x4_2 = {iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7423. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]};
  7424. const uint32x4_t aux32x4_3 = {iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7425. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]};
  7426. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | (signs[1] << 16)));
  7427. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7428. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7429. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7430. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7431. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7432. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7433. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | (signs[3] << 16)));
  7434. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7435. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7436. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7437. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7438. signs += 4;
  7439. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7440. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7441. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7442. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7443. #if QK_K == 256
  7444. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7445. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7446. #else
  7447. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7448. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7449. #endif
  7450. }
  7451. sumf += d*(sumi1 + sumi2);
  7452. }
  7453. *s = sumf;
  7454. #elif defined(__AVX2__)
  7455. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7456. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7457. };
  7458. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7459. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7460. };
  7461. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7462. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7463. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7464. const __m256i idx_mask = _mm256_set1_epi32(256);
  7465. typedef union {
  7466. __m256i vec[2];
  7467. uint32_t index[16];
  7468. } index_t;
  7469. index_t idx;
  7470. __m256 accumf = _mm256_setzero_ps();
  7471. for (int i = 0; i < nb; ++i) {
  7472. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7473. const uint8_t * restrict qs = x[i].qs;
  7474. const uint8_t * restrict qh = x[i].qh;
  7475. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7476. const int8_t * restrict q8 = y[i].qs;
  7477. __m256i sumi1 = _mm256_setzero_si256();
  7478. __m256i sumi2 = _mm256_setzero_si256();
  7479. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7480. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7481. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7482. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7483. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7484. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7485. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7486. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7487. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7488. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7489. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7490. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7491. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7492. const __m256i q2_1 = _mm256_set_epi32(
  7493. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7494. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7495. );
  7496. const __m256i q2_2 = _mm256_set_epi32(
  7497. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7498. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7499. );
  7500. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7501. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7502. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7503. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7504. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7505. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7506. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7507. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7508. signs += 4;
  7509. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7510. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7511. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7512. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7513. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7514. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7515. sumi1 = _mm256_add_epi32(sumi1, p1);
  7516. sumi2 = _mm256_add_epi32(sumi2, p2);
  7517. }
  7518. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7519. }
  7520. *s = hsum_float_8(accumf);
  7521. #else
  7522. float sumf = 0.f;
  7523. for (int i = 0; i < nb; ++i) {
  7524. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7525. const uint8_t * restrict qs = x[i].qs;
  7526. const uint8_t * restrict qh = x[i].qh;
  7527. const uint8_t * restrict signs = x[i].signs;
  7528. const int8_t * restrict q8 = y[i].qs;
  7529. int32_t bsum = 0;
  7530. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7531. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7532. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7533. int32_t sumi = 0;
  7534. for (int l = 0; l < 4; ++l) {
  7535. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7536. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7537. for (int j = 0; j < 4; ++j) {
  7538. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7539. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7540. }
  7541. q8 += 8;
  7542. }
  7543. qs += 8;
  7544. signs += 4;
  7545. bsum += sumi * ls1;
  7546. sumi = 0;
  7547. for (int l = 0; l < 4; ++l) {
  7548. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7549. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7550. for (int j = 0; j < 4; ++j) {
  7551. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7552. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7553. }
  7554. q8 += 8;
  7555. }
  7556. qs += 8;
  7557. signs += 4;
  7558. bsum += sumi * ls2;
  7559. }
  7560. sumf += d * bsum;
  7561. }
  7562. *s = sumf;
  7563. #endif
  7564. }
  7565. #ifdef __AVX2__
  7566. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7567. const __m256i ax = _mm256_sign_epi8(x, x);
  7568. const __m256i sy = _mm256_sign_epi8(y, x);
  7569. return _mm256_maddubs_epi16(ax, sy);
  7570. }
  7571. #endif
  7572. void ggml_vec_dot_iq1_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7573. assert(n % QK_K == 0);
  7574. assert(nrc == 1);
  7575. UNUSED(nrc);
  7576. UNUSED(bx);
  7577. UNUSED(by);
  7578. UNUSED(bs);
  7579. const block_iq1_s * restrict x = vx;
  7580. const block_q8_K * restrict y = vy;
  7581. const int nb = n / QK_K;
  7582. #if defined __ARM_NEON
  7583. ggml_int8x16x4_t q1b;
  7584. ggml_int8x16x4_t q8b;
  7585. float sumf = 0;
  7586. for (int i = 0; i < nb; ++i) {
  7587. const int8_t * q8 = y[i].qs;
  7588. const uint8_t * qs = x[i].qs;
  7589. const uint16_t * qh = x[i].qh;
  7590. int sumi1 = 0, sumi2 = 0;
  7591. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7592. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7593. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7594. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7595. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7596. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7597. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7598. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7599. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7600. qs += 8;
  7601. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7602. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
  7603. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
  7604. sumi1 += vaddvq_s32(p1) * (2*(qh[ib+0] >> 12) + 1);
  7605. sumi2 += vaddvq_s32(p2) * (2*(qh[ib+1] >> 12) + 1);
  7606. }
  7607. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2);
  7608. }
  7609. *s = sumf;
  7610. #elif defined __AVX2__
  7611. __m256 accum = _mm256_setzero_ps();
  7612. for (int i = 0; i < nb; ++i) {
  7613. const int8_t * q8 = y[i].qs;
  7614. const uint8_t * qs = x[i].qs;
  7615. const uint16_t * qh = x[i].qh;
  7616. __m256i sumi = _mm256_setzero_si256();
  7617. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7618. const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
  7619. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7620. const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
  7621. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7622. qs += 8;
  7623. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7624. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7625. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7626. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7627. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*(qh[ib+0] >> 12) + 1));
  7628. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*(qh[ib+1] >> 12) + 1));
  7629. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7630. }
  7631. accum = _mm256_fmadd_ps(_mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d)), _mm256_cvtepi32_ps(sumi), accum);
  7632. }
  7633. *s = hsum_float_8(accum);
  7634. #else
  7635. float sumf = 0;
  7636. for (int i = 0; i < nb; i++) {
  7637. const int8_t * q8 = y[i].qs;
  7638. const uint8_t * qs = x[i].qs;
  7639. const uint16_t * qh = x[i].qh;
  7640. int sumi = 0;
  7641. for (int ib = 0; ib < QK_K/32; ++ib) {
  7642. const int ls = 2*(qh[ib] >> 12) + 1;
  7643. int lsum = 0;
  7644. for (int l = 0; l < 4; ++l) {
  7645. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7646. for (int j = 0; j < 8; ++j) {
  7647. lsum += q8[j] * grid[j];
  7648. }
  7649. q8 += 8;
  7650. }
  7651. sumi += ls * lsum;
  7652. qs += 4;
  7653. }
  7654. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * sumi;
  7655. }
  7656. *s = sumf;
  7657. #endif
  7658. }
  7659. void ggml_vec_dot_iq4_nl_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7660. assert(nrc == 1);
  7661. UNUSED(nrc);
  7662. UNUSED(bx);
  7663. UNUSED(by);
  7664. UNUSED(bs);
  7665. assert(n % QK4_NL == 0);
  7666. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7667. const block_iq4_nl * restrict x = vx;
  7668. const block_q8_0 * restrict y = vy;
  7669. const int nb = n / QK4_NL;
  7670. #if defined __ARM_NEON
  7671. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7672. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7673. uint8x16x2_t q4bits;
  7674. int8x16x4_t q4b;
  7675. int8x16x4_t q8b;
  7676. int32x4_t prod_1, prod_2;
  7677. float sumf = 0;
  7678. for (int ib = 0; ib < nb; ib += 2) {
  7679. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7680. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7681. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7682. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7683. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7684. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7685. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7686. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7687. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7688. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7689. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7690. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7691. sumf +=
  7692. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7693. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7694. }
  7695. *s = sumf;
  7696. #elif defined __AVX2__
  7697. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7698. const __m128i m4b = _mm_set1_epi8(0x0f);
  7699. const __m256i mone = _mm256_set1_epi16(1);
  7700. __m256 accum1 = _mm256_setzero_ps();
  7701. __m256 accum2 = _mm256_setzero_ps();
  7702. for (int ib = 0; ib < nb; ib += 2) {
  7703. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7704. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7705. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7706. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7707. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7708. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7709. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7710. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7711. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7712. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7713. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7714. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7715. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7716. _mm256_cvtepi32_ps(p_1), accum1);
  7717. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7718. _mm256_cvtepi32_ps(p_2), accum2);
  7719. y += 2;
  7720. x += 2;
  7721. }
  7722. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7723. #else
  7724. float sumf = 0;
  7725. for (int ib = 0; ib < nb; ++ib) {
  7726. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7727. int sumi1 = 0, sumi2 = 0;
  7728. for (int j = 0; j < QK4_NL/2; ++j) {
  7729. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7730. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7731. }
  7732. sumf += d * (sumi1 + sumi2);
  7733. }
  7734. *s = sumf;
  7735. #endif
  7736. }
  7737. void ggml_vec_dot_iq4_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7738. assert(nrc == 1);
  7739. UNUSED(nrc);
  7740. UNUSED(bx);
  7741. UNUSED(by);
  7742. UNUSED(bs);
  7743. assert(n % QK_K == 0);
  7744. #if QK_K == 64
  7745. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7746. #else
  7747. const block_iq4_xs * restrict x = vx;
  7748. const block_q8_K * restrict y = vy;
  7749. const int nb = n / QK_K;
  7750. #if defined __ARM_NEON
  7751. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7752. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7753. ggml_uint8x16x2_t q4bits;
  7754. ggml_int8x16x4_t q4b;
  7755. ggml_int8x16x4_t q8b;
  7756. int32x4_t prod_1, prod_2;
  7757. float sumf = 0;
  7758. for (int ibl = 0; ibl < nb; ++ibl) {
  7759. const int8_t * q8 = y[ibl].qs;
  7760. const uint8_t * q4 = x[ibl].qs;
  7761. uint16_t h = x[ibl].scales_h;
  7762. int sumi1 = 0, sumi2 = 0;
  7763. for (int ib = 0; ib < QK_K/64; ++ib) {
  7764. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7765. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7766. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7767. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7768. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7769. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7770. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7771. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7772. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7773. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7774. h >>= 4;
  7775. sumi1 += vaddvq_s32(prod_1) * ls1;
  7776. sumi2 += vaddvq_s32(prod_2) * ls2;
  7777. }
  7778. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7779. }
  7780. *s = sumf;
  7781. #elif defined __AVX2__
  7782. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7783. const __m128i m4b = _mm_set1_epi8(0x0f);
  7784. __m256 accum = _mm256_setzero_ps();
  7785. for (int ibl = 0; ibl < nb; ++ibl) {
  7786. const uint8_t * qs = x[ibl].qs;
  7787. const int8_t * q8 = y[ibl].qs;
  7788. uint16_t sh = x[ibl].scales_h;
  7789. __m256i sumi1 = _mm256_setzero_si256();
  7790. __m256i sumi2 = _mm256_setzero_si256();
  7791. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7792. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7793. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7794. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7795. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7796. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7797. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7798. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7799. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7800. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7801. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7802. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7803. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7804. sh >>= 4;
  7805. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7806. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7807. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7808. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7809. }
  7810. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7811. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7812. }
  7813. *s = hsum_float_8(accum);
  7814. #else
  7815. float sumf = 0;
  7816. for (int ibl = 0; ibl < nb; ++ibl) {
  7817. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7818. uint16_t h = x[ibl].scales_h;
  7819. const uint8_t * qs = x[ibl].qs;
  7820. const int8_t * q8 = y[ibl].qs;
  7821. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7822. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7823. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7824. h >>= 4;
  7825. const float d1 = d4d8*(ls1 - 32);
  7826. const float d2 = d4d8*(ls2 - 32);
  7827. int sumi1 = 0, sumi2 = 0;
  7828. for (int j = 0; j < 16; ++j) {
  7829. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7830. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7831. }
  7832. sumf += d1 * (sumi1 + sumi2);
  7833. qs += 16;
  7834. q8 += 32;
  7835. sumi1 = sumi2 = 0;
  7836. for (int j = 0; j < 16; ++j) {
  7837. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7838. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7839. }
  7840. sumf += d2 * (sumi1 + sumi2);
  7841. qs += 16;
  7842. q8 += 32;
  7843. }
  7844. }
  7845. *s = sumf;
  7846. #endif
  7847. #endif
  7848. }
  7849. // ================================ IQ2 quantization =============================================
  7850. typedef struct {
  7851. uint64_t * grid;
  7852. int * map;
  7853. uint16_t * neighbours;
  7854. } iq2_entry_t;
  7855. static iq2_entry_t iq2_data[4] = {
  7856. {NULL, NULL, NULL},
  7857. {NULL, NULL, NULL},
  7858. {NULL, NULL, NULL},
  7859. {NULL, NULL, NULL},
  7860. };
  7861. static inline int iq2_data_index(enum ggml_type type) {
  7862. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  7863. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7864. type == GGML_TYPE_IQ2_XS ? 1 :
  7865. type == GGML_TYPE_IQ1_S ? 2 : 3;
  7866. }
  7867. static inline int iq2_grid_size(enum ggml_type type) {
  7868. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  7869. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7870. type == GGML_TYPE_IQ2_XS ? 512 :
  7871. type == GGML_TYPE_IQ1_S ? NGRID_IQ1S : 1024;
  7872. }
  7873. static int iq2_compare_func(const void * left, const void * right) {
  7874. const int * l = (const int *)left;
  7875. const int * r = (const int *)right;
  7876. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7877. }
  7878. void iq2xs_init_impl(enum ggml_type type) {
  7879. const int gindex = iq2_data_index(type);
  7880. const int grid_size = iq2_grid_size(type);
  7881. if (iq2_data[gindex].grid) {
  7882. return;
  7883. }
  7884. static const uint16_t kgrid_2bit_256[256] = {
  7885. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7886. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7887. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7888. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7889. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7890. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7891. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7892. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7893. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7894. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7895. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7896. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7897. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7898. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7899. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7900. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7901. };
  7902. static const uint16_t kgrid_2bit_512[512] = {
  7903. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7904. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7905. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7906. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7907. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7908. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7909. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7910. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7911. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7912. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7913. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7914. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7915. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7916. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7917. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7918. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7919. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7920. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7921. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7922. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7923. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7924. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7925. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7926. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7927. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7928. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7929. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7930. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7931. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7932. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7933. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7934. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7935. };
  7936. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7937. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7938. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7939. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7940. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7941. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7942. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7943. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7944. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7945. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7946. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7947. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7948. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7949. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7950. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7951. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7952. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7953. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  7954. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  7955. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  7956. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  7957. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  7958. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  7959. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  7960. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  7961. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  7962. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  7963. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  7964. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  7965. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  7966. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  7967. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  7968. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  7969. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  7970. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  7971. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  7972. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  7973. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  7974. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  7975. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  7976. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  7977. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  7978. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  7979. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  7980. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  7981. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  7982. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  7983. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  7984. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  7985. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  7986. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  7987. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  7988. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  7989. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  7990. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  7991. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  7992. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  7993. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  7994. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  7995. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  7996. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  7997. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  7998. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  7999. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8000. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8001. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8002. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8003. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8004. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8005. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8006. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8007. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8008. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8009. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8010. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8011. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8012. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8013. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8014. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8015. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8016. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8017. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8018. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8019. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8020. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8021. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8022. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8023. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8024. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8025. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8026. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8027. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8028. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8029. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8030. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8031. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8032. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8033. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8034. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8035. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8036. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8037. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8038. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8039. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8040. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8041. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8042. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8043. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8044. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8045. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8046. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8047. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8048. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8049. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8050. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8051. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8052. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8053. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8054. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8055. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8056. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8057. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8058. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8059. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8060. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8061. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8062. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8063. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8064. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8065. };
  8066. static const uint16_t kgrid_2bit_1024[1024] = {
  8067. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8068. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8069. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8070. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8071. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8072. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8073. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8074. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8075. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8076. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8077. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8078. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8079. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8080. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8081. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8082. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8083. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8084. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8085. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8086. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8087. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8088. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8089. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8090. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8091. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8092. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8093. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8094. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8095. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8096. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8097. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8098. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8099. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8100. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8101. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8102. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8103. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8104. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8105. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8106. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8107. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8108. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8109. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8110. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8111. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8112. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8113. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8114. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8115. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8116. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8117. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8118. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8119. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8120. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8121. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8122. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8123. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8124. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8125. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8126. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8127. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8128. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8129. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8130. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8131. };
  8132. const int kmap_size = 43692;
  8133. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8134. const int nwant = type == GGML_TYPE_IQ1_S ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8135. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8136. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8137. type == GGML_TYPE_IQ1_S ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8138. uint64_t * kgrid_q2xs;
  8139. int * kmap_q2xs;
  8140. uint16_t * kneighbors_q2xs;
  8141. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8142. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8143. for (int k = 0; k < grid_size; ++k) {
  8144. int8_t * pos = (int8_t *)(the_grid + k);
  8145. for (int i = 0; i < 8; ++i) {
  8146. int l = (kgrid[k] >> 2*i) & 0x3;
  8147. pos[i] = 2*l + 1;
  8148. }
  8149. }
  8150. kgrid_q2xs = the_grid;
  8151. iq2_data[gindex].grid = the_grid;
  8152. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8153. iq2_data[gindex].map = kmap_q2xs;
  8154. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8155. uint64_t aux64;
  8156. uint8_t * aux8 = (uint8_t *)&aux64;
  8157. for (int i = 0; i < grid_size; ++i) {
  8158. aux64 = kgrid_q2xs[i];
  8159. uint16_t index = 0;
  8160. for (int k=0; k<8; ++k) {
  8161. uint16_t q = (aux8[k] - 1)/2;
  8162. index |= (q << 2*k);
  8163. }
  8164. kmap_q2xs[index] = i;
  8165. }
  8166. int8_t pos[8];
  8167. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8168. int num_neighbors = 0, num_not_in_map = 0;
  8169. for (int i = 0; i < kmap_size; ++i) {
  8170. if (kmap_q2xs[i] >= 0) continue;
  8171. ++num_not_in_map;
  8172. for (int k = 0; k < 8; ++k) {
  8173. int l = (i >> 2*k) & 0x3;
  8174. pos[k] = 2*l + 1;
  8175. }
  8176. for (int j = 0; j < grid_size; ++j) {
  8177. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8178. int d2 = 0;
  8179. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8180. dist2[2*j+0] = d2;
  8181. dist2[2*j+1] = j;
  8182. }
  8183. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8184. int n = 0; int d2 = dist2[0];
  8185. int nhave = 1;
  8186. for (int j = 0; j < grid_size; ++j) {
  8187. if (dist2[2*j] > d2) {
  8188. if (nhave == nwant) break;
  8189. d2 = dist2[2*j];
  8190. ++nhave;
  8191. }
  8192. ++n;
  8193. }
  8194. num_neighbors += n;
  8195. }
  8196. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8197. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8198. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8199. int counter = 0;
  8200. for (int i = 0; i < kmap_size; ++i) {
  8201. if (kmap_q2xs[i] >= 0) continue;
  8202. for (int k = 0; k < 8; ++k) {
  8203. int l = (i >> 2*k) & 0x3;
  8204. pos[k] = 2*l + 1;
  8205. }
  8206. for (int j = 0; j < grid_size; ++j) {
  8207. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8208. int d2 = 0;
  8209. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8210. dist2[2*j+0] = d2;
  8211. dist2[2*j+1] = j;
  8212. }
  8213. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8214. kmap_q2xs[i] = -(counter + 1);
  8215. int d2 = dist2[0];
  8216. uint16_t * start = &kneighbors_q2xs[counter++];
  8217. int n = 0, nhave = 1;
  8218. for (int j = 0; j < grid_size; ++j) {
  8219. if (dist2[2*j] > d2) {
  8220. if (nhave == nwant) break;
  8221. d2 = dist2[2*j];
  8222. ++nhave;
  8223. }
  8224. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8225. ++n;
  8226. }
  8227. *start = n;
  8228. }
  8229. free(dist2);
  8230. }
  8231. void iq2xs_free_impl(enum ggml_type type) {
  8232. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  8233. const int gindex = iq2_data_index(type);
  8234. if (iq2_data[gindex].grid) {
  8235. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8236. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8237. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8238. }
  8239. }
  8240. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8241. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8242. int num_neighbors = neighbours[0];
  8243. GGML_ASSERT(num_neighbors > 0);
  8244. float best_d2 = FLT_MAX;
  8245. int grid_index = -1;
  8246. for (int j = 1; j <= num_neighbors; ++j) {
  8247. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8248. float d2 = 0;
  8249. for (int i = 0; i < 8; ++i) {
  8250. float q = pg[i];
  8251. float diff = scale*q - xval[i];
  8252. d2 += weight[i]*diff*diff;
  8253. }
  8254. if (d2 < best_d2) {
  8255. best_d2 = d2; grid_index = neighbours[j];
  8256. }
  8257. }
  8258. GGML_ASSERT(grid_index >= 0);
  8259. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8260. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8261. return grid_index;
  8262. }
  8263. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8264. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8265. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8266. const int * kmap_q2xs = iq2_data[gindex].map;
  8267. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8268. GGML_ASSERT(quant_weights && "missing quantization weights");
  8269. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8270. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8271. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8272. GGML_ASSERT(n%QK_K == 0);
  8273. const int kMaxQ = 3;
  8274. const int nbl = n/QK_K;
  8275. block_iq2_xxs * y = vy;
  8276. float scales[QK_K/32];
  8277. float weight[32];
  8278. float xval[32];
  8279. int8_t L[32];
  8280. int8_t Laux[32];
  8281. float waux[32];
  8282. uint8_t block_signs[4];
  8283. uint32_t q2[2*(QK_K/32)];
  8284. for (int ibl = 0; ibl < nbl; ++ibl) {
  8285. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8286. memset(q2, 0, QK_K/4);
  8287. float max_scale = 0;
  8288. const float * xbl = x + QK_K*ibl;
  8289. float sumx2 = 0;
  8290. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8291. float sigma2 = sumx2/QK_K;
  8292. for (int ib = 0; ib < QK_K/32; ++ib) {
  8293. const float * xb = xbl + 32*ib;
  8294. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8295. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8296. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8297. for (int k = 0; k < 4; ++k) {
  8298. int nflip = 0;
  8299. uint8_t s = 0;
  8300. for (int i = 0; i < 8; ++i) {
  8301. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8302. else {
  8303. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8304. }
  8305. }
  8306. if (nflip%2) {
  8307. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8308. for (int i = 1; i < 8; ++i) {
  8309. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8310. if (ax < min) {
  8311. min = ax; imin = i;
  8312. }
  8313. }
  8314. xval[8*k+imin] = -xval[8*k+imin];
  8315. s ^= (1 << imin);
  8316. }
  8317. block_signs[k] = s & 127;
  8318. }
  8319. float max = xval[0];
  8320. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8321. if (!max) {
  8322. scales[ib] = 0;
  8323. memset(L, 0, 32);
  8324. continue;
  8325. }
  8326. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8327. float eff_max = scale*kMaxQ;
  8328. float best = 0;
  8329. for (int is = -6; is <= 6; ++is) {
  8330. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8331. float this_scale = 1/id;
  8332. for (int k = 0; k < 4; ++k) {
  8333. for (int i = 0; i < 8; ++i) {
  8334. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8335. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8336. }
  8337. uint16_t u = 0;
  8338. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8339. int grid_index = kmap_q2xs[u];
  8340. if (grid_index < 0) {
  8341. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8342. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8343. }
  8344. }
  8345. float sumqx = 0, sumq2 = 0;
  8346. for (int i = 0; i < 32; ++i) {
  8347. float w = weight[i];
  8348. float q = 2*Laux[i] + 1;
  8349. sumqx += w*xval[i]*q;
  8350. sumq2 += w*q*q;
  8351. }
  8352. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8353. scale = sumqx/sumq2; best = scale*sumqx;
  8354. memcpy(L, Laux, 32);
  8355. }
  8356. }
  8357. if (scale > 0) {
  8358. float id = 1/scale;
  8359. for (int k = 0; k < 4; ++k) {
  8360. uint16_t u = 0;
  8361. for (int i = 0; i < 8; ++i) {
  8362. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8363. l = MAX(0, MIN(kMaxQ-1, l));
  8364. u |= (l << 2*i);
  8365. }
  8366. int grid_index = kmap_q2xs[u];
  8367. if (grid_index < 0) {
  8368. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8369. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8370. }
  8371. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8372. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8373. }
  8374. float sumqx = 0, sumq2 = 0;
  8375. for (int i = 0; i < 32; ++i) {
  8376. float w = weight[i];
  8377. float q = 2*L[i] + 1;
  8378. sumqx += w*xval[i]*q;
  8379. sumq2 += w*q*q;
  8380. }
  8381. if (sumq2 > 0) scale = sumqx/sumq2;
  8382. }
  8383. if (scale < 0) {
  8384. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8385. // and correspondingly flip quant signs.
  8386. scale = -scale;
  8387. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8388. }
  8389. for (int k = 0; k < 4; ++k) {
  8390. uint16_t u = 0;
  8391. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8392. int grid_index = kmap_q2xs[u];
  8393. if (grid_index < 0) {
  8394. printf("Oops: found point %u not on grid:", u);
  8395. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8396. printf("\n");
  8397. GGML_ASSERT(false);
  8398. }
  8399. q2[2*ib+0] |= (grid_index << 8*k);
  8400. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8401. }
  8402. GGML_ASSERT(scale >= 0);
  8403. scales[ib] = scale;
  8404. max_scale = MAX(max_scale, scale);
  8405. }
  8406. if (!max_scale) {
  8407. memset(y[ibl].qs, 0, QK_K/4);
  8408. continue;
  8409. }
  8410. float d = max_scale/31;
  8411. y[ibl].d = GGML_FP32_TO_FP16(d);
  8412. float id = 1/d;
  8413. for (int ib = 0; ib < QK_K/32; ++ib) {
  8414. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8415. l = MAX(0, MIN(15, l));
  8416. q2[2*ib+1] |= ((uint32_t)l << 28);
  8417. }
  8418. memcpy(y[ibl].qs, q2, QK_K/4);
  8419. }
  8420. }
  8421. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8422. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8423. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8424. const int * kmap_q2xs = iq2_data[gindex].map;
  8425. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8426. GGML_ASSERT(quant_weights && "missing quantization weights");
  8427. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8428. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8429. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8430. GGML_ASSERT(n%QK_K == 0);
  8431. const int kMaxQ = 3;
  8432. const int nbl = n/QK_K;
  8433. block_iq2_xs * y = vy;
  8434. float scales[QK_K/16];
  8435. float weight[16];
  8436. float xval[16];
  8437. int8_t L[16];
  8438. int8_t Laux[16];
  8439. float waux[16];
  8440. bool is_on_grid[2];
  8441. bool is_on_grid_aux[2];
  8442. uint8_t block_signs[2];
  8443. uint16_t q2[2*(QK_K/16)];
  8444. for (int ibl = 0; ibl < nbl; ++ibl) {
  8445. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8446. memset(q2, 0, QK_K/4);
  8447. memset(y[ibl].scales, 0, QK_K/32);
  8448. float max_scale = 0;
  8449. const float * xbl = x + QK_K*ibl;
  8450. float sumx2 = 0;
  8451. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8452. float sigma2 = sumx2/QK_K;
  8453. for (int ib = 0; ib < QK_K/16; ++ib) {
  8454. const float * xb = xbl + 16*ib;
  8455. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8456. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8457. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8458. for (int k = 0; k < 2; ++k) {
  8459. int nflip = 0;
  8460. uint8_t s = 0;
  8461. for (int i = 0; i < 8; ++i) {
  8462. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8463. else {
  8464. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8465. }
  8466. }
  8467. if (nflip%2) {
  8468. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8469. for (int i = 1; i < 8; ++i) {
  8470. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8471. if (ax < min) {
  8472. min = ax; imin = i;
  8473. }
  8474. }
  8475. xval[8*k+imin] = -xval[8*k+imin];
  8476. s ^= (1 << imin);
  8477. }
  8478. block_signs[k] = s & 127;
  8479. }
  8480. float max = xval[0];
  8481. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8482. if (!max) {
  8483. scales[ib] = 0;
  8484. memset(L, 0, 16);
  8485. continue;
  8486. }
  8487. float best = 0;
  8488. float scale = max/(2*kMaxQ-1);
  8489. is_on_grid[0] = is_on_grid[1] = true;
  8490. for (int is = -9; is <= 9; ++is) {
  8491. float id = (2*kMaxQ-1+is*0.1f)/max;
  8492. float this_scale = 1/id;
  8493. for (int k = 0; k < 2; ++k) {
  8494. for (int i = 0; i < 8; ++i) {
  8495. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8496. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8497. }
  8498. uint16_t u = 0;
  8499. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8500. int grid_index = kmap_q2xs[u];
  8501. is_on_grid_aux[k] = true;
  8502. if (grid_index < 0) {
  8503. is_on_grid_aux[k] = false;
  8504. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8505. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8506. }
  8507. }
  8508. float sumqx = 0, sumq2 = 0;
  8509. for (int i = 0; i < 16; ++i) {
  8510. float w = weight[i];
  8511. float q = 2*Laux[i] + 1;
  8512. sumqx += w*xval[i]*q;
  8513. sumq2 += w*q*q;
  8514. }
  8515. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8516. scale = sumqx/sumq2; best = scale*sumqx;
  8517. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8518. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8519. }
  8520. }
  8521. int n_not_ongrid = 0;
  8522. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8523. if (n_not_ongrid > 0 && scale > 0) {
  8524. float id = 1/scale;
  8525. for (int k = 0; k < 2; ++k) {
  8526. if (is_on_grid[k]) continue;
  8527. uint16_t u = 0;
  8528. for (int i = 0; i < 8; ++i) {
  8529. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8530. l = MAX(0, MIN(kMaxQ-1, l));
  8531. u |= (l << 2*i);
  8532. L[8*k + i] = l;
  8533. }
  8534. int grid_index = kmap_q2xs[u];
  8535. if (grid_index < 0) {
  8536. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8537. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8538. }
  8539. }
  8540. float sumqx = 0, sumq2 = 0;
  8541. for (int i = 0; i < 16; ++i) {
  8542. float w = weight[i];
  8543. float q = 2*L[i] + 1;
  8544. sumqx += w*xval[i]*q;
  8545. sumq2 += w*q*q;
  8546. }
  8547. if (sumq2 > 0) scale = sumqx/sumq2;
  8548. }
  8549. if (scale < 0) {
  8550. scale = -scale;
  8551. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8552. }
  8553. for (int k = 0; k < 2; ++k) {
  8554. uint16_t u = 0;
  8555. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8556. int grid_index = kmap_q2xs[u];
  8557. if (grid_index < 0) {
  8558. printf("Oops: found point %u not on grid:", u);
  8559. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8560. printf("\n");
  8561. GGML_ASSERT(false);
  8562. }
  8563. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8564. }
  8565. GGML_ASSERT(scale >= 0);
  8566. scales[ib] = scale;
  8567. max_scale = MAX(max_scale, scale);
  8568. }
  8569. if (!max_scale) {
  8570. memset(y[ibl].qs, 0, QK_K/4);
  8571. continue;
  8572. }
  8573. float d = max_scale/31;
  8574. y[ibl].d = GGML_FP32_TO_FP16(d);
  8575. float id = 1/d;
  8576. for (int ib = 0; ib < QK_K/16; ++ib) {
  8577. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8578. l = MAX(0, MIN(15, l));
  8579. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8580. else y[ibl].scales[ib/2] |= (l << 4);
  8581. }
  8582. memcpy(y[ibl].qs, q2, QK_K/4);
  8583. }
  8584. }
  8585. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8586. GGML_ASSERT(n_per_row%QK_K == 0);
  8587. int nblock = n_per_row/QK_K;
  8588. char * qrow = (char *)dst;
  8589. for (int row = 0; row < nrow; ++row) {
  8590. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8591. src += n_per_row;
  8592. qrow += nblock*sizeof(block_iq2_xxs);
  8593. }
  8594. return nrow * nblock * sizeof(block_iq2_xxs);
  8595. }
  8596. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8597. GGML_ASSERT(n_per_row%QK_K == 0);
  8598. int nblock = n_per_row/QK_K;
  8599. char * qrow = (char *)dst;
  8600. for (int row = 0; row < nrow; ++row) {
  8601. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8602. src += n_per_row;
  8603. qrow += nblock*sizeof(block_iq2_xs);
  8604. }
  8605. return nrow * nblock * sizeof(block_iq2_xs);
  8606. }
  8607. //
  8608. // ============================================= 3-bit using D4 lattice
  8609. //
  8610. typedef struct {
  8611. uint32_t * grid;
  8612. int * map;
  8613. uint16_t * neighbours;
  8614. } iq3_entry_t;
  8615. static iq3_entry_t iq3_data[2] = {
  8616. {NULL, NULL, NULL},
  8617. {NULL, NULL, NULL},
  8618. };
  8619. static inline int iq3_data_index(int grid_size) {
  8620. (void)grid_size;
  8621. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8622. return grid_size == 256 ? 0 : 1;
  8623. }
  8624. static int iq3_compare_func(const void * left, const void * right) {
  8625. const int * l = (const int *)left;
  8626. const int * r = (const int *)right;
  8627. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8628. }
  8629. void iq3xs_init_impl(int grid_size) {
  8630. const int gindex = iq3_data_index(grid_size);
  8631. if (iq3_data[gindex].grid) {
  8632. return;
  8633. }
  8634. static const uint16_t kgrid_256[256] = {
  8635. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8636. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8637. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8638. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8639. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8640. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8641. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8642. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8643. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8644. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8645. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8646. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8647. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8648. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8649. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8650. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8651. };
  8652. static const uint16_t kgrid_512[512] = {
  8653. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8654. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8655. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8656. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8657. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8658. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8659. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8660. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8661. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8662. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8663. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8664. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8665. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8666. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8667. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8668. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8669. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8670. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8671. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8672. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8673. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8674. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8675. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8676. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8677. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8678. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8679. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8680. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8681. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8682. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8683. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8684. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8685. };
  8686. const int kmap_size = 4096;
  8687. const int nwant = grid_size == 256 ? 2 : 3;
  8688. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8689. uint32_t * kgrid_q3xs;
  8690. int * kmap_q3xs;
  8691. uint16_t * kneighbors_q3xs;
  8692. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8693. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8694. for (int k = 0; k < grid_size; ++k) {
  8695. int8_t * pos = (int8_t *)(the_grid + k);
  8696. for (int i = 0; i < 4; ++i) {
  8697. int l = (kgrid[k] >> 3*i) & 0x7;
  8698. pos[i] = 2*l + 1;
  8699. }
  8700. }
  8701. kgrid_q3xs = the_grid;
  8702. iq3_data[gindex].grid = the_grid;
  8703. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8704. iq3_data[gindex].map = kmap_q3xs;
  8705. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8706. uint32_t aux32;
  8707. uint8_t * aux8 = (uint8_t *)&aux32;
  8708. for (int i = 0; i < grid_size; ++i) {
  8709. aux32 = kgrid_q3xs[i];
  8710. uint16_t index = 0;
  8711. for (int k=0; k<4; ++k) {
  8712. uint16_t q = (aux8[k] - 1)/2;
  8713. index |= (q << 3*k);
  8714. }
  8715. kmap_q3xs[index] = i;
  8716. }
  8717. int8_t pos[4];
  8718. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8719. int num_neighbors = 0, num_not_in_map = 0;
  8720. for (int i = 0; i < kmap_size; ++i) {
  8721. if (kmap_q3xs[i] >= 0) continue;
  8722. ++num_not_in_map;
  8723. for (int k = 0; k < 4; ++k) {
  8724. int l = (i >> 3*k) & 0x7;
  8725. pos[k] = 2*l + 1;
  8726. }
  8727. for (int j = 0; j < grid_size; ++j) {
  8728. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8729. int d2 = 0;
  8730. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8731. dist2[2*j+0] = d2;
  8732. dist2[2*j+1] = j;
  8733. }
  8734. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8735. int n = 0; int d2 = dist2[0];
  8736. int nhave = 1;
  8737. for (int j = 0; j < grid_size; ++j) {
  8738. if (dist2[2*j] > d2) {
  8739. if (nhave == nwant) break;
  8740. d2 = dist2[2*j];
  8741. ++nhave;
  8742. }
  8743. ++n;
  8744. }
  8745. num_neighbors += n;
  8746. }
  8747. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8748. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8749. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8750. int counter = 0;
  8751. for (int i = 0; i < kmap_size; ++i) {
  8752. if (kmap_q3xs[i] >= 0) continue;
  8753. for (int k = 0; k < 4; ++k) {
  8754. int l = (i >> 3*k) & 0x7;
  8755. pos[k] = 2*l + 1;
  8756. }
  8757. for (int j = 0; j < grid_size; ++j) {
  8758. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8759. int d2 = 0;
  8760. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8761. dist2[2*j+0] = d2;
  8762. dist2[2*j+1] = j;
  8763. }
  8764. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8765. kmap_q3xs[i] = -(counter + 1);
  8766. int d2 = dist2[0];
  8767. uint16_t * start = &kneighbors_q3xs[counter++];
  8768. int n = 0, nhave = 1;
  8769. for (int j = 0; j < grid_size; ++j) {
  8770. if (dist2[2*j] > d2) {
  8771. if (nhave == nwant) break;
  8772. d2 = dist2[2*j];
  8773. ++nhave;
  8774. }
  8775. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8776. ++n;
  8777. }
  8778. *start = n;
  8779. }
  8780. free(dist2);
  8781. }
  8782. void iq3xs_free_impl(int grid_size) {
  8783. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8784. const int gindex = iq3_data_index(grid_size);
  8785. if (iq3_data[gindex].grid) {
  8786. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8787. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8788. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8789. }
  8790. }
  8791. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8792. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8793. int num_neighbors = neighbours[0];
  8794. GGML_ASSERT(num_neighbors > 0);
  8795. float best_d2 = FLT_MAX;
  8796. int grid_index = -1;
  8797. for (int j = 1; j <= num_neighbors; ++j) {
  8798. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8799. float d2 = 0;
  8800. for (int i = 0; i < 4; ++i) {
  8801. float q = pg[i];
  8802. float diff = scale*q - xval[i];
  8803. d2 += weight[i]*diff*diff;
  8804. }
  8805. if (d2 < best_d2) {
  8806. best_d2 = d2; grid_index = neighbours[j];
  8807. }
  8808. }
  8809. GGML_ASSERT(grid_index >= 0);
  8810. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8811. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8812. return grid_index;
  8813. }
  8814. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int n,
  8815. const float * restrict quant_weights) {
  8816. const int gindex = iq3_data_index(grid_size);
  8817. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8818. const int * kmap_q3xs = iq3_data[gindex].map;
  8819. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8820. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8821. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8822. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8823. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8824. GGML_ASSERT(n%QK_K == 0);
  8825. const int kMaxQ = 8;
  8826. const int nbl = n/QK_K;
  8827. ggml_fp16_t * dh;
  8828. uint8_t * qs;
  8829. int block_size;
  8830. if (grid_size == 256) {
  8831. block_iq3_xxs * y = vy;
  8832. dh = &y->d;
  8833. qs = y->qs;
  8834. block_size = sizeof(block_iq3_xxs);
  8835. } else {
  8836. block_iq3_s * y = vy;
  8837. dh = &y->d;
  8838. qs = y->qs;
  8839. block_size = sizeof(block_iq3_s);
  8840. }
  8841. int quant_size = block_size - sizeof(ggml_fp16_t);
  8842. float scales[QK_K/32];
  8843. float weight[32];
  8844. float xval[32];
  8845. int8_t L[32];
  8846. int8_t Laux[32];
  8847. float waux[32];
  8848. bool is_on_grid[8];
  8849. bool is_on_grid_aux[8];
  8850. uint8_t block_signs[8];
  8851. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8852. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8853. uint8_t * qh = q3 + 3*(QK_K/8);
  8854. for (int ibl = 0; ibl < nbl; ++ibl) {
  8855. dh[0] = GGML_FP32_TO_FP16(0.f);
  8856. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8857. float max_scale = 0;
  8858. const float * xbl = x + QK_K*ibl;
  8859. float sumx2 = 0;
  8860. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8861. float sigma2 = 2*sumx2/QK_K;
  8862. for (int ib = 0; ib < QK_K/32; ++ib) {
  8863. const float * xb = xbl + 32*ib;
  8864. if (quant_weights) {
  8865. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8866. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8867. } else {
  8868. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8869. }
  8870. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8871. for (int k = 0; k < 4; ++k) {
  8872. int nflip = 0;
  8873. uint8_t s = 0;
  8874. for (int i = 0; i < 8; ++i) {
  8875. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8876. else {
  8877. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8878. }
  8879. }
  8880. if (nflip%2) {
  8881. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8882. for (int i = 1; i < 8; ++i) {
  8883. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8884. if (ax < min) {
  8885. min = ax; imin = i;
  8886. }
  8887. }
  8888. xval[8*k+imin] = -xval[8*k+imin];
  8889. s ^= (1 << imin);
  8890. }
  8891. block_signs[k] = s & 127;
  8892. }
  8893. float max = xval[0];
  8894. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8895. if (!max) {
  8896. scales[ib] = 0;
  8897. memset(L, 0, 32);
  8898. continue;
  8899. }
  8900. float best = 0;
  8901. float scale = max/(2*kMaxQ-1);
  8902. for (int is = -15; is <= 15; ++is) {
  8903. float id = (2*kMaxQ-1+is*0.2f)/max;
  8904. float this_scale = 1/id;
  8905. for (int k = 0; k < 8; ++k) {
  8906. for (int i = 0; i < 4; ++i) {
  8907. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8908. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8909. }
  8910. uint16_t u = 0;
  8911. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8912. int grid_index = kmap_q3xs[u];
  8913. is_on_grid_aux[k] = true;
  8914. if (grid_index < 0) {
  8915. is_on_grid_aux[k] = false;
  8916. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8917. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8918. }
  8919. }
  8920. float sumqx = 0, sumq2 = 0;
  8921. for (int i = 0; i < 32; ++i) {
  8922. float w = weight[i];
  8923. float q = 2*Laux[i] + 1;
  8924. sumqx += w*xval[i]*q;
  8925. sumq2 += w*q*q;
  8926. }
  8927. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8928. scale = sumqx/sumq2; best = scale*sumqx;
  8929. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8930. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8931. }
  8932. }
  8933. int n_not_ongrid = 0;
  8934. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8935. if (n_not_ongrid > 0 && scale > 0) {
  8936. float id = 1/scale;
  8937. for (int k = 0; k < 8; ++k) {
  8938. if (is_on_grid[k]) continue;
  8939. uint16_t u = 0;
  8940. for (int i = 0; i < 4; ++i) {
  8941. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8942. l = MAX(0, MIN(kMaxQ-1, l));
  8943. u |= (l << 3*i);
  8944. }
  8945. int grid_index = kmap_q3xs[u];
  8946. if (grid_index < 0) {
  8947. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8948. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8949. }
  8950. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8951. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8952. }
  8953. float sumqx = 0, sumq2 = 0;
  8954. for (int i = 0; i < 32; ++i) {
  8955. float w = weight[i];
  8956. float q = 2*L[i] + 1;
  8957. sumqx += w*xval[i]*q;
  8958. sumq2 += w*q*q;
  8959. }
  8960. if (sumq2 > 0) scale = sumqx/sumq2;
  8961. }
  8962. if (scale < 0) {
  8963. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8964. // and correspondingly flip quant signs.
  8965. scale = -scale;
  8966. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8967. }
  8968. for (int k = 0; k < 8; ++k) {
  8969. uint16_t u = 0;
  8970. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  8971. int grid_index = kmap_q3xs[u];
  8972. if (grid_index < 0) {
  8973. printf("Oops: found point %u not on grid:", u);
  8974. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  8975. printf("\n");
  8976. GGML_ASSERT(false);
  8977. }
  8978. if (grid_size == 256) {
  8979. q3[8*ib+k] = grid_index;
  8980. } else {
  8981. q3[8*ib+k] = grid_index & 255;
  8982. qh[ib] |= ((grid_index >> 8) << k);
  8983. }
  8984. }
  8985. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  8986. GGML_ASSERT(scale >= 0);
  8987. scales[ib] = scale;
  8988. max_scale = MAX(max_scale, scale);
  8989. }
  8990. if (!max_scale) {
  8991. memset(qs, 0, quant_size);
  8992. dh += block_size/sizeof(ggml_fp16_t);
  8993. qs += block_size;
  8994. continue;
  8995. }
  8996. float d = max_scale/31;
  8997. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  8998. float id = 1/d;
  8999. for (int ib = 0; ib < QK_K/32; ++ib) {
  9000. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9001. l = MAX(0, MIN(15, l));
  9002. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9003. }
  9004. memcpy(qs, q3, quant_size);
  9005. dh += block_size/sizeof(ggml_fp16_t);
  9006. qs += block_size;
  9007. }
  9008. }
  9009. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9010. GGML_ASSERT(n_per_row%QK_K == 0);
  9011. int nblock = n_per_row/QK_K;
  9012. char * qrow = (char *)dst;
  9013. for (int row = 0; row < nrow; ++row) {
  9014. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9015. src += n_per_row;
  9016. qrow += nblock*sizeof(block_iq3_xxs);
  9017. }
  9018. return nrow * nblock * sizeof(block_iq3_xxs);
  9019. }
  9020. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  9021. assert(k % QK_K == 0);
  9022. block_iq3_xxs * restrict y = vy;
  9023. quantize_row_iq3_xxs_reference(x, y, k);
  9024. }
  9025. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  9026. assert(k % QK_K == 0);
  9027. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9028. }
  9029. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9030. const float * restrict quant_weights,
  9031. float * scales,
  9032. float * weight,
  9033. float * xval,
  9034. int8_t * L,
  9035. int8_t * Laux,
  9036. float * waux,
  9037. bool * is_on_grid,
  9038. bool * is_on_grid_aux,
  9039. uint8_t * block_signs) {
  9040. const int gindex = iq3_data_index(512);
  9041. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9042. const int * kmap_q3xs = iq3_data[gindex].map;
  9043. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9044. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9045. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9046. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9047. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9048. GGML_ASSERT(n%QK_K == 0);
  9049. const int kMaxQ = 8;
  9050. const int nbl = n/QK_K;
  9051. block_iq3_s * y = vy;
  9052. const int bs4 = block_size/4;
  9053. const int bs8 = block_size/8;
  9054. for (int ibl = 0; ibl < nbl; ++ibl) {
  9055. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9056. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9057. uint8_t * qs = y[ibl].qs;
  9058. uint8_t * qh = y[ibl].qh;
  9059. uint8_t * signs = y[ibl].signs;
  9060. float max_scale = 0;
  9061. const float * xbl = x + QK_K*ibl;
  9062. float sumx2 = 0;
  9063. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9064. float sigma2 = 2*sumx2/QK_K;
  9065. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9066. const float * xb = xbl + block_size*ib;
  9067. if (quant_weights) {
  9068. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9069. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9070. } else {
  9071. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9072. }
  9073. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9074. for (int k = 0; k < bs8; ++k) {
  9075. uint8_t s = 0;
  9076. for (int i = 0; i < 8; ++i) {
  9077. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9078. else {
  9079. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9080. }
  9081. }
  9082. block_signs[k] = s;
  9083. }
  9084. float max = xval[0];
  9085. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9086. if (!max) {
  9087. scales[ib] = 0;
  9088. continue;
  9089. }
  9090. float best = 0;
  9091. float scale = max/(2*kMaxQ-1);
  9092. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9093. for (int is = -9; is <= 9; ++is) {
  9094. float id = (2*kMaxQ-1+is*0.2f)/max;
  9095. float this_scale = 1/id;
  9096. for (int k = 0; k < bs4; ++k) {
  9097. for (int i = 0; i < 4; ++i) {
  9098. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9099. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9100. }
  9101. uint16_t u = 0;
  9102. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9103. int grid_index = kmap_q3xs[u];
  9104. is_on_grid_aux[k] = true;
  9105. if (grid_index < 0) {
  9106. is_on_grid_aux[k] = false;
  9107. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9108. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9109. }
  9110. }
  9111. float sumqx = 0, sumq2 = 0;
  9112. for (int i = 0; i < block_size; ++i) {
  9113. float w = weight[i];
  9114. float q = 2*Laux[i] + 1;
  9115. sumqx += w*xval[i]*q;
  9116. sumq2 += w*q*q;
  9117. }
  9118. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9119. scale = sumqx/sumq2; best = scale*sumqx;
  9120. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9121. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9122. }
  9123. }
  9124. int n_not_ongrid = 0;
  9125. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9126. if (n_not_ongrid > 0 && scale > 0) {
  9127. float id = 1/scale;
  9128. for (int k = 0; k < bs4; ++k) {
  9129. //if (is_on_grid[k]) continue;
  9130. uint16_t u = 0;
  9131. for (int i = 0; i < 4; ++i) {
  9132. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9133. l = MAX(0, MIN(kMaxQ-1, l));
  9134. u |= (l << 3*i);
  9135. }
  9136. int grid_index = kmap_q3xs[u];
  9137. if (grid_index < 0) {
  9138. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9139. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9140. }
  9141. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9142. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9143. }
  9144. float sumqx = 0, sumq2 = 0;
  9145. for (int i = 0; i < block_size; ++i) {
  9146. float w = weight[i];
  9147. float q = 2*L[i] + 1;
  9148. sumqx += w*xval[i]*q;
  9149. sumq2 += w*q*q;
  9150. }
  9151. if (sumq2 > 0) scale = sumqx/sumq2;
  9152. }
  9153. if (scale < 0) {
  9154. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9155. // and correspondingly flip quant signs.
  9156. scale = -scale;
  9157. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9158. }
  9159. for (int k = 0; k < bs4; ++k) {
  9160. uint16_t u = 0;
  9161. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9162. int grid_index = kmap_q3xs[u];
  9163. if (grid_index < 0) {
  9164. printf("Oops: found point %u not on grid:", u);
  9165. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9166. printf("\n");
  9167. GGML_ASSERT(false);
  9168. }
  9169. qs[k] = grid_index & 255;
  9170. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9171. }
  9172. qs += bs4;
  9173. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9174. signs += bs8;
  9175. GGML_ASSERT(scale >= 0);
  9176. scales[ib] = scale;
  9177. max_scale = MAX(max_scale, scale);
  9178. }
  9179. if (!max_scale) {
  9180. continue;
  9181. }
  9182. float d = max_scale/31;
  9183. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9184. float id = 1/d;
  9185. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9186. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9187. l1 = MAX(0, MIN(15, l1));
  9188. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9189. l2 = MAX(0, MIN(15, l2));
  9190. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9191. }
  9192. }
  9193. }
  9194. #define IQ3S_BLOCK_SIZE 32
  9195. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9196. GGML_ASSERT(n_per_row%QK_K == 0);
  9197. int nblock = n_per_row/QK_K;
  9198. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9199. float weight[IQ3S_BLOCK_SIZE];
  9200. float xval[IQ3S_BLOCK_SIZE];
  9201. int8_t L[IQ3S_BLOCK_SIZE];
  9202. int8_t Laux[IQ3S_BLOCK_SIZE];
  9203. float waux[IQ3S_BLOCK_SIZE];
  9204. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9205. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9206. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9207. char * qrow = (char *)dst;
  9208. for (int row = 0; row < nrow; ++row) {
  9209. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9210. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9211. src += n_per_row;
  9212. qrow += nblock*sizeof(block_iq3_s);
  9213. }
  9214. return nrow * nblock * sizeof(block_iq3_s);
  9215. }
  9216. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int k) {
  9217. assert(k % QK_K == 0);
  9218. block_iq3_s * restrict y = vy;
  9219. quantize_row_iq3_s_reference(x, y, k);
  9220. }
  9221. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int k) {
  9222. assert(k % QK_K == 0);
  9223. quantize_iq3_s(x, y, 1, k, NULL);
  9224. }
  9225. // =================================== 1.5 bpw ===================================================
  9226. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9227. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9228. int num_neighbors = neighbours[0];
  9229. GGML_ASSERT(num_neighbors > 0);
  9230. float best_score = 0;
  9231. int grid_index = -1;
  9232. for (int j = 1; j <= num_neighbors; ++j) {
  9233. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9234. float sumqx = 0, sumq2 = 0;
  9235. for (int i = 0; i < 8; ++i) {
  9236. float q = (pg[i] - 3)/2;
  9237. float w = weight[i];
  9238. sumqx += w*q*xval[i];
  9239. sumq2 += w*q*q;
  9240. }
  9241. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9242. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9243. grid_index = neighbours[j];
  9244. }
  9245. }
  9246. if (grid_index < 0) {
  9247. for (int i = 0; i < ngrid; ++i) {
  9248. const int8_t * grid_i = (const int8_t *)(grid + i);
  9249. float sumqx = 0, sumq2 = 0;
  9250. for (int j = 0; j < 8; ++j) {
  9251. float w = weight[j];
  9252. float q = (grid_i[j] - 3)/2;
  9253. sumqx += w*q*xval[j];
  9254. sumq2 += w*q*q;
  9255. }
  9256. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9257. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9258. grid_index = i;
  9259. }
  9260. }
  9261. }
  9262. if (grid_index < 0) {
  9263. printf("Oops, did not find grid point\n");
  9264. printf("Have %d neighbours\n", num_neighbors);
  9265. for (int j = 1; j <= num_neighbors; ++j) {
  9266. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9267. float sumqx = 0, sumq2 = 0;
  9268. for (int i = 0; i < 8; ++i) {
  9269. float q = (pg[i] - 3)/2;
  9270. float w = weight[i];
  9271. sumqx += w*q*xval[i];
  9272. sumq2 += w*q*q;
  9273. }
  9274. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9275. }
  9276. }
  9277. GGML_ASSERT(grid_index >= 0);
  9278. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9279. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9280. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9281. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9282. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9283. return grid_index;
  9284. }
  9285. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9286. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L, int ngrid) {
  9287. int num_neighbors = neighbours[0];
  9288. GGML_ASSERT(num_neighbors > 0);
  9289. float best_score = FLT_MAX;
  9290. int grid_index = -1;
  9291. for (int j = 1; j <= num_neighbors; ++j) {
  9292. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9293. float d2 = 0;
  9294. for (int i = 0; i < 8; ++i) {
  9295. float q = (pg[i] - 3)/2;
  9296. float w = weight[i];
  9297. float diff = scale*q - xval[i];
  9298. d2 += w*diff*diff;
  9299. }
  9300. if (d2 < best_score) {
  9301. best_score = d2;
  9302. grid_index = neighbours[j];
  9303. }
  9304. }
  9305. if (grid_index < 0) {
  9306. for (int i = 0; i < ngrid; ++i) {
  9307. const int8_t * grid_i = (const int8_t *)(grid + i);
  9308. float d2 = 0;
  9309. for (int j = 0; j < 8; ++j) {
  9310. float w = weight[j];
  9311. float q = (grid_i[j] - 3)/2;
  9312. float diff = scale*q - xval[i];
  9313. d2 += w*diff*diff;
  9314. }
  9315. if (d2 < best_score) {
  9316. best_score = d2;
  9317. grid_index = i;
  9318. }
  9319. }
  9320. }
  9321. if (grid_index < 0) {
  9322. printf("Oops, did not find grid point\n");
  9323. printf("Have %d neighbours\n", num_neighbors);
  9324. for (int j = 1; j <= num_neighbors; ++j) {
  9325. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9326. float sumqx = 0, sumq2 = 0;
  9327. for (int i = 0; i < 8; ++i) {
  9328. float q = (pg[i] - 3)/2;
  9329. float w = weight[i];
  9330. sumqx += w*q*xval[i];
  9331. sumq2 += w*q*q;
  9332. }
  9333. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9334. }
  9335. }
  9336. GGML_ASSERT(grid_index >= 0);
  9337. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9338. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9339. return grid_index;
  9340. }
  9341. static int iq1_sort_helper(const void * left, const void * right) {
  9342. const float * l = left;
  9343. const float * r = right;
  9344. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9345. }
  9346. #define IQ1S_BLOCK_SIZE 32
  9347. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  9348. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9349. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9350. const int * kmap_q2xs = iq2_data[gindex].map;
  9351. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9352. GGML_ASSERT(quant_weights && "missing quantization weights");
  9353. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9354. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9355. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9356. GGML_ASSERT(n%QK_K == 0);
  9357. const int nbl = n/QK_K;
  9358. block_iq1_s * y = vy;
  9359. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9360. float weight[IQ1S_BLOCK_SIZE];
  9361. int8_t L[IQ1S_BLOCK_SIZE];
  9362. float sumx[IQ1S_BLOCK_SIZE+1];
  9363. float sumw[IQ1S_BLOCK_SIZE+1];
  9364. float pairs[2*IQ1S_BLOCK_SIZE];
  9365. int * idx = (int *)(pairs + 1);
  9366. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9367. for (int ibl = 0; ibl < nbl; ++ibl) {
  9368. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9369. memset(y[ibl].qs, 0, QK_K/8);
  9370. memset(y[ibl].qh, 0, QK_K/16);
  9371. float max_scale = 0;
  9372. const float * xbl = x + QK_K*ibl;
  9373. float sumx2 = 0;
  9374. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9375. float sigma2 = 2*sumx2/QK_K;
  9376. for (int ib = 0; ib < QK_K/IQ1S_BLOCK_SIZE; ++ib) {
  9377. const float * xb = xbl + IQ1S_BLOCK_SIZE*ib;
  9378. const float * qw = quant_weights + QK_K*ibl + IQ1S_BLOCK_SIZE*ib;
  9379. for (int i = 0; i < IQ1S_BLOCK_SIZE; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9380. float max = fabsf(xb[0]);
  9381. for (int i = 1; i < IQ1S_BLOCK_SIZE; ++i) max = MAX(max, fabsf(xb[i]));
  9382. if (!max) {
  9383. scales[ib] = 0;
  9384. memset(L, 1, IQ1S_BLOCK_SIZE);
  9385. continue;
  9386. }
  9387. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9388. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9389. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9390. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9391. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9392. // for each possible and score for each split.
  9393. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) {
  9394. pairs[2*j] = xb[j];
  9395. idx[2*j] = j;
  9396. }
  9397. qsort(pairs, IQ1S_BLOCK_SIZE, 2*sizeof(float), iq1_sort_helper);
  9398. {
  9399. sumx[0] = sumw[0] = 0;
  9400. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) {
  9401. int i = idx[2*j];
  9402. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9403. sumw[j+1] = sumw[j] + weight[i];
  9404. }
  9405. }
  9406. float best_score = 0, scale = max;
  9407. int besti1 = 0, besti2 = 0;
  9408. for (int i1 = 0; i1 <= IQ1S_BLOCK_SIZE; ++i1) {
  9409. for (int i2 = i1; i2 <= IQ1S_BLOCK_SIZE; ++i2) {
  9410. float sumqx = -(sumx[i1] - sumx[0]) + (sumx[IQ1S_BLOCK_SIZE] - sumx[i2]);
  9411. float sumq2 = (sumw[i1] - sumw[0]) + (sumw[IQ1S_BLOCK_SIZE] - sumw[i2]);
  9412. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9413. scale = sumqx/sumq2; best_score = scale*sumqx;
  9414. besti1 = i1; besti2 = i2;
  9415. }
  9416. }
  9417. }
  9418. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9419. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9420. for (int j = besti2; j < IQ1S_BLOCK_SIZE; ++j) L[idx[2*j]] = 2;
  9421. if (scale < 0) {
  9422. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) L[j] = 2 - L[j];
  9423. scale = -scale;
  9424. }
  9425. bool all_on_grid = true;
  9426. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9427. uint16_t u = 0;
  9428. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9429. int grid_index = kmap_q2xs[u];
  9430. if (grid_index < 0) {
  9431. all_on_grid = false;
  9432. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9433. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, L + 8*k, NGRID_IQ1S);
  9434. GGML_ASSERT(grid_index >= 0);
  9435. }
  9436. index[k] = grid_index;
  9437. }
  9438. if (!all_on_grid) {
  9439. float sumqx = 0, sumq2 = 0;
  9440. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9441. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9442. for (int j = 0; j < 8; ++j) {
  9443. float w = weight[8*k + j];
  9444. float q = (pg[j] - 3)/2;
  9445. sumqx += w*q*xb[8*k+j];
  9446. sumq2 += w*q*q;
  9447. }
  9448. }
  9449. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9450. }
  9451. uint16_t h = 0;
  9452. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9453. y[ibl].qs[(IQ1S_BLOCK_SIZE/8)*ib + k] = index[k] & 255;
  9454. h |= (index[k] >> 8) << 3*k;
  9455. }
  9456. y[ibl].qh[ib] = h;
  9457. GGML_ASSERT(scale >= 0);
  9458. scales[ib] = scale;
  9459. max_scale = MAX(max_scale, scale);
  9460. }
  9461. if (!max_scale) {
  9462. memset(y[ibl].qs, 0, QK_K/8);
  9463. continue;
  9464. }
  9465. float d = max_scale/31;
  9466. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.085f is another fudge factor. Don't ask me why it is needed.
  9467. float id = 1/d;
  9468. for (int ib = 0; ib < QK_K/IQ1S_BLOCK_SIZE; ++ib) {
  9469. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9470. l = MAX(0, MIN(15, l));
  9471. y[ibl].qh[ib] |= (l << 12);
  9472. }
  9473. }
  9474. }
  9475. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9476. GGML_ASSERT(n_per_row%QK_K == 0);
  9477. int nblock = n_per_row/QK_K;
  9478. char * qrow = (char *)dst;
  9479. for (int row = 0; row < nrow; ++row) {
  9480. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights);
  9481. src += n_per_row;
  9482. qrow += nblock*sizeof(block_iq1_s);
  9483. }
  9484. return nrow * nblock * sizeof(block_iq1_s);
  9485. }
  9486. // ============================ 4-bit non-linear quants
  9487. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9488. if (x <= val[0]) return 0;
  9489. if (x >= val[n-1]) return n-1;
  9490. int ml = 0, mu = n-1;
  9491. while (mu-ml > 1) {
  9492. int mav = (ml+mu)/2;
  9493. if (x < val[mav]) mu = mav; else ml = mav;
  9494. }
  9495. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9496. }
  9497. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9498. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9499. float * scales, float * weight, uint8_t * L,
  9500. const int8_t * values,
  9501. const float * quant_weights) {
  9502. const int ntry = 7;
  9503. float sigma2 = 0;
  9504. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9505. sigma2 *= 2.f/super_block_size;
  9506. memset(q4, 0, super_block_size/2);
  9507. dh[0] = GGML_FP32_TO_FP16(0.f);
  9508. float max_scale = 0, amax_scale = 0;
  9509. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9510. const float * xb = x + ib*block_size;
  9511. if (quant_weights) {
  9512. const float * qw = quant_weights + ib*block_size;
  9513. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9514. } else {
  9515. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9516. }
  9517. float amax = 0, max = 0;
  9518. for (int j = 0; j < block_size; ++j) {
  9519. float ax = fabsf(xb[j]);
  9520. if (ax > amax) {
  9521. amax = ax; max = xb[j];
  9522. }
  9523. }
  9524. if (!amax) {
  9525. scales[ib] = 0;
  9526. continue;
  9527. }
  9528. float d = -max/values[0];
  9529. float id = 1/d;
  9530. float sumqx = 0, sumq2 = 0;
  9531. for (int j = 0; j < block_size; ++j) {
  9532. float al = id*xb[j];
  9533. int l = best_index_int8(16, values, al);
  9534. float q = values[l];
  9535. float w = weight[j];
  9536. sumqx += w*q*xb[j];
  9537. sumq2 += w*q*q;
  9538. }
  9539. d = sumqx/sumq2;
  9540. float best = d*sumqx;
  9541. for (int itry = -ntry; itry <= ntry; ++itry) {
  9542. id = (itry + values[0])/max;
  9543. sumqx = sumq2 = 0;
  9544. for (int j = 0; j < block_size; ++j) {
  9545. float al = id*xb[j];
  9546. int l = best_index_int8(16, values, al);
  9547. float q = values[l];
  9548. float w = weight[j];
  9549. sumqx += w*q*xb[j];
  9550. sumq2 += w*q*q;
  9551. }
  9552. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9553. d = sumqx/sumq2; best = d * sumqx;
  9554. }
  9555. }
  9556. scales[ib] = d;
  9557. float abs_d = fabsf(d);
  9558. if (abs_d > amax_scale) {
  9559. amax_scale = abs_d; max_scale = d;
  9560. }
  9561. }
  9562. if (super_block_size/block_size > 1) {
  9563. int nb = super_block_size/block_size;
  9564. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9565. float d = -max_scale/32;
  9566. dh[0] = GGML_FP32_TO_FP16(d);
  9567. float id = d ? 1/d : 0.f;
  9568. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9569. int l = nearest_int(id*scales[ib]);
  9570. l = MAX(-32, MIN(31, l));
  9571. float dl = d * l;
  9572. float idl = dl ? 1/dl : 0.f;
  9573. uint8_t * Lb = L + ib*block_size;
  9574. const float * xb = x + ib*block_size;
  9575. for (int j = 0; j < block_size; ++j) {
  9576. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9577. }
  9578. l += 32;
  9579. uint8_t l_l = l & 0xf;
  9580. uint8_t l_h = l >> 4;
  9581. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9582. else scales_l[ib/2] |= (l_l << 4);
  9583. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9584. }
  9585. } else {
  9586. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9587. float id = scales[0] ? 1/scales[0] : 0;
  9588. for (int j = 0; j < super_block_size; ++j) {
  9589. L[j] = best_index_int8(16, values, id*x[j]);
  9590. }
  9591. }
  9592. for (int i = 0; i < super_block_size/32; ++i) {
  9593. for (int j = 0; j < 16; ++j) {
  9594. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9595. }
  9596. }
  9597. }
  9598. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9599. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9600. int nblock = n_per_row/QK4_NL;
  9601. char * qrow = (char *)dst;
  9602. uint8_t L[QK4_NL];
  9603. float weight[QK4_NL];
  9604. uint16_t unused_h;
  9605. uint8_t * unused_l = NULL;
  9606. float scale;
  9607. for (int row = 0; row < nrow; ++row) {
  9608. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9609. for (int ibl = 0; ibl < nblock; ++ibl) {
  9610. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9611. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9612. &scale, weight, L, kvalues_iq4nl, qw);
  9613. }
  9614. src += n_per_row;
  9615. qrow += nblock*sizeof(block_iq4_nl);
  9616. }
  9617. return nrow * nblock * sizeof(block_iq4_nl);
  9618. }
  9619. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int k) {
  9620. assert(k % QK4_NL == 0);
  9621. block_iq4_nl * restrict y = vy;
  9622. quantize_row_iq4_nl_reference(x, y, k);
  9623. }
  9624. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int k) {
  9625. assert(k % QK4_NL == 0);
  9626. quantize_iq4_nl(x, y, 1, k, NULL);
  9627. }
  9628. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9629. #if QK_K == 64
  9630. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9631. #else
  9632. GGML_ASSERT(n_per_row%QK_K == 0);
  9633. int nblock = n_per_row/QK_K;
  9634. char * qrow = (char *)dst;
  9635. uint8_t L[QK_K];
  9636. float weight[32];
  9637. float scales[QK_K/32];
  9638. for (int row = 0; row < nrow; ++row) {
  9639. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9640. for (int ibl = 0; ibl < nblock; ++ibl) {
  9641. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9642. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  9643. scales, weight, L, kvalues_iq4nl, qw);
  9644. }
  9645. src += n_per_row;
  9646. qrow += nblock*sizeof(block_iq4_xs);
  9647. }
  9648. return nrow * nblock * sizeof(block_iq4_xs);
  9649. #endif
  9650. }
  9651. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int k) {
  9652. assert(k % QK_K == 0);
  9653. block_iq4_xs * restrict y = vy;
  9654. quantize_row_iq4_xs_reference(x, y, k);
  9655. }
  9656. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int k) {
  9657. assert(k % QK_K == 0);
  9658. quantize_iq4_xs(x, y, 1, k, NULL);
  9659. }
  9660. // =============================== 2.5625 bpw
  9661. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  9662. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  9663. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9664. const int * kmap_q2xs = iq2_data[gindex].map;
  9665. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9666. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9667. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9668. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9669. GGML_ASSERT(n%QK_K == 0);
  9670. const int kMaxQ = 3;
  9671. const int nbl = n/QK_K;
  9672. block_iq2_s * y = vy;
  9673. float scales[QK_K/16];
  9674. float weight[16];
  9675. float xval[16];
  9676. int8_t L[16];
  9677. int8_t Laux[16];
  9678. float waux[16];
  9679. bool is_on_grid[2];
  9680. bool is_on_grid_aux[2];
  9681. uint8_t block_signs[2];
  9682. for (int ibl = 0; ibl < nbl; ++ibl) {
  9683. memset(&y[ibl], 0, sizeof(block_iq2_s));
  9684. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9685. float max_scale = 0;
  9686. const float * xbl = x + QK_K*ibl;
  9687. float sumx2 = 0;
  9688. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9689. float sigma2 = 2*sumx2/QK_K;
  9690. for (int ib = 0; ib < QK_K/16; ++ib) {
  9691. const float * xb = xbl + 16*ib;
  9692. if (quant_weights) {
  9693. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  9694. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9695. } else {
  9696. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  9697. }
  9698. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  9699. for (int k = 0; k < 2; ++k) {
  9700. uint8_t s = 0;
  9701. for (int i = 0; i < 8; ++i) {
  9702. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9703. else {
  9704. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9705. }
  9706. }
  9707. block_signs[k] = s;
  9708. }
  9709. float max = xval[0];
  9710. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  9711. if (!max) {
  9712. scales[ib] = 0;
  9713. continue;
  9714. }
  9715. float best = 0;
  9716. float scale = max/(2*kMaxQ-1);
  9717. is_on_grid[0] = is_on_grid[1] = true;
  9718. for (int is = -9; is <= 9; ++is) {
  9719. float id = (2*kMaxQ-1+is*0.1f)/max;
  9720. float this_scale = 1/id;
  9721. for (int k = 0; k < 2; ++k) {
  9722. for (int i = 0; i < 8; ++i) {
  9723. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9724. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9725. }
  9726. uint16_t u = 0;
  9727. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  9728. int grid_index = kmap_q2xs[u];
  9729. is_on_grid_aux[k] = true;
  9730. if (grid_index < 0) {
  9731. is_on_grid_aux[k] = false;
  9732. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9733. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  9734. }
  9735. }
  9736. float sumqx = 0, sumq2 = 0;
  9737. for (int i = 0; i < 16; ++i) {
  9738. float w = weight[i];
  9739. float q = 2*Laux[i] + 1;
  9740. sumqx += w*xval[i]*q;
  9741. sumq2 += w*q*q;
  9742. }
  9743. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9744. scale = sumqx/sumq2; best = scale*sumqx;
  9745. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  9746. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9747. }
  9748. }
  9749. int n_not_ongrid = 0;
  9750. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9751. if (n_not_ongrid > 0 && scale > 0) {
  9752. float id = 1/scale;
  9753. for (int k = 0; k < 2; ++k) {
  9754. if (is_on_grid[k]) continue;
  9755. uint16_t u = 0;
  9756. for (int i = 0; i < 8; ++i) {
  9757. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9758. l = MAX(0, MIN(kMaxQ-1, l));
  9759. u |= (l << 2*i);
  9760. L[8*k + i] = l;
  9761. }
  9762. int grid_index = kmap_q2xs[u];
  9763. if (grid_index < 0) {
  9764. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9765. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  9766. }
  9767. }
  9768. float sumqx = 0, sumq2 = 0;
  9769. for (int i = 0; i < 16; ++i) {
  9770. float w = weight[i];
  9771. float q = 2*L[i] + 1;
  9772. sumqx += w*xval[i]*q;
  9773. sumq2 += w*q*q;
  9774. }
  9775. if (sumq2 > 0) scale = sumqx/sumq2;
  9776. }
  9777. if (scale < 0) {
  9778. scale = -scale;
  9779. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  9780. }
  9781. for (int k = 0; k < 2; ++k) {
  9782. uint16_t u = 0;
  9783. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  9784. int grid_index = kmap_q2xs[u];
  9785. if (grid_index < 0) {
  9786. printf("Oops: found point %u not on grid:", u);
  9787. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  9788. printf("\n");
  9789. GGML_ASSERT(false);
  9790. }
  9791. const int i8 = 2*ib + k;
  9792. y[ibl].qs[i8] = grid_index & 255;
  9793. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  9794. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  9795. }
  9796. GGML_ASSERT(scale >= 0);
  9797. scales[ib] = scale;
  9798. max_scale = MAX(max_scale, scale);
  9799. }
  9800. if (!max_scale) {
  9801. continue;
  9802. }
  9803. float d = max_scale/31;
  9804. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  9805. float id = 1/d;
  9806. for (int ib = 0; ib < QK_K/16; ++ib) {
  9807. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9808. l = MAX(0, MIN(15, l));
  9809. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  9810. else y[ibl].scales[ib/2] |= (l << 4);
  9811. }
  9812. }
  9813. }
  9814. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9815. GGML_ASSERT(n_per_row%QK_K == 0);
  9816. int nblock = n_per_row/QK_K;
  9817. char * qrow = (char *)dst;
  9818. for (int row = 0; row < nrow; ++row) {
  9819. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  9820. src += n_per_row;
  9821. qrow += nblock*sizeof(block_iq2_s);
  9822. }
  9823. return nrow * nblock * sizeof(block_iq2_s);
  9824. }
  9825. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int k) {
  9826. assert(k % QK_K == 0);
  9827. quantize_iq2_s(x, y, 1, k, NULL);
  9828. }
  9829. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int k) {
  9830. assert(k % QK_K == 0);
  9831. block_iq2_s * restrict y = vy;
  9832. quantize_row_iq2_s_reference(x, y, k);
  9833. }