ggml-quants.c 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <float.h>
  7. #include <stdlib.h> // for qsort
  8. #include <stdio.h> // for GGML_ASSERT
  9. #ifdef __ARM_NEON
  10. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  11. //
  12. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  13. //
  14. #include <arm_neon.h>
  15. #else
  16. #ifdef __wasm_simd128__
  17. #include <wasm_simd128.h>
  18. #else
  19. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  20. #include <altivec.h>
  21. #undef bool
  22. #define bool _Bool
  23. #else
  24. #if defined(_MSC_VER) || defined(__MINGW32__)
  25. #include <intrin.h>
  26. #else
  27. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  28. #if !defined(__riscv)
  29. #include <immintrin.h>
  30. #endif
  31. #endif
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #ifdef __riscv_v_intrinsic
  37. #include <riscv_vector.h>
  38. #endif
  39. #undef MIN
  40. #undef MAX
  41. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  42. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  43. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  44. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  45. // multiply int8_t, add results pairwise twice
  46. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  47. // Get absolute values of x vectors
  48. const __m128i ax = _mm_sign_epi8(x, x);
  49. // Sign the values of the y vectors
  50. const __m128i sy = _mm_sign_epi8(y, x);
  51. // Perform multiplication and create 16-bit values
  52. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  53. const __m128i ones = _mm_set1_epi16(1);
  54. return _mm_madd_epi16(ones, dot);
  55. }
  56. #if __AVX__ || __AVX2__ || __AVX512F__
  57. // horizontally add 8 floats
  58. static inline float hsum_float_8(const __m256 x) {
  59. __m128 res = _mm256_extractf128_ps(x, 1);
  60. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  61. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  62. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  63. return _mm_cvtss_f32(res);
  64. }
  65. // horizontally add 8 int32_t
  66. static inline int hsum_i32_8(const __m256i a) {
  67. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  68. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  69. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  70. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  71. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  72. }
  73. // horizontally add 4 int32_t
  74. static inline int hsum_i32_4(const __m128i a) {
  75. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  76. const __m128i sum64 = _mm_add_epi32(hi64, a);
  77. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  78. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  79. }
  80. #if defined(__AVX2__) || defined(__AVX512F__)
  81. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  82. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  83. uint32_t x32;
  84. memcpy(&x32, x, sizeof(uint32_t));
  85. const __m256i shuf_mask = _mm256_set_epi64x(
  86. 0x0303030303030303, 0x0202020202020202,
  87. 0x0101010101010101, 0x0000000000000000);
  88. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  89. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  90. bytes = _mm256_or_si256(bytes, bit_mask);
  91. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  92. }
  93. // Unpack 32 4-bit fields into 32 bytes
  94. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  95. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  96. {
  97. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  98. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  99. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  100. return _mm256_and_si256(lowMask, bytes);
  101. }
  102. // add int16_t pairwise and return as float vector
  103. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  104. const __m256i ones = _mm256_set1_epi16(1);
  105. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  106. return _mm256_cvtepi32_ps(summed_pairs);
  107. }
  108. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  109. #if __AVXVNNI__
  110. const __m256i zero = _mm256_setzero_si256();
  111. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. #else
  114. // Perform multiplication and create 16-bit values
  115. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  116. return sum_i16_pairs_float(dot);
  117. #endif
  118. }
  119. // multiply int8_t, add results pairwise twice and return as float vector
  120. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  121. #if __AVXVNNIINT8__
  122. const __m256i zero = _mm256_setzero_si256();
  123. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  124. return _mm256_cvtepi32_ps(summed_pairs);
  125. #else
  126. // Get absolute values of x vectors
  127. const __m256i ax = _mm256_sign_epi8(x, x);
  128. // Sign the values of the y vectors
  129. const __m256i sy = _mm256_sign_epi8(y, x);
  130. return mul_sum_us8_pairs_float(ax, sy);
  131. #endif
  132. }
  133. static inline __m128i packNibbles( __m256i bytes )
  134. {
  135. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  136. #if __AVX512F__
  137. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  138. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  139. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  140. #else
  141. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  142. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  143. __m256i low = _mm256_and_si256( lowByte, bytes );
  144. high = _mm256_srli_epi16( high, 4 );
  145. bytes = _mm256_or_si256( low, high );
  146. // Compress uint16_t lanes into bytes
  147. __m128i r0 = _mm256_castsi256_si128( bytes );
  148. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  149. return _mm_packus_epi16( r0, r1 );
  150. #endif
  151. }
  152. #elif defined(__AVX__)
  153. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  154. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  155. uint32_t x32;
  156. memcpy(&x32, x, sizeof(uint32_t));
  157. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  158. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  159. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  160. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  161. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  162. bytesl = _mm_or_si128(bytesl, bit_mask);
  163. bytesh = _mm_or_si128(bytesh, bit_mask);
  164. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  165. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  166. return MM256_SET_M128I(bytesh, bytesl);
  167. }
  168. // Unpack 32 4-bit fields into 32 bytes
  169. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  170. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  171. {
  172. // Load 16 bytes from memory
  173. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  174. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  175. const __m128i lowMask = _mm_set1_epi8(0xF);
  176. tmpl = _mm_and_si128(lowMask, tmpl);
  177. tmph = _mm_and_si128(lowMask, tmph);
  178. return MM256_SET_M128I(tmph, tmpl);
  179. }
  180. // add int16_t pairwise and return as float vector
  181. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  182. const __m128i ones = _mm_set1_epi16(1);
  183. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  184. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  185. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  186. return _mm256_cvtepi32_ps(summed_pairs);
  187. }
  188. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  189. const __m128i axl = _mm256_castsi256_si128(ax);
  190. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  191. const __m128i syl = _mm256_castsi256_si128(sy);
  192. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  193. // Perform multiplication and create 16-bit values
  194. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  195. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  196. return sum_i16_pairs_float(doth, dotl);
  197. }
  198. // multiply int8_t, add results pairwise twice and return as float vector
  199. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  200. const __m128i xl = _mm256_castsi256_si128(x);
  201. const __m128i xh = _mm256_extractf128_si256(x, 1);
  202. const __m128i yl = _mm256_castsi256_si128(y);
  203. const __m128i yh = _mm256_extractf128_si256(y, 1);
  204. // Get absolute values of x vectors
  205. const __m128i axl = _mm_sign_epi8(xl, xl);
  206. const __m128i axh = _mm_sign_epi8(xh, xh);
  207. // Sign the values of the y vectors
  208. const __m128i syl = _mm_sign_epi8(yl, xl);
  209. const __m128i syh = _mm_sign_epi8(yh, xh);
  210. // Perform multiplication and create 16-bit values
  211. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  212. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  213. return sum_i16_pairs_float(doth, dotl);
  214. }
  215. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  216. {
  217. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  218. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  219. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  220. __m128i low = _mm_and_si128( lowByte, bytes1 );
  221. high = _mm_srli_epi16( high, 4 );
  222. bytes1 = _mm_or_si128( low, high );
  223. high = _mm_andnot_si128( lowByte, bytes2 );
  224. low = _mm_and_si128( lowByte, bytes2 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes2 = _mm_or_si128( low, high );
  227. return _mm_packus_epi16( bytes1, bytes2);
  228. }
  229. #endif
  230. #elif defined(__SSSE3__)
  231. // horizontally add 4x4 floats
  232. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  233. __m128 res_0 =_mm_hadd_ps(a, b);
  234. __m128 res_1 =_mm_hadd_ps(c, d);
  235. __m128 res =_mm_hadd_ps(res_0, res_1);
  236. res =_mm_hadd_ps(res, res);
  237. res =_mm_hadd_ps(res, res);
  238. return _mm_cvtss_f32(res);
  239. }
  240. #endif // __AVX__ || __AVX2__ || __AVX512F__
  241. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  242. #if defined(__ARM_NEON)
  243. #if !defined(__aarch64__)
  244. // 64-bit compatibility
  245. // vaddvq_s16
  246. // vpaddq_s16
  247. // vpaddq_s32
  248. // vaddvq_s32
  249. // vaddvq_f32
  250. // vmaxvq_f32
  251. // vcvtnq_s32_f32
  252. // vzip1_u8
  253. // vzip2_u8
  254. inline static int32_t vaddvq_s16(int16x8_t v) {
  255. return
  256. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  257. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  258. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  259. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  260. }
  261. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  262. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  263. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  264. return vcombine_s16(a0, b0);
  265. }
  266. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  267. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  268. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  269. return vcombine_s32(a0, b0);
  270. }
  271. inline static int32_t vaddvq_s32(int32x4_t v) {
  272. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  273. }
  274. inline static float vaddvq_f32(float32x4_t v) {
  275. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  276. }
  277. inline static float vmaxvq_f32(float32x4_t v) {
  278. return
  279. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  280. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  281. }
  282. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  283. int32x4_t res;
  284. res[0] = roundf(vgetq_lane_f32(v, 0));
  285. res[1] = roundf(vgetq_lane_f32(v, 1));
  286. res[2] = roundf(vgetq_lane_f32(v, 2));
  287. res[3] = roundf(vgetq_lane_f32(v, 3));
  288. return res;
  289. }
  290. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  291. uint8x8_t res;
  292. res[0] = a[0]; res[1] = b[0];
  293. res[2] = a[1]; res[3] = b[1];
  294. res[4] = a[2]; res[5] = b[2];
  295. res[6] = a[3]; res[7] = b[3];
  296. return res;
  297. }
  298. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  299. uint8x8_t res;
  300. res[0] = a[4]; res[1] = b[4];
  301. res[2] = a[5]; res[3] = b[5];
  302. res[4] = a[6]; res[5] = b[6];
  303. res[6] = a[7]; res[7] = b[7];
  304. return res;
  305. }
  306. // vld1q_s16_x2
  307. // vld1q_u8_x2
  308. // vld1q_u8_x4
  309. // vld1q_s8_x2
  310. // vld1q_s8_x4
  311. // TODO: double-check these work correctly
  312. typedef struct ggml_int16x8x2_t {
  313. int16x8_t val[2];
  314. } ggml_int16x8x2_t;
  315. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  316. ggml_int16x8x2_t res;
  317. res.val[0] = vld1q_s16(ptr + 0);
  318. res.val[1] = vld1q_s16(ptr + 8);
  319. return res;
  320. }
  321. typedef struct ggml_uint8x16x2_t {
  322. uint8x16_t val[2];
  323. } ggml_uint8x16x2_t;
  324. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  325. ggml_uint8x16x2_t res;
  326. res.val[0] = vld1q_u8(ptr + 0);
  327. res.val[1] = vld1q_u8(ptr + 16);
  328. return res;
  329. }
  330. typedef struct ggml_uint8x16x4_t {
  331. uint8x16_t val[4];
  332. } ggml_uint8x16x4_t;
  333. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  334. ggml_uint8x16x4_t res;
  335. res.val[0] = vld1q_u8(ptr + 0);
  336. res.val[1] = vld1q_u8(ptr + 16);
  337. res.val[2] = vld1q_u8(ptr + 32);
  338. res.val[3] = vld1q_u8(ptr + 48);
  339. return res;
  340. }
  341. typedef struct ggml_int8x16x2_t {
  342. int8x16_t val[2];
  343. } ggml_int8x16x2_t;
  344. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  345. ggml_int8x16x2_t res;
  346. res.val[0] = vld1q_s8(ptr + 0);
  347. res.val[1] = vld1q_s8(ptr + 16);
  348. return res;
  349. }
  350. typedef struct ggml_int8x16x4_t {
  351. int8x16_t val[4];
  352. } ggml_int8x16x4_t;
  353. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  354. ggml_int8x16x4_t res;
  355. res.val[0] = vld1q_s8(ptr + 0);
  356. res.val[1] = vld1q_s8(ptr + 16);
  357. res.val[2] = vld1q_s8(ptr + 32);
  358. res.val[3] = vld1q_s8(ptr + 48);
  359. return res;
  360. }
  361. #else
  362. #define ggml_int16x8x2_t int16x8x2_t
  363. #define ggml_uint8x16x2_t uint8x16x2_t
  364. #define ggml_uint8x16x4_t uint8x16x4_t
  365. #define ggml_int8x16x2_t int8x16x2_t
  366. #define ggml_int8x16x4_t int8x16x4_t
  367. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  368. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  369. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  370. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  371. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  372. #endif
  373. #if !defined(__ARM_FEATURE_DOTPROD)
  374. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  375. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  376. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  377. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  378. }
  379. #else
  380. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  381. #endif
  382. #endif
  383. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  384. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  385. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  386. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  387. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  388. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  389. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  390. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  391. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  392. // precomputed tables for expanding 8bits to 8 bytes:
  393. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  394. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  395. #endif
  396. // reference implementation for deterministic creation of model files
  397. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  398. static const int qk = QK4_0;
  399. assert(k % qk == 0);
  400. const int nb = k / qk;
  401. for (int i = 0; i < nb; i++) {
  402. float amax = 0.0f; // absolute max
  403. float max = 0.0f;
  404. for (int j = 0; j < qk; j++) {
  405. const float v = x[i*qk + j];
  406. if (amax < fabsf(v)) {
  407. amax = fabsf(v);
  408. max = v;
  409. }
  410. }
  411. const float d = max / -8;
  412. const float id = d ? 1.0f/d : 0.0f;
  413. y[i].d = GGML_FP32_TO_FP16(d);
  414. for (int j = 0; j < qk/2; ++j) {
  415. const float x0 = x[i*qk + 0 + j]*id;
  416. const float x1 = x[i*qk + qk/2 + j]*id;
  417. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  418. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  419. y[i].qs[j] = xi0;
  420. y[i].qs[j] |= xi1 << 4;
  421. }
  422. }
  423. }
  424. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  425. quantize_row_q4_0_reference(x, y, k);
  426. }
  427. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  428. const int qk = QK4_1;
  429. assert(k % qk == 0);
  430. const int nb = k / qk;
  431. for (int i = 0; i < nb; i++) {
  432. float min = FLT_MAX;
  433. float max = -FLT_MAX;
  434. for (int j = 0; j < qk; j++) {
  435. const float v = x[i*qk + j];
  436. if (v < min) min = v;
  437. if (v > max) max = v;
  438. }
  439. const float d = (max - min) / ((1 << 4) - 1);
  440. const float id = d ? 1.0f/d : 0.0f;
  441. y[i].d = GGML_FP32_TO_FP16(d);
  442. y[i].m = GGML_FP32_TO_FP16(min);
  443. for (int j = 0; j < qk/2; ++j) {
  444. const float x0 = (x[i*qk + 0 + j] - min)*id;
  445. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  446. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  447. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  448. y[i].qs[j] = xi0;
  449. y[i].qs[j] |= xi1 << 4;
  450. }
  451. }
  452. }
  453. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  454. quantize_row_q4_1_reference(x, y, k);
  455. }
  456. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  457. static const int qk = QK5_0;
  458. assert(k % qk == 0);
  459. const int nb = k / qk;
  460. for (int i = 0; i < nb; i++) {
  461. float amax = 0.0f; // absolute max
  462. float max = 0.0f;
  463. for (int j = 0; j < qk; j++) {
  464. const float v = x[i*qk + j];
  465. if (amax < fabsf(v)) {
  466. amax = fabsf(v);
  467. max = v;
  468. }
  469. }
  470. const float d = max / -16;
  471. const float id = d ? 1.0f/d : 0.0f;
  472. y[i].d = GGML_FP32_TO_FP16(d);
  473. uint32_t qh = 0;
  474. for (int j = 0; j < qk/2; ++j) {
  475. const float x0 = x[i*qk + 0 + j]*id;
  476. const float x1 = x[i*qk + qk/2 + j]*id;
  477. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  478. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  479. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  480. // get the 5-th bit and store it in qh at the right position
  481. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  482. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  483. }
  484. memcpy(&y[i].qh, &qh, sizeof(qh));
  485. }
  486. }
  487. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  488. quantize_row_q5_0_reference(x, y, k);
  489. }
  490. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  491. const int qk = QK5_1;
  492. assert(k % qk == 0);
  493. const int nb = k / qk;
  494. for (int i = 0; i < nb; i++) {
  495. float min = FLT_MAX;
  496. float max = -FLT_MAX;
  497. for (int j = 0; j < qk; j++) {
  498. const float v = x[i*qk + j];
  499. if (v < min) min = v;
  500. if (v > max) max = v;
  501. }
  502. const float d = (max - min) / ((1 << 5) - 1);
  503. const float id = d ? 1.0f/d : 0.0f;
  504. y[i].d = GGML_FP32_TO_FP16(d);
  505. y[i].m = GGML_FP32_TO_FP16(min);
  506. uint32_t qh = 0;
  507. for (int j = 0; j < qk/2; ++j) {
  508. const float x0 = (x[i*qk + 0 + j] - min)*id;
  509. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  510. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  511. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  512. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  513. // get the 5-th bit and store it in qh at the right position
  514. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  515. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  516. }
  517. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  518. }
  519. }
  520. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  521. quantize_row_q5_1_reference(x, y, k);
  522. }
  523. // reference implementation for deterministic creation of model files
  524. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  525. assert(k % QK8_0 == 0);
  526. const int nb = k / QK8_0;
  527. for (int i = 0; i < nb; i++) {
  528. float amax = 0.0f; // absolute max
  529. for (int j = 0; j < QK8_0; j++) {
  530. const float v = x[i*QK8_0 + j];
  531. amax = MAX(amax, fabsf(v));
  532. }
  533. const float d = amax / ((1 << 7) - 1);
  534. const float id = d ? 1.0f/d : 0.0f;
  535. y[i].d = GGML_FP32_TO_FP16(d);
  536. for (int j = 0; j < QK8_0; ++j) {
  537. const float x0 = x[i*QK8_0 + j]*id;
  538. y[i].qs[j] = roundf(x0);
  539. }
  540. }
  541. }
  542. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  543. assert(QK8_0 == 32);
  544. assert(k % QK8_0 == 0);
  545. const int nb = k / QK8_0;
  546. block_q8_0 * restrict y = vy;
  547. #if defined(__ARM_NEON)
  548. for (int i = 0; i < nb; i++) {
  549. float32x4_t srcv [8];
  550. float32x4_t asrcv[8];
  551. float32x4_t amaxv[8];
  552. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  553. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  554. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  555. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  556. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  557. const float amax = vmaxvq_f32(amaxv[0]);
  558. const float d = amax / ((1 << 7) - 1);
  559. const float id = d ? 1.0f/d : 0.0f;
  560. y[i].d = GGML_FP32_TO_FP16(d);
  561. for (int j = 0; j < 8; j++) {
  562. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  563. const int32x4_t vi = vcvtnq_s32_f32(v);
  564. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  565. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  566. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  567. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  568. }
  569. }
  570. #elif defined(__wasm_simd128__)
  571. for (int i = 0; i < nb; i++) {
  572. v128_t srcv [8];
  573. v128_t asrcv[8];
  574. v128_t amaxv[8];
  575. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  576. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  577. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  578. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  579. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  580. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  581. wasm_f32x4_extract_lane(amaxv[0], 1)),
  582. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  583. wasm_f32x4_extract_lane(amaxv[0], 3)));
  584. const float d = amax / ((1 << 7) - 1);
  585. const float id = d ? 1.0f/d : 0.0f;
  586. y[i].d = GGML_FP32_TO_FP16(d);
  587. for (int j = 0; j < 8; j++) {
  588. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  589. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  590. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  591. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  592. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  593. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  594. }
  595. }
  596. #elif defined(__AVX2__) || defined(__AVX__)
  597. for (int i = 0; i < nb; i++) {
  598. // Load elements into 4 AVX vectors
  599. __m256 v0 = _mm256_loadu_ps( x );
  600. __m256 v1 = _mm256_loadu_ps( x + 8 );
  601. __m256 v2 = _mm256_loadu_ps( x + 16 );
  602. __m256 v3 = _mm256_loadu_ps( x + 24 );
  603. x += 32;
  604. // Compute max(abs(e)) for the block
  605. const __m256 signBit = _mm256_set1_ps( -0.0f );
  606. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  607. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  608. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  609. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  610. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  611. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  612. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  613. const float maxScalar = _mm_cvtss_f32( max4 );
  614. // Quantize these floats
  615. const float d = maxScalar / 127.f;
  616. y[i].d = GGML_FP32_TO_FP16(d);
  617. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  618. const __m256 mul = _mm256_set1_ps( id );
  619. // Apply the multiplier
  620. v0 = _mm256_mul_ps( v0, mul );
  621. v1 = _mm256_mul_ps( v1, mul );
  622. v2 = _mm256_mul_ps( v2, mul );
  623. v3 = _mm256_mul_ps( v3, mul );
  624. // Round to nearest integer
  625. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  626. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  627. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  628. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  629. // Convert floats to integers
  630. __m256i i0 = _mm256_cvtps_epi32( v0 );
  631. __m256i i1 = _mm256_cvtps_epi32( v1 );
  632. __m256i i2 = _mm256_cvtps_epi32( v2 );
  633. __m256i i3 = _mm256_cvtps_epi32( v3 );
  634. #if defined(__AVX2__)
  635. // Convert int32 to int16
  636. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  637. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  638. // Convert int16 to int8
  639. 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
  640. // We got our precious signed bytes, but the order is now wrong
  641. // These AVX2 pack instructions process 16-byte pieces independently
  642. // The following instruction is fixing the order
  643. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  644. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  645. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  646. #else
  647. // Since we don't have in AVX some necessary functions,
  648. // we split the registers in half and call AVX2 analogs from SSE
  649. __m128i ni0 = _mm256_castsi256_si128( i0 );
  650. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  651. __m128i ni2 = _mm256_castsi256_si128( i1 );
  652. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  653. __m128i ni4 = _mm256_castsi256_si128( i2 );
  654. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  655. __m128i ni6 = _mm256_castsi256_si128( i3 );
  656. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  657. // Convert int32 to int16
  658. ni0 = _mm_packs_epi32( ni0, ni1 );
  659. ni2 = _mm_packs_epi32( ni2, ni3 );
  660. ni4 = _mm_packs_epi32( ni4, ni5 );
  661. ni6 = _mm_packs_epi32( ni6, ni7 );
  662. // Convert int16 to int8
  663. ni0 = _mm_packs_epi16( ni0, ni2 );
  664. ni4 = _mm_packs_epi16( ni4, ni6 );
  665. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  666. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  667. #endif
  668. }
  669. #elif defined(__riscv_v_intrinsic)
  670. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  671. for (int i = 0; i < nb; i++) {
  672. // load elements
  673. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  674. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  675. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  676. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  677. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  678. const float d = amax / ((1 << 7) - 1);
  679. const float id = d ? 1.0f/d : 0.0f;
  680. y[i].d = GGML_FP32_TO_FP16(d);
  681. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  682. // convert to integer
  683. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  684. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  685. // store result
  686. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  687. }
  688. #else
  689. GGML_UNUSED(nb);
  690. // scalar
  691. quantize_row_q8_0_reference(x, y, k);
  692. #endif
  693. }
  694. // reference implementation for deterministic creation of model files
  695. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  696. assert(QK8_1 == 32);
  697. assert(k % QK8_1 == 0);
  698. const int nb = k / QK8_1;
  699. for (int i = 0; i < nb; i++) {
  700. float amax = 0.0f; // absolute max
  701. for (int j = 0; j < QK8_1; j++) {
  702. const float v = x[i*QK8_1 + j];
  703. amax = MAX(amax, fabsf(v));
  704. }
  705. const float d = amax / ((1 << 7) - 1);
  706. const float id = d ? 1.0f/d : 0.0f;
  707. y[i].d = d;
  708. int sum = 0;
  709. for (int j = 0; j < QK8_1/2; ++j) {
  710. const float v0 = x[i*QK8_1 + j]*id;
  711. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  712. y[i].qs[ j] = roundf(v0);
  713. y[i].qs[QK8_1/2 + j] = roundf(v1);
  714. sum += y[i].qs[ j];
  715. sum += y[i].qs[QK8_1/2 + j];
  716. }
  717. y[i].s = sum*d;
  718. }
  719. }
  720. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  721. assert(k % QK8_1 == 0);
  722. const int nb = k / QK8_1;
  723. block_q8_1 * restrict y = vy;
  724. #if defined(__ARM_NEON)
  725. for (int i = 0; i < nb; i++) {
  726. float32x4_t srcv [8];
  727. float32x4_t asrcv[8];
  728. float32x4_t amaxv[8];
  729. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  730. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  731. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  732. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  733. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  734. const float amax = vmaxvq_f32(amaxv[0]);
  735. const float d = amax / ((1 << 7) - 1);
  736. const float id = d ? 1.0f/d : 0.0f;
  737. y[i].d = d;
  738. int32x4_t accv = vdupq_n_s32(0);
  739. for (int j = 0; j < 8; j++) {
  740. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  741. const int32x4_t vi = vcvtnq_s32_f32(v);
  742. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  743. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  744. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  745. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  746. accv = vaddq_s32(accv, vi);
  747. }
  748. y[i].s = d * vaddvq_s32(accv);
  749. }
  750. #elif defined(__wasm_simd128__)
  751. for (int i = 0; i < nb; i++) {
  752. v128_t srcv [8];
  753. v128_t asrcv[8];
  754. v128_t amaxv[8];
  755. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  756. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  757. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  758. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  759. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  760. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  761. wasm_f32x4_extract_lane(amaxv[0], 1)),
  762. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  763. wasm_f32x4_extract_lane(amaxv[0], 3)));
  764. const float d = amax / ((1 << 7) - 1);
  765. const float id = d ? 1.0f/d : 0.0f;
  766. y[i].d = d;
  767. v128_t accv = wasm_i32x4_splat(0);
  768. for (int j = 0; j < 8; j++) {
  769. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  770. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  771. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  772. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  773. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  774. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  775. accv = wasm_i32x4_add(accv, vi);
  776. }
  777. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  778. wasm_i32x4_extract_lane(accv, 1) +
  779. wasm_i32x4_extract_lane(accv, 2) +
  780. wasm_i32x4_extract_lane(accv, 3));
  781. }
  782. #elif defined(__AVX2__) || defined(__AVX__)
  783. for (int i = 0; i < nb; i++) {
  784. // Load elements into 4 AVX vectors
  785. __m256 v0 = _mm256_loadu_ps( x );
  786. __m256 v1 = _mm256_loadu_ps( x + 8 );
  787. __m256 v2 = _mm256_loadu_ps( x + 16 );
  788. __m256 v3 = _mm256_loadu_ps( x + 24 );
  789. x += 32;
  790. // Compute max(abs(e)) for the block
  791. const __m256 signBit = _mm256_set1_ps( -0.0f );
  792. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  793. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  794. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  795. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  796. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  797. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  798. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  799. const float maxScalar = _mm_cvtss_f32( max4 );
  800. // Quantize these floats
  801. const float d = maxScalar / 127.f;
  802. y[i].d = d;
  803. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  804. const __m256 mul = _mm256_set1_ps( id );
  805. // Apply the multiplier
  806. v0 = _mm256_mul_ps( v0, mul );
  807. v1 = _mm256_mul_ps( v1, mul );
  808. v2 = _mm256_mul_ps( v2, mul );
  809. v3 = _mm256_mul_ps( v3, mul );
  810. // Round to nearest integer
  811. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  812. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  813. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  814. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  815. // Convert floats to integers
  816. __m256i i0 = _mm256_cvtps_epi32( v0 );
  817. __m256i i1 = _mm256_cvtps_epi32( v1 );
  818. __m256i i2 = _mm256_cvtps_epi32( v2 );
  819. __m256i i3 = _mm256_cvtps_epi32( v3 );
  820. #if defined(__AVX2__)
  821. // Compute the sum of the quants and set y[i].s
  822. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  823. // Convert int32 to int16
  824. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  825. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  826. // Convert int16 to int8
  827. 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
  828. // We got our precious signed bytes, but the order is now wrong
  829. // These AVX2 pack instructions process 16-byte pieces independently
  830. // The following instruction is fixing the order
  831. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  832. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  833. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  834. #else
  835. // Since we don't have in AVX some necessary functions,
  836. // we split the registers in half and call AVX2 analogs from SSE
  837. __m128i ni0 = _mm256_castsi256_si128( i0 );
  838. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  839. __m128i ni2 = _mm256_castsi256_si128( i1 );
  840. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  841. __m128i ni4 = _mm256_castsi256_si128( i2 );
  842. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  843. __m128i ni6 = _mm256_castsi256_si128( i3 );
  844. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  845. // Compute the sum of the quants and set y[i].s
  846. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  847. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  848. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  849. // Convert int32 to int16
  850. ni0 = _mm_packs_epi32( ni0, ni1 );
  851. ni2 = _mm_packs_epi32( ni2, ni3 );
  852. ni4 = _mm_packs_epi32( ni4, ni5 );
  853. ni6 = _mm_packs_epi32( ni6, ni7 );
  854. // Convert int16 to int8
  855. ni0 = _mm_packs_epi16( ni0, ni2 );
  856. ni4 = _mm_packs_epi16( ni4, ni6 );
  857. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  858. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  859. #endif
  860. }
  861. #elif defined(__riscv_v_intrinsic)
  862. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  863. for (int i = 0; i < nb; i++) {
  864. // load elements
  865. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  866. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  867. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  868. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  869. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  870. const float d = amax / ((1 << 7) - 1);
  871. const float id = d ? 1.0f/d : 0.0f;
  872. y[i].d = d;
  873. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  874. // convert to integer
  875. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  876. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  877. // store result
  878. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  879. // compute sum for y[i].s
  880. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  881. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  882. // set y[i].s
  883. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  884. y[i].s = sum*d;
  885. }
  886. #else
  887. GGML_UNUSED(nb);
  888. // scalar
  889. quantize_row_q8_1_reference(x, y, k);
  890. #endif
  891. }
  892. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  893. static const int qk = QK4_0;
  894. assert(k % qk == 0);
  895. const int nb = k / qk;
  896. for (int i = 0; i < nb; i++) {
  897. const float d = GGML_FP16_TO_FP32(x[i].d);
  898. for (int j = 0; j < qk/2; ++j) {
  899. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  900. const int x1 = (x[i].qs[j] >> 4) - 8;
  901. y[i*qk + j + 0 ] = x0*d;
  902. y[i*qk + j + qk/2] = x1*d;
  903. }
  904. }
  905. }
  906. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  907. static const int qk = QK4_1;
  908. assert(k % qk == 0);
  909. const int nb = k / qk;
  910. for (int i = 0; i < nb; i++) {
  911. const float d = GGML_FP16_TO_FP32(x[i].d);
  912. const float m = GGML_FP16_TO_FP32(x[i].m);
  913. for (int j = 0; j < qk/2; ++j) {
  914. const int x0 = (x[i].qs[j] & 0x0F);
  915. const int x1 = (x[i].qs[j] >> 4);
  916. y[i*qk + j + 0 ] = x0*d + m;
  917. y[i*qk + j + qk/2] = x1*d + m;
  918. }
  919. }
  920. }
  921. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  922. static const int qk = QK5_0;
  923. assert(k % qk == 0);
  924. const int nb = k / qk;
  925. for (int i = 0; i < nb; i++) {
  926. const float d = GGML_FP16_TO_FP32(x[i].d);
  927. uint32_t qh;
  928. memcpy(&qh, x[i].qh, sizeof(qh));
  929. for (int j = 0; j < qk/2; ++j) {
  930. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  931. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  932. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  933. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  934. y[i*qk + j + 0 ] = x0*d;
  935. y[i*qk + j + qk/2] = x1*d;
  936. }
  937. }
  938. }
  939. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  940. static const int qk = QK5_1;
  941. assert(k % qk == 0);
  942. const int nb = k / qk;
  943. for (int i = 0; i < nb; i++) {
  944. const float d = GGML_FP16_TO_FP32(x[i].d);
  945. const float m = GGML_FP16_TO_FP32(x[i].m);
  946. uint32_t qh;
  947. memcpy(&qh, x[i].qh, sizeof(qh));
  948. for (int j = 0; j < qk/2; ++j) {
  949. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  950. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  951. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  952. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  953. y[i*qk + j + 0 ] = x0*d + m;
  954. y[i*qk + j + qk/2] = x1*d + m;
  955. }
  956. }
  957. }
  958. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  959. static const int qk = QK8_0;
  960. assert(k % qk == 0);
  961. const int nb = k / qk;
  962. for (int i = 0; i < nb; i++) {
  963. const float d = GGML_FP16_TO_FP32(x[i].d);
  964. for (int j = 0; j < qk; ++j) {
  965. y[i*qk + j] = x[i].qs[j]*d;
  966. }
  967. }
  968. }
  969. //
  970. // 2-6 bit quantization in super-blocks
  971. //
  972. //
  973. // ===================== Helper functions
  974. //
  975. static inline int nearest_int(float fval) {
  976. assert(fval <= 4194303.f);
  977. float val = fval + 12582912.f;
  978. int i; memcpy(&i, &val, sizeof(int));
  979. return (i & 0x007fffff) - 0x00400000;
  980. }
  981. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  982. const float * restrict qw) {
  983. float max = 0;
  984. float amax = 0;
  985. for (int i = 0; i < n; ++i) {
  986. float ax = fabsf(x[i]);
  987. if (ax > amax) { amax = ax; max = x[i]; }
  988. }
  989. if (amax < 1e-30f) { // all zero
  990. for (int i = 0; i < n; ++i) {
  991. L[i] = 0;
  992. }
  993. return 0.f;
  994. }
  995. float iscale = -nmax / max;
  996. if (rmse_type == 0) {
  997. for (int i = 0; i < n; ++i) {
  998. int l = nearest_int(iscale * x[i]);
  999. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1000. }
  1001. return 1/iscale;
  1002. }
  1003. bool return_early = false;
  1004. if (rmse_type < 0) {
  1005. rmse_type = -rmse_type;
  1006. return_early = true;
  1007. }
  1008. float sumlx = 0;
  1009. float suml2 = 0;
  1010. for (int i = 0; i < n; ++i) {
  1011. int l = nearest_int(iscale * x[i]);
  1012. l = MAX(-nmax, MIN(nmax-1, l));
  1013. L[i] = l + nmax;
  1014. 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]));
  1015. sumlx += w*x[i]*l;
  1016. suml2 += w*l*l;
  1017. }
  1018. float scale = sumlx/suml2;
  1019. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1020. float best = scale * sumlx;
  1021. for (int is = -9; is <= 9; ++is) {
  1022. if (is == 0) {
  1023. continue;
  1024. }
  1025. iscale = -(nmax + 0.1f*is) / max;
  1026. sumlx = suml2 = 0;
  1027. for (int i = 0; i < n; ++i) {
  1028. int l = nearest_int(iscale * x[i]);
  1029. l = MAX(-nmax, MIN(nmax-1, l));
  1030. 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]));
  1031. sumlx += w*x[i]*l;
  1032. suml2 += w*l*l;
  1033. }
  1034. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1035. for (int i = 0; i < n; ++i) {
  1036. int l = nearest_int(iscale * x[i]);
  1037. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1038. }
  1039. scale = sumlx/suml2; best = scale*sumlx;
  1040. }
  1041. }
  1042. return scale;
  1043. }
  1044. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1045. float max = 0;
  1046. float amax = 0;
  1047. for (int i = 0; i < n; ++i) {
  1048. float ax = fabsf(x[i]);
  1049. if (ax > amax) { amax = ax; max = x[i]; }
  1050. }
  1051. if (!amax) { // all zero
  1052. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1053. return 0.f;
  1054. }
  1055. float iscale = -nmax / max;
  1056. if (do_rmse) {
  1057. float sumlx = 0;
  1058. float suml2 = 0;
  1059. for (int i = 0; i < n; ++i) {
  1060. int l = nearest_int(iscale * x[i]);
  1061. l = MAX(-nmax, MIN(nmax-1, l));
  1062. L[i] = l;
  1063. float w = x[i]*x[i];
  1064. sumlx += w*x[i]*l;
  1065. suml2 += w*l*l;
  1066. }
  1067. for (int itry = 0; itry < 5; ++itry) {
  1068. int n_changed = 0;
  1069. for (int i = 0; i < n; ++i) {
  1070. float w = x[i]*x[i];
  1071. float slx = sumlx - w*x[i]*L[i];
  1072. if (slx > 0) {
  1073. float sl2 = suml2 - w*L[i]*L[i];
  1074. int new_l = nearest_int(x[i] * sl2 / slx);
  1075. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1076. if (new_l != L[i]) {
  1077. slx += w*x[i]*new_l;
  1078. sl2 += w*new_l*new_l;
  1079. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1080. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1081. ++n_changed;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. if (!n_changed) {
  1087. break;
  1088. }
  1089. }
  1090. for (int i = 0; i < n; ++i) {
  1091. L[i] += nmax;
  1092. }
  1093. return sumlx / suml2;
  1094. }
  1095. for (int i = 0; i < n; ++i) {
  1096. int l = nearest_int(iscale * x[i]);
  1097. l = MAX(-nmax, MIN(nmax-1, l));
  1098. L[i] = l + nmax;
  1099. }
  1100. return 1/iscale;
  1101. }
  1102. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1103. int ntry, float alpha) {
  1104. float min = x[0];
  1105. float max = x[0];
  1106. for (int i = 1; i < n; ++i) {
  1107. if (x[i] < min) min = x[i];
  1108. if (x[i] > max) max = x[i];
  1109. }
  1110. if (max == min) {
  1111. for (int i = 0; i < n; ++i) L[i] = 0;
  1112. *the_min = 0;
  1113. return 0.f;
  1114. }
  1115. if (min > 0) min = 0;
  1116. float iscale = nmax/(max - min);
  1117. float scale = 1/iscale;
  1118. for (int itry = 0; itry < ntry; ++itry) {
  1119. float sumlx = 0; int suml2 = 0;
  1120. bool did_change = false;
  1121. for (int i = 0; i < n; ++i) {
  1122. int l = nearest_int(iscale*(x[i] - min));
  1123. l = MAX(0, MIN(nmax, l));
  1124. if (l != L[i]) {
  1125. L[i] = l;
  1126. did_change = true;
  1127. }
  1128. sumlx += (x[i] - min)*l;
  1129. suml2 += l*l;
  1130. }
  1131. scale = sumlx/suml2;
  1132. float sum = 0;
  1133. for (int i = 0; i < n; ++i) {
  1134. sum += x[i] - scale*L[i];
  1135. }
  1136. min = alpha*min + (1 - alpha)*sum/n;
  1137. if (min > 0) min = 0;
  1138. iscale = 1/scale;
  1139. if (!did_change) break;
  1140. }
  1141. *the_min = -min;
  1142. return scale;
  1143. }
  1144. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1145. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1146. float rmin, float rdelta, int nstep, bool use_mad) {
  1147. float min = x[0];
  1148. float max = x[0];
  1149. float sum_w = weights[0];
  1150. float sum_x = sum_w * x[0];
  1151. #ifdef HAVE_BUGGY_APPLE_LINKER
  1152. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1153. for (volatile int i = 1; i < n; ++i) {
  1154. #else
  1155. for (int i = 1; i < n; ++i) {
  1156. #endif
  1157. if (x[i] < min) min = x[i];
  1158. if (x[i] > max) max = x[i];
  1159. float w = weights[i];
  1160. sum_w += w;
  1161. sum_x += w * x[i];
  1162. }
  1163. if (min > 0) min = 0;
  1164. if (max == min) {
  1165. for (int i = 0; i < n; ++i) L[i] = 0;
  1166. *the_min = -min;
  1167. return 0.f;
  1168. }
  1169. float iscale = nmax/(max - min);
  1170. float scale = 1/iscale;
  1171. float best_mad = 0;
  1172. for (int i = 0; i < n; ++i) {
  1173. int l = nearest_int(iscale*(x[i] - min));
  1174. L[i] = MAX(0, MIN(nmax, l));
  1175. float diff = scale * L[i] + min - x[i];
  1176. diff = use_mad ? fabsf(diff) : diff * diff;
  1177. float w = weights[i];
  1178. best_mad += w * diff;
  1179. }
  1180. if (nstep < 1) {
  1181. *the_min = -min;
  1182. return scale;
  1183. }
  1184. for (int is = 0; is <= nstep; ++is) {
  1185. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1186. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1187. for (int i = 0; i < n; ++i) {
  1188. int l = nearest_int(iscale*(x[i] - min));
  1189. l = MAX(0, MIN(nmax, l));
  1190. Laux[i] = l;
  1191. float w = weights[i];
  1192. sum_l += w*l;
  1193. sum_l2 += w*l*l;
  1194. sum_xl += w*l*x[i];
  1195. }
  1196. float D = sum_w * sum_l2 - sum_l * sum_l;
  1197. if (D > 0) {
  1198. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1199. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1200. if (this_min > 0) {
  1201. this_min = 0;
  1202. this_scale = sum_xl / sum_l2;
  1203. }
  1204. float mad = 0;
  1205. for (int i = 0; i < n; ++i) {
  1206. float diff = this_scale * Laux[i] + this_min - x[i];
  1207. diff = use_mad ? fabsf(diff) : diff * diff;
  1208. float w = weights[i];
  1209. mad += w * diff;
  1210. }
  1211. if (mad < best_mad) {
  1212. for (int i = 0; i < n; ++i) {
  1213. L[i] = Laux[i];
  1214. }
  1215. best_mad = mad;
  1216. scale = this_scale;
  1217. min = this_min;
  1218. }
  1219. }
  1220. }
  1221. *the_min = -min;
  1222. return scale;
  1223. }
  1224. #if QK_K == 256
  1225. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1226. if (j < 4) {
  1227. *d = q[j] & 63; *m = q[j + 4] & 63;
  1228. } else {
  1229. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1230. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1231. }
  1232. }
  1233. #endif
  1234. //========================- 2-bit (de)-quantization
  1235. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1236. assert(k % QK_K == 0);
  1237. const int nb = k / QK_K;
  1238. uint8_t L[QK_K];
  1239. uint8_t Laux[16];
  1240. float weights[16];
  1241. float mins[QK_K/16];
  1242. float scales[QK_K/16];
  1243. const float q4scale = 15.f;
  1244. for (int i = 0; i < nb; i++) {
  1245. float max_scale = 0; // as we are deducting the min, scales are always positive
  1246. float max_min = 0;
  1247. for (int j = 0; j < QK_K/16; ++j) {
  1248. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1249. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1250. float scale = scales[j];
  1251. if (scale > max_scale) {
  1252. max_scale = scale;
  1253. }
  1254. float min = mins[j];
  1255. if (min > max_min) {
  1256. max_min = min;
  1257. }
  1258. }
  1259. if (max_scale > 0) {
  1260. float iscale = q4scale/max_scale;
  1261. for (int j = 0; j < QK_K/16; ++j) {
  1262. int l = nearest_int(iscale*scales[j]);
  1263. y[i].scales[j] = l;
  1264. }
  1265. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1266. } else {
  1267. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1268. y[i].d = GGML_FP32_TO_FP16(0.f);
  1269. }
  1270. if (max_min > 0) {
  1271. float iscale = q4scale/max_min;
  1272. for (int j = 0; j < QK_K/16; ++j) {
  1273. int l = nearest_int(iscale*mins[j]);
  1274. y[i].scales[j] |= (l << 4);
  1275. }
  1276. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1277. } else {
  1278. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1279. }
  1280. for (int j = 0; j < QK_K/16; ++j) {
  1281. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1282. if (!d) continue;
  1283. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1284. for (int ii = 0; ii < 16; ++ii) {
  1285. int l = nearest_int((x[16*j + ii] + dm)/d);
  1286. l = MAX(0, MIN(3, l));
  1287. L[16*j + ii] = l;
  1288. }
  1289. }
  1290. #if QK_K == 256
  1291. for (int j = 0; j < QK_K; j += 128) {
  1292. for (int l = 0; l < 32; ++l) {
  1293. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1294. }
  1295. }
  1296. #else
  1297. for (int l = 0; l < 16; ++l) {
  1298. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1299. }
  1300. #endif
  1301. x += QK_K;
  1302. }
  1303. }
  1304. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1305. assert(k % QK_K == 0);
  1306. const int nb = k / QK_K;
  1307. for (int i = 0; i < nb; i++) {
  1308. const float d = GGML_FP16_TO_FP32(x[i].d);
  1309. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1310. const uint8_t * q = x[i].qs;
  1311. #if QK_K == 256
  1312. int is = 0;
  1313. float dl, ml;
  1314. for (int n = 0; n < QK_K; n += 128) {
  1315. int shift = 0;
  1316. for (int j = 0; j < 4; ++j) {
  1317. uint8_t sc = x[i].scales[is++];
  1318. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1319. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1320. sc = x[i].scales[is++];
  1321. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1322. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1323. shift += 2;
  1324. }
  1325. q += 32;
  1326. }
  1327. #else
  1328. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1329. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1330. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1331. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1332. for (int l = 0; l < 16; ++l) {
  1333. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1334. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1335. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1336. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1337. }
  1338. y += QK_K;
  1339. #endif
  1340. }
  1341. }
  1342. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1343. quantize_row_q2_K_reference(x, vy, k);
  1344. }
  1345. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1346. (void)hist; // TODO: collect histograms
  1347. for (int j = 0; j < n; j += k) {
  1348. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1349. quantize_row_q2_K_reference(src + j, y, k);
  1350. }
  1351. return (n/QK_K*sizeof(block_q2_K));
  1352. }
  1353. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1354. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1355. float rmin, float rdelta, int nstep, bool use_mad) {
  1356. float min = x[0];
  1357. float max = x[0];
  1358. float sum_w = weights ? weights[0] : x[0]*x[0];
  1359. float sum_x = sum_w * x[0];
  1360. for (int i = 1; i < n; ++i) {
  1361. if (x[i] < min) min = x[i];
  1362. if (x[i] > max) max = x[i];
  1363. float w = weights ? weights[i] : x[i]*x[i];
  1364. sum_w += w;
  1365. sum_x += w * x[i];
  1366. }
  1367. if (min > 0) {
  1368. min = 0;
  1369. }
  1370. if (max <= min) {
  1371. for (int i = 0; i < n; ++i) L[i] = 0;
  1372. *the_min = -min;
  1373. return 0.f;
  1374. }
  1375. float iscale = nmax/(max - min);
  1376. float scale = 1/iscale;
  1377. float best_mad = 0;
  1378. for (int i = 0; i < n; ++i) {
  1379. int l = nearest_int(iscale*(x[i] - min));
  1380. L[i] = MAX(0, MIN(nmax, l));
  1381. float diff = scale * L[i] + min - x[i];
  1382. diff = use_mad ? fabsf(diff) : diff*diff;
  1383. float w = weights ? weights[i] : x[i]*x[i];
  1384. best_mad += w * diff;
  1385. }
  1386. if (nstep < 1) {
  1387. *the_min = -min;
  1388. return scale;
  1389. }
  1390. for (int is = 0; is <= nstep; ++is) {
  1391. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1392. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1393. for (int i = 0; i < n; ++i) {
  1394. int l = nearest_int(iscale*(x[i] - min));
  1395. l = MAX(0, MIN(nmax, l));
  1396. Laux[i] = l;
  1397. float w = weights ? weights[i] : x[i]*x[i];
  1398. sum_l += w*l;
  1399. sum_l2 += w*l*l;
  1400. sum_xl += w*l*x[i];
  1401. }
  1402. float D = sum_w * sum_l2 - sum_l * sum_l;
  1403. if (D > 0) {
  1404. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1405. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1406. if (this_min > 0) {
  1407. this_min = 0;
  1408. this_scale = sum_xl / sum_l2;
  1409. }
  1410. float mad = 0;
  1411. for (int i = 0; i < n; ++i) {
  1412. float diff = this_scale * Laux[i] + this_min - x[i];
  1413. diff = use_mad ? fabsf(diff) : diff*diff;
  1414. float w = weights ? weights[i] : x[i]*x[i];
  1415. mad += w * diff;
  1416. }
  1417. if (mad < best_mad) {
  1418. for (int i = 0; i < n; ++i) {
  1419. L[i] = Laux[i];
  1420. }
  1421. best_mad = mad;
  1422. scale = this_scale;
  1423. min = this_min;
  1424. }
  1425. }
  1426. }
  1427. *the_min = -min;
  1428. return scale;
  1429. }
  1430. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1431. float max = 0;
  1432. for (int i = 0; i < n; ++i) {
  1433. max = MAX(max, x[i]);
  1434. }
  1435. if (!max) { // all zero
  1436. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1437. return 0.f;
  1438. }
  1439. float iscale = nmax / max;
  1440. for (int i = 0; i < n; ++i) {
  1441. L[i] = nearest_int(iscale * x[i]);
  1442. }
  1443. float scale = 1/iscale;
  1444. float best_mse = 0;
  1445. for (int i = 0; i < n; ++i) {
  1446. float diff = x[i] - scale*L[i];
  1447. float w = quant_weights[i];
  1448. best_mse += w*diff*diff;
  1449. }
  1450. for (int is = -4; is <= 4; ++is) {
  1451. if (is == 0) continue;
  1452. float iscale_is = (0.1f*is + nmax)/max;
  1453. float scale_is = 1/iscale_is;
  1454. float mse = 0;
  1455. for (int i = 0; i < n; ++i) {
  1456. int l = nearest_int(iscale_is*x[i]);
  1457. l = MIN(nmax, l);
  1458. float diff = x[i] - scale_is*l;
  1459. float w = quant_weights[i];
  1460. mse += w*diff*diff;
  1461. }
  1462. if (mse < best_mse) {
  1463. best_mse = mse;
  1464. iscale = iscale_is;
  1465. }
  1466. }
  1467. float sumlx = 0;
  1468. float suml2 = 0;
  1469. for (int i = 0; i < n; ++i) {
  1470. int l = nearest_int(iscale * x[i]);
  1471. l = MIN(nmax, l);
  1472. L[i] = l;
  1473. float w = quant_weights[i];
  1474. sumlx += w*x[i]*l;
  1475. suml2 += w*l*l;
  1476. }
  1477. for (int itry = 0; itry < 5; ++itry) {
  1478. int n_changed = 0;
  1479. for (int i = 0; i < n; ++i) {
  1480. float w = quant_weights[i];
  1481. float slx = sumlx - w*x[i]*L[i];
  1482. float sl2 = suml2 - w*L[i]*L[i];
  1483. if (slx > 0 && sl2 > 0) {
  1484. int new_l = nearest_int(x[i] * sl2 / slx);
  1485. new_l = MIN(nmax, new_l);
  1486. if (new_l != L[i]) {
  1487. slx += w*x[i]*new_l;
  1488. sl2 += w*new_l*new_l;
  1489. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1490. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1491. ++n_changed;
  1492. }
  1493. }
  1494. }
  1495. }
  1496. if (!n_changed) {
  1497. break;
  1498. }
  1499. }
  1500. return sumlx / suml2;
  1501. }
  1502. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1503. GGML_ASSERT(quant_weights);
  1504. assert(k % QK_K == 0);
  1505. const int nb = k / QK_K;
  1506. const bool requantize = true;
  1507. uint8_t L[QK_K];
  1508. uint8_t Laux[16];
  1509. float mins[QK_K/16];
  1510. float scales[QK_K/16];
  1511. float sw[QK_K/16];
  1512. float weight[QK_K/16];
  1513. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1514. for (int i = 0; i < nb; i++) {
  1515. memset(sw, 0, QK_K/16*sizeof(float));
  1516. float sumx2 = 0;
  1517. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1518. float sigma2 = sumx2/QK_K;
  1519. for (int j = 0; j < QK_K/16; ++j) {
  1520. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1521. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1522. for (int l = 0; l < 16; ++l) sw[j] += weight[l];
  1523. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1524. }
  1525. float dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1526. float mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1527. y[i].d = GGML_FP32_TO_FP16(dm);
  1528. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1529. dm = GGML_FP16_TO_FP32(y[i].d);
  1530. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1531. for (int j = 0; j < QK_K/16; ++j) {
  1532. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1533. }
  1534. if (requantize) {
  1535. for (int j = 0; j < QK_K/16; ++j) {
  1536. const float d = dm * (y[i].scales[j] & 0xF);
  1537. if (!d) continue;
  1538. const float m = mm * (y[i].scales[j] >> 4);
  1539. for (int ii = 0; ii < 16; ++ii) {
  1540. int l = nearest_int((x[16*j + ii] + m)/d);
  1541. l = MAX(0, MIN(3, l));
  1542. L[16*j + ii] = l;
  1543. }
  1544. }
  1545. }
  1546. #if QK_K == 256
  1547. for (int j = 0; j < QK_K; j += 128) {
  1548. for (int l = 0; l < 32; ++l) {
  1549. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1550. }
  1551. }
  1552. #else
  1553. for (int l = 0; l < 16; ++l) {
  1554. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1555. }
  1556. #endif
  1557. x += QK_K;
  1558. }
  1559. }
  1560. size_t quantize_q2_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1561. (void)hist;
  1562. int row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1563. if (!quant_weights) {
  1564. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1565. }
  1566. else {
  1567. char * qrow = (char *)dst;
  1568. for (int row = 0; row < nrow; ++row) {
  1569. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1570. src += n_per_row;
  1571. qrow += row_size;
  1572. }
  1573. }
  1574. return nrow * row_size;
  1575. }
  1576. //========================= 3-bit (de)-quantization
  1577. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1578. assert(k % QK_K == 0);
  1579. const int nb = k / QK_K;
  1580. int8_t L[QK_K];
  1581. float scales[QK_K / 16];
  1582. for (int i = 0; i < nb; i++) {
  1583. float max_scale = 0;
  1584. float amax = 0;
  1585. for (int j = 0; j < QK_K/16; ++j) {
  1586. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1587. float scale = fabsf(scales[j]);
  1588. if (scale > amax) {
  1589. amax = scale; max_scale = scales[j];
  1590. }
  1591. }
  1592. #if QK_K == 256
  1593. memset(y[i].scales, 0, 12);
  1594. if (max_scale) {
  1595. float iscale = -32.f/max_scale;
  1596. for (int j = 0; j < QK_K/16; ++j) {
  1597. int8_t l = nearest_int(iscale*scales[j]);
  1598. l = MAX(-32, MIN(31, l)) + 32;
  1599. if (j < 8) {
  1600. y[i].scales[j] = l & 0xF;
  1601. } else {
  1602. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1603. }
  1604. l >>= 4;
  1605. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1606. }
  1607. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1608. } else {
  1609. y[i].d = GGML_FP32_TO_FP16(0.f);
  1610. }
  1611. int8_t sc;
  1612. for (int j = 0; j < QK_K/16; ++j) {
  1613. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1614. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1615. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1616. if (!d) {
  1617. continue;
  1618. }
  1619. for (int ii = 0; ii < 16; ++ii) {
  1620. int l = nearest_int(x[16*j + ii]/d);
  1621. l = MAX(-4, MIN(3, l));
  1622. L[16*j + ii] = l + 4;
  1623. }
  1624. }
  1625. #else
  1626. if (max_scale) {
  1627. float iscale = -8.f/max_scale;
  1628. for (int j = 0; j < QK_K/16; j+=2) {
  1629. int l1 = nearest_int(iscale*scales[j]);
  1630. l1 = 8 + MAX(-8, MIN(7, l1));
  1631. int l2 = nearest_int(iscale*scales[j+1]);
  1632. l2 = 8 + MAX(-8, MIN(7, l2));
  1633. y[i].scales[j/2] = l1 | (l2 << 4);
  1634. }
  1635. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1636. } else {
  1637. for (int j = 0; j < QK_K/16; j+=2) {
  1638. y[i].scales[j/2] = 0;
  1639. }
  1640. y[i].d = GGML_FP32_TO_FP16(0.f);
  1641. }
  1642. for (int j = 0; j < QK_K/16; ++j) {
  1643. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1644. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1645. if (!d) {
  1646. continue;
  1647. }
  1648. for (int ii = 0; ii < 16; ++ii) {
  1649. int l = nearest_int(x[16*j + ii]/d);
  1650. l = MAX(-4, MIN(3, l));
  1651. L[16*j + ii] = l + 4;
  1652. }
  1653. }
  1654. #endif
  1655. memset(y[i].hmask, 0, QK_K/8);
  1656. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1657. int m = 0;
  1658. uint8_t hm = 1;
  1659. for (int j = 0; j < QK_K; ++j) {
  1660. if (L[j] > 3) {
  1661. y[i].hmask[m] |= hm;
  1662. L[j] -= 4;
  1663. }
  1664. if (++m == QK_K/8) {
  1665. m = 0; hm <<= 1;
  1666. }
  1667. }
  1668. #if QK_K == 256
  1669. for (int j = 0; j < QK_K; j += 128) {
  1670. for (int l = 0; l < 32; ++l) {
  1671. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1672. }
  1673. }
  1674. #else
  1675. for (int l = 0; l < 16; ++l) {
  1676. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1677. }
  1678. #endif
  1679. x += QK_K;
  1680. }
  1681. }
  1682. #if QK_K == 256
  1683. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1684. assert(k % QK_K == 0);
  1685. const int nb = k / QK_K;
  1686. const uint32_t kmask1 = 0x03030303;
  1687. const uint32_t kmask2 = 0x0f0f0f0f;
  1688. uint32_t aux[4];
  1689. const int8_t * scales = (const int8_t*)aux;
  1690. for (int i = 0; i < nb; i++) {
  1691. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1692. const uint8_t * restrict q = x[i].qs;
  1693. const uint8_t * restrict hm = x[i].hmask;
  1694. uint8_t m = 1;
  1695. memcpy(aux, x[i].scales, 12);
  1696. uint32_t tmp = aux[2];
  1697. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1698. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1699. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1700. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1701. int is = 0;
  1702. float dl;
  1703. for (int n = 0; n < QK_K; n += 128) {
  1704. int shift = 0;
  1705. for (int j = 0; j < 4; ++j) {
  1706. dl = d_all * (scales[is++] - 32);
  1707. for (int l = 0; l < 16; ++l) {
  1708. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1709. }
  1710. dl = d_all * (scales[is++] - 32);
  1711. for (int l = 0; l < 16; ++l) {
  1712. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1713. }
  1714. shift += 2;
  1715. m <<= 1;
  1716. }
  1717. q += 32;
  1718. }
  1719. }
  1720. }
  1721. #else
  1722. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1723. assert(k % QK_K == 0);
  1724. assert(QK_K == 64);
  1725. const int nb = k / QK_K;
  1726. for (int i = 0; i < nb; i++) {
  1727. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1728. const uint8_t * restrict q = x[i].qs;
  1729. const uint8_t * restrict hm = x[i].hmask;
  1730. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1731. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1732. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1733. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1734. for (int l=0; l<8; ++l) {
  1735. uint8_t h = hm[l];
  1736. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1737. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1738. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1739. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1740. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1741. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1742. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1743. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1744. }
  1745. y += QK_K;
  1746. }
  1747. }
  1748. #endif
  1749. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1750. quantize_row_q3_K_reference(x, vy, k);
  1751. }
  1752. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1753. (void)hist; // TODO: collect histograms
  1754. for (int j = 0; j < n; j += k) {
  1755. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1756. quantize_row_q3_K_reference(src + j, y, k);
  1757. }
  1758. return (n/QK_K*sizeof(block_q3_K));
  1759. }
  1760. 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) {
  1761. #if QK_K != 256
  1762. (void)quant_weights;
  1763. quantize_row_q3_K_reference(x, y, n_per_row);
  1764. #else
  1765. assert(n_per_row % QK_K == 0);
  1766. const int nb = n_per_row / QK_K;
  1767. int8_t L[QK_K];
  1768. float scales[QK_K / 16];
  1769. float weight[16];
  1770. float sw[QK_K / 16];
  1771. int8_t Ls[QK_K / 16];
  1772. for (int i = 0; i < nb; i++) {
  1773. float sumx2 = 0;
  1774. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1775. float sigma2 = 2*sumx2/QK_K;
  1776. for (int j = 0; j < QK_K/16; ++j) {
  1777. if (quant_weights) {
  1778. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1779. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1780. } else {
  1781. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1782. }
  1783. float sumw = 0;
  1784. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1785. sw[j] = sumw;
  1786. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1787. }
  1788. memset(y[i].scales, 0, 12);
  1789. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1790. for (int j = 0; j < QK_K/16; ++j) {
  1791. int l = Ls[j];
  1792. if (j < 8) {
  1793. y[i].scales[j] = l & 0xF;
  1794. } else {
  1795. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1796. }
  1797. l >>= 4;
  1798. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1799. }
  1800. y[i].d = GGML_FP32_TO_FP16(d_block);
  1801. int8_t sc;
  1802. for (int j = 0; j < QK_K/16; ++j) {
  1803. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1804. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1805. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1806. if (!d) {
  1807. continue;
  1808. }
  1809. for (int ii = 0; ii < 16; ++ii) {
  1810. int l = nearest_int(x[16*j + ii]/d);
  1811. l = MAX(-4, MIN(3, l));
  1812. L[16*j + ii] = l + 4;
  1813. }
  1814. }
  1815. memset(y[i].hmask, 0, QK_K/8);
  1816. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1817. int m = 0;
  1818. uint8_t hm = 1;
  1819. for (int j = 0; j < QK_K; ++j) {
  1820. if (L[j] > 3) {
  1821. y[i].hmask[m] |= hm;
  1822. L[j] -= 4;
  1823. }
  1824. if (++m == QK_K/8) {
  1825. m = 0; hm <<= 1;
  1826. }
  1827. }
  1828. for (int j = 0; j < QK_K; j += 128) {
  1829. for (int l = 0; l < 32; ++l) {
  1830. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1831. }
  1832. }
  1833. x += QK_K;
  1834. }
  1835. #endif
  1836. }
  1837. size_t quantize_q3_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1838. (void)hist;
  1839. int row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1840. if (!quant_weights) {
  1841. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1842. }
  1843. else {
  1844. char * qrow = (char *)dst;
  1845. for (int row = 0; row < nrow; ++row) {
  1846. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1847. src += n_per_row;
  1848. qrow += row_size;
  1849. }
  1850. }
  1851. return nrow * row_size;
  1852. }
  1853. // ====================== 4-bit (de)-quantization
  1854. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1855. assert(k % QK_K == 0);
  1856. const int nb = k / QK_K;
  1857. uint8_t L[QK_K];
  1858. uint8_t Laux[32];
  1859. float weights[32];
  1860. float mins[QK_K/32];
  1861. float scales[QK_K/32];
  1862. for (int i = 0; i < nb; i++) {
  1863. float max_scale = 0; // as we are deducting the min, scales are always positive
  1864. float max_min = 0;
  1865. for (int j = 0; j < QK_K/32; ++j) {
  1866. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1867. float sum_x2 = 0;
  1868. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1869. float av_x = sqrtf(sum_x2/32);
  1870. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1871. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1872. float scale = scales[j];
  1873. if (scale > max_scale) {
  1874. max_scale = scale;
  1875. }
  1876. float min = mins[j];
  1877. if (min > max_min) {
  1878. max_min = min;
  1879. }
  1880. }
  1881. #if QK_K == 256
  1882. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1883. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1884. for (int j = 0; j < QK_K/32; ++j) {
  1885. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1886. uint8_t lm = nearest_int(inv_min*mins[j]);
  1887. ls = MIN(63, ls);
  1888. lm = MIN(63, lm);
  1889. if (j < 4) {
  1890. y[i].scales[j] = ls;
  1891. y[i].scales[j+4] = lm;
  1892. } else {
  1893. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1894. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1895. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1896. }
  1897. }
  1898. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1899. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1900. uint8_t sc, m;
  1901. for (int j = 0; j < QK_K/32; ++j) {
  1902. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1903. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1904. if (!d) continue;
  1905. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1906. for (int ii = 0; ii < 32; ++ii) {
  1907. int l = nearest_int((x[32*j + ii] + dm)/d);
  1908. l = MAX(0, MIN(15, l));
  1909. L[32*j + ii] = l;
  1910. }
  1911. }
  1912. #else
  1913. const float s_factor = 15.f;
  1914. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1915. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1916. int d1 = nearest_int(inv_scale*scales[0]);
  1917. int m1 = nearest_int(inv_min*mins[0]);
  1918. int d2 = nearest_int(inv_scale*scales[1]);
  1919. int m2 = nearest_int(inv_min*mins[1]);
  1920. y[i].scales[0] = d1 | (m1 << 4);
  1921. y[i].scales[1] = d2 | (m2 << 4);
  1922. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1923. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1924. float sumlx = 0;
  1925. int suml2 = 0;
  1926. for (int j = 0; j < QK_K/32; ++j) {
  1927. const uint8_t sd = y[i].scales[j] & 0xF;
  1928. const uint8_t sm = y[i].scales[j] >> 4;
  1929. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1930. if (!d) continue;
  1931. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1932. for (int ii = 0; ii < 32; ++ii) {
  1933. int l = nearest_int((x[32*j + ii] + m)/d);
  1934. l = MAX(0, MIN(15, l));
  1935. L[32*j + ii] = l;
  1936. sumlx += (x[32*j + ii] + m)*l*sd;
  1937. suml2 += l*l*sd*sd;
  1938. }
  1939. }
  1940. if (suml2) {
  1941. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1942. }
  1943. #endif
  1944. uint8_t * q = y[i].qs;
  1945. for (int j = 0; j < QK_K; j += 64) {
  1946. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1947. q += 32;
  1948. }
  1949. x += QK_K;
  1950. }
  1951. }
  1952. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1953. assert(k % QK_K == 0);
  1954. const int nb = k / QK_K;
  1955. for (int i = 0; i < nb; i++) {
  1956. const uint8_t * q = x[i].qs;
  1957. #if QK_K == 256
  1958. const float d = GGML_FP16_TO_FP32(x[i].d);
  1959. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1960. int is = 0;
  1961. uint8_t sc, m;
  1962. for (int j = 0; j < QK_K; j += 64) {
  1963. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1964. const float d1 = d * sc; const float m1 = min * m;
  1965. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1966. const float d2 = d * sc; const float m2 = min * m;
  1967. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1968. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1969. q += 32; is += 2;
  1970. }
  1971. #else
  1972. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1973. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1974. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1975. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1976. for (int l = 0; l < 32; ++l) {
  1977. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1978. y[l+32] = d2 * (q[l] >> 4) - m2;
  1979. }
  1980. y += QK_K;
  1981. #endif
  1982. }
  1983. }
  1984. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1985. assert(k % QK_K == 0);
  1986. block_q4_K * restrict y = vy;
  1987. quantize_row_q4_K_reference(x, y, k);
  1988. }
  1989. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1990. assert(k % QK_K == 0);
  1991. (void)hist; // TODO: collect histograms
  1992. for (int j = 0; j < n; j += k) {
  1993. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  1994. quantize_row_q4_K_reference(src + j, y, k);
  1995. }
  1996. return (n/QK_K*sizeof(block_q4_K));
  1997. }
  1998. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  1999. #if QK_K != 256
  2000. (void)quant_weights;
  2001. quantize_row_q4_K_reference(x, y, n_per_row);
  2002. #else
  2003. assert(n_per_row % QK_K == 0);
  2004. const int nb = n_per_row / QK_K;
  2005. uint8_t L[QK_K];
  2006. uint8_t Laux[32];
  2007. float weights[32];
  2008. float mins[QK_K/32];
  2009. float scales[QK_K/32];
  2010. for (int i = 0; i < nb; i++) {
  2011. float sum_x2 = 0;
  2012. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2013. float sigma2 = sum_x2/QK_K;
  2014. float av_x = sqrtf(sigma2);
  2015. float max_scale = 0; // as we are deducting the min, scales are always positive
  2016. float max_min = 0;
  2017. for (int j = 0; j < QK_K/32; ++j) {
  2018. if (quant_weights) {
  2019. const float * qw = quant_weights + QK_K*i + 32*j;
  2020. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2021. } else {
  2022. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2023. }
  2024. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2025. //scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  2026. float scale = scales[j];
  2027. if (scale > max_scale) {
  2028. max_scale = scale;
  2029. }
  2030. float min = mins[j];
  2031. if (min > max_min) {
  2032. max_min = min;
  2033. }
  2034. }
  2035. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2036. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2037. for (int j = 0; j < QK_K/32; ++j) {
  2038. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2039. uint8_t lm = nearest_int(inv_min*mins[j]);
  2040. ls = MIN(63, ls);
  2041. lm = MIN(63, lm);
  2042. if (j < 4) {
  2043. y[i].scales[j] = ls;
  2044. y[i].scales[j+4] = lm;
  2045. } else {
  2046. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2047. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2048. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2049. }
  2050. }
  2051. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2052. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2053. uint8_t sc, m;
  2054. for (int j = 0; j < QK_K/32; ++j) {
  2055. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2056. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2057. if (!d) continue;
  2058. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2059. for (int ii = 0; ii < 32; ++ii) {
  2060. int l = nearest_int((x[32*j + ii] + dm)/d);
  2061. l = MAX(0, MIN(15, l));
  2062. L[32*j + ii] = l;
  2063. }
  2064. }
  2065. uint8_t * q = y[i].qs;
  2066. for (int j = 0; j < QK_K; j += 64) {
  2067. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2068. q += 32;
  2069. }
  2070. x += QK_K;
  2071. }
  2072. #endif
  2073. }
  2074. size_t quantize_q4_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2075. (void)hist;
  2076. int row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2077. if (!quant_weights) {
  2078. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2079. }
  2080. else {
  2081. char * qrow = (char *)dst;
  2082. for (int row = 0; row < nrow; ++row) {
  2083. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2084. src += n_per_row;
  2085. qrow += row_size;
  2086. }
  2087. }
  2088. return nrow * row_size;
  2089. }
  2090. // ====================== 5-bit (de)-quantization
  2091. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2092. assert(k % QK_K == 0);
  2093. const int nb = k / QK_K;
  2094. #if QK_K == 256
  2095. uint8_t L[QK_K];
  2096. float mins[QK_K/32];
  2097. float scales[QK_K/32];
  2098. float weights[32];
  2099. uint8_t Laux[32];
  2100. #else
  2101. int8_t L[QK_K];
  2102. float scales[QK_K/16];
  2103. #endif
  2104. for (int i = 0; i < nb; i++) {
  2105. #if QK_K == 256
  2106. float max_scale = 0; // as we are deducting the min, scales are always positive
  2107. float max_min = 0;
  2108. for (int j = 0; j < QK_K/32; ++j) {
  2109. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2110. float sum_x2 = 0;
  2111. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2112. float av_x = sqrtf(sum_x2/32);
  2113. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2114. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2115. float scale = scales[j];
  2116. if (scale > max_scale) {
  2117. max_scale = scale;
  2118. }
  2119. float min = mins[j];
  2120. if (min > max_min) {
  2121. max_min = min;
  2122. }
  2123. }
  2124. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2125. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2126. for (int j = 0; j < QK_K/32; ++j) {
  2127. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2128. uint8_t lm = nearest_int(inv_min*mins[j]);
  2129. ls = MIN(63, ls);
  2130. lm = MIN(63, lm);
  2131. if (j < 4) {
  2132. y[i].scales[j] = ls;
  2133. y[i].scales[j+4] = lm;
  2134. } else {
  2135. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2136. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2137. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2138. }
  2139. }
  2140. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2141. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2142. uint8_t sc, m;
  2143. for (int j = 0; j < QK_K/32; ++j) {
  2144. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2145. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2146. if (!d) continue;
  2147. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2148. for (int ii = 0; ii < 32; ++ii) {
  2149. int l = nearest_int((x[32*j + ii] + dm)/d);
  2150. l = MAX(0, MIN(31, l));
  2151. L[32*j + ii] = l;
  2152. }
  2153. }
  2154. uint8_t * restrict qh = y[i].qh;
  2155. uint8_t * restrict ql = y[i].qs;
  2156. memset(qh, 0, QK_K/8);
  2157. uint8_t m1 = 1, m2 = 2;
  2158. for (int n = 0; n < QK_K; n += 64) {
  2159. for (int j = 0; j < 32; ++j) {
  2160. int l1 = L[n + j];
  2161. if (l1 > 15) {
  2162. l1 -= 16; qh[j] |= m1;
  2163. }
  2164. int l2 = L[n + j + 32];
  2165. if (l2 > 15) {
  2166. l2 -= 16; qh[j] |= m2;
  2167. }
  2168. ql[j] = l1 | (l2 << 4);
  2169. }
  2170. m1 <<= 2; m2 <<= 2;
  2171. ql += 32;
  2172. }
  2173. #else
  2174. float max_scale = 0, amax = 0;
  2175. for (int j = 0; j < QK_K/16; ++j) {
  2176. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2177. float abs_scale = fabsf(scales[j]);
  2178. if (abs_scale > amax) {
  2179. amax = abs_scale;
  2180. max_scale = scales[j];
  2181. }
  2182. }
  2183. float iscale = -128.f/max_scale;
  2184. for (int j = 0; j < QK_K/16; ++j) {
  2185. int l = nearest_int(iscale*scales[j]);
  2186. y[i].scales[j] = MAX(-128, MIN(127, l));
  2187. }
  2188. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2189. for (int j = 0; j < QK_K/16; ++j) {
  2190. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2191. if (!d) continue;
  2192. for (int ii = 0; ii < 16; ++ii) {
  2193. int l = nearest_int(x[16*j + ii]/d);
  2194. l = MAX(-16, MIN(15, l));
  2195. L[16*j + ii] = l + 16;
  2196. }
  2197. }
  2198. uint8_t * restrict qh = y[i].qh;
  2199. uint8_t * restrict ql = y[i].qs;
  2200. memset(qh, 0, QK_K/8);
  2201. for (int j = 0; j < 32; ++j) {
  2202. int jm = j%8;
  2203. int is = j/8;
  2204. int l1 = L[j];
  2205. if (l1 > 15) {
  2206. l1 -= 16; qh[jm] |= (1 << is);
  2207. }
  2208. int l2 = L[j + 32];
  2209. if (l2 > 15) {
  2210. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2211. }
  2212. ql[j] = l1 | (l2 << 4);
  2213. }
  2214. #endif
  2215. x += QK_K;
  2216. }
  2217. }
  2218. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2219. assert(k % QK_K == 0);
  2220. const int nb = k / QK_K;
  2221. for (int i = 0; i < nb; i++) {
  2222. const uint8_t * ql = x[i].qs;
  2223. const uint8_t * qh = x[i].qh;
  2224. #if QK_K == 256
  2225. const float d = GGML_FP16_TO_FP32(x[i].d);
  2226. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2227. int is = 0;
  2228. uint8_t sc, m;
  2229. uint8_t u1 = 1, u2 = 2;
  2230. for (int j = 0; j < QK_K; j += 64) {
  2231. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2232. const float d1 = d * sc; const float m1 = min * m;
  2233. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2234. const float d2 = d * sc; const float m2 = min * m;
  2235. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2236. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2237. ql += 32; is += 2;
  2238. u1 <<= 2; u2 <<= 2;
  2239. }
  2240. #else
  2241. float d = GGML_FP16_TO_FP32(x[i].d);
  2242. const int8_t * restrict s = x[i].scales;
  2243. for (int l = 0; l < 8; ++l) {
  2244. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2245. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2246. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2247. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2248. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2249. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2250. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2251. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2252. }
  2253. y += QK_K;
  2254. #endif
  2255. }
  2256. }
  2257. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2258. assert(k % QK_K == 0);
  2259. block_q5_K * restrict y = vy;
  2260. quantize_row_q5_K_reference(x, y, k);
  2261. }
  2262. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2263. assert(k % QK_K == 0);
  2264. (void)hist; // TODO: collect histograms
  2265. for (int j = 0; j < n; j += k) {
  2266. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  2267. quantize_row_q5_K_reference(src + j, y, k);
  2268. }
  2269. return (n/QK_K*sizeof(block_q5_K));
  2270. }
  2271. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2272. #if QK_K != 256
  2273. (void)quant_weights;
  2274. quantize_row_q5_K_reference(x, y, n_per_row);
  2275. #else
  2276. assert(n_per_row % QK_K == 0);
  2277. const int nb = n_per_row / QK_K;
  2278. uint8_t L[QK_K];
  2279. float mins[QK_K/32];
  2280. float scales[QK_K/32];
  2281. float weights[32];
  2282. uint8_t Laux[32];
  2283. for (int i = 0; i < nb; i++) {
  2284. float sum_x2 = 0;
  2285. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2286. float sigma2 = sum_x2/QK_K;
  2287. float av_x = sqrtf(sigma2);
  2288. float max_scale = 0; // as we are deducting the min, scales are always positive
  2289. float max_min = 0;
  2290. for (int j = 0; j < QK_K/32; ++j) {
  2291. if (quant_weights) {
  2292. const float * qw = quant_weights + QK_K*i + 32*j;
  2293. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2294. } else {
  2295. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2296. }
  2297. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2298. float scale = scales[j];
  2299. if (scale > max_scale) {
  2300. max_scale = scale;
  2301. }
  2302. float min = mins[j];
  2303. if (min > max_min) {
  2304. max_min = min;
  2305. }
  2306. }
  2307. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2308. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2309. for (int j = 0; j < QK_K/32; ++j) {
  2310. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2311. uint8_t lm = nearest_int(inv_min*mins[j]);
  2312. ls = MIN(63, ls);
  2313. lm = MIN(63, lm);
  2314. if (j < 4) {
  2315. y[i].scales[j] = ls;
  2316. y[i].scales[j+4] = lm;
  2317. } else {
  2318. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2319. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2320. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2321. }
  2322. }
  2323. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2324. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2325. uint8_t sc, m;
  2326. for (int j = 0; j < QK_K/32; ++j) {
  2327. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2328. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2329. if (!d) continue;
  2330. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2331. for (int ii = 0; ii < 32; ++ii) {
  2332. int l = nearest_int((x[32*j + ii] + dm)/d);
  2333. l = MAX(0, MIN(31, l));
  2334. L[32*j + ii] = l;
  2335. }
  2336. }
  2337. uint8_t * restrict qh = y[i].qh;
  2338. uint8_t * restrict ql = y[i].qs;
  2339. memset(qh, 0, QK_K/8);
  2340. uint8_t m1 = 1, m2 = 2;
  2341. for (int n = 0; n < QK_K; n += 64) {
  2342. for (int j = 0; j < 32; ++j) {
  2343. int l1 = L[n + j];
  2344. if (l1 > 15) {
  2345. l1 -= 16; qh[j] |= m1;
  2346. }
  2347. int l2 = L[n + j + 32];
  2348. if (l2 > 15) {
  2349. l2 -= 16; qh[j] |= m2;
  2350. }
  2351. ql[j] = l1 | (l2 << 4);
  2352. }
  2353. m1 <<= 2; m2 <<= 2;
  2354. ql += 32;
  2355. }
  2356. x += QK_K;
  2357. }
  2358. #endif
  2359. }
  2360. size_t quantize_q5_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2361. (void)hist;
  2362. int row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2363. if (!quant_weights) {
  2364. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2365. }
  2366. else {
  2367. char * qrow = (char *)dst;
  2368. for (int row = 0; row < nrow; ++row) {
  2369. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2370. src += n_per_row;
  2371. qrow += row_size;
  2372. }
  2373. }
  2374. return nrow * row_size;
  2375. }
  2376. // ====================== 6-bit (de)-quantization
  2377. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2378. assert(k % QK_K == 0);
  2379. const int nb = k / QK_K;
  2380. int8_t L[QK_K];
  2381. float scales[QK_K/16];
  2382. for (int i = 0; i < nb; i++) {
  2383. float max_scale = 0;
  2384. float max_abs_scale = 0;
  2385. for (int ib = 0; ib < QK_K/16; ++ib) {
  2386. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2387. scales[ib] = scale;
  2388. const float abs_scale = fabsf(scale);
  2389. if (abs_scale > max_abs_scale) {
  2390. max_abs_scale = abs_scale;
  2391. max_scale = scale;
  2392. }
  2393. }
  2394. if (!max_abs_scale) {
  2395. memset(&y[i], 0, sizeof(block_q6_K));
  2396. y[i].d = GGML_FP32_TO_FP16(0.f);
  2397. x += QK_K;
  2398. continue;
  2399. }
  2400. float iscale = -128.f/max_scale;
  2401. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2402. for (int ib = 0; ib < QK_K/16; ++ib) {
  2403. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2404. }
  2405. for (int j = 0; j < QK_K/16; ++j) {
  2406. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2407. if (!d) {
  2408. continue;
  2409. }
  2410. for (int ii = 0; ii < 16; ++ii) {
  2411. int l = nearest_int(x[16*j + ii]/d);
  2412. l = MAX(-32, MIN(31, l));
  2413. L[16*j + ii] = l + 32;
  2414. }
  2415. }
  2416. uint8_t * restrict ql = y[i].ql;
  2417. uint8_t * restrict qh = y[i].qh;
  2418. #if QK_K == 256
  2419. for (int j = 0; j < QK_K; j += 128) {
  2420. for (int l = 0; l < 32; ++l) {
  2421. const uint8_t q1 = L[j + l + 0] & 0xF;
  2422. const uint8_t q2 = L[j + l + 32] & 0xF;
  2423. const uint8_t q3 = L[j + l + 64] & 0xF;
  2424. const uint8_t q4 = L[j + l + 96] & 0xF;
  2425. ql[l+ 0] = q1 | (q3 << 4);
  2426. ql[l+32] = q2 | (q4 << 4);
  2427. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2428. }
  2429. ql += 64;
  2430. qh += 32;
  2431. }
  2432. #else
  2433. for (int l = 0; l < 32; ++l) {
  2434. const uint8_t q1 = L[l + 0] & 0xF;
  2435. const uint8_t q2 = L[l + 32] & 0xF;
  2436. ql[l] = q1 | (q2 << 4);
  2437. }
  2438. for (int l = 0; l < 16; ++l) {
  2439. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2440. }
  2441. #endif
  2442. x += QK_K;
  2443. }
  2444. }
  2445. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2446. assert(k % QK_K == 0);
  2447. const int nb = k / QK_K;
  2448. for (int i = 0; i < nb; i++) {
  2449. const float d = GGML_FP16_TO_FP32(x[i].d);
  2450. const uint8_t * restrict ql = x[i].ql;
  2451. const uint8_t * restrict qh = x[i].qh;
  2452. const int8_t * restrict sc = x[i].scales;
  2453. #if QK_K == 256
  2454. for (int n = 0; n < QK_K; n += 128) {
  2455. for (int l = 0; l < 32; ++l) {
  2456. int is = l/16;
  2457. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2458. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2459. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2460. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2461. y[l + 0] = d * sc[is + 0] * q1;
  2462. y[l + 32] = d * sc[is + 2] * q2;
  2463. y[l + 64] = d * sc[is + 4] * q3;
  2464. y[l + 96] = d * sc[is + 6] * q4;
  2465. }
  2466. y += 128;
  2467. ql += 64;
  2468. qh += 32;
  2469. sc += 8;
  2470. }
  2471. #else
  2472. for (int l = 0; l < 16; ++l) {
  2473. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2474. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2475. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2476. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2477. y[l+ 0] = d * sc[0] * q1;
  2478. y[l+16] = d * sc[1] * q2;
  2479. y[l+32] = d * sc[2] * q3;
  2480. y[l+48] = d * sc[3] * q4;
  2481. }
  2482. y += 64;
  2483. #endif
  2484. }
  2485. }
  2486. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2487. assert(k % QK_K == 0);
  2488. block_q6_K * restrict y = vy;
  2489. quantize_row_q6_K_reference(x, y, k);
  2490. }
  2491. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  2492. assert(k % QK_K == 0);
  2493. (void)hist; // TODO: collect histograms
  2494. for (int j = 0; j < n; j += k) {
  2495. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  2496. quantize_row_q6_K_reference(src + j, y, k);
  2497. }
  2498. return (n/QK_K*sizeof(block_q6_K));
  2499. }
  2500. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2501. #if QK_K != 256
  2502. (void)quant_weights;
  2503. quantize_row_q6_K_reference(x, y, n_per_row);
  2504. #else
  2505. assert(n_per_row % QK_K == 0);
  2506. const int nb = n_per_row / QK_K;
  2507. int8_t L[QK_K];
  2508. float scales[QK_K/16];
  2509. //float weights[16];
  2510. for (int i = 0; i < nb; i++) {
  2511. //float sum_x2 = 0;
  2512. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2513. //float sigma2 = sum_x2/QK_K;
  2514. float max_scale = 0;
  2515. float max_abs_scale = 0;
  2516. for (int ib = 0; ib < QK_K/16; ++ib) {
  2517. float scale;
  2518. if (quant_weights) {
  2519. const float * qw = quant_weights + QK_K*i + 16*ib;
  2520. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2521. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2522. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2523. } else {
  2524. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2525. }
  2526. scales[ib] = scale;
  2527. const float abs_scale = fabsf(scale);
  2528. if (abs_scale > max_abs_scale) {
  2529. max_abs_scale = abs_scale;
  2530. max_scale = scale;
  2531. }
  2532. }
  2533. if (!max_abs_scale) {
  2534. memset(&y[i], 0, sizeof(block_q6_K));
  2535. y[i].d = GGML_FP32_TO_FP16(0.f);
  2536. x += QK_K;
  2537. continue;
  2538. }
  2539. float iscale = -128.f/max_scale;
  2540. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2541. for (int ib = 0; ib < QK_K/16; ++ib) {
  2542. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2543. }
  2544. for (int j = 0; j < QK_K/16; ++j) {
  2545. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2546. if (!d) {
  2547. continue;
  2548. }
  2549. for (int ii = 0; ii < 16; ++ii) {
  2550. int l = nearest_int(x[16*j + ii]/d);
  2551. l = MAX(-32, MIN(31, l));
  2552. L[16*j + ii] = l + 32;
  2553. }
  2554. }
  2555. uint8_t * restrict ql = y[i].ql;
  2556. uint8_t * restrict qh = y[i].qh;
  2557. for (int j = 0; j < QK_K; j += 128) {
  2558. for (int l = 0; l < 32; ++l) {
  2559. const uint8_t q1 = L[j + l + 0] & 0xF;
  2560. const uint8_t q2 = L[j + l + 32] & 0xF;
  2561. const uint8_t q3 = L[j + l + 64] & 0xF;
  2562. const uint8_t q4 = L[j + l + 96] & 0xF;
  2563. ql[l+ 0] = q1 | (q3 << 4);
  2564. ql[l+32] = q2 | (q4 << 4);
  2565. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2566. }
  2567. ql += 64;
  2568. qh += 32;
  2569. }
  2570. x += QK_K;
  2571. }
  2572. #endif
  2573. }
  2574. size_t quantize_q6_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2575. (void)hist;
  2576. int row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2577. if (!quant_weights) {
  2578. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2579. }
  2580. else {
  2581. char * qrow = (char *)dst;
  2582. for (int row = 0; row < nrow; ++row) {
  2583. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2584. src += n_per_row;
  2585. qrow += row_size;
  2586. }
  2587. }
  2588. return nrow * row_size;
  2589. }
  2590. // ====================== "True" 2-bit (de)-quantization
  2591. static const uint64_t iq2xxs_grid[256] = {
  2592. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2593. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2594. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2595. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2596. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2597. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2598. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2599. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2600. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2601. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2602. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2603. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2604. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2605. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2606. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2607. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2608. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2609. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2610. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2611. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2612. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2613. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2614. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2615. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2616. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2617. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2618. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2619. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2620. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2621. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2622. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2623. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2624. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2625. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2626. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2627. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2628. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2629. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2630. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2631. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2632. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2633. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2634. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2635. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2636. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2637. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2638. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2639. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2640. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2641. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2642. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2643. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2644. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2645. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2646. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2647. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2648. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2649. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2650. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2651. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2652. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2653. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2654. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2655. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2656. };
  2657. static const uint64_t iq2xs_grid[512] = {
  2658. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2659. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2660. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2661. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2662. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2663. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2664. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2665. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2666. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2667. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2668. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2669. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2670. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2671. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2672. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2673. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2674. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2675. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2676. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2677. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2678. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2679. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2680. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2681. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2682. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2683. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2684. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2685. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2686. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2687. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2688. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2689. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2690. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2691. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2692. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2693. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2694. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2695. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2696. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2697. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2698. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2699. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2700. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2701. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2702. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2703. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2704. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2705. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2706. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2707. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2708. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2709. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2710. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2711. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2712. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2713. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2714. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2715. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2716. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2717. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2718. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2719. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2720. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2721. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2722. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2723. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2724. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2725. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2726. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2727. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2728. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2729. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2730. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2731. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2732. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2733. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2734. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2735. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2736. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2737. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2738. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2739. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2740. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2741. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2742. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2743. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2744. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2745. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2746. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2747. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2748. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2749. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2750. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2751. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2752. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2753. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2754. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2755. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2756. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2757. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2758. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2759. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2760. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2761. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2762. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2763. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2764. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2765. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2766. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2767. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2768. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2769. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2770. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2771. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2772. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2773. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2774. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2775. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2776. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2777. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2778. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2779. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2780. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2781. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2782. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2783. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2784. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2785. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2786. };
  2787. static const uint8_t ksigns_iq2xs[128] = {
  2788. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2789. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2790. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2791. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2792. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2793. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2794. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2795. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2796. };
  2797. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2798. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2799. assert(k % QK_K == 0);
  2800. const int nb = k / QK_K;
  2801. uint32_t aux32[2];
  2802. const uint8_t * aux8 = (const uint8_t *)aux32;
  2803. for (int i = 0; i < nb; i++) {
  2804. const float d = GGML_FP16_TO_FP32(x[i].d);
  2805. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2806. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2807. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2808. for (int l = 0; l < 4; ++l) {
  2809. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2810. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2811. for (int j = 0; j < 8; ++j) {
  2812. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2813. }
  2814. y += 8;
  2815. }
  2816. }
  2817. }
  2818. }
  2819. // ====================== 2.3125 bpw (de)-quantization
  2820. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2821. assert(k % QK_K == 0);
  2822. const int nb = k / QK_K;
  2823. float db[2];
  2824. for (int i = 0; i < nb; i++) {
  2825. const float d = GGML_FP16_TO_FP32(x[i].d);
  2826. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2827. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2828. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2829. for (int l = 0; l < 4; ++l) {
  2830. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2831. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2832. for (int j = 0; j < 8; ++j) {
  2833. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2834. }
  2835. y += 8;
  2836. }
  2837. }
  2838. }
  2839. }
  2840. //===================================== Q8_K ==============================================
  2841. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  2842. assert(k % QK_K == 0);
  2843. const int nb = k / QK_K;
  2844. for (int i = 0; i < nb; i++) {
  2845. float max = 0;
  2846. float amax = 0;
  2847. for (int j = 0; j < QK_K; ++j) {
  2848. float ax = fabsf(x[j]);
  2849. if (ax > amax) {
  2850. amax = ax; max = x[j];
  2851. }
  2852. }
  2853. if (!amax) {
  2854. y[i].d = 0;
  2855. memset(y[i].qs, 0, QK_K);
  2856. x += QK_K;
  2857. continue;
  2858. }
  2859. //const float iscale = -128.f/max;
  2860. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2861. const float iscale = -127.f/max;
  2862. for (int j = 0; j < QK_K; ++j) {
  2863. int v = nearest_int(iscale*x[j]);
  2864. y[i].qs[j] = MIN(127, v);
  2865. }
  2866. for (int j = 0; j < QK_K/16; ++j) {
  2867. int sum = 0;
  2868. for (int ii = 0; ii < 16; ++ii) {
  2869. sum += y[i].qs[j*16 + ii];
  2870. }
  2871. y[i].bsums[j] = sum;
  2872. }
  2873. y[i].d = 1/iscale;
  2874. x += QK_K;
  2875. }
  2876. }
  2877. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  2878. assert(k % QK_K == 0);
  2879. const int nb = k / QK_K;
  2880. for (int i = 0; i < nb; i++) {
  2881. for (int j = 0; j < QK_K; ++j) {
  2882. *y++ = x[i].d * x[i].qs[j];
  2883. }
  2884. }
  2885. }
  2886. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  2887. quantize_row_q8_K_reference(x, y, k);
  2888. }
  2889. //===================================== Dot ptoducts =================================
  2890. //
  2891. // Helper functions
  2892. //
  2893. #if __AVX__ || __AVX2__ || __AVX512F__
  2894. // shuffles to pick the required scales in dot products
  2895. static inline __m256i get_scale_shuffle_q3k(int i) {
  2896. static const uint8_t k_shuffle[128] = {
  2897. 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,
  2898. 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,
  2899. 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,
  2900. 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,
  2901. };
  2902. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2903. }
  2904. static inline __m256i get_scale_shuffle_k4(int i) {
  2905. static const uint8_t k_shuffle[256] = {
  2906. 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,
  2907. 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,
  2908. 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,
  2909. 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,
  2910. 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,
  2911. 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,
  2912. 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,
  2913. 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
  2914. };
  2915. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2916. }
  2917. static inline __m128i get_scale_shuffle(int i) {
  2918. static const uint8_t k_shuffle[128] = {
  2919. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2920. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2921. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2922. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2923. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2924. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2925. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2926. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2927. };
  2928. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2929. }
  2930. #endif
  2931. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2932. const int qk = QK8_0;
  2933. const int nb = n / qk;
  2934. assert(n % qk == 0);
  2935. const block_q4_0 * restrict x = vx;
  2936. const block_q8_0 * restrict y = vy;
  2937. #if defined(__ARM_NEON)
  2938. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2939. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2940. assert(nb % 2 == 0); // TODO: handle odd nb
  2941. for (int i = 0; i < nb; i += 2) {
  2942. const block_q4_0 * restrict x0 = &x[i + 0];
  2943. const block_q4_0 * restrict x1 = &x[i + 1];
  2944. const block_q8_0 * restrict y0 = &y[i + 0];
  2945. const block_q8_0 * restrict y1 = &y[i + 1];
  2946. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2947. const int8x16_t s8b = vdupq_n_s8(0x8);
  2948. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2949. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2950. // 4-bit -> 8-bit
  2951. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2952. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2953. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2954. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2955. // sub 8
  2956. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2957. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2958. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  2959. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  2960. // load y
  2961. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2962. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2963. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2964. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2965. // dot product into int32x4_t
  2966. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  2967. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  2968. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2969. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2970. }
  2971. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2972. #elif defined(__AVX2__)
  2973. // Initialize accumulator with zeros
  2974. __m256 acc = _mm256_setzero_ps();
  2975. // Main loop
  2976. for (int i = 0; i < nb; ++i) {
  2977. /* Compute combined scale for the block */
  2978. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2979. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2980. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  2981. const __m256i off = _mm256_set1_epi8( 8 );
  2982. bx = _mm256_sub_epi8( bx, off );
  2983. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2984. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2985. /* Multiply q with scale and accumulate */
  2986. acc = _mm256_fmadd_ps( d, q, acc );
  2987. }
  2988. *s = hsum_float_8(acc);
  2989. #elif defined(__AVX__)
  2990. // Initialize accumulator with zeros
  2991. __m256 acc = _mm256_setzero_ps();
  2992. // Main loop
  2993. for (int i = 0; i < nb; ++i) {
  2994. // Compute combined scale for the block
  2995. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2996. const __m128i lowMask = _mm_set1_epi8(0xF);
  2997. const __m128i off = _mm_set1_epi8(8);
  2998. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  2999. __m128i bx = _mm_and_si128(lowMask, tmp);
  3000. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  3001. bx = _mm_sub_epi8(bx, off);
  3002. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  3003. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3004. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3005. bx = _mm_sub_epi8(bx, off);
  3006. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  3007. // Convert int32_t to float
  3008. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3009. // Apply the scale, and accumulate
  3010. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3011. }
  3012. *s = hsum_float_8(acc);
  3013. #elif defined(__SSSE3__)
  3014. // set constants
  3015. const __m128i lowMask = _mm_set1_epi8(0xF);
  3016. const __m128i off = _mm_set1_epi8(8);
  3017. // Initialize accumulator with zeros
  3018. __m128 acc_0 = _mm_setzero_ps();
  3019. __m128 acc_1 = _mm_setzero_ps();
  3020. __m128 acc_2 = _mm_setzero_ps();
  3021. __m128 acc_3 = _mm_setzero_ps();
  3022. // First round without accumulation
  3023. {
  3024. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3025. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3026. // Compute combined scale for the block 0 and 1
  3027. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3028. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3029. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3030. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3031. bx_0 = _mm_sub_epi8(bx_0, off);
  3032. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3033. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3034. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3035. bx_1 = _mm_sub_epi8(bx_1, off);
  3036. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3037. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3038. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3039. // Compute combined scale for the block 2 and 3
  3040. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3041. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3042. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3043. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3044. bx_2 = _mm_sub_epi8(bx_2, off);
  3045. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3046. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3047. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3048. bx_3 = _mm_sub_epi8(bx_3, off);
  3049. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3050. // Convert int32_t to float
  3051. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3052. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3053. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3054. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3055. // Apply the scale
  3056. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3057. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3058. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3059. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3060. }
  3061. assert(nb % 2 == 0); // TODO: handle odd nb
  3062. // Main loop
  3063. for (int i = 2; i < nb; i+=2) {
  3064. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3065. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3066. // Compute combined scale for the block 0 and 1
  3067. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3068. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3069. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3070. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3071. bx_0 = _mm_sub_epi8(bx_0, off);
  3072. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3073. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3074. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3075. bx_1 = _mm_sub_epi8(bx_1, off);
  3076. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3077. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3078. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3079. // Compute combined scale for the block 2 and 3
  3080. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3081. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3082. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3083. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3084. bx_2 = _mm_sub_epi8(bx_2, off);
  3085. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3086. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3087. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3088. bx_3 = _mm_sub_epi8(bx_3, off);
  3089. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3090. // Convert int32_t to float
  3091. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3092. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3093. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3094. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3095. // Apply the scale
  3096. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3097. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3098. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3099. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3100. // Acummulate
  3101. acc_0 = _mm_add_ps(p0_d, acc_0);
  3102. acc_1 = _mm_add_ps(p1_d, acc_1);
  3103. acc_2 = _mm_add_ps(p2_d, acc_2);
  3104. acc_3 = _mm_add_ps(p3_d, acc_3);
  3105. }
  3106. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3107. #elif defined(__riscv_v_intrinsic)
  3108. float sumf = 0.0;
  3109. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3110. for (int i = 0; i < nb; i++) {
  3111. // load elements
  3112. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3113. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3114. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3115. // mask and store lower part of x, and then upper part
  3116. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3117. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3118. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3119. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3120. // subtract offset
  3121. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3122. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3123. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3124. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3125. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3126. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3127. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3128. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3129. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3130. }
  3131. *s = sumf;
  3132. #else
  3133. // scalar
  3134. float sumf = 0.0;
  3135. for (int i = 0; i < nb; i++) {
  3136. int sumi = 0;
  3137. for (int j = 0; j < qk/2; ++j) {
  3138. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3139. const int v1 = (x[i].qs[j] >> 4) - 8;
  3140. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3141. }
  3142. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3143. }
  3144. *s = sumf;
  3145. #endif
  3146. }
  3147. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3148. const int qk = QK8_1;
  3149. const int nb = n / qk;
  3150. assert(n % qk == 0);
  3151. const block_q4_1 * restrict x = vx;
  3152. const block_q8_1 * restrict y = vy;
  3153. // TODO: add WASM SIMD
  3154. #if defined(__ARM_NEON)
  3155. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3156. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3157. float summs = 0;
  3158. assert(nb % 2 == 0); // TODO: handle odd nb
  3159. for (int i = 0; i < nb; i += 2) {
  3160. const block_q4_1 * restrict x0 = &x[i + 0];
  3161. const block_q4_1 * restrict x1 = &x[i + 1];
  3162. const block_q8_1 * restrict y0 = &y[i + 0];
  3163. const block_q8_1 * restrict y1 = &y[i + 1];
  3164. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3165. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3166. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3167. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3168. // 4-bit -> 8-bit
  3169. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3170. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3171. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3172. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3173. // load y
  3174. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3175. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3176. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3177. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3178. // dot product into int32x4_t
  3179. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3180. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3181. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3182. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3183. }
  3184. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3185. #elif defined(__AVX2__) || defined(__AVX__)
  3186. // Initialize accumulator with zeros
  3187. __m256 acc = _mm256_setzero_ps();
  3188. float summs = 0;
  3189. // Main loop
  3190. for (int i = 0; i < nb; ++i) {
  3191. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3192. const float d1 = y[i].d;
  3193. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3194. const __m256 d0v = _mm256_set1_ps( d0 );
  3195. const __m256 d1v = _mm256_set1_ps( d1 );
  3196. // Compute combined scales
  3197. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3198. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3199. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3200. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3201. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  3202. // Accumulate d0*d1*x*y
  3203. #if defined(__AVX2__)
  3204. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3205. #else
  3206. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3207. #endif
  3208. }
  3209. *s = hsum_float_8(acc) + summs;
  3210. #elif defined(__riscv_v_intrinsic)
  3211. float sumf = 0.0;
  3212. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3213. for (int i = 0; i < nb; i++) {
  3214. // load elements
  3215. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3216. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3217. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3218. // mask and store lower part of x, and then upper part
  3219. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3220. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3221. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3222. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3223. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3224. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3225. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3226. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3227. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3228. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3229. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3230. }
  3231. *s = sumf;
  3232. #else
  3233. // scalar
  3234. float sumf = 0.0;
  3235. for (int i = 0; i < nb; i++) {
  3236. int sumi = 0;
  3237. for (int j = 0; j < qk/2; ++j) {
  3238. const int v0 = (x[i].qs[j] & 0x0F);
  3239. const int v1 = (x[i].qs[j] >> 4);
  3240. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3241. }
  3242. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3243. }
  3244. *s = sumf;
  3245. #endif
  3246. }
  3247. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3248. const int qk = QK8_0;
  3249. const int nb = n / qk;
  3250. assert(n % qk == 0);
  3251. assert(qk == QK5_0);
  3252. const block_q5_0 * restrict x = vx;
  3253. const block_q8_0 * restrict y = vy;
  3254. #if defined(__ARM_NEON)
  3255. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3256. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3257. uint32_t qh0;
  3258. uint32_t qh1;
  3259. uint64_t tmp0[4];
  3260. uint64_t tmp1[4];
  3261. assert(nb % 2 == 0); // TODO: handle odd nb
  3262. for (int i = 0; i < nb; i += 2) {
  3263. const block_q5_0 * restrict x0 = &x[i];
  3264. const block_q5_0 * restrict x1 = &x[i + 1];
  3265. const block_q8_0 * restrict y0 = &y[i];
  3266. const block_q8_0 * restrict y1 = &y[i + 1];
  3267. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3268. // extract the 5th bit via lookup table ((!b) << 4)
  3269. memcpy(&qh0, x0->qh, sizeof(qh0));
  3270. memcpy(&qh1, x1->qh, sizeof(qh1));
  3271. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3272. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3273. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3274. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3275. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3276. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3277. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3278. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3279. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3280. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3281. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3282. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3283. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3284. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3285. // 4-bit -> 8-bit
  3286. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3287. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3288. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3289. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3290. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3291. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3292. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3293. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3294. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3295. // load y
  3296. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3297. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3298. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3299. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3300. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3301. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3302. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3303. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3304. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3305. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3306. }
  3307. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3308. #elif defined(__wasm_simd128__)
  3309. v128_t sumv = wasm_f32x4_splat(0.0f);
  3310. uint32_t qh;
  3311. uint64_t tmp[4];
  3312. // TODO: check if unrolling this is better
  3313. for (int i = 0; i < nb; ++i) {
  3314. const block_q5_0 * restrict x0 = &x[i];
  3315. const block_q8_0 * restrict y0 = &y[i];
  3316. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3317. // extract the 5th bit
  3318. memcpy(&qh, x0->qh, sizeof(qh));
  3319. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3320. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3321. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3322. tmp[3] = table_b2b_1[(qh >> 24) ];
  3323. const v128_t qhl = wasm_v128_load(tmp + 0);
  3324. const v128_t qhh = wasm_v128_load(tmp + 2);
  3325. const v128_t v0 = wasm_v128_load(x0->qs);
  3326. // 4-bit -> 8-bit
  3327. const v128_t v0l = wasm_v128_and (v0, m4b);
  3328. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3329. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3330. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3331. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3332. // load y
  3333. const v128_t v1l = wasm_v128_load(y0->qs);
  3334. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3335. // int8x16 -> int16x8
  3336. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3337. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3338. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3339. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3340. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3341. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3342. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3343. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3344. // dot product
  3345. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3346. wasm_i32x4_add(
  3347. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3348. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3349. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3350. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3351. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3352. }
  3353. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3354. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3355. #elif defined(__AVX2__)
  3356. // Initialize accumulator with zeros
  3357. __m256 acc = _mm256_setzero_ps();
  3358. // Main loop
  3359. for (int i = 0; i < nb; i++) {
  3360. /* Compute combined scale for the block */
  3361. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3362. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3363. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3364. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3365. bx = _mm256_or_si256(bx, bxhi);
  3366. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3367. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3368. /* Multiply q with scale and accumulate */
  3369. acc = _mm256_fmadd_ps(d, q, acc);
  3370. }
  3371. *s = hsum_float_8(acc);
  3372. #elif defined(__AVX__)
  3373. // Initialize accumulator with zeros
  3374. __m256 acc = _mm256_setzero_ps();
  3375. __m128i mask = _mm_set1_epi8((char)0xF0);
  3376. // Main loop
  3377. for (int i = 0; i < nb; i++) {
  3378. /* Compute combined scale for the block */
  3379. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3380. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3381. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3382. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3383. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3384. bxhil = _mm_andnot_si128(bxhil, mask);
  3385. bxhih = _mm_andnot_si128(bxhih, mask);
  3386. __m128i bxl = _mm256_castsi256_si128(bx);
  3387. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3388. bxl = _mm_or_si128(bxl, bxhil);
  3389. bxh = _mm_or_si128(bxh, bxhih);
  3390. bx = MM256_SET_M128I(bxh, bxl);
  3391. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3392. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3393. /* Multiply q with scale and accumulate */
  3394. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3395. }
  3396. *s = hsum_float_8(acc);
  3397. #elif defined(__riscv_v_intrinsic)
  3398. float sumf = 0.0;
  3399. uint32_t qh;
  3400. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3401. // These temporary registers are for masking and shift operations
  3402. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3403. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3404. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3405. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3406. for (int i = 0; i < nb; i++) {
  3407. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3408. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3409. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3410. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3411. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3412. // ((qh & (1u << (j + 16))) >> (j + 12));
  3413. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3414. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3415. // narrowing
  3416. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3417. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3418. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3419. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3420. // load
  3421. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3422. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3423. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3424. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3425. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3426. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3427. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3428. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3429. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3430. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3431. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3432. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3433. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3434. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3435. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3436. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3437. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3438. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3439. }
  3440. *s = sumf;
  3441. #else
  3442. // scalar
  3443. float sumf = 0.0;
  3444. for (int i = 0; i < nb; i++) {
  3445. uint32_t qh;
  3446. memcpy(&qh, x[i].qh, sizeof(qh));
  3447. int sumi = 0;
  3448. for (int j = 0; j < qk/2; ++j) {
  3449. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3450. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3451. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3452. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3453. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3454. }
  3455. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3456. }
  3457. *s = sumf;
  3458. #endif
  3459. }
  3460. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3461. const int qk = QK8_1;
  3462. const int nb = n / qk;
  3463. assert(n % qk == 0);
  3464. assert(qk == QK5_1);
  3465. const block_q5_1 * restrict x = vx;
  3466. const block_q8_1 * restrict y = vy;
  3467. #if defined(__ARM_NEON)
  3468. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3469. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3470. float summs0 = 0.0f;
  3471. float summs1 = 0.0f;
  3472. uint32_t qh0;
  3473. uint32_t qh1;
  3474. uint64_t tmp0[4];
  3475. uint64_t tmp1[4];
  3476. assert(nb % 2 == 0); // TODO: handle odd nb
  3477. for (int i = 0; i < nb; i += 2) {
  3478. const block_q5_1 * restrict x0 = &x[i];
  3479. const block_q5_1 * restrict x1 = &x[i + 1];
  3480. const block_q8_1 * restrict y0 = &y[i];
  3481. const block_q8_1 * restrict y1 = &y[i + 1];
  3482. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3483. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3484. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3485. // extract the 5th bit via lookup table ((b) << 4)
  3486. memcpy(&qh0, x0->qh, sizeof(qh0));
  3487. memcpy(&qh1, x1->qh, sizeof(qh1));
  3488. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3489. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3490. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3491. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3492. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3493. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3494. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3495. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3496. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3497. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3498. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3499. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3500. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3501. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3502. // 4-bit -> 8-bit
  3503. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3504. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3505. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3506. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3507. // add high bit
  3508. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3509. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3510. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3511. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3512. // load y
  3513. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3514. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3515. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3516. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3517. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3518. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3519. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3520. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3521. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3522. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3523. }
  3524. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3525. #elif defined(__wasm_simd128__)
  3526. v128_t sumv = wasm_f32x4_splat(0.0f);
  3527. float summs = 0.0f;
  3528. uint32_t qh;
  3529. uint64_t tmp[4];
  3530. // TODO: check if unrolling this is better
  3531. for (int i = 0; i < nb; ++i) {
  3532. const block_q5_1 * restrict x0 = &x[i];
  3533. const block_q8_1 * restrict y0 = &y[i];
  3534. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3535. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3536. // extract the 5th bit
  3537. memcpy(&qh, x0->qh, sizeof(qh));
  3538. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3539. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3540. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3541. tmp[3] = table_b2b_0[(qh >> 24) ];
  3542. const v128_t qhl = wasm_v128_load(tmp + 0);
  3543. const v128_t qhh = wasm_v128_load(tmp + 2);
  3544. const v128_t v0 = wasm_v128_load(x0->qs);
  3545. // 4-bit -> 8-bit
  3546. const v128_t v0l = wasm_v128_and (v0, m4b);
  3547. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3548. // add high bit
  3549. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3550. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3551. // load y
  3552. const v128_t v1l = wasm_v128_load(y0->qs);
  3553. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3554. // int8x16 -> int16x8
  3555. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3556. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3557. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3558. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3559. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3560. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3561. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3562. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3563. // dot product
  3564. sumv = wasm_f32x4_add(sumv,
  3565. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3566. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3567. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3568. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3569. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3570. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3571. }
  3572. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3573. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3574. #elif defined(__AVX2__)
  3575. // Initialize accumulator with zeros
  3576. __m256 acc = _mm256_setzero_ps();
  3577. float summs = 0.0f;
  3578. // Main loop
  3579. for (int i = 0; i < nb; i++) {
  3580. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3581. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3582. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3583. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3584. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3585. bx = _mm256_or_si256(bx, bxhi);
  3586. const __m256 dy = _mm256_set1_ps(y[i].d);
  3587. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3588. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3589. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3590. }
  3591. *s = hsum_float_8(acc) + summs;
  3592. #elif defined(__AVX__)
  3593. // Initialize accumulator with zeros
  3594. __m256 acc = _mm256_setzero_ps();
  3595. __m128i mask = _mm_set1_epi8(0x10);
  3596. float summs = 0.0f;
  3597. // Main loop
  3598. for (int i = 0; i < nb; i++) {
  3599. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3600. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3601. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3602. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3603. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3604. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3605. bxhil = _mm_and_si128(bxhil, mask);
  3606. bxhih = _mm_and_si128(bxhih, mask);
  3607. __m128i bxl = _mm256_castsi256_si128(bx);
  3608. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3609. bxl = _mm_or_si128(bxl, bxhil);
  3610. bxh = _mm_or_si128(bxh, bxhih);
  3611. bx = MM256_SET_M128I(bxh, bxl);
  3612. const __m256 dy = _mm256_set1_ps(y[i].d);
  3613. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3614. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3615. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3616. }
  3617. *s = hsum_float_8(acc) + summs;
  3618. #elif defined(__riscv_v_intrinsic)
  3619. float sumf = 0.0;
  3620. uint32_t qh;
  3621. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3622. // temporary registers for shift operations
  3623. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3624. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3625. for (int i = 0; i < nb; i++) {
  3626. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3627. // load qh
  3628. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3629. // ((qh >> (j + 0)) << 4) & 0x10;
  3630. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3631. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3632. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3633. // ((qh >> (j + 12)) ) & 0x10;
  3634. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3635. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3636. // narrowing
  3637. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3638. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3639. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3640. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3641. // load
  3642. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3643. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3644. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3645. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3646. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3647. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3648. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3649. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3650. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3651. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3652. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3653. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3654. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3655. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3656. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3657. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3658. }
  3659. *s = sumf;
  3660. #else
  3661. // scalar
  3662. float sumf = 0.0;
  3663. for (int i = 0; i < nb; i++) {
  3664. uint32_t qh;
  3665. memcpy(&qh, x[i].qh, sizeof(qh));
  3666. int sumi = 0;
  3667. for (int j = 0; j < qk/2; ++j) {
  3668. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3669. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3670. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3671. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3672. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3673. }
  3674. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3675. }
  3676. *s = sumf;
  3677. #endif
  3678. }
  3679. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3680. const int qk = QK8_0;
  3681. const int nb = n / qk;
  3682. assert(n % qk == 0);
  3683. const block_q8_0 * restrict x = vx;
  3684. const block_q8_0 * restrict y = vy;
  3685. #if defined(__ARM_NEON)
  3686. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3687. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3688. assert(nb % 2 == 0); // TODO: handle odd nb
  3689. for (int i = 0; i < nb; i += 2) {
  3690. const block_q8_0 * restrict x0 = &x[i + 0];
  3691. const block_q8_0 * restrict x1 = &x[i + 1];
  3692. const block_q8_0 * restrict y0 = &y[i + 0];
  3693. const block_q8_0 * restrict y1 = &y[i + 1];
  3694. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3695. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3696. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3697. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3698. // load y
  3699. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3700. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3701. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3702. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3703. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3704. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3705. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3706. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3707. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3708. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3709. }
  3710. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3711. #elif defined(__AVX2__) || defined(__AVX__)
  3712. // Initialize accumulator with zeros
  3713. __m256 acc = _mm256_setzero_ps();
  3714. // Main loop
  3715. for (int i = 0; i < nb; ++i) {
  3716. // Compute combined scale for the block
  3717. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3718. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3719. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3720. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3721. // Multiply q with scale and accumulate
  3722. #if defined(__AVX2__)
  3723. acc = _mm256_fmadd_ps( d, q, acc );
  3724. #else
  3725. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3726. #endif
  3727. }
  3728. *s = hsum_float_8(acc);
  3729. #elif defined(__riscv_v_intrinsic)
  3730. float sumf = 0.0;
  3731. size_t vl = __riscv_vsetvl_e8m1(qk);
  3732. for (int i = 0; i < nb; i++) {
  3733. // load elements
  3734. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3735. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3736. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3737. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3738. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3739. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3740. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3741. }
  3742. *s = sumf;
  3743. #else
  3744. // scalar
  3745. float sumf = 0.0;
  3746. for (int i = 0; i < nb; i++) {
  3747. int sumi = 0;
  3748. for (int j = 0; j < qk; j++) {
  3749. sumi += x[i].qs[j]*y[i].qs[j];
  3750. }
  3751. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3752. }
  3753. *s = sumf;
  3754. #endif
  3755. }
  3756. #if QK_K == 256
  3757. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3758. const block_q2_K * restrict x = vx;
  3759. const block_q8_K * restrict y = vy;
  3760. const int nb = n / QK_K;
  3761. #ifdef __ARM_NEON
  3762. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3763. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3764. const int32x4_t vzero = vdupq_n_s32(0);
  3765. ggml_int8x16x2_t q2bytes;
  3766. uint8_t aux[16];
  3767. float sum = 0;
  3768. for (int i = 0; i < nb; ++i) {
  3769. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3770. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3771. const uint8_t * restrict q2 = x[i].qs;
  3772. const int8_t * restrict q8 = y[i].qs;
  3773. const uint8_t * restrict sc = x[i].scales;
  3774. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3775. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3776. vst1q_u8(aux, scales);
  3777. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3778. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3779. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3780. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3781. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3782. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3783. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3784. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3785. int isum = 0;
  3786. int is = 0;
  3787. // We use this macro instead of a function call because for some reason
  3788. // the code runs 2-3% slower, even if the function is declared inline
  3789. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3790. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3791. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3792. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3793. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3794. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3795. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3796. MULTIPLY_ACCUM_WITH_SCALE((index));
  3797. for (int j = 0; j < QK_K/128; ++j) {
  3798. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3799. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3800. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3801. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3802. MULTIPLY_ACCUM_WITH_SCALE(0);
  3803. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3804. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3805. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3806. is += 8;
  3807. }
  3808. sum += d * isum;
  3809. }
  3810. *s = sum;
  3811. #elif defined __AVX2__
  3812. const __m256i m3 = _mm256_set1_epi8(3);
  3813. const __m128i m4 = _mm_set1_epi8(0xF);
  3814. __m256 acc = _mm256_setzero_ps();
  3815. for (int i = 0; i < nb; ++i) {
  3816. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3817. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3818. const uint8_t * restrict q2 = x[i].qs;
  3819. const int8_t * restrict q8 = y[i].qs;
  3820. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3821. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3822. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3823. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3824. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3825. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3826. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3827. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3828. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3829. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3830. __m256i sumi = _mm256_setzero_si256();
  3831. for (int j = 0; j < QK_K/128; ++j) {
  3832. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  3833. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3834. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3835. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3836. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3837. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  3838. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  3839. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  3840. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  3841. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3842. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3843. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  3844. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  3845. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  3846. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  3847. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  3848. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  3849. p0 = _mm256_add_epi32(p0, p1);
  3850. p2 = _mm256_add_epi32(p2, p3);
  3851. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  3852. }
  3853. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3854. }
  3855. *s = hsum_float_8(acc);
  3856. #elif defined __AVX__
  3857. const __m128i m3 = _mm_set1_epi8(0x3);
  3858. const __m128i m4 = _mm_set1_epi8(0xF);
  3859. const __m128i m2 = _mm_set1_epi8(0x2);
  3860. __m256 acc = _mm256_setzero_ps();
  3861. for (int i = 0; i < nb; ++i) {
  3862. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3863. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3864. const uint8_t * restrict q2 = x[i].qs;
  3865. const int8_t * restrict q8 = y[i].qs;
  3866. // load mins and scales from block_q2_K.scales[QK_K/16]
  3867. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3868. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  3869. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3870. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  3871. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  3872. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  3873. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  3874. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  3875. // sumf += -dmin * summs in 32bits*8
  3876. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  3877. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  3878. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  3879. const __m128i scales[2] = { scales_0, scales_1 };
  3880. __m128i sumi_0 = _mm_setzero_si128();
  3881. __m128i sumi_1 = _mm_setzero_si128();
  3882. for (int j = 0; j < QK_K/128; ++j) {
  3883. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  3884. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3885. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3886. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3887. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3888. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3889. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3890. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3891. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3892. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  3893. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3894. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3895. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3896. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3897. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3898. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3899. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  3900. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3901. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3902. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3903. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  3904. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  3905. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  3906. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  3907. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  3908. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  3909. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  3910. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  3911. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  3912. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  3913. __m128i shuffle = _mm_set1_epi16(0x0100);
  3914. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  3915. shuffle = _mm_add_epi16(shuffle, m2);
  3916. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  3917. shuffle = _mm_add_epi16(shuffle, m2);
  3918. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  3919. shuffle = _mm_add_epi16(shuffle, m2);
  3920. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  3921. shuffle = _mm_add_epi16(shuffle, m2);
  3922. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  3923. shuffle = _mm_add_epi16(shuffle, m2);
  3924. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  3925. shuffle = _mm_add_epi16(shuffle, m2);
  3926. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  3927. shuffle = _mm_add_epi16(shuffle, m2);
  3928. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  3929. p0 = _mm_add_epi32(p0, p1);
  3930. p2 = _mm_add_epi32(p2, p3);
  3931. p4 = _mm_add_epi32(p4, p5);
  3932. p6 = _mm_add_epi32(p6, p7);
  3933. // isum in 32bits*4*2
  3934. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  3935. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  3936. }
  3937. // sumf += dall * isum - dmin * summs in 32bits
  3938. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3939. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  3940. }
  3941. *s = hsum_float_8(acc);
  3942. #elif defined __riscv_v_intrinsic
  3943. float sumf = 0;
  3944. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3945. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  3946. for (int i = 0; i < nb; ++i) {
  3947. const uint8_t * q2 = x[i].qs;
  3948. const int8_t * q8 = y[i].qs;
  3949. const uint8_t * sc = x[i].scales;
  3950. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3951. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3952. size_t vl = 16;
  3953. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  3954. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  3955. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  3956. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  3957. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  3958. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  3959. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  3960. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3961. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  3962. vl = 32;
  3963. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3964. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  3965. uint8_t is=0;
  3966. int isum=0;
  3967. for (int j = 0; j < QK_K/128; ++j) {
  3968. // load Q2
  3969. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  3970. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  3971. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  3972. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  3973. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  3974. // duplicate scale elements for product
  3975. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  3976. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  3977. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  3978. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  3979. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  3980. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  3981. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  3982. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  3983. // load Q8
  3984. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  3985. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  3986. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  3987. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  3988. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  3989. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  3990. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  3991. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  3992. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  3993. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  3994. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  3995. q2+=32; q8+=128; is=8;
  3996. }
  3997. sumf += dall * isum;
  3998. }
  3999. *s = sumf;
  4000. #else
  4001. float sumf = 0;
  4002. for (int i = 0; i < nb; ++i) {
  4003. const uint8_t * q2 = x[i].qs;
  4004. const int8_t * q8 = y[i].qs;
  4005. const uint8_t * sc = x[i].scales;
  4006. int summs = 0;
  4007. for (int j = 0; j < 16; ++j) {
  4008. summs += y[i].bsums[j] * (sc[j] >> 4);
  4009. }
  4010. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4011. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4012. int isum = 0;
  4013. int is = 0;
  4014. int d;
  4015. for (int k = 0; k < QK_K/128; ++k) {
  4016. int shift = 0;
  4017. for (int j = 0; j < 4; ++j) {
  4018. d = sc[is++] & 0xF;
  4019. int isuml = 0;
  4020. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4021. isum += d * isuml;
  4022. d = sc[is++] & 0xF;
  4023. isuml = 0;
  4024. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4025. isum += d * isuml;
  4026. shift += 2;
  4027. q8 += 32;
  4028. }
  4029. q2 += 32;
  4030. }
  4031. sumf += dall * isum - dmin * summs;
  4032. }
  4033. *s = sumf;
  4034. #endif
  4035. }
  4036. #else
  4037. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4038. const block_q2_K * restrict x = vx;
  4039. const block_q8_K * restrict y = vy;
  4040. const int nb = n / QK_K;
  4041. #ifdef __ARM_NEON
  4042. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4043. const int32x4_t vzero = vdupq_n_s32(0);
  4044. ggml_int8x16x4_t q2bytes;
  4045. uint32_t aux32[2];
  4046. const uint8_t * scales = (const uint8_t *)aux32;
  4047. float sum = 0;
  4048. for (int i = 0; i < nb; ++i) {
  4049. const float d = y[i].d * (float)x[i].d;
  4050. const float dmin = -y[i].d * (float)x[i].dmin;
  4051. const uint8_t * restrict q2 = x[i].qs;
  4052. const int8_t * restrict q8 = y[i].qs;
  4053. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4054. aux32[0] = sc[0] & 0x0f0f0f0f;
  4055. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4056. 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]);
  4057. int isum1 = 0, isum2 = 0;
  4058. const uint8x16_t q2bits = vld1q_u8(q2);
  4059. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4060. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4061. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4062. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4063. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4064. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4065. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4066. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4067. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4068. sum += d * (isum1 + isum2);
  4069. }
  4070. *s = sum;
  4071. #elif defined __AVX2__
  4072. const __m256i m3 = _mm256_set1_epi8(3);
  4073. __m256 acc = _mm256_setzero_ps();
  4074. uint32_t ud, um;
  4075. const uint8_t * restrict db = (const uint8_t *)&ud;
  4076. const uint8_t * restrict mb = (const uint8_t *)&um;
  4077. float summs = 0;
  4078. // TODO: optimize this
  4079. for (int i = 0; i < nb; ++i) {
  4080. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4081. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4082. const uint8_t * restrict q2 = x[i].qs;
  4083. const int8_t * restrict q8 = y[i].qs;
  4084. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4085. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4086. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4087. 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];
  4088. summs += dmin * smin;
  4089. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4090. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4091. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4092. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4093. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4094. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4095. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4096. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4097. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4098. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4099. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4100. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4101. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4102. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4103. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4104. }
  4105. *s = hsum_float_8(acc) + summs;
  4106. #elif defined __AVX__
  4107. const __m128i m3 = _mm_set1_epi8(3);
  4108. __m256 acc = _mm256_setzero_ps();
  4109. uint32_t ud, um;
  4110. const uint8_t * restrict db = (const uint8_t *)&ud;
  4111. const uint8_t * restrict mb = (const uint8_t *)&um;
  4112. float summs = 0;
  4113. // TODO: optimize this
  4114. for (int i = 0; i < nb; ++i) {
  4115. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4116. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4117. const uint8_t * restrict q2 = x[i].qs;
  4118. const int8_t * restrict q8 = y[i].qs;
  4119. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4120. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4121. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4122. 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];
  4123. summs += dmin * smin;
  4124. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4125. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4126. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4127. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4128. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4129. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4130. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4131. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4132. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4133. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4134. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4135. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4136. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4137. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4138. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4139. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4140. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4141. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4142. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4143. }
  4144. *s = hsum_float_8(acc) + summs;
  4145. #elif defined __riscv_v_intrinsic
  4146. uint32_t aux32[2];
  4147. const uint8_t * scales = (const uint8_t *)aux32;
  4148. float sumf = 0;
  4149. for (int i = 0; i < nb; ++i) {
  4150. const float d = y[i].d * (float)x[i].d;
  4151. const float dmin = -y[i].d * (float)x[i].dmin;
  4152. const uint8_t * restrict q2 = x[i].qs;
  4153. const int8_t * restrict q8 = y[i].qs;
  4154. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4155. aux32[0] = sc[0] & 0x0f0f0f0f;
  4156. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4157. 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]);
  4158. int isum1 = 0;
  4159. int isum2 = 0;
  4160. size_t vl = 16;
  4161. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4162. // load Q2
  4163. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4164. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4165. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4166. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4167. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4168. // load Q8, and take product with Q2
  4169. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4170. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4171. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4172. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4173. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4174. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4175. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4176. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4177. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4178. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4179. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4180. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4181. sumf += d * (isum1 + isum2);
  4182. }
  4183. *s = sumf;
  4184. #else
  4185. float sumf = 0;
  4186. int isum[4];
  4187. for (int i = 0; i < nb; ++i) {
  4188. const uint8_t * q2 = x[i].qs;
  4189. const int8_t * q8 = y[i].qs;
  4190. const uint8_t * sc = x[i].scales;
  4191. int summs = 0;
  4192. for (int j = 0; j < QK_K/16; ++j) {
  4193. summs += y[i].bsums[j] * (sc[j] >> 4);
  4194. }
  4195. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4196. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4197. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  4198. for (int l = 0; l < 16; ++l) {
  4199. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4200. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4201. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4202. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4203. }
  4204. for (int l = 0; l < 4; ++l) {
  4205. isum[l] *= (sc[l] & 0xF);
  4206. }
  4207. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4208. }
  4209. *s = sumf;
  4210. #endif
  4211. }
  4212. #endif
  4213. #if QK_K == 256
  4214. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4215. assert(n % QK_K == 0);
  4216. const uint32_t kmask1 = 0x03030303;
  4217. const uint32_t kmask2 = 0x0f0f0f0f;
  4218. const block_q3_K * restrict x = vx;
  4219. const block_q8_K * restrict y = vy;
  4220. const int nb = n / QK_K;
  4221. #ifdef __ARM_NEON
  4222. uint32_t aux[3];
  4223. uint32_t utmp[4];
  4224. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4225. const int32x4_t vzero = vdupq_n_s32(0);
  4226. const uint8x16_t m0 = vdupq_n_u8(1);
  4227. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4228. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4229. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4230. const int8_t m32 = 32;
  4231. ggml_int8x16x4_t q3bytes;
  4232. float sum = 0;
  4233. for (int i = 0; i < nb; ++i) {
  4234. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4235. const uint8_t * restrict q3 = x[i].qs;
  4236. const uint8_t * restrict qh = x[i].hmask;
  4237. const int8_t * restrict q8 = y[i].qs;
  4238. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4239. ggml_uint8x16x4_t q3h;
  4240. int32_t isum = 0;
  4241. // Set up scales
  4242. memcpy(aux, x[i].scales, 12);
  4243. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4244. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4245. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4246. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4247. int8_t * scale = (int8_t *)utmp;
  4248. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4249. for (int j = 0; j < QK_K/128; ++j) {
  4250. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4251. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4252. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4253. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4254. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4255. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4256. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4257. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4258. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4259. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4260. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4261. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4262. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4263. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4264. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4265. scale += 4;
  4266. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4267. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4268. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4269. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4270. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4271. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4272. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4273. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4274. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4275. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4276. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4277. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4278. scale += 4;
  4279. if (j == 0) {
  4280. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4281. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4282. }
  4283. }
  4284. sum += d * isum;
  4285. }
  4286. *s = sum;
  4287. #elif defined __AVX2__
  4288. const __m256i m3 = _mm256_set1_epi8(3);
  4289. const __m256i mone = _mm256_set1_epi8(1);
  4290. const __m128i m32 = _mm_set1_epi8(32);
  4291. __m256 acc = _mm256_setzero_ps();
  4292. uint32_t aux[3];
  4293. for (int i = 0; i < nb; ++i) {
  4294. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4295. const uint8_t * restrict q3 = x[i].qs;
  4296. const int8_t * restrict q8 = y[i].qs;
  4297. // Set up scales
  4298. memcpy(aux, x[i].scales, 12);
  4299. __m128i scales128 = _mm_set_epi32(
  4300. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4301. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4302. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4303. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4304. scales128 = _mm_sub_epi8(scales128, m32);
  4305. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4306. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4307. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4308. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4309. // high bit
  4310. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4311. // integer accumulator
  4312. __m256i sumi = _mm256_setzero_si256();
  4313. int bit = 0;
  4314. int is = 0;
  4315. for (int j = 0; j < QK_K/128; ++j) {
  4316. // load low 2 bits
  4317. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4318. // prepare low and high bits
  4319. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4320. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4321. ++bit;
  4322. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4323. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4324. ++bit;
  4325. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4326. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4327. ++bit;
  4328. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4329. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4330. ++bit;
  4331. // load Q8 quants
  4332. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4333. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4334. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4335. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4336. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4337. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4338. // and 2 if the high bit was set)
  4339. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4340. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4341. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4342. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4343. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4344. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4345. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4346. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4347. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4348. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4349. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4350. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4351. // multiply with scales
  4352. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4353. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4354. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4355. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4356. // accumulate
  4357. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4358. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4359. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4360. }
  4361. // multiply with block scale and accumulate
  4362. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4363. }
  4364. *s = hsum_float_8(acc);
  4365. #elif defined __AVX__
  4366. const __m128i m3 = _mm_set1_epi8(3);
  4367. const __m128i mone = _mm_set1_epi8(1);
  4368. const __m128i m32 = _mm_set1_epi8(32);
  4369. const __m128i m2 = _mm_set1_epi8(2);
  4370. __m256 acc = _mm256_setzero_ps();
  4371. const uint32_t *aux;
  4372. for (int i = 0; i < nb; ++i) {
  4373. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4374. const uint8_t * restrict q3 = x[i].qs;
  4375. const int8_t * restrict q8 = y[i].qs;
  4376. // Set up scales
  4377. aux = (const uint32_t *)x[i].scales;
  4378. __m128i scales128 = _mm_set_epi32(
  4379. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4380. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4381. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4382. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4383. scales128 = _mm_sub_epi8(scales128, m32);
  4384. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4385. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4386. const __m128i scales[2] = { scales_0, scales_1 };
  4387. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4388. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4389. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4390. // integer accumulator
  4391. __m128i sumi_0 = _mm_setzero_si128();
  4392. __m128i sumi_1 = _mm_setzero_si128();
  4393. for (int j = 0; j < QK_K/128; ++j) {
  4394. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4395. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4396. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4397. // prepare low and high bits
  4398. const int bit = j << 2;
  4399. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4400. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4401. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4402. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4403. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4404. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4405. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4406. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4407. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4408. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4409. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4410. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4411. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4412. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4413. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4414. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4415. // load Q8 quants from block_q8_K.qs[QK_K]
  4416. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4417. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4418. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4419. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4420. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4421. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4422. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4423. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4424. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4425. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4426. // and 2 if the high bit was set)
  4427. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4428. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4429. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4430. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4431. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4432. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4433. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4434. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4435. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4436. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4437. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4438. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4439. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4440. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4441. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4442. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4443. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4444. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4445. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4446. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4447. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4448. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4449. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4450. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4451. // multiply with scales
  4452. __m128i shuffle = _mm_set1_epi16(0x0100);
  4453. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4454. shuffle = _mm_add_epi16(shuffle, m2);
  4455. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4456. shuffle = _mm_add_epi16(shuffle, m2);
  4457. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4458. shuffle = _mm_add_epi16(shuffle, m2);
  4459. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4460. shuffle = _mm_add_epi16(shuffle, m2);
  4461. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4462. shuffle = _mm_add_epi16(shuffle, m2);
  4463. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4464. shuffle = _mm_add_epi16(shuffle, m2);
  4465. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4466. shuffle = _mm_add_epi16(shuffle, m2);
  4467. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4468. // accumulate
  4469. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4470. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4471. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4472. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4473. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4474. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4475. }
  4476. // multiply with block scale and accumulate
  4477. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4478. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4479. }
  4480. *s = hsum_float_8(acc);
  4481. #elif defined __riscv_v_intrinsic
  4482. uint32_t aux[3];
  4483. uint32_t utmp[4];
  4484. float sumf = 0;
  4485. for (int i = 0; i < nb; ++i) {
  4486. const uint8_t * restrict q3 = x[i].qs;
  4487. const uint8_t * restrict qh = x[i].hmask;
  4488. const int8_t * restrict q8 = y[i].qs;
  4489. memcpy(aux, x[i].scales, 12);
  4490. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4491. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4492. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4493. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4494. int8_t * scale = (int8_t *)utmp;
  4495. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4496. size_t vl = 32;
  4497. uint8_t m = 1;
  4498. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4499. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4500. int sum_t = 0;
  4501. for (int j = 0; j < QK_K; j += 128) {
  4502. vl = 32;
  4503. // load Q3
  4504. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4505. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4506. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4507. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4508. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4509. // compute mask for subtraction
  4510. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4511. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4512. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4513. m <<= 1;
  4514. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4515. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4516. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4517. m <<= 1;
  4518. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4519. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4520. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4521. m <<= 1;
  4522. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4523. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4524. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4525. m <<= 1;
  4526. // load Q8 and take product with Q3
  4527. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4528. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4529. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4530. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4531. vl = 16;
  4532. // retrieve lane to multiply with scale
  4533. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4534. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4535. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4536. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4537. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4538. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4539. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4540. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4541. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4542. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4543. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4544. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4545. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4546. q3 += 32; q8 += 128; scale += 8;
  4547. }
  4548. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4549. sumf += d*sum_t;
  4550. }
  4551. *s = sumf;
  4552. #else
  4553. // scalar version
  4554. // This function is written like this so the compiler can manage to vectorize most of it
  4555. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4556. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4557. // The ideal situation would be if we could just write the code once, and the compiler would
  4558. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4559. // write vectorized versions for AVX, ARM_NEON, etc.
  4560. int8_t aux8[QK_K];
  4561. int16_t aux16[8];
  4562. float sums [8];
  4563. int32_t aux32[8];
  4564. memset(sums, 0, 8*sizeof(float));
  4565. uint32_t auxs[4];
  4566. const int8_t * scales = (const int8_t*)auxs;
  4567. float sumf = 0;
  4568. for (int i = 0; i < nb; ++i) {
  4569. const uint8_t * restrict q3 = x[i].qs;
  4570. const uint8_t * restrict hm = x[i].hmask;
  4571. const int8_t * restrict q8 = y[i].qs;
  4572. memset(aux32, 0, 8*sizeof(int32_t));
  4573. int8_t * restrict a = aux8;
  4574. uint8_t m = 1;
  4575. for (int j = 0; j < QK_K; j += 128) {
  4576. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4577. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4578. a += 32; m <<= 1;
  4579. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4580. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4581. a += 32; m <<= 1;
  4582. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4583. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4584. a += 32; m <<= 1;
  4585. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4586. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4587. a += 32; m <<= 1;
  4588. q3 += 32;
  4589. }
  4590. a = aux8;
  4591. memcpy(auxs, x[i].scales, 12);
  4592. uint32_t tmp = auxs[2];
  4593. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4594. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4595. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4596. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4597. for (int j = 0; j < QK_K/16; ++j) {
  4598. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4599. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4600. q8 += 8; a += 8;
  4601. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4602. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4603. q8 += 8; a += 8;
  4604. }
  4605. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4606. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4607. }
  4608. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4609. *s = sumf;
  4610. #endif
  4611. }
  4612. #else
  4613. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4614. assert(n % QK_K == 0);
  4615. const block_q3_K * restrict x = vx;
  4616. const block_q8_K * restrict y = vy;
  4617. const int nb = n / QK_K;
  4618. #ifdef __ARM_NEON
  4619. const int32x4_t vzero = vdupq_n_s32(0);
  4620. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4621. const uint8x16_t mh = vdupq_n_u8(4);
  4622. ggml_int8x16x4_t q3bytes;
  4623. uint16_t aux16[2];
  4624. int8_t * scales = (int8_t *)aux16;
  4625. float sum = 0;
  4626. for (int i = 0; i < nb; ++i) {
  4627. ggml_uint8x16x4_t q3h;
  4628. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4629. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4630. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4631. const uint16_t a = *(const uint16_t *)x[i].scales;
  4632. aux16[0] = a & 0x0f0f;
  4633. aux16[1] = (a >> 4) & 0x0f0f;
  4634. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4635. 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]);
  4636. const float d = y[i].d * (float)x[i].d;
  4637. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4638. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4639. q3h.val[1] = vandq_u8(mh, htmp);
  4640. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4641. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4642. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4643. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4644. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4645. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4646. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4647. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4648. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4649. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4650. sum += d * isum;
  4651. }
  4652. *s = sum;
  4653. #elif defined __AVX2__
  4654. const __m256i m3 = _mm256_set1_epi8(3);
  4655. const __m256i m1 = _mm256_set1_epi8(1);
  4656. __m256 acc = _mm256_setzero_ps();
  4657. uint64_t aux64;
  4658. uint16_t aux16[2];
  4659. const int8_t * aux8 = (const int8_t *)aux16;
  4660. for (int i = 0; i < nb; ++i) {
  4661. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4662. const uint8_t * restrict q3 = x[i].qs;
  4663. const int8_t * restrict q8 = y[i].qs;
  4664. const uint16_t a = *(const uint16_t *)x[i].scales;
  4665. aux16[0] = a & 0x0f0f;
  4666. aux16[1] = (a >> 4) & 0x0f0f;
  4667. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4668. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4669. memcpy(&aux64, x[i].hmask, 8);
  4670. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4671. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4672. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4673. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4674. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4675. // load low 2 bits
  4676. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4677. // prepare low and high bits
  4678. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4679. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4680. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4681. // load Q8 quants
  4682. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4683. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4684. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4685. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4686. // and 2 if the high bit was set)
  4687. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4688. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4689. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4690. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4691. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4692. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4693. // multiply with scales
  4694. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4695. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4696. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4697. // multiply with block scale and accumulate
  4698. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4699. }
  4700. *s = hsum_float_8(acc);
  4701. #elif defined __AVX__
  4702. const __m128i m3 = _mm_set1_epi8(3);
  4703. const __m128i m1 = _mm_set1_epi8(1);
  4704. __m256 acc = _mm256_setzero_ps();
  4705. uint64_t aux64;
  4706. uint16_t aux16[2];
  4707. const int8_t * aux8 = (const int8_t *)aux16;
  4708. for (int i = 0; i < nb; ++i) {
  4709. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4710. const uint8_t * restrict q3 = x[i].qs;
  4711. const int8_t * restrict q8 = y[i].qs;
  4712. const uint16_t a = *(const uint16_t *)x[i].scales;
  4713. aux16[0] = a & 0x0f0f;
  4714. aux16[1] = (a >> 4) & 0x0f0f;
  4715. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4716. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4717. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4718. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4719. memcpy(&aux64, x[i].hmask, 8);
  4720. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4721. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4722. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4723. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4724. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4725. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4726. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4727. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4728. // load low 2 bits
  4729. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4730. // prepare low and high bits
  4731. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4732. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4733. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4734. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4735. // load Q8 quants
  4736. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4737. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4738. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4739. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4740. // and 2 if the high bit was set)
  4741. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4742. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4743. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4744. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4745. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4746. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4747. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4748. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4749. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4750. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4751. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4752. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4753. // multiply with scales
  4754. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4755. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4756. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4757. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4758. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4759. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4760. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4761. // multiply with block scale and accumulate
  4762. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4763. }
  4764. *s = hsum_float_8(acc);
  4765. #elif defined __riscv_v_intrinsic
  4766. uint16_t aux16[2];
  4767. int8_t * scales = (int8_t *)aux16;
  4768. float sumf = 0;
  4769. for (int i = 0; i < nb; ++i) {
  4770. const uint8_t * restrict q3 = x[i].qs;
  4771. const int8_t * restrict q8 = y[i].qs;
  4772. const uint16_t a = *(const uint16_t *)x[i].scales;
  4773. aux16[0] = a & 0x0f0f;
  4774. aux16[1] = (a >> 4) & 0x0f0f;
  4775. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4776. 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]);
  4777. const float d = y[i].d * (float)x[i].d;
  4778. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4779. // load qh
  4780. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4781. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4782. size_t vl = 16;
  4783. // extend and combine both qh_x1 and qh_x2
  4784. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4785. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4786. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4787. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4788. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4789. // load Q3
  4790. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4791. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4792. 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);
  4793. 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);
  4794. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4795. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4796. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4797. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4798. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4799. // load Q8 and take product with Q3
  4800. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4801. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4802. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4803. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4804. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4805. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4806. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4807. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4808. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4809. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4810. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4811. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4812. sumf += d * isum;
  4813. }
  4814. *s = sumf;
  4815. #else
  4816. int8_t aux8[QK_K];
  4817. int16_t aux16[8];
  4818. float sums [8];
  4819. int32_t aux32[8];
  4820. int32_t scales[4];
  4821. memset(sums, 0, 8*sizeof(float));
  4822. float sumf = 0;
  4823. for (int i = 0; i < nb; ++i) {
  4824. const uint8_t * restrict q3 = x[i].qs;
  4825. const uint8_t * restrict hm = x[i].hmask;
  4826. const int8_t * restrict q8 = y[i].qs;
  4827. int8_t * restrict a = aux8;
  4828. for (int l = 0; l < 8; ++l) {
  4829. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  4830. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  4831. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  4832. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  4833. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  4834. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  4835. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  4836. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  4837. }
  4838. scales[0] = (x[i].scales[0] & 0xF) - 8;
  4839. scales[1] = (x[i].scales[0] >> 4) - 8;
  4840. scales[2] = (x[i].scales[1] & 0xF) - 8;
  4841. scales[3] = (x[i].scales[1] >> 4) - 8;
  4842. memset(aux32, 0, 8*sizeof(int32_t));
  4843. for (int j = 0; j < QK_K/16; ++j) {
  4844. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4845. q8 += 8; a += 8;
  4846. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  4847. q8 += 8; a += 8;
  4848. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  4849. }
  4850. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4851. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4852. }
  4853. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4854. *s = sumf;
  4855. #endif
  4856. }
  4857. #endif
  4858. #if QK_K == 256
  4859. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4860. assert(n % QK_K == 0);
  4861. const block_q4_K * restrict x = vx;
  4862. const block_q8_K * restrict y = vy;
  4863. const int nb = n / QK_K;
  4864. static const uint32_t kmask1 = 0x3f3f3f3f;
  4865. static const uint32_t kmask2 = 0x0f0f0f0f;
  4866. static const uint32_t kmask3 = 0x03030303;
  4867. uint32_t utmp[4];
  4868. #ifdef __ARM_NEON
  4869. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4870. const int32x4_t mzero = vdupq_n_s32(0);
  4871. ggml_int8x16x2_t q4bytes;
  4872. ggml_int8x16x2_t q8bytes;
  4873. float sumf = 0;
  4874. for (int i = 0; i < nb; ++i) {
  4875. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4876. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4877. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4878. memcpy(utmp, x[i].scales, 12);
  4879. uint32x2_t mins8 = { 0 };
  4880. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  4881. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  4882. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4883. utmp[0] &= kmask1;
  4884. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  4885. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4886. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4887. sumf -= dmin * vaddvq_s32(prod);
  4888. const uint8_t * scales = (const uint8_t *)utmp;
  4889. const uint8_t * restrict q4 = x[i].qs;
  4890. const int8_t * restrict q8 = y[i].qs;
  4891. int32_t sumi1 = 0;
  4892. int32_t sumi2 = 0;
  4893. for (int j = 0; j < QK_K/64; ++j) {
  4894. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  4895. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4896. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4897. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4898. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4899. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  4900. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4901. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4902. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4903. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4904. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  4905. }
  4906. sumf += d * (sumi1 + sumi2);
  4907. }
  4908. *s = sumf;
  4909. #elif defined __AVX2__
  4910. const __m256i m4 = _mm256_set1_epi8(0xF);
  4911. __m256 acc = _mm256_setzero_ps();
  4912. __m128 acc_m = _mm_setzero_ps();
  4913. for (int i = 0; i < nb; ++i) {
  4914. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4915. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4916. memcpy(utmp, x[i].scales, 12);
  4917. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4918. const uint32_t uaux = utmp[1] & kmask1;
  4919. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4920. utmp[2] = uaux;
  4921. utmp[0] &= kmask1;
  4922. const uint8_t * restrict q4 = x[i].qs;
  4923. const int8_t * restrict q8 = y[i].qs;
  4924. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4925. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4926. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4927. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4928. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  4929. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4930. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4931. __m256i sumi = _mm256_setzero_si256();
  4932. for (int j = 0; j < QK_K/64; ++j) {
  4933. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4934. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4935. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  4936. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4937. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4938. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4939. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4940. p16l = _mm256_madd_epi16(scale_l, p16l);
  4941. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4942. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4943. p16h = _mm256_madd_epi16(scale_h, p16h);
  4944. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  4945. sumi = _mm256_add_epi32(sumi, sumj);
  4946. }
  4947. __m256 vd = _mm256_set1_ps(d);
  4948. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4949. }
  4950. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4951. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4952. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4953. #elif defined __AVX__
  4954. const __m128i m4 = _mm_set1_epi8(0xF);
  4955. const __m128i m2 = _mm_set1_epi8(0x2);
  4956. __m256 acc = _mm256_setzero_ps();
  4957. __m128 acc_m = _mm_setzero_ps();
  4958. for (int i = 0; i < nb; ++i) {
  4959. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4960. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4961. const uint8_t * restrict q4 = x[i].qs;
  4962. const int8_t * restrict q8 = y[i].qs;
  4963. memcpy(utmp, x[i].scales, 12);
  4964. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4965. const uint32_t uaux = utmp[1] & kmask1;
  4966. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4967. utmp[2] = uaux;
  4968. utmp[0] &= kmask1;
  4969. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4970. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4971. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4972. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4973. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4974. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4975. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4976. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  4977. __m128i sumi_0 = _mm_setzero_si128();
  4978. __m128i sumi_1 = _mm_setzero_si128();
  4979. __m128i shuffle = _mm_set1_epi16(0x0100);
  4980. for (int j = 0; j < QK_K/64; ++j) {
  4981. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  4982. shuffle = _mm_add_epi16(shuffle, m2);
  4983. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  4984. shuffle = _mm_add_epi16(shuffle, m2);
  4985. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4986. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  4987. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4988. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4989. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  4990. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4991. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4992. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  4993. p16l = _mm_madd_epi16(scale_l, p16l);
  4994. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  4995. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4996. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  4997. p16l = _mm_madd_epi16(scale_l, p16l);
  4998. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  4999. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5000. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5001. p16h = _mm_madd_epi16(scale_h, p16h);
  5002. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5003. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5004. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5005. p16h = _mm_madd_epi16(scale_h, p16h);
  5006. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5007. }
  5008. __m256 vd = _mm256_set1_ps(d);
  5009. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5010. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5011. }
  5012. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5013. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5014. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5015. #elif defined __riscv_v_intrinsic
  5016. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5017. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5018. float sumf = 0;
  5019. for (int i = 0; i < nb; ++i) {
  5020. size_t vl = 8;
  5021. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5022. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5023. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5024. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5025. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5026. memcpy(utmp, x[i].scales, 12);
  5027. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5028. const uint32_t uaux = utmp[1] & kmask1;
  5029. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5030. utmp[2] = uaux;
  5031. utmp[0] &= kmask1;
  5032. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5033. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5034. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5035. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5036. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5037. const uint8_t * restrict q4 = x[i].qs;
  5038. const int8_t * restrict q8 = y[i].qs;
  5039. vl = 32;
  5040. int32_t sum_1 = 0;
  5041. int32_t sum_2 = 0;
  5042. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5043. for (int j = 0; j < QK_K/64; ++j) {
  5044. // load Q4
  5045. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5046. // load Q8 and multiply it with lower Q4 nibble
  5047. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5048. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5049. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5050. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5051. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5052. // load Q8 and multiply it with upper Q4 nibble
  5053. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5054. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5055. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5056. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5057. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5058. q4 += 32; q8 += 64;
  5059. }
  5060. sumf += d*(sum_1 + sum_2);
  5061. }
  5062. *s = sumf;
  5063. #else
  5064. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5065. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5066. int8_t aux8[QK_K];
  5067. int16_t aux16[8];
  5068. float sums [8];
  5069. int32_t aux32[8];
  5070. memset(sums, 0, 8*sizeof(float));
  5071. float sumf = 0;
  5072. for (int i = 0; i < nb; ++i) {
  5073. const uint8_t * restrict q4 = x[i].qs;
  5074. const int8_t * restrict q8 = y[i].qs;
  5075. memset(aux32, 0, 8*sizeof(int32_t));
  5076. int8_t * restrict a = aux8;
  5077. for (int j = 0; j < QK_K/64; ++j) {
  5078. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5079. a += 32;
  5080. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5081. a += 32; q4 += 32;
  5082. }
  5083. memcpy(utmp, x[i].scales, 12);
  5084. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5085. const uint32_t uaux = utmp[1] & kmask1;
  5086. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5087. utmp[2] = uaux;
  5088. utmp[0] &= kmask1;
  5089. int sumi = 0;
  5090. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5091. a = aux8;
  5092. int is = 0;
  5093. for (int j = 0; j < QK_K/32; ++j) {
  5094. int32_t scale = scales[is++];
  5095. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5096. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5097. q8 += 8; a += 8;
  5098. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5099. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5100. q8 += 8; a += 8;
  5101. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5102. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5103. q8 += 8; a += 8;
  5104. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5105. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5106. q8 += 8; a += 8;
  5107. }
  5108. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5109. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5110. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5111. sumf -= dmin * sumi;
  5112. }
  5113. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5114. *s = sumf;
  5115. #endif
  5116. }
  5117. #else
  5118. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5119. assert(n % QK_K == 0);
  5120. const block_q4_K * restrict x = vx;
  5121. const block_q8_K * restrict y = vy;
  5122. const int nb = n / QK_K;
  5123. #ifdef __ARM_NEON
  5124. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5125. const int32x4_t mzero = vdupq_n_s32(0);
  5126. float sumf = 0;
  5127. ggml_int8x16x2_t q4bytes;
  5128. ggml_int8x16x4_t q8bytes;
  5129. float sum_mins = 0.f;
  5130. uint16_t aux16[2];
  5131. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5132. for (int i = 0; i < nb; ++i) {
  5133. const uint8_t * restrict q4 = x[i].qs;
  5134. const int8_t * restrict q8 = y[i].qs;
  5135. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5136. aux16[0] = a[0] & 0x0f0f;
  5137. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5138. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5139. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  5140. const float d = y[i].d * (float)x[i].d[0];
  5141. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5142. q8bytes = ggml_vld1q_s8_x4(q8);
  5143. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5144. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5145. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5146. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5147. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5148. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5149. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5150. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5151. sumf += d * (sumi1 + sumi2);
  5152. }
  5153. *s = sumf - sum_mins;
  5154. #elif defined __AVX2__
  5155. const __m256i m4 = _mm256_set1_epi8(0xF);
  5156. __m256 acc = _mm256_setzero_ps();
  5157. float summs = 0;
  5158. uint16_t aux16[2];
  5159. const uint8_t * scales = (const uint8_t *)aux16;
  5160. for (int i = 0; i < nb; ++i) {
  5161. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5162. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5163. const __m256 vd = _mm256_set1_ps(d);
  5164. const uint16_t * a = (const uint16_t *)x[i].scales;
  5165. aux16[0] = a[0] & 0x0f0f;
  5166. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5167. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5168. const uint8_t * restrict q4 = x[i].qs;
  5169. const int8_t * restrict q8 = y[i].qs;
  5170. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5171. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5172. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5173. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5174. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5175. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5176. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5177. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5178. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5179. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5180. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5181. }
  5182. *s = hsum_float_8(acc) - summs;
  5183. #elif defined __AVX__
  5184. const __m128i m4 = _mm_set1_epi8(0xF);
  5185. __m256 acc = _mm256_setzero_ps();
  5186. float summs = 0;
  5187. uint16_t aux16[2];
  5188. const uint8_t * scales = (const uint8_t *)aux16;
  5189. for (int i = 0; i < nb; ++i) {
  5190. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5191. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5192. const __m256 vd = _mm256_set1_ps(d);
  5193. const uint16_t * a = (const uint16_t *)x[i].scales;
  5194. aux16[0] = a[0] & 0x0f0f;
  5195. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5196. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5197. const uint8_t * restrict q4 = x[i].qs;
  5198. const int8_t * restrict q8 = y[i].qs;
  5199. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5200. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5201. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5202. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5203. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5204. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5205. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5206. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5207. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5208. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5209. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5210. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5211. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5212. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5213. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5214. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5215. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5216. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5217. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5218. }
  5219. *s = hsum_float_8(acc) - summs;
  5220. #elif defined __riscv_v_intrinsic
  5221. uint16_t s16[2];
  5222. const uint8_t * restrict scales = (const uint8_t *)s16;
  5223. float sumf = 0;
  5224. for (int i = 0; i < nb; ++i) {
  5225. const uint8_t * restrict q4 = x[i].qs;
  5226. const int8_t * restrict q8 = y[i].qs;
  5227. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5228. s16[0] = b[0] & 0x0f0f;
  5229. s16[1] = (b[0] >> 4) & 0x0f0f;
  5230. 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]));
  5231. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5232. size_t vl = 32;
  5233. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5234. // load Q4
  5235. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5236. // load Q8 and multiply it with lower Q4 nibble
  5237. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5238. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5239. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5240. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5241. // load Q8 and multiply it with upper Q4 nibble
  5242. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5243. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5244. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5245. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5246. }
  5247. *s = sumf;
  5248. #else
  5249. uint8_t aux8[QK_K];
  5250. int16_t aux16[16];
  5251. float sums [8];
  5252. memset(sums, 0, 8*sizeof(float));
  5253. uint16_t s16[2];
  5254. const uint8_t * restrict scales = (const uint8_t *)s16;
  5255. float sumf = 0;
  5256. for (int i = 0; i < nb; ++i) {
  5257. const uint8_t * restrict q4 = x[i].qs;
  5258. const int8_t * restrict q8 = y[i].qs;
  5259. uint8_t * restrict a = aux8;
  5260. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5261. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5262. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5263. s16[0] = b[0] & 0x0f0f;
  5264. s16[1] = (b[0] >> 4) & 0x0f0f;
  5265. 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]));
  5266. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5267. for (int j = 0; j < QK_K/32; ++j) {
  5268. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5269. q8 += 16; a += 16;
  5270. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5271. q8 += 16; a += 16;
  5272. const float dl = d * scales[j];
  5273. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5274. }
  5275. }
  5276. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5277. *s = sumf;
  5278. #endif
  5279. }
  5280. #endif
  5281. #if QK_K == 256
  5282. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5283. assert(n % QK_K == 0);
  5284. const block_q5_K * restrict x = vx;
  5285. const block_q8_K * restrict y = vy;
  5286. const int nb = n / QK_K;
  5287. static const uint32_t kmask1 = 0x3f3f3f3f;
  5288. static const uint32_t kmask2 = 0x0f0f0f0f;
  5289. static const uint32_t kmask3 = 0x03030303;
  5290. uint32_t utmp[4];
  5291. #ifdef __ARM_NEON
  5292. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5293. const uint8x16_t mone = vdupq_n_u8(1);
  5294. const uint8x16_t mtwo = vdupq_n_u8(2);
  5295. const int32x4_t mzero = vdupq_n_s32(0);
  5296. ggml_int8x16x4_t q5bytes;
  5297. float sumf = 0;
  5298. for (int i = 0; i < nb; ++i) {
  5299. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5300. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5301. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5302. memcpy(utmp, x[i].scales, 12);
  5303. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5304. const uint32_t uaux = utmp[1] & kmask1;
  5305. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5306. utmp[2] = uaux;
  5307. utmp[0] &= kmask1;
  5308. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5309. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5310. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5311. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5312. int32_t sumi_mins = vaddvq_s32(prod);
  5313. const uint8_t * scales = (const uint8_t *)utmp;
  5314. const uint8_t * restrict q5 = x[i].qs;
  5315. const uint8_t * restrict qh = x[i].qh;
  5316. const int8_t * restrict q8 = y[i].qs;
  5317. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5318. ggml_uint8x16x4_t q5h;
  5319. int32_t sumi = 0;
  5320. for (int j = 0; j < QK_K/64; ++j) {
  5321. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5322. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5323. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5324. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5325. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5326. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5327. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5328. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5329. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5330. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5331. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5332. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5333. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5334. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5335. }
  5336. sumf += d * sumi - dmin * sumi_mins;
  5337. }
  5338. *s = sumf;
  5339. #elif defined __AVX2__
  5340. const __m256i m4 = _mm256_set1_epi8(0xF);
  5341. const __m128i mzero = _mm_setzero_si128();
  5342. const __m256i mone = _mm256_set1_epi8(1);
  5343. __m256 acc = _mm256_setzero_ps();
  5344. float summs = 0.f;
  5345. for (int i = 0; i < nb; ++i) {
  5346. const uint8_t * restrict q5 = x[i].qs;
  5347. const int8_t * restrict q8 = y[i].qs;
  5348. #if QK_K == 256
  5349. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5350. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5351. memcpy(utmp, x[i].scales, 12);
  5352. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5353. const uint32_t uaux = utmp[1] & kmask1;
  5354. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5355. utmp[2] = uaux;
  5356. utmp[0] &= kmask1;
  5357. #else
  5358. // TODO
  5359. const float d = 0, dmin = 0;
  5360. #endif
  5361. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5362. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5363. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5364. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5365. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5366. summs += dmin * _mm_extract_epi32(hsum, 0);
  5367. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5368. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5369. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5370. __m256i hmask = mone;
  5371. __m256i sumi = _mm256_setzero_si256();
  5372. int bit = 0;
  5373. for (int j = 0; j < QK_K/64; ++j) {
  5374. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5375. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5376. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5377. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5378. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5379. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5380. hmask = _mm256_slli_epi16(hmask, 1);
  5381. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5382. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5383. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5384. hmask = _mm256_slli_epi16(hmask, 1);
  5385. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5386. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5387. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5388. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5389. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5390. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5391. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5392. }
  5393. __m256 vd = _mm256_set1_ps(d);
  5394. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5395. }
  5396. *s = hsum_float_8(acc) + summs;
  5397. #elif defined __AVX__
  5398. const __m128i m4 = _mm_set1_epi8(0xF);
  5399. const __m128i mzero = _mm_setzero_si128();
  5400. const __m128i mone = _mm_set1_epi8(1);
  5401. const __m128i m2 = _mm_set1_epi8(2);
  5402. __m256 acc = _mm256_setzero_ps();
  5403. float summs = 0.f;
  5404. for (int i = 0; i < nb; ++i) {
  5405. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5406. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5407. const uint8_t * restrict q5 = x[i].qs;
  5408. const int8_t * restrict q8 = y[i].qs;
  5409. memcpy(utmp, x[i].scales, 12);
  5410. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5411. const uint32_t uaux = utmp[1] & kmask1;
  5412. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5413. utmp[2] = uaux;
  5414. utmp[0] &= kmask1;
  5415. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5416. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5417. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5418. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5419. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5420. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5421. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5422. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5423. summs += dmin * _mm_extract_epi32(hsum, 0);
  5424. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5425. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5426. __m128i hmask = mone;
  5427. __m128i sumi_0 = _mm_setzero_si128();
  5428. __m128i sumi_1 = _mm_setzero_si128();
  5429. int bit = 0;
  5430. __m128i shuffle = _mm_set1_epi16(0x0100);
  5431. for (int j = 0; j < QK_K/64; ++j) {
  5432. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5433. shuffle = _mm_add_epi16(shuffle, m2);
  5434. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5435. shuffle = _mm_add_epi16(shuffle, m2);
  5436. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5437. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5438. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5439. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5440. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5441. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5442. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5443. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5444. hmask = _mm_slli_epi16(hmask, 1);
  5445. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5446. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5447. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5448. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5449. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5450. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5451. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5452. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5453. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5454. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5455. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5456. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5457. hmask = _mm_slli_epi16(hmask, 1);
  5458. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5459. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5460. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5461. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5462. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5463. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5464. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5465. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5466. }
  5467. __m256 vd = _mm256_set1_ps(d);
  5468. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5469. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5470. }
  5471. *s = hsum_float_8(acc) + summs;
  5472. #elif defined __riscv_v_intrinsic
  5473. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5474. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5475. float sumf = 0;
  5476. float sums = 0.0;
  5477. size_t vl;
  5478. for (int i = 0; i < nb; ++i) {
  5479. vl = 8;
  5480. const uint8_t * restrict q5 = x[i].qs;
  5481. const uint8_t * restrict hm = x[i].qh;
  5482. const int8_t * restrict q8 = y[i].qs;
  5483. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5484. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5485. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5486. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5487. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5488. memcpy(utmp, x[i].scales, 12);
  5489. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5490. const uint32_t uaux = utmp[1] & kmask1;
  5491. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5492. utmp[2] = uaux;
  5493. utmp[0] &= kmask1;
  5494. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5495. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5496. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5497. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5498. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5499. vl = 32;
  5500. int32_t aux32 = 0;
  5501. int is = 0;
  5502. uint8_t m = 1;
  5503. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5504. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5505. for (int j = 0; j < QK_K/64; ++j) {
  5506. // load Q5 and Q8
  5507. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5508. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5509. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5510. // compute mask for addition
  5511. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5512. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5513. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5514. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5515. m <<= 1;
  5516. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5517. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5518. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5519. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5520. m <<= 1;
  5521. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5522. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5523. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5524. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5525. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5526. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5527. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5528. q5 += 32; q8 += 64;
  5529. }
  5530. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5531. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5532. }
  5533. *s = sumf+sums;
  5534. #else
  5535. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5536. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5537. int8_t aux8[QK_K];
  5538. int16_t aux16[8];
  5539. float sums [8];
  5540. int32_t aux32[8];
  5541. memset(sums, 0, 8*sizeof(float));
  5542. float sumf = 0;
  5543. for (int i = 0; i < nb; ++i) {
  5544. const uint8_t * restrict q4 = x[i].qs;
  5545. const uint8_t * restrict hm = x[i].qh;
  5546. const int8_t * restrict q8 = y[i].qs;
  5547. memset(aux32, 0, 8*sizeof(int32_t));
  5548. int8_t * restrict a = aux8;
  5549. uint8_t m = 1;
  5550. for (int j = 0; j < QK_K/64; ++j) {
  5551. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5552. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5553. a += 32; m <<= 1;
  5554. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5555. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5556. a += 32; m <<= 1;
  5557. q4 += 32;
  5558. }
  5559. memcpy(utmp, x[i].scales, 12);
  5560. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5561. const uint32_t uaux = utmp[1] & kmask1;
  5562. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5563. utmp[2] = uaux;
  5564. utmp[0] &= kmask1;
  5565. int sumi = 0;
  5566. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5567. a = aux8;
  5568. int is = 0;
  5569. for (int j = 0; j < QK_K/32; ++j) {
  5570. int32_t scale = scales[is++];
  5571. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5572. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5573. q8 += 8; a += 8;
  5574. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5575. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5576. q8 += 8; a += 8;
  5577. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5578. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5579. q8 += 8; a += 8;
  5580. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5581. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5582. q8 += 8; a += 8;
  5583. }
  5584. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5585. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5586. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5587. sumf -= dmin * sumi;
  5588. }
  5589. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5590. *s = sumf;
  5591. #endif
  5592. }
  5593. #else
  5594. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5595. assert(n % QK_K == 0);
  5596. const block_q5_K * restrict x = vx;
  5597. const block_q8_K * restrict y = vy;
  5598. const int nb = n / QK_K;
  5599. #ifdef __ARM_NEON
  5600. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5601. const uint8x16_t mh = vdupq_n_u8(16);
  5602. const int32x4_t mzero = vdupq_n_s32(0);
  5603. ggml_int8x16x4_t q5bytes;
  5604. ggml_uint8x16x4_t q5h;
  5605. float sumf = 0;
  5606. for (int i = 0; i < nb; ++i) {
  5607. const float d = y[i].d * (float)x[i].d;
  5608. const int8_t * sc = x[i].scales;
  5609. const uint8_t * restrict q5 = x[i].qs;
  5610. const uint8_t * restrict qh = x[i].qh;
  5611. const int8_t * restrict q8 = y[i].qs;
  5612. const uint8x8_t qhbits = vld1_u8(qh);
  5613. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5614. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5615. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5616. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5617. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5618. q5h.val[2] = vbicq_u8(mh, htmp);
  5619. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5620. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5621. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5622. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5623. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5624. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5625. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5626. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5627. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5628. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5629. }
  5630. *s = sumf;
  5631. #elif defined __AVX2__
  5632. const __m256i m4 = _mm256_set1_epi8(0xF);
  5633. const __m256i mone = _mm256_set1_epi8(1);
  5634. __m256 acc = _mm256_setzero_ps();
  5635. for (int i = 0; i < nb; ++i) {
  5636. const uint8_t * restrict q5 = x[i].qs;
  5637. const int8_t * restrict q8 = y[i].qs;
  5638. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5639. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5640. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5641. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5642. int64_t aux64;
  5643. memcpy(&aux64, x[i].qh, 8);
  5644. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5645. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5646. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5647. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5648. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5649. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5650. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5651. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5652. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5653. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5654. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5655. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5656. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5657. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5658. }
  5659. *s = hsum_float_8(acc);
  5660. #elif defined __AVX__
  5661. const __m128i m4 = _mm_set1_epi8(0xF);
  5662. const __m128i mone = _mm_set1_epi8(1);
  5663. __m256 acc = _mm256_setzero_ps();
  5664. for (int i = 0; i < nb; ++i) {
  5665. const uint8_t * restrict q5 = x[i].qs;
  5666. const int8_t * restrict q8 = y[i].qs;
  5667. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5668. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5669. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5670. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5671. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5672. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5673. int64_t aux64;
  5674. memcpy(&aux64, x[i].qh, 8);
  5675. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5676. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5677. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5678. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5679. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5680. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5681. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5682. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5683. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5684. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5685. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5686. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5687. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5688. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5689. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5690. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5691. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5692. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5693. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5694. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5695. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5696. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5697. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5698. }
  5699. *s = hsum_float_8(acc);
  5700. #elif defined __riscv_v_intrinsic
  5701. float sumf = 0;
  5702. for (int i = 0; i < nb; ++i) {
  5703. const float d = y[i].d * (float)x[i].d;
  5704. const int8_t * sc = x[i].scales;
  5705. const uint8_t * restrict q5 = x[i].qs;
  5706. const uint8_t * restrict qh = x[i].qh;
  5707. const int8_t * restrict q8 = y[i].qs;
  5708. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5709. // load qh
  5710. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5711. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5712. size_t vl = 16;
  5713. // combine both qh_1 and qh_2
  5714. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5715. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5716. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5717. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5718. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5719. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5720. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5721. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5722. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5723. // load q5
  5724. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5725. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5726. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5727. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5728. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5729. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5730. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5731. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5732. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5733. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5734. // load Q8 and multiply it with Q5
  5735. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5736. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5737. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5738. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5739. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5740. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5741. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5742. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5743. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5744. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5745. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5746. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5747. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5748. }
  5749. *s = sumf;
  5750. #else
  5751. int8_t aux8[QK_K];
  5752. int16_t aux16[16];
  5753. float sums [8];
  5754. memset(sums, 0, 8*sizeof(float));
  5755. float sumf = 0;
  5756. for (int i = 0; i < nb; ++i) {
  5757. const uint8_t * restrict q4 = x[i].qs;
  5758. const uint8_t * restrict hm = x[i].qh;
  5759. const int8_t * restrict q8 = y[i].qs;
  5760. int8_t * restrict a = aux8;
  5761. for (int l = 0; l < 32; ++l) {
  5762. a[l+ 0] = q4[l] & 0xF;
  5763. a[l+32] = q4[l] >> 4;
  5764. }
  5765. for (int is = 0; is < 8; ++is) {
  5766. uint8_t m = 1 << is;
  5767. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5768. }
  5769. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5770. const int8_t * restrict sc = x[i].scales;
  5771. for (int j = 0; j < QK_K/16; ++j) {
  5772. const float dl = d * sc[j];
  5773. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5774. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5775. q8 += 16; a += 16;
  5776. }
  5777. }
  5778. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5779. *s = sumf;
  5780. #endif
  5781. }
  5782. #endif
  5783. #if QK_K == 256
  5784. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5785. assert(n % QK_K == 0);
  5786. const block_q6_K * restrict x = vx;
  5787. const block_q8_K * restrict y = vy;
  5788. const int nb = n / QK_K;
  5789. #ifdef __ARM_NEON
  5790. float sum = 0;
  5791. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5792. const int32x4_t vzero = vdupq_n_s32(0);
  5793. //const int8x16_t m32s = vdupq_n_s8(32);
  5794. const uint8x16_t mone = vdupq_n_u8(3);
  5795. ggml_int8x16x4_t q6bytes;
  5796. ggml_uint8x16x4_t q6h;
  5797. for (int i = 0; i < nb; ++i) {
  5798. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5799. const uint8_t * restrict q6 = x[i].ql;
  5800. const uint8_t * restrict qh = x[i].qh;
  5801. const int8_t * restrict q8 = y[i].qs;
  5802. const int8_t * restrict scale = x[i].scales;
  5803. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  5804. const int8x16_t scales = vld1q_s8(scale);
  5805. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  5806. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5807. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5808. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5809. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5810. int32_t isum_mins = vaddvq_s32(prod);
  5811. int32_t isum = 0;
  5812. for (int j = 0; j < QK_K/128; ++j) {
  5813. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  5814. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  5815. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5816. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5817. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5818. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5819. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5820. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5821. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5822. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5823. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5824. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5825. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5826. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5827. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  5828. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  5829. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  5830. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5831. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5832. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5833. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5834. scale += 4;
  5835. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5836. shifted = vshrq_n_u8(qhbits.val[0], 4);
  5837. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5838. shifted = vshrq_n_u8(qhbits.val[1], 4);
  5839. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5840. shifted = vshrq_n_u8(qhbits.val[0], 6);
  5841. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5842. shifted = vshrq_n_u8(qhbits.val[1], 6);
  5843. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5844. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  5845. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  5846. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  5847. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  5848. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  5849. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  5850. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  5851. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  5852. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5853. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5854. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5855. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5856. scale += 4;
  5857. }
  5858. //sum += isum * d_all * y[i].d;
  5859. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  5860. }
  5861. *s = sum;
  5862. #elif defined __AVX2__
  5863. const __m256i m4 = _mm256_set1_epi8(0xF);
  5864. const __m256i m2 = _mm256_set1_epi8(3);
  5865. const __m256i m32s = _mm256_set1_epi8(32);
  5866. __m256 acc = _mm256_setzero_ps();
  5867. for (int i = 0; i < nb; ++i) {
  5868. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5869. const uint8_t * restrict q4 = x[i].ql;
  5870. const uint8_t * restrict qh = x[i].qh;
  5871. const int8_t * restrict q8 = y[i].qs;
  5872. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5873. __m256i sumi = _mm256_setzero_si256();
  5874. int is = 0;
  5875. for (int j = 0; j < QK_K/128; ++j) {
  5876. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  5877. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  5878. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  5879. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  5880. is += 4;
  5881. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5882. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5883. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  5884. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  5885. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  5886. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  5887. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  5888. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5889. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  5890. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  5891. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  5892. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5893. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5894. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5895. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5896. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5897. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5898. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  5899. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  5900. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5901. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5902. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  5903. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  5904. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5905. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5906. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5907. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5908. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5909. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5910. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  5911. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  5912. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5913. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  5914. }
  5915. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5916. }
  5917. *s = hsum_float_8(acc);
  5918. #elif defined __AVX__
  5919. const __m128i m4 = _mm_set1_epi8(0xF);
  5920. const __m128i m3 = _mm_set1_epi8(3);
  5921. const __m128i m32s = _mm_set1_epi8(32);
  5922. const __m128i m2 = _mm_set1_epi8(2);
  5923. __m256 acc = _mm256_setzero_ps();
  5924. for (int i = 0; i < nb; ++i) {
  5925. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5926. const uint8_t * restrict q4 = x[i].ql;
  5927. const uint8_t * restrict qh = x[i].qh;
  5928. const int8_t * restrict q8 = y[i].qs;
  5929. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5930. __m128i sumi_0 = _mm_setzero_si128();
  5931. __m128i sumi_1 = _mm_setzero_si128();
  5932. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  5933. for (int j = 0; j < QK_K/128; ++j) {
  5934. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5935. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5936. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  5937. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  5938. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  5939. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  5940. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  5941. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  5942. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  5943. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  5944. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5945. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5946. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5947. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5948. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  5949. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  5950. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  5951. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  5952. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  5953. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  5954. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  5955. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  5956. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5957. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5958. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5959. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5960. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5961. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5962. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5963. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5964. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  5965. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  5966. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  5967. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  5968. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  5969. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  5970. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  5971. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  5972. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  5973. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  5974. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  5975. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  5976. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  5977. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  5978. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  5979. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  5980. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5981. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5982. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5983. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5984. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5985. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5986. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5987. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5988. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5989. shuffle = _mm_add_epi8(shuffle, m2);
  5990. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5991. shuffle = _mm_add_epi8(shuffle, m2);
  5992. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  5993. shuffle = _mm_add_epi8(shuffle, m2);
  5994. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  5995. shuffle = _mm_add_epi8(shuffle, m2);
  5996. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5997. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5998. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5999. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6000. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6001. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6002. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6003. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6004. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6005. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6006. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6007. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6008. }
  6009. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6010. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6011. }
  6012. *s = hsum_float_8(acc);
  6013. #elif defined __riscv_v_intrinsic
  6014. float sumf = 0;
  6015. for (int i = 0; i < nb; ++i) {
  6016. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6017. const uint8_t * restrict q6 = x[i].ql;
  6018. const uint8_t * restrict qh = x[i].qh;
  6019. const int8_t * restrict q8 = y[i].qs;
  6020. const int8_t * restrict scale = x[i].scales;
  6021. size_t vl;
  6022. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6023. int sum_t = 0;
  6024. int is = 0;
  6025. for (int j = 0; j < QK_K/128; ++j) {
  6026. vl = 32;
  6027. // load qh
  6028. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6029. // load Q6
  6030. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6031. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6032. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6033. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6034. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6035. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6036. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6037. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6038. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6039. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6040. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6041. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6042. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6043. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6044. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6045. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6046. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6047. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6048. // load Q8 and take product
  6049. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6050. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6051. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6052. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6053. vl = 16;
  6054. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6055. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6056. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6057. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6058. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6059. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6060. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6061. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6062. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6063. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6064. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6065. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6066. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6067. q6 += 64; qh += 32; q8 += 128; is=8;
  6068. }
  6069. sumf += d * sum_t;
  6070. }
  6071. *s = sumf;
  6072. #else
  6073. int8_t aux8[QK_K];
  6074. int16_t aux16[8];
  6075. float sums [8];
  6076. int32_t aux32[8];
  6077. memset(sums, 0, 8*sizeof(float));
  6078. float sumf = 0;
  6079. for (int i = 0; i < nb; ++i) {
  6080. const uint8_t * restrict q4 = x[i].ql;
  6081. const uint8_t * restrict qh = x[i].qh;
  6082. const int8_t * restrict q8 = y[i].qs;
  6083. memset(aux32, 0, 8*sizeof(int32_t));
  6084. int8_t * restrict a = aux8;
  6085. for (int j = 0; j < QK_K; j += 128) {
  6086. for (int l = 0; l < 32; ++l) {
  6087. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6088. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6089. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6090. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6091. }
  6092. a += 128;
  6093. q4 += 64;
  6094. qh += 32;
  6095. }
  6096. a = aux8;
  6097. int is = 0;
  6098. for (int j = 0; j < QK_K/16; ++j) {
  6099. int scale = x[i].scales[is++];
  6100. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6101. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6102. q8 += 8; a += 8;
  6103. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6104. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6105. q8 += 8; a += 8;
  6106. }
  6107. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6108. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6109. }
  6110. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6111. *s = sumf;
  6112. #endif
  6113. }
  6114. #else
  6115. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6116. assert(n % QK_K == 0);
  6117. const block_q6_K * restrict x = vx;
  6118. const block_q8_K * restrict y = vy;
  6119. const int nb = n / QK_K;
  6120. #ifdef __ARM_NEON
  6121. float sum = 0;
  6122. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6123. const int8x16_t m32s = vdupq_n_s8(32);
  6124. const int32x4_t vzero = vdupq_n_s32(0);
  6125. const uint8x16_t mone = vdupq_n_u8(3);
  6126. ggml_int8x16x4_t q6bytes;
  6127. ggml_uint8x16x4_t q6h;
  6128. for (int i = 0; i < nb; ++i) {
  6129. const float d_all = (float)x[i].d;
  6130. const uint8_t * restrict q6 = x[i].ql;
  6131. const uint8_t * restrict qh = x[i].qh;
  6132. const int8_t * restrict q8 = y[i].qs;
  6133. const int8_t * restrict scale = x[i].scales;
  6134. int32_t isum = 0;
  6135. uint8x16_t qhbits = vld1q_u8(qh);
  6136. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6137. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6138. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6139. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6140. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6141. shifted = vshrq_n_u8(qhbits, 4);
  6142. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6143. shifted = vshrq_n_u8(qhbits, 6);
  6144. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6145. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6146. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6147. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6148. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6149. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6150. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6151. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6152. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6153. sum += isum * d_all * y[i].d;
  6154. }
  6155. *s = sum;
  6156. #elif defined __AVX2__
  6157. const __m256i m4 = _mm256_set1_epi8(0xF);
  6158. const __m256i m2 = _mm256_set1_epi8(3);
  6159. const __m256i m32s = _mm256_set1_epi8(32);
  6160. __m256 acc = _mm256_setzero_ps();
  6161. for (int i = 0; i < nb; ++i) {
  6162. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6163. const uint8_t * restrict q4 = x[i].ql;
  6164. const uint8_t * restrict qh = x[i].qh;
  6165. const int8_t * restrict q8 = y[i].qs;
  6166. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6167. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6168. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6169. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6170. __m256i sumi = _mm256_setzero_si256();
  6171. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6172. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6173. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6174. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6175. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6176. 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);
  6177. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6178. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6179. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6180. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6181. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6182. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6183. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6184. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6185. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6186. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6187. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6188. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6189. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6190. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6191. }
  6192. *s = hsum_float_8(acc);
  6193. #elif defined __AVX__
  6194. const __m128i m4 = _mm_set1_epi8(0xF);
  6195. const __m128i m2 = _mm_set1_epi8(3);
  6196. const __m128i m32s = _mm_set1_epi8(32);
  6197. __m256 acc = _mm256_setzero_ps();
  6198. for (int i = 0; i < nb; ++i) {
  6199. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6200. const uint8_t * restrict q4 = x[i].ql;
  6201. const uint8_t * restrict qh = x[i].qh;
  6202. const int8_t * restrict q8 = y[i].qs;
  6203. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6204. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6205. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6206. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6207. __m128i sumi_0 = _mm_setzero_si128();
  6208. __m128i sumi_1 = _mm_setzero_si128();
  6209. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6210. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6211. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6212. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6213. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6214. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6215. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6216. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6217. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6218. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6219. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6220. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6221. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6222. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6223. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6224. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6225. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6226. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6227. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6228. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6229. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6230. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6231. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6232. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6233. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6234. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6235. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6236. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6237. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6238. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6239. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6240. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6241. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6242. }
  6243. *s = hsum_float_8(acc);
  6244. #elif defined __riscv_v_intrinsic
  6245. float sumf = 0;
  6246. for (int i = 0; i < nb; ++i) {
  6247. const float d_all = (float)x[i].d;
  6248. const uint8_t * restrict q6 = x[i].ql;
  6249. const uint8_t * restrict qh = x[i].qh;
  6250. const int8_t * restrict q8 = y[i].qs;
  6251. const int8_t * restrict scale = x[i].scales;
  6252. int32_t isum = 0;
  6253. size_t vl = 16;
  6254. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6255. // load Q6
  6256. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6257. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6258. // load qh
  6259. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6260. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6261. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6262. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6263. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6264. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6265. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6266. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6267. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6268. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6269. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6270. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6271. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6272. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6273. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6274. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6275. // load Q8 and take product
  6276. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6277. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6278. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6279. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6280. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6281. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6282. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6283. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6284. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6285. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6286. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6287. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6288. sumf += isum * d_all * y[i].d;
  6289. }
  6290. *s = sumf;
  6291. #else
  6292. int8_t aux8[QK_K];
  6293. int16_t aux16[8];
  6294. float sums [8];
  6295. int32_t aux32[8];
  6296. memset(sums, 0, 8*sizeof(float));
  6297. float sumf = 0;
  6298. for (int i = 0; i < nb; ++i) {
  6299. const uint8_t * restrict q4 = x[i].ql;
  6300. const uint8_t * restrict qh = x[i].qh;
  6301. const int8_t * restrict q8 = y[i].qs;
  6302. memset(aux32, 0, 8*sizeof(int32_t));
  6303. int8_t * restrict a = aux8;
  6304. for (int l = 0; l < 16; ++l) {
  6305. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6306. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6307. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6308. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6309. }
  6310. int is = 0;
  6311. for (int j = 0; j < QK_K/16; ++j) {
  6312. int scale = x[i].scales[is++];
  6313. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6314. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6315. q8 += 8; a += 8;
  6316. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6317. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6318. q8 += 8; a += 8;
  6319. }
  6320. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6321. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6322. }
  6323. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6324. *s = sumf;
  6325. #endif
  6326. }
  6327. #endif
  6328. static const int8_t keven_signs_q2xs[1024] = {
  6329. 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,
  6330. 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,
  6331. 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,
  6332. 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,
  6333. 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,
  6334. 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,
  6335. 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,
  6336. 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,
  6337. 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,
  6338. 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,
  6339. 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,
  6340. 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,
  6341. 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,
  6342. 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,
  6343. 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,
  6344. 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,
  6345. 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,
  6346. 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,
  6347. 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,
  6348. 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,
  6349. 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,
  6350. 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,
  6351. 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,
  6352. 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,
  6353. 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,
  6354. 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,
  6355. 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,
  6356. 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,
  6357. 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,
  6358. 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,
  6359. 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,
  6360. 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,
  6361. };
  6362. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6363. assert(n % QK_K == 0);
  6364. const block_iq2_xxs * restrict x = vx;
  6365. const block_q8_K * restrict y = vy;
  6366. const int nb = n / QK_K;
  6367. #if defined(__ARM_NEON)
  6368. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6369. uint32_t aux32[4];
  6370. const uint8_t * aux8 = (const uint8_t *)aux32;
  6371. ggml_int8x16x4_t q2u;
  6372. ggml_int8x16x4_t q2s;
  6373. ggml_int8x16x4_t q8b;
  6374. float sumf = 0;
  6375. for (int i = 0; i < nb; ++i) {
  6376. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6377. const uint16_t * restrict q2 = x[i].qs;
  6378. const int8_t * restrict q8 = y[i].qs;
  6379. float sumf1 = 0, sumf2 = 0;
  6380. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6381. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6382. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6383. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6384. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6385. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6386. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6387. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6388. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6389. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6390. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6391. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6392. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6393. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6394. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6395. 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]);
  6396. 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]);
  6397. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6398. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6399. }
  6400. sumf += d*(sumf1 + sumf2);
  6401. }
  6402. *s = 0.25f * sumf;
  6403. #elif defined(__AVX2__)
  6404. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6405. uint32_t aux32[4];
  6406. const uint8_t * aux8 = (const uint8_t *)aux32;
  6407. __m256 accumf = _mm256_setzero_ps();
  6408. for (int i = 0; i < nb; ++i) {
  6409. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6410. const uint16_t * restrict q2 = x[i].qs;
  6411. const int8_t * restrict q8 = y[i].qs;
  6412. __m256i sumi1 = _mm256_setzero_si256();
  6413. __m256i sumi2 = _mm256_setzero_si256();
  6414. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6415. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6416. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6417. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6418. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6419. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6420. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6421. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6422. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6423. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6424. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6425. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6426. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6427. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6428. const uint16_t ls1 = aux32[1] >> 28;
  6429. const uint16_t ls2 = aux32[3] >> 28;
  6430. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6431. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6432. sumi1 = _mm256_add_epi32(sumi1, p1);
  6433. sumi2 = _mm256_add_epi32(sumi2, p2);
  6434. }
  6435. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6436. }
  6437. *s = 0.125f * hsum_float_8(accumf);
  6438. #else
  6439. uint32_t aux32[2];
  6440. const uint8_t * aux8 = (const uint8_t *)aux32;
  6441. float sumf = 0.f;
  6442. for (int i = 0; i < nb; ++i) {
  6443. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6444. const uint16_t * restrict q2 = x[i].qs;
  6445. const int8_t * restrict q8 = y[i].qs;
  6446. int32_t bsum = 0;
  6447. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6448. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6449. q2 += 4;
  6450. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6451. int32_t sumi = 0;
  6452. for (int l = 0; l < 4; ++l) {
  6453. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6454. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6455. for (int j = 0; j < 8; ++j) {
  6456. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6457. }
  6458. q8 += 8;
  6459. }
  6460. bsum += sumi * ls;
  6461. }
  6462. sumf += d * bsum;
  6463. }
  6464. *s = 0.125f * sumf;
  6465. #endif
  6466. }
  6467. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6468. assert(n % QK_K == 0);
  6469. const block_iq2_xs * restrict x = vx;
  6470. const block_q8_K * restrict y = vy;
  6471. const int nb = n / QK_K;
  6472. #if defined(__ARM_NEON)
  6473. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6474. ggml_int8x16x4_t q2u;
  6475. ggml_int8x16x4_t q2s;
  6476. ggml_int8x16x4_t q8b;
  6477. int32x4x4_t scales32;
  6478. float sumf = 0;
  6479. for (int i = 0; i < nb; ++i) {
  6480. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6481. const uint16_t * restrict q2 = x[i].qs;
  6482. const int8_t * restrict q8 = y[i].qs;
  6483. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6484. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6485. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6486. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6487. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6488. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6489. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6490. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6491. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6492. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6493. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6494. int32x4_t sumi = vdupq_n_s32(0);
  6495. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6496. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6497. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6498. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6499. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6500. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6501. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6502. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6503. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6504. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6505. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6506. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6507. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6508. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6509. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6510. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6511. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6512. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6513. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6514. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6515. q2 += 8;
  6516. }
  6517. sumf += d*vaddvq_s32(sumi);
  6518. }
  6519. *s = 0.125f * sumf;
  6520. #elif defined(__AVX2__)
  6521. const __m128i m4 = _mm_set1_epi8(0xf);
  6522. const __m128i m1 = _mm_set1_epi8(1);
  6523. const __m128i m511 = _mm_set1_epi16(511);
  6524. const __m128i m127 = _mm_set1_epi16(127);
  6525. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6526. uint64_t aux64;
  6527. // somewhat hacky, but gives a significant boost in performance
  6528. __m128i aux_gindex, aux_sindex;
  6529. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6530. const uint16_t * sindex = (const uint16_t *)&aux_sindex;
  6531. __m256 accumf = _mm256_setzero_ps();
  6532. for (int i = 0; i < nb; ++i) {
  6533. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6534. const uint16_t * restrict q2 = x[i].qs;
  6535. const int8_t * restrict q8 = y[i].qs;
  6536. memcpy(&aux64, x[i].scales, 8);
  6537. __m128i stmp = _mm_set1_epi64x(aux64);
  6538. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6539. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6540. __m256i sumi1 = _mm256_setzero_si256();
  6541. __m256i sumi2 = _mm256_setzero_si256();
  6542. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6543. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6544. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6545. const __m128i q2_data = _mm_loadu_si128((const __m128i*)q2); q2 += 8;
  6546. aux_gindex = _mm_and_si128(q2_data, m511);
  6547. aux_sindex = _mm_and_si128(_mm_srli_epi16(q2_data, 9), m127);
  6548. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]], iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
  6549. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]], iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
  6550. const __m256i s2_1 = _mm256_set_epi64x(signs64[sindex[3]], signs64[sindex[2]], signs64[sindex[1]], signs64[sindex[0]]);
  6551. const __m256i s2_2 = _mm256_set_epi64x(signs64[sindex[7]], signs64[sindex[6]], signs64[sindex[5]], signs64[sindex[4]]);
  6552. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6553. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6554. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6555. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6556. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6557. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6558. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6559. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6560. }
  6561. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6562. }
  6563. *s = 0.125f * hsum_float_8(accumf);
  6564. #else
  6565. float sumf = 0.f;
  6566. for (int i = 0; i < nb; ++i) {
  6567. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6568. const uint16_t * restrict q2 = x[i].qs;
  6569. const uint8_t * restrict sc = x[i].scales;
  6570. const int8_t * restrict q8 = y[i].qs;
  6571. int32_t bsum = 0;
  6572. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6573. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6574. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6575. int32_t sumi = 0;
  6576. for (int l = 0; l < 2; ++l) {
  6577. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6578. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6579. for (int j = 0; j < 8; ++j) {
  6580. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6581. }
  6582. q8 += 8;
  6583. }
  6584. bsum += sumi * ls1;
  6585. sumi = 0;
  6586. for (int l = 2; l < 4; ++l) {
  6587. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6588. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6589. for (int j = 0; j < 8; ++j) {
  6590. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6591. }
  6592. q8 += 8;
  6593. }
  6594. bsum += sumi * ls2;
  6595. q2 += 4;
  6596. }
  6597. sumf += d * bsum;
  6598. }
  6599. *s = 0.125f * sumf;
  6600. #endif
  6601. }
  6602. // ================================ IQ2 quantization =============================================
  6603. typedef struct {
  6604. uint64_t * grid;
  6605. int * map;
  6606. uint16_t * neighbours;
  6607. } iq2_entry_t;
  6608. static iq2_entry_t iq2_data[2] = {
  6609. {NULL, NULL, NULL},
  6610. {NULL, NULL, NULL},
  6611. };
  6612. static inline int iq2_data_index(int grid_size) {
  6613. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  6614. return grid_size == 256 ? 0 : 1;
  6615. }
  6616. static int iq2_compare_func(const void * left, const void * right) {
  6617. const int * l = (const int *)left;
  6618. const int * r = (const int *)right;
  6619. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  6620. }
  6621. static void q2xs_init_impl(int grid_size) {
  6622. const int gindex = iq2_data_index(grid_size);
  6623. if (iq2_data[gindex].grid) {
  6624. return;
  6625. }
  6626. static const uint16_t kgrid_256[256] = {
  6627. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  6628. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  6629. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  6630. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  6631. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  6632. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  6633. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  6634. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  6635. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  6636. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  6637. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  6638. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  6639. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  6640. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  6641. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  6642. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  6643. };
  6644. static const uint16_t kgrid_512[512] = {
  6645. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  6646. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  6647. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  6648. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  6649. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  6650. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  6651. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  6652. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  6653. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  6654. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  6655. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  6656. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  6657. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  6658. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  6659. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  6660. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  6661. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  6662. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  6663. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  6664. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  6665. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  6666. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  6667. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  6668. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  6669. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  6670. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  6671. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  6672. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  6673. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  6674. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  6675. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  6676. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  6677. };
  6678. const int kmap_size = 43692;
  6679. const int nwant = 2;
  6680. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  6681. uint64_t * kgrid_q2xs;
  6682. int * kmap_q2xs;
  6683. uint16_t * kneighbors_q2xs;
  6684. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  6685. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  6686. for (int k = 0; k < grid_size; ++k) {
  6687. int8_t * pos = (int8_t *)(the_grid + k);
  6688. for (int i = 0; i < 8; ++i) {
  6689. int l = (kgrid[k] >> 2*i) & 0x3;
  6690. pos[i] = 2*l + 1;
  6691. }
  6692. }
  6693. kgrid_q2xs = the_grid;
  6694. iq2_data[gindex].grid = the_grid;
  6695. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  6696. iq2_data[gindex].map = kmap_q2xs;
  6697. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  6698. uint64_t aux64;
  6699. uint8_t * aux8 = (uint8_t *)&aux64;
  6700. for (int i = 0; i < grid_size; ++i) {
  6701. aux64 = kgrid_q2xs[i];
  6702. uint16_t index = 0;
  6703. for (int k=0; k<8; ++k) {
  6704. uint16_t q = (aux8[k] - 1)/2;
  6705. index |= (q << 2*k);
  6706. }
  6707. kmap_q2xs[index] = i;
  6708. }
  6709. int8_t pos[8];
  6710. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  6711. int num_neighbors = 0, num_not_in_map = 0;
  6712. for (int i = 0; i < kmap_size; ++i) {
  6713. if (kmap_q2xs[i] >= 0) continue;
  6714. ++num_not_in_map;
  6715. for (int k = 0; k < 8; ++k) {
  6716. int l = (i >> 2*k) & 0x3;
  6717. pos[k] = 2*l + 1;
  6718. }
  6719. for (int j = 0; j < grid_size; ++j) {
  6720. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6721. int d2 = 0;
  6722. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6723. dist2[2*j+0] = d2;
  6724. dist2[2*j+1] = j;
  6725. }
  6726. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6727. int n = 0; int d2 = dist2[0];
  6728. int nhave = 1;
  6729. for (int j = 0; j < grid_size; ++j) {
  6730. if (dist2[2*j] > d2) {
  6731. if (nhave == nwant) break;
  6732. d2 = dist2[2*j];
  6733. ++nhave;
  6734. }
  6735. ++n;
  6736. }
  6737. num_neighbors += n;
  6738. }
  6739. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  6740. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  6741. iq2_data[gindex].neighbours = kneighbors_q2xs;
  6742. int counter = 0;
  6743. for (int i = 0; i < kmap_size; ++i) {
  6744. if (kmap_q2xs[i] >= 0) continue;
  6745. for (int k = 0; k < 8; ++k) {
  6746. int l = (i >> 2*k) & 0x3;
  6747. pos[k] = 2*l + 1;
  6748. }
  6749. for (int j = 0; j < grid_size; ++j) {
  6750. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6751. int d2 = 0;
  6752. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6753. dist2[2*j+0] = d2;
  6754. dist2[2*j+1] = j;
  6755. }
  6756. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6757. kmap_q2xs[i] = -(counter + 1);
  6758. int d2 = dist2[0];
  6759. uint16_t * start = &kneighbors_q2xs[counter++];
  6760. int n = 0, nhave = 1;
  6761. for (int j = 0; j < grid_size; ++j) {
  6762. if (dist2[2*j] > d2) {
  6763. if (nhave == nwant) break;
  6764. d2 = dist2[2*j];
  6765. ++nhave;
  6766. }
  6767. kneighbors_q2xs[counter++] = dist2[2*j+1];
  6768. ++n;
  6769. }
  6770. *start = n;
  6771. }
  6772. free(dist2);
  6773. }
  6774. void ggml_init_iq2_quantization(enum ggml_type type) {
  6775. if (type == GGML_TYPE_IQ2_XXS) {
  6776. q2xs_init_impl(256);
  6777. }
  6778. else if (type == GGML_TYPE_IQ2_XS) {
  6779. q2xs_init_impl(512);
  6780. }
  6781. else {
  6782. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6783. }
  6784. }
  6785. static void q2xs_deinit_impl(int grid_size) {
  6786. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  6787. const int gindex = iq2_data_index(grid_size);
  6788. if (iq2_data[gindex].grid) {
  6789. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  6790. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  6791. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  6792. }
  6793. }
  6794. void ggml_deinit_iq2_quantization(enum ggml_type type) {
  6795. if (type == GGML_TYPE_IQ2_XXS) {
  6796. q2xs_deinit_impl(256);
  6797. }
  6798. else if (type == GGML_TYPE_IQ2_XS) {
  6799. q2xs_deinit_impl(512);
  6800. }
  6801. else {
  6802. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6803. }
  6804. }
  6805. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  6806. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  6807. int num_neighbors = neighbours[0];
  6808. GGML_ASSERT(num_neighbors > 0);
  6809. float best_d2 = FLT_MAX;
  6810. int grid_index = -1;
  6811. for (int j = 1; j <= num_neighbors; ++j) {
  6812. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  6813. float d2 = 0;
  6814. for (int i = 0; i < 8; ++i) {
  6815. float q = pg[i];
  6816. float diff = scale*q - xval[i];
  6817. d2 += weight[i]*diff*diff;
  6818. }
  6819. if (d2 < best_d2) {
  6820. best_d2 = d2; grid_index = neighbours[j];
  6821. }
  6822. }
  6823. GGML_ASSERT(grid_index >= 0);
  6824. const int8_t * pg = (const int8_t *)(grid + grid_index);
  6825. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  6826. return grid_index;
  6827. }
  6828. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  6829. const int gindex = iq2_data_index(256);
  6830. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  6831. const int * kmap_q2xs = iq2_data[gindex].map;
  6832. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  6833. GGML_ASSERT(quant_weights);
  6834. GGML_ASSERT(kgrid_q2xs);
  6835. GGML_ASSERT(kmap_q2xs);
  6836. GGML_ASSERT(kneighbors_q2xs);
  6837. GGML_ASSERT(n%QK_K == 0);
  6838. const int kMaxQ = 3;
  6839. const int nbl = n/256;
  6840. block_iq2_xxs * y = vy;
  6841. float scales[QK_K/32];
  6842. float weight[32];
  6843. float xval[32];
  6844. int8_t L[32];
  6845. int8_t Laux[32];
  6846. float waux[32];
  6847. bool is_on_grid[4];
  6848. bool is_on_grid_aux[4];
  6849. uint8_t block_signs[4];
  6850. uint32_t q2[2*(QK_K/32)];
  6851. for (int ibl = 0; ibl < nbl; ++ibl) {
  6852. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  6853. memset(q2, 0, QK_K/4);
  6854. float max_scale = 0;
  6855. const float * xbl = x + QK_K*ibl;
  6856. float sumx2 = 0;
  6857. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  6858. float sigma2 = sumx2/QK_K;
  6859. for (int ib = 0; ib < QK_K/32; ++ib) {
  6860. const float * xb = xbl + 32*ib;
  6861. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  6862. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  6863. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  6864. for (int k = 0; k < 4; ++k) {
  6865. int nflip = 0;
  6866. uint8_t s = 0;
  6867. for (int i = 0; i < 8; ++i) {
  6868. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  6869. else {
  6870. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  6871. }
  6872. }
  6873. if (nflip%2) {
  6874. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  6875. for (int i = 1; i < 8; ++i) {
  6876. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  6877. if (ax < min) {
  6878. min = ax; imin = i;
  6879. }
  6880. }
  6881. xval[8*k+imin] = -xval[8*k+imin];
  6882. s ^= (1 << imin);
  6883. }
  6884. block_signs[k] = s & 127;
  6885. }
  6886. float max = xval[0];
  6887. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  6888. if (!max) {
  6889. scales[ib] = 0;
  6890. memset(L, 0, 32);
  6891. continue;
  6892. }
  6893. float best = 0;
  6894. float scale = max/(2*kMaxQ-1);
  6895. for (int is = -9; is <= 9; ++is) {
  6896. float id = (2*kMaxQ-1+is*0.1f)/max;
  6897. float this_scale = 1/id;
  6898. for (int k = 0; k < 4; ++k) {
  6899. for (int i = 0; i < 8; ++i) {
  6900. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6901. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  6902. }
  6903. uint16_t u = 0;
  6904. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  6905. int grid_index = kmap_q2xs[u];
  6906. is_on_grid_aux[k] = true;
  6907. if (grid_index < 0) {
  6908. is_on_grid_aux[k] = false;
  6909. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6910. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  6911. }
  6912. }
  6913. float sumqx = 0, sumq2 = 0;
  6914. for (int i = 0; i < 32; ++i) {
  6915. float w = weight[i];
  6916. float q = 2*Laux[i] + 1;
  6917. sumqx += w*xval[i]*q;
  6918. sumq2 += w*q*q;
  6919. }
  6920. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  6921. scale = sumqx/sumq2; best = scale*sumqx;
  6922. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  6923. for (int k = 0; k < 4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  6924. }
  6925. }
  6926. int n_not_ongrid = 0;
  6927. for (int k = 0; k < 4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  6928. if (n_not_ongrid > 0 && scale > 0) {
  6929. float id = 1/scale;
  6930. for (int k = 0; k < 4; ++k) {
  6931. if (is_on_grid[k]) continue;
  6932. uint16_t u = 0;
  6933. for (int i = 0; i < 8; ++i) {
  6934. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6935. l = MAX(0, MIN(kMaxQ-1, l));
  6936. u |= (l << 2*i);
  6937. }
  6938. int grid_index = kmap_q2xs[u];
  6939. if (grid_index < 0) {
  6940. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6941. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  6942. }
  6943. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  6944. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  6945. }
  6946. float sumqx = 0, sumq2 = 0;
  6947. for (int i = 0; i < 32; ++i) {
  6948. float w = weight[i];
  6949. float q = 2*L[i] + 1;
  6950. sumqx += w*xval[i]*q;
  6951. sumq2 += w*q*q;
  6952. }
  6953. if (sumq2 > 0) scale = sumqx/sumq2;
  6954. }
  6955. if (scale < 0) {
  6956. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  6957. // and correspondingly flip quant signs.
  6958. scale = -scale;
  6959. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  6960. }
  6961. for (int k = 0; k < 4; ++k) {
  6962. uint16_t u = 0;
  6963. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  6964. int grid_index = kmap_q2xs[u];
  6965. if (grid_index < 0) {
  6966. printf("Oops: found point %u not on grid:", u);
  6967. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  6968. printf("\n");
  6969. GGML_ASSERT(false);
  6970. }
  6971. q2[2*ib+0] |= (grid_index << 8*k);
  6972. q2[2*ib+1] |= (block_signs[k] << 7*k);
  6973. }
  6974. GGML_ASSERT(scale >= 0);
  6975. scales[ib] = scale;
  6976. max_scale = MAX(max_scale, scale);
  6977. }
  6978. if (!max_scale) {
  6979. memset(y[ibl].qs, 0, QK_K/4);
  6980. continue;
  6981. }
  6982. float d = max_scale/31;
  6983. y[ibl].d = GGML_FP32_TO_FP16(d);
  6984. float id = 1/d;
  6985. float sumqx = 0, sumq2 = 0;
  6986. for (int ib = 0; ib < QK_K/32; ++ib) {
  6987. int l = nearest_int(0.5f*(id*scales[ib]-1));
  6988. l = MAX(0, MIN(15, l));
  6989. q2[2*ib+1] |= ((uint32_t)l << 28);
  6990. const float * xb = xbl + 32*ib;
  6991. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  6992. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  6993. const uint8_t * aux8 = (const uint8_t *)(q2 + 2*ib);
  6994. const float db = d * (1 + 2*l);
  6995. uint32_t u = 0;
  6996. for (int k = 0; k < 4; ++k) {
  6997. const int8_t * signs = keven_signs_q2xs + 8*((q2[2*ib+1] >> 7*k) & 127);
  6998. const float * xk = xb + 8*k;
  6999. const float * wk = weight + 8*k;
  7000. const uint8_t * grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7001. float best_mse = 0; int best_index = aux8[k];
  7002. for (int j = 0; j < 8; ++j) {
  7003. float diff = db * grid[j] * signs[j] - xk[j];
  7004. best_mse += wk[j] * diff * diff;
  7005. }
  7006. for (int idx = 0; idx < 256; ++idx) {
  7007. grid = (const uint8_t *)(kgrid_q2xs + idx);
  7008. float mse = 0;
  7009. for (int j = 0; j < 8; ++j) {
  7010. float diff = db * grid[j] * signs[j] - xk[j];
  7011. mse += wk[j] * diff * diff;
  7012. }
  7013. if (mse < best_mse) {
  7014. best_mse = mse; best_index = idx;
  7015. }
  7016. }
  7017. u |= (best_index << 8*k);
  7018. grid = (const uint8_t *)(kgrid_q2xs + best_index);
  7019. //grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7020. for (int j = 0; j < 8; ++j) {
  7021. float q = db * grid[j] * signs[j];
  7022. sumqx += wk[j] * q * xk[j];
  7023. sumq2 += wk[j] * q * q;
  7024. }
  7025. }
  7026. q2[2*ib] = u;
  7027. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  7028. }
  7029. memcpy(y[ibl].qs, q2, QK_K/4);
  7030. }
  7031. }
  7032. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7033. const int gindex = iq2_data_index(512);
  7034. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7035. const int * kmap_q2xs = iq2_data[gindex].map;
  7036. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7037. GGML_ASSERT(quant_weights);
  7038. GGML_ASSERT(kmap_q2xs);
  7039. GGML_ASSERT(kgrid_q2xs);
  7040. GGML_ASSERT(kneighbors_q2xs);
  7041. GGML_ASSERT(n%QK_K == 0);
  7042. const int kMaxQ = 3;
  7043. const int nbl = n/256;
  7044. block_iq2_xs * y = vy;
  7045. float scales[QK_K/16];
  7046. float weight[16];
  7047. float xval[16];
  7048. int8_t L[16];
  7049. int8_t Laux[16];
  7050. float waux[16];
  7051. bool is_on_grid[2];
  7052. bool is_on_grid_aux[2];
  7053. uint8_t block_signs[2];
  7054. uint16_t q2[2*(QK_K/16)];
  7055. for (int ibl = 0; ibl < nbl; ++ibl) {
  7056. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7057. memset(q2, 0, QK_K/4);
  7058. memset(y[ibl].scales, 0, QK_K/32);
  7059. float max_scale = 0;
  7060. const float * xbl = x + QK_K*ibl;
  7061. float sumx2 = 0;
  7062. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7063. float sigma2 = sumx2/QK_K;
  7064. for (int ib = 0; ib < QK_K/16; ++ib) {
  7065. const float * xb = xbl + 16*ib;
  7066. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  7067. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7068. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  7069. for (int k = 0; k < 2; ++k) {
  7070. int nflip = 0;
  7071. uint8_t s = 0;
  7072. for (int i = 0; i < 8; ++i) {
  7073. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7074. else {
  7075. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7076. }
  7077. }
  7078. if (nflip%2) {
  7079. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7080. for (int i = 1; i < 8; ++i) {
  7081. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7082. if (ax < min) {
  7083. min = ax; imin = i;
  7084. }
  7085. }
  7086. xval[8*k+imin] = -xval[8*k+imin];
  7087. s ^= (1 << imin);
  7088. }
  7089. block_signs[k] = s & 127;
  7090. }
  7091. float max = xval[0];
  7092. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  7093. if (!max) {
  7094. scales[ib] = 0;
  7095. memset(L, 0, 16);
  7096. continue;
  7097. }
  7098. float best = 0;
  7099. float scale = max/(2*kMaxQ-1);
  7100. is_on_grid[0] = is_on_grid[1] = true;
  7101. for (int is = -9; is <= 9; ++is) {
  7102. float id = (2*kMaxQ-1+is*0.1f)/max;
  7103. float this_scale = 1/id;
  7104. for (int k = 0; k < 2; ++k) {
  7105. for (int i = 0; i < 8; ++i) {
  7106. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7107. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7108. }
  7109. uint16_t u = 0;
  7110. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7111. int grid_index = kmap_q2xs[u];
  7112. is_on_grid_aux[k] = true;
  7113. if (grid_index < 0) {
  7114. is_on_grid_aux[k] = false;
  7115. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7116. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7117. }
  7118. }
  7119. float sumqx = 0, sumq2 = 0;
  7120. for (int i = 0; i < 16; ++i) {
  7121. float w = weight[i];
  7122. float q = 2*Laux[i] + 1;
  7123. sumqx += w*xval[i]*q;
  7124. sumq2 += w*q*q;
  7125. }
  7126. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7127. scale = sumqx/sumq2; best = scale*sumqx;
  7128. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  7129. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7130. }
  7131. }
  7132. int n_not_ongrid = 0;
  7133. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7134. if (n_not_ongrid > 0 && scale > 0) {
  7135. float id = 1/scale;
  7136. for (int k = 0; k < 2; ++k) {
  7137. if (is_on_grid[k]) continue;
  7138. uint16_t u = 0;
  7139. for (int i = 0; i < 8; ++i) {
  7140. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7141. l = MAX(0, MIN(kMaxQ-1, l));
  7142. u |= (l << 2*i);
  7143. L[8*k + i] = l;
  7144. }
  7145. int grid_index = kmap_q2xs[u];
  7146. if (grid_index < 0) {
  7147. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7148. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7149. }
  7150. }
  7151. float sumqx = 0, sumq2 = 0;
  7152. for (int i = 0; i < 16; ++i) {
  7153. float w = weight[i];
  7154. float q = 2*L[i] + 1;
  7155. sumqx += w*xval[i]*q;
  7156. sumq2 += w*q*q;
  7157. }
  7158. if (sumq2 > 0) scale = sumqx/sumq2;
  7159. }
  7160. if (scale < 0) {
  7161. scale = -scale;
  7162. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7163. }
  7164. for (int k = 0; k < 2; ++k) {
  7165. uint16_t u = 0;
  7166. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7167. int grid_index = kmap_q2xs[u];
  7168. if (grid_index < 0) {
  7169. printf("Oops: found point %u not on grid:", u);
  7170. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7171. printf("\n");
  7172. GGML_ASSERT(false);
  7173. }
  7174. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  7175. }
  7176. GGML_ASSERT(scale >= 0);
  7177. scales[ib] = scale;
  7178. max_scale = MAX(max_scale, scale);
  7179. }
  7180. if (!max_scale) {
  7181. memset(y[ibl].qs, 0, QK_K/4);
  7182. continue;
  7183. }
  7184. float d = max_scale/31;
  7185. y[ibl].d = GGML_FP32_TO_FP16(d);
  7186. float id = 1/d;
  7187. for (int ib = 0; ib < QK_K/16; ++ib) {
  7188. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7189. l = MAX(0, MIN(15, l));
  7190. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  7191. else y[ibl].scales[ib/2] |= (l << 4);
  7192. }
  7193. memcpy(y[ibl].qs, q2, QK_K/4);
  7194. }
  7195. }
  7196. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7197. (void)hist;
  7198. GGML_ASSERT(n_per_row%QK_K == 0);
  7199. int nblock = n_per_row/QK_K;
  7200. char * qrow = (char *)dst;
  7201. for (int row = 0; row < nrow; ++row) {
  7202. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  7203. src += n_per_row;
  7204. qrow += nblock*sizeof(block_iq2_xxs);
  7205. }
  7206. return nrow * nblock * sizeof(block_iq2_xxs);
  7207. }
  7208. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7209. (void)hist;
  7210. GGML_ASSERT(n_per_row%QK_K == 0);
  7211. int nblock = n_per_row/QK_K;
  7212. char * qrow = (char *)dst;
  7213. for (int row = 0; row < nrow; ++row) {
  7214. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  7215. src += n_per_row;
  7216. qrow += nblock*sizeof(block_iq2_xs);
  7217. }
  7218. return nrow * nblock * sizeof(block_iq2_xs);
  7219. }