ggml-quants.c 360 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205
  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. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2591. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2592. if (!quant_weights) {
  2593. quantize_row_q4_0_reference(x, y, n_per_row);
  2594. return;
  2595. }
  2596. float weight[QK4_0];
  2597. int8_t L[QK4_0];
  2598. float sum_x2 = 0;
  2599. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2600. float sigma2 = sum_x2/n_per_row;
  2601. const int nb = n_per_row/QK4_0;
  2602. for (int ib = 0; ib < nb; ++ib) {
  2603. const float * xb = x + QK4_0 * ib;
  2604. const float * qw = quant_weights + QK4_0 * ib;
  2605. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2606. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2607. y[ib].d = GGML_FP32_TO_FP16(d);
  2608. for (int j = 0; j < 16; ++j) {
  2609. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2610. }
  2611. }
  2612. }
  2613. size_t quantize_q4_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2614. if (!quant_weights) {
  2615. return ggml_quantize_q4_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2616. }
  2617. int row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2618. char * qrow = (char *)dst;
  2619. for (int row = 0; row < nrow; ++row) {
  2620. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2621. src += n_per_row;
  2622. qrow += row_size;
  2623. }
  2624. return nrow * row_size;
  2625. }
  2626. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2627. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2628. if (!quant_weights) {
  2629. quantize_row_q4_1_reference(x, y, n_per_row);
  2630. return;
  2631. }
  2632. float weight[QK4_1];
  2633. uint8_t L[QK4_1], Laux[QK4_1];
  2634. float sum_x2 = 0;
  2635. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2636. float sigma2 = sum_x2/n_per_row;
  2637. const int nb = n_per_row/QK4_1;
  2638. for (int ib = 0; ib < nb; ++ib) {
  2639. const float * xb = x + QK4_1 * ib;
  2640. const float * qw = quant_weights + QK4_1 * ib;
  2641. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2642. float min;
  2643. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2644. y[ib].d = GGML_FP32_TO_FP16(d);
  2645. y[ib].m = GGML_FP32_TO_FP16(-min);
  2646. for (int j = 0; j < 16; ++j) {
  2647. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2648. }
  2649. }
  2650. }
  2651. size_t quantize_q4_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2652. if (!quant_weights) {
  2653. return ggml_quantize_q4_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2654. }
  2655. int row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2656. char * qrow = (char *)dst;
  2657. for (int row = 0; row < nrow; ++row) {
  2658. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2659. src += n_per_row;
  2660. qrow += row_size;
  2661. }
  2662. return nrow * row_size;
  2663. }
  2664. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2665. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2666. if (!quant_weights) {
  2667. quantize_row_q5_0_reference(x, y, n_per_row);
  2668. return;
  2669. }
  2670. float weight[QK5_0];
  2671. int8_t L[QK5_0];
  2672. float sum_x2 = 0;
  2673. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2674. float sigma2 = sum_x2/n_per_row;
  2675. const int nb = n_per_row/QK5_0;
  2676. for (int ib = 0; ib < nb; ++ib) {
  2677. const float * xb = x + QK5_0 * ib;
  2678. const float * qw = quant_weights + QK5_0 * ib;
  2679. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2680. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2681. y[ib].d = GGML_FP32_TO_FP16(d);
  2682. uint32_t qh = 0;
  2683. for (int j = 0; j < 16; ++j) {
  2684. const uint8_t xi0 = L[j];
  2685. const uint8_t xi1 = L[j+16];
  2686. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2687. // get the 5-th bit and store it in qh at the right position
  2688. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2689. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2690. }
  2691. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2692. }
  2693. }
  2694. size_t quantize_q5_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2695. if (!quant_weights) {
  2696. return ggml_quantize_q5_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2697. }
  2698. int row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2699. char * qrow = (char *)dst;
  2700. for (int row = 0; row < nrow; ++row) {
  2701. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2702. src += n_per_row;
  2703. qrow += row_size;
  2704. }
  2705. return nrow * row_size;
  2706. }
  2707. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2708. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2709. if (!quant_weights) {
  2710. quantize_row_q5_1_reference(x, y, n_per_row);
  2711. return;
  2712. }
  2713. float weight[QK5_1];
  2714. uint8_t L[QK5_1], Laux[QK5_1];
  2715. float sum_x2 = 0;
  2716. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2717. float sigma2 = sum_x2/n_per_row;
  2718. const int nb = n_per_row/QK5_1;
  2719. for (int ib = 0; ib < nb; ++ib) {
  2720. const float * xb = x + QK5_1 * ib;
  2721. const float * qw = quant_weights + QK5_1 * ib;
  2722. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2723. float min;
  2724. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2725. y[ib].d = GGML_FP32_TO_FP16(d);
  2726. y[ib].m = GGML_FP32_TO_FP16(-min);
  2727. uint32_t qh = 0;
  2728. for (int j = 0; j < 16; ++j) {
  2729. const uint8_t xi0 = L[j];
  2730. const uint8_t xi1 = L[j+16];
  2731. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2732. // get the 5-th bit and store it in qh at the right position
  2733. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2734. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2735. }
  2736. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2737. }
  2738. }
  2739. size_t quantize_q5_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2740. if (!quant_weights) {
  2741. return ggml_quantize_q5_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2742. }
  2743. int row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2744. char * qrow = (char *)dst;
  2745. for (int row = 0; row < nrow; ++row) {
  2746. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2747. src += n_per_row;
  2748. qrow += row_size;
  2749. }
  2750. return nrow * row_size;
  2751. }
  2752. // ====================== "True" 2-bit (de)-quantization
  2753. static const uint64_t iq2xxs_grid[256] = {
  2754. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2755. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2756. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2757. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2758. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2759. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2760. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2761. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2762. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2763. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2764. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2765. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2766. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2767. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2768. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2769. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2770. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2771. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2772. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2773. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2774. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2775. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2776. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2777. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2778. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2779. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2780. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2781. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2782. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2783. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2784. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2785. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2786. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2787. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2788. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2789. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2790. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2791. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2792. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2793. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2794. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2795. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2796. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2797. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2798. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2799. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2800. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2801. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2802. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2803. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2804. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2805. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2806. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2807. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2808. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2809. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2810. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2811. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2812. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2813. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2814. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2815. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2816. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2817. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2818. };
  2819. static const uint64_t iq2xs_grid[512] = {
  2820. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2821. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2822. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2823. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2824. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2825. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2826. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2827. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2828. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2829. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2830. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2831. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2832. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2833. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2834. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2835. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2836. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2837. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2838. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2839. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2840. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2841. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2842. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2843. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2844. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2845. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2846. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2847. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2848. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2849. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2850. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2851. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2852. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2853. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2854. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2855. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2856. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2857. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2858. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2859. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2860. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2861. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2862. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2863. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2864. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2865. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2866. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2867. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2868. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2869. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2870. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2871. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2872. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2873. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2874. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2875. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2876. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2877. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2878. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2879. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2880. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2881. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2882. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2883. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2884. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2885. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2886. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2887. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2888. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2889. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2890. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2891. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2892. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2893. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2894. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2895. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2896. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2897. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2898. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2899. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2900. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2901. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2902. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2903. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2904. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2905. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2906. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2907. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2908. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2909. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2910. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2911. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2912. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2913. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2914. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2915. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2916. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2917. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2918. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2919. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2920. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2921. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2922. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2923. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2924. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2925. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2926. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2927. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2928. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2929. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2930. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2931. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2932. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2933. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2934. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2935. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2936. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2937. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2938. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2939. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2940. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2941. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2942. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2943. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2944. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2945. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2946. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2947. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2948. };
  2949. static const uint8_t ksigns_iq2xs[128] = {
  2950. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2951. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2952. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2953. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2954. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2955. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2956. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2957. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2958. };
  2959. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2960. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2961. assert(k % QK_K == 0);
  2962. const int nb = k / QK_K;
  2963. uint32_t aux32[2];
  2964. const uint8_t * aux8 = (const uint8_t *)aux32;
  2965. for (int i = 0; i < nb; i++) {
  2966. const float d = GGML_FP16_TO_FP32(x[i].d);
  2967. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2968. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2969. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2970. for (int l = 0; l < 4; ++l) {
  2971. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2972. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2973. for (int j = 0; j < 8; ++j) {
  2974. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2975. }
  2976. y += 8;
  2977. }
  2978. }
  2979. }
  2980. }
  2981. // ====================== 2.3125 bpw (de)-quantization
  2982. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2983. assert(k % QK_K == 0);
  2984. const int nb = k / QK_K;
  2985. float db[2];
  2986. for (int i = 0; i < nb; i++) {
  2987. const float d = GGML_FP16_TO_FP32(x[i].d);
  2988. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2989. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2990. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2991. for (int l = 0; l < 4; ++l) {
  2992. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2993. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2994. for (int j = 0; j < 8; ++j) {
  2995. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2996. }
  2997. y += 8;
  2998. }
  2999. }
  3000. }
  3001. }
  3002. //===================================== Q8_K ==============================================
  3003. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3004. assert(k % QK_K == 0);
  3005. const int nb = k / QK_K;
  3006. for (int i = 0; i < nb; i++) {
  3007. float max = 0;
  3008. float amax = 0;
  3009. for (int j = 0; j < QK_K; ++j) {
  3010. float ax = fabsf(x[j]);
  3011. if (ax > amax) {
  3012. amax = ax; max = x[j];
  3013. }
  3014. }
  3015. if (!amax) {
  3016. y[i].d = 0;
  3017. memset(y[i].qs, 0, QK_K);
  3018. x += QK_K;
  3019. continue;
  3020. }
  3021. //const float iscale = -128.f/max;
  3022. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3023. const float iscale = -127.f/max;
  3024. for (int j = 0; j < QK_K; ++j) {
  3025. int v = nearest_int(iscale*x[j]);
  3026. y[i].qs[j] = MIN(127, v);
  3027. }
  3028. for (int j = 0; j < QK_K/16; ++j) {
  3029. int sum = 0;
  3030. for (int ii = 0; ii < 16; ++ii) {
  3031. sum += y[i].qs[j*16 + ii];
  3032. }
  3033. y[i].bsums[j] = sum;
  3034. }
  3035. y[i].d = 1/iscale;
  3036. x += QK_K;
  3037. }
  3038. }
  3039. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3040. assert(k % QK_K == 0);
  3041. const int nb = k / QK_K;
  3042. for (int i = 0; i < nb; i++) {
  3043. for (int j = 0; j < QK_K; ++j) {
  3044. *y++ = x[i].d * x[i].qs[j];
  3045. }
  3046. }
  3047. }
  3048. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3049. quantize_row_q8_K_reference(x, y, k);
  3050. }
  3051. //===================================== Dot ptoducts =================================
  3052. //
  3053. // Helper functions
  3054. //
  3055. #if __AVX__ || __AVX2__ || __AVX512F__
  3056. // shuffles to pick the required scales in dot products
  3057. static inline __m256i get_scale_shuffle_q3k(int i) {
  3058. static const uint8_t k_shuffle[128] = {
  3059. 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,
  3060. 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,
  3061. 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,
  3062. 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,
  3063. };
  3064. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3065. }
  3066. static inline __m256i get_scale_shuffle_k4(int i) {
  3067. static const uint8_t k_shuffle[256] = {
  3068. 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,
  3069. 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,
  3070. 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,
  3071. 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,
  3072. 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,
  3073. 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,
  3074. 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,
  3075. 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
  3076. };
  3077. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3078. }
  3079. static inline __m128i get_scale_shuffle(int i) {
  3080. static const uint8_t k_shuffle[128] = {
  3081. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3082. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3083. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3084. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3085. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3086. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3087. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3088. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3089. };
  3090. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3091. }
  3092. #endif
  3093. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3094. const int qk = QK8_0;
  3095. const int nb = n / qk;
  3096. assert(n % qk == 0);
  3097. const block_q4_0 * restrict x = vx;
  3098. const block_q8_0 * restrict y = vy;
  3099. #if defined(__ARM_NEON)
  3100. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3101. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3102. assert(nb % 2 == 0); // TODO: handle odd nb
  3103. for (int i = 0; i < nb; i += 2) {
  3104. const block_q4_0 * restrict x0 = &x[i + 0];
  3105. const block_q4_0 * restrict x1 = &x[i + 1];
  3106. const block_q8_0 * restrict y0 = &y[i + 0];
  3107. const block_q8_0 * restrict y1 = &y[i + 1];
  3108. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3109. const int8x16_t s8b = vdupq_n_s8(0x8);
  3110. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3111. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3112. // 4-bit -> 8-bit
  3113. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3114. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3115. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3116. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3117. // sub 8
  3118. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3119. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3120. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3121. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3122. // load y
  3123. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3124. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3125. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3126. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3127. // dot product into int32x4_t
  3128. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3129. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3130. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3131. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3132. }
  3133. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3134. #elif defined(__AVX2__)
  3135. // Initialize accumulator with zeros
  3136. __m256 acc = _mm256_setzero_ps();
  3137. // Main loop
  3138. for (int i = 0; i < nb; ++i) {
  3139. /* Compute combined scale for the block */
  3140. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3141. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3142. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3143. const __m256i off = _mm256_set1_epi8( 8 );
  3144. bx = _mm256_sub_epi8( bx, off );
  3145. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3146. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3147. /* Multiply q with scale and accumulate */
  3148. acc = _mm256_fmadd_ps( d, q, acc );
  3149. }
  3150. *s = hsum_float_8(acc);
  3151. #elif defined(__AVX__)
  3152. // Initialize accumulator with zeros
  3153. __m256 acc = _mm256_setzero_ps();
  3154. // Main loop
  3155. for (int i = 0; i < nb; ++i) {
  3156. // Compute combined scale for the block
  3157. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3158. const __m128i lowMask = _mm_set1_epi8(0xF);
  3159. const __m128i off = _mm_set1_epi8(8);
  3160. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3161. __m128i bx = _mm_and_si128(lowMask, tmp);
  3162. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  3163. bx = _mm_sub_epi8(bx, off);
  3164. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  3165. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3166. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3167. bx = _mm_sub_epi8(bx, off);
  3168. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  3169. // Convert int32_t to float
  3170. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3171. // Apply the scale, and accumulate
  3172. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3173. }
  3174. *s = hsum_float_8(acc);
  3175. #elif defined(__SSSE3__)
  3176. // set constants
  3177. const __m128i lowMask = _mm_set1_epi8(0xF);
  3178. const __m128i off = _mm_set1_epi8(8);
  3179. // Initialize accumulator with zeros
  3180. __m128 acc_0 = _mm_setzero_ps();
  3181. __m128 acc_1 = _mm_setzero_ps();
  3182. __m128 acc_2 = _mm_setzero_ps();
  3183. __m128 acc_3 = _mm_setzero_ps();
  3184. // First round without accumulation
  3185. {
  3186. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3187. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3188. // Compute combined scale for the block 0 and 1
  3189. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3190. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3191. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3192. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3193. bx_0 = _mm_sub_epi8(bx_0, off);
  3194. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3195. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3196. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3197. bx_1 = _mm_sub_epi8(bx_1, off);
  3198. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3199. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3200. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3201. // Compute combined scale for the block 2 and 3
  3202. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3203. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3204. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3205. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3206. bx_2 = _mm_sub_epi8(bx_2, off);
  3207. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3208. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3209. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3210. bx_3 = _mm_sub_epi8(bx_3, off);
  3211. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3212. // Convert int32_t to float
  3213. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3214. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3215. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3216. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3217. // Apply the scale
  3218. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3219. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3220. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3221. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3222. }
  3223. assert(nb % 2 == 0); // TODO: handle odd nb
  3224. // Main loop
  3225. for (int i = 2; i < nb; i+=2) {
  3226. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3227. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3228. // Compute combined scale for the block 0 and 1
  3229. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3230. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3231. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3232. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3233. bx_0 = _mm_sub_epi8(bx_0, off);
  3234. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3235. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3236. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3237. bx_1 = _mm_sub_epi8(bx_1, off);
  3238. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3239. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3240. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3241. // Compute combined scale for the block 2 and 3
  3242. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3243. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3244. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3245. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3246. bx_2 = _mm_sub_epi8(bx_2, off);
  3247. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3248. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3249. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3250. bx_3 = _mm_sub_epi8(bx_3, off);
  3251. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3252. // Convert int32_t to float
  3253. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3254. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3255. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3256. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3257. // Apply the scale
  3258. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3259. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3260. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3261. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3262. // Acummulate
  3263. acc_0 = _mm_add_ps(p0_d, acc_0);
  3264. acc_1 = _mm_add_ps(p1_d, acc_1);
  3265. acc_2 = _mm_add_ps(p2_d, acc_2);
  3266. acc_3 = _mm_add_ps(p3_d, acc_3);
  3267. }
  3268. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3269. #elif defined(__riscv_v_intrinsic)
  3270. float sumf = 0.0;
  3271. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3272. for (int i = 0; i < nb; i++) {
  3273. // load elements
  3274. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3275. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3276. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3277. // mask and store lower part of x, and then upper part
  3278. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3279. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3280. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3281. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3282. // subtract offset
  3283. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3284. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3285. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3286. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3287. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3288. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3289. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3290. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3291. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3292. }
  3293. *s = sumf;
  3294. #else
  3295. // scalar
  3296. float sumf = 0.0;
  3297. for (int i = 0; i < nb; i++) {
  3298. int sumi = 0;
  3299. for (int j = 0; j < qk/2; ++j) {
  3300. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3301. const int v1 = (x[i].qs[j] >> 4) - 8;
  3302. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3303. }
  3304. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3305. }
  3306. *s = sumf;
  3307. #endif
  3308. }
  3309. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3310. const int qk = QK8_1;
  3311. const int nb = n / qk;
  3312. assert(n % qk == 0);
  3313. const block_q4_1 * restrict x = vx;
  3314. const block_q8_1 * restrict y = vy;
  3315. // TODO: add WASM SIMD
  3316. #if defined(__ARM_NEON)
  3317. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3318. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3319. float summs = 0;
  3320. assert(nb % 2 == 0); // TODO: handle odd nb
  3321. for (int i = 0; i < nb; i += 2) {
  3322. const block_q4_1 * restrict x0 = &x[i + 0];
  3323. const block_q4_1 * restrict x1 = &x[i + 1];
  3324. const block_q8_1 * restrict y0 = &y[i + 0];
  3325. const block_q8_1 * restrict y1 = &y[i + 1];
  3326. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3327. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3328. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3329. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3330. // 4-bit -> 8-bit
  3331. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3332. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3333. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3334. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3335. // load y
  3336. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3337. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3338. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3339. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3340. // dot product into int32x4_t
  3341. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3342. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3343. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3344. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3345. }
  3346. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3347. #elif defined(__AVX2__) || defined(__AVX__)
  3348. // Initialize accumulator with zeros
  3349. __m256 acc = _mm256_setzero_ps();
  3350. float summs = 0;
  3351. // Main loop
  3352. for (int i = 0; i < nb; ++i) {
  3353. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3354. const float d1 = y[i].d;
  3355. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3356. const __m256 d0v = _mm256_set1_ps( d0 );
  3357. const __m256 d1v = _mm256_set1_ps( d1 );
  3358. // Compute combined scales
  3359. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3360. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3361. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3362. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3363. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  3364. // Accumulate d0*d1*x*y
  3365. #if defined(__AVX2__)
  3366. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3367. #else
  3368. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3369. #endif
  3370. }
  3371. *s = hsum_float_8(acc) + summs;
  3372. #elif defined(__riscv_v_intrinsic)
  3373. float sumf = 0.0;
  3374. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3375. for (int i = 0; i < nb; i++) {
  3376. // load elements
  3377. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3378. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3379. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3380. // mask and store lower part of x, and then upper part
  3381. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3382. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3383. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3384. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3385. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3386. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3387. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3388. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3389. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3390. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3391. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3392. }
  3393. *s = sumf;
  3394. #else
  3395. // scalar
  3396. float sumf = 0.0;
  3397. for (int i = 0; i < nb; i++) {
  3398. int sumi = 0;
  3399. for (int j = 0; j < qk/2; ++j) {
  3400. const int v0 = (x[i].qs[j] & 0x0F);
  3401. const int v1 = (x[i].qs[j] >> 4);
  3402. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3403. }
  3404. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3405. }
  3406. *s = sumf;
  3407. #endif
  3408. }
  3409. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3410. const int qk = QK8_0;
  3411. const int nb = n / qk;
  3412. assert(n % qk == 0);
  3413. assert(qk == QK5_0);
  3414. const block_q5_0 * restrict x = vx;
  3415. const block_q8_0 * restrict y = vy;
  3416. #if defined(__ARM_NEON)
  3417. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3418. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3419. uint32_t qh0;
  3420. uint32_t qh1;
  3421. uint64_t tmp0[4];
  3422. uint64_t tmp1[4];
  3423. assert(nb % 2 == 0); // TODO: handle odd nb
  3424. for (int i = 0; i < nb; i += 2) {
  3425. const block_q5_0 * restrict x0 = &x[i];
  3426. const block_q5_0 * restrict x1 = &x[i + 1];
  3427. const block_q8_0 * restrict y0 = &y[i];
  3428. const block_q8_0 * restrict y1 = &y[i + 1];
  3429. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3430. // extract the 5th bit via lookup table ((!b) << 4)
  3431. memcpy(&qh0, x0->qh, sizeof(qh0));
  3432. memcpy(&qh1, x1->qh, sizeof(qh1));
  3433. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3434. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3435. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3436. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3437. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3438. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3439. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3440. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3441. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3442. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3443. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3444. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3445. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3446. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3447. // 4-bit -> 8-bit
  3448. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3449. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3450. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3451. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3452. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3453. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3454. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3455. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3456. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3457. // load y
  3458. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3459. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3460. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3461. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3462. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3463. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3464. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3465. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3466. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3467. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3468. }
  3469. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3470. #elif defined(__wasm_simd128__)
  3471. v128_t sumv = wasm_f32x4_splat(0.0f);
  3472. uint32_t qh;
  3473. uint64_t tmp[4];
  3474. // TODO: check if unrolling this is better
  3475. for (int i = 0; i < nb; ++i) {
  3476. const block_q5_0 * restrict x0 = &x[i];
  3477. const block_q8_0 * restrict y0 = &y[i];
  3478. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3479. // extract the 5th bit
  3480. memcpy(&qh, x0->qh, sizeof(qh));
  3481. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3482. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3483. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3484. tmp[3] = table_b2b_1[(qh >> 24) ];
  3485. const v128_t qhl = wasm_v128_load(tmp + 0);
  3486. const v128_t qhh = wasm_v128_load(tmp + 2);
  3487. const v128_t v0 = wasm_v128_load(x0->qs);
  3488. // 4-bit -> 8-bit
  3489. const v128_t v0l = wasm_v128_and (v0, m4b);
  3490. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3491. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3492. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3493. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3494. // load y
  3495. const v128_t v1l = wasm_v128_load(y0->qs);
  3496. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3497. // int8x16 -> int16x8
  3498. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3499. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3500. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3501. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3502. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3503. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3504. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3505. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3506. // dot product
  3507. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3508. wasm_i32x4_add(
  3509. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3510. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3511. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3512. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3513. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3514. }
  3515. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3516. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3517. #elif defined(__AVX2__)
  3518. // Initialize accumulator with zeros
  3519. __m256 acc = _mm256_setzero_ps();
  3520. // Main loop
  3521. for (int i = 0; i < nb; i++) {
  3522. /* Compute combined scale for the block */
  3523. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3524. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3525. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3526. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3527. bx = _mm256_or_si256(bx, bxhi);
  3528. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3529. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3530. /* Multiply q with scale and accumulate */
  3531. acc = _mm256_fmadd_ps(d, q, acc);
  3532. }
  3533. *s = hsum_float_8(acc);
  3534. #elif defined(__AVX__)
  3535. // Initialize accumulator with zeros
  3536. __m256 acc = _mm256_setzero_ps();
  3537. __m128i mask = _mm_set1_epi8((char)0xF0);
  3538. // Main loop
  3539. for (int i = 0; i < nb; i++) {
  3540. /* Compute combined scale for the block */
  3541. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3542. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3543. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3544. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3545. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3546. bxhil = _mm_andnot_si128(bxhil, mask);
  3547. bxhih = _mm_andnot_si128(bxhih, mask);
  3548. __m128i bxl = _mm256_castsi256_si128(bx);
  3549. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3550. bxl = _mm_or_si128(bxl, bxhil);
  3551. bxh = _mm_or_si128(bxh, bxhih);
  3552. bx = MM256_SET_M128I(bxh, bxl);
  3553. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3554. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3555. /* Multiply q with scale and accumulate */
  3556. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3557. }
  3558. *s = hsum_float_8(acc);
  3559. #elif defined(__riscv_v_intrinsic)
  3560. float sumf = 0.0;
  3561. uint32_t qh;
  3562. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3563. // These temporary registers are for masking and shift operations
  3564. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3565. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3566. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3567. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3568. for (int i = 0; i < nb; i++) {
  3569. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3570. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3571. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3572. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3573. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3574. // ((qh & (1u << (j + 16))) >> (j + 12));
  3575. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3576. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3577. // narrowing
  3578. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3579. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3580. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3581. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3582. // load
  3583. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3584. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3585. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3586. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3587. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3588. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3589. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3590. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3591. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3592. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3593. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3594. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3595. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3596. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3597. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3598. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3599. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3600. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3601. }
  3602. *s = sumf;
  3603. #else
  3604. // scalar
  3605. float sumf = 0.0;
  3606. for (int i = 0; i < nb; i++) {
  3607. uint32_t qh;
  3608. memcpy(&qh, x[i].qh, sizeof(qh));
  3609. int sumi = 0;
  3610. for (int j = 0; j < qk/2; ++j) {
  3611. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3612. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3613. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3614. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3615. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3616. }
  3617. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3618. }
  3619. *s = sumf;
  3620. #endif
  3621. }
  3622. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3623. const int qk = QK8_1;
  3624. const int nb = n / qk;
  3625. assert(n % qk == 0);
  3626. assert(qk == QK5_1);
  3627. const block_q5_1 * restrict x = vx;
  3628. const block_q8_1 * restrict y = vy;
  3629. #if defined(__ARM_NEON)
  3630. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3631. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3632. float summs0 = 0.0f;
  3633. float summs1 = 0.0f;
  3634. uint32_t qh0;
  3635. uint32_t qh1;
  3636. uint64_t tmp0[4];
  3637. uint64_t tmp1[4];
  3638. assert(nb % 2 == 0); // TODO: handle odd nb
  3639. for (int i = 0; i < nb; i += 2) {
  3640. const block_q5_1 * restrict x0 = &x[i];
  3641. const block_q5_1 * restrict x1 = &x[i + 1];
  3642. const block_q8_1 * restrict y0 = &y[i];
  3643. const block_q8_1 * restrict y1 = &y[i + 1];
  3644. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3645. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3646. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3647. // extract the 5th bit via lookup table ((b) << 4)
  3648. memcpy(&qh0, x0->qh, sizeof(qh0));
  3649. memcpy(&qh1, x1->qh, sizeof(qh1));
  3650. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3651. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3652. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3653. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3654. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3655. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3656. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3657. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3658. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3659. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3660. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3661. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3662. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3663. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3664. // 4-bit -> 8-bit
  3665. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3666. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3667. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3668. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3669. // add high bit
  3670. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3671. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3672. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3673. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3674. // load y
  3675. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3676. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3677. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3678. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3679. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3680. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3681. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3682. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3683. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3684. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3685. }
  3686. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3687. #elif defined(__wasm_simd128__)
  3688. v128_t sumv = wasm_f32x4_splat(0.0f);
  3689. float summs = 0.0f;
  3690. uint32_t qh;
  3691. uint64_t tmp[4];
  3692. // TODO: check if unrolling this is better
  3693. for (int i = 0; i < nb; ++i) {
  3694. const block_q5_1 * restrict x0 = &x[i];
  3695. const block_q8_1 * restrict y0 = &y[i];
  3696. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3697. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3698. // extract the 5th bit
  3699. memcpy(&qh, x0->qh, sizeof(qh));
  3700. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3701. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3702. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3703. tmp[3] = table_b2b_0[(qh >> 24) ];
  3704. const v128_t qhl = wasm_v128_load(tmp + 0);
  3705. const v128_t qhh = wasm_v128_load(tmp + 2);
  3706. const v128_t v0 = wasm_v128_load(x0->qs);
  3707. // 4-bit -> 8-bit
  3708. const v128_t v0l = wasm_v128_and (v0, m4b);
  3709. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3710. // add high bit
  3711. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3712. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3713. // load y
  3714. const v128_t v1l = wasm_v128_load(y0->qs);
  3715. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3716. // int8x16 -> int16x8
  3717. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3718. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3719. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3720. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3721. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3722. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3723. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3724. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3725. // dot product
  3726. sumv = wasm_f32x4_add(sumv,
  3727. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3728. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3729. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3730. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3731. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3732. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3733. }
  3734. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3735. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3736. #elif defined(__AVX2__)
  3737. // Initialize accumulator with zeros
  3738. __m256 acc = _mm256_setzero_ps();
  3739. float summs = 0.0f;
  3740. // Main loop
  3741. for (int i = 0; i < nb; i++) {
  3742. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3743. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3744. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3745. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3746. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3747. bx = _mm256_or_si256(bx, bxhi);
  3748. const __m256 dy = _mm256_set1_ps(y[i].d);
  3749. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3750. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3751. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3752. }
  3753. *s = hsum_float_8(acc) + summs;
  3754. #elif defined(__AVX__)
  3755. // Initialize accumulator with zeros
  3756. __m256 acc = _mm256_setzero_ps();
  3757. __m128i mask = _mm_set1_epi8(0x10);
  3758. float summs = 0.0f;
  3759. // Main loop
  3760. for (int i = 0; i < nb; i++) {
  3761. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3762. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3763. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3764. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3765. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3766. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3767. bxhil = _mm_and_si128(bxhil, mask);
  3768. bxhih = _mm_and_si128(bxhih, mask);
  3769. __m128i bxl = _mm256_castsi256_si128(bx);
  3770. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3771. bxl = _mm_or_si128(bxl, bxhil);
  3772. bxh = _mm_or_si128(bxh, bxhih);
  3773. bx = MM256_SET_M128I(bxh, bxl);
  3774. const __m256 dy = _mm256_set1_ps(y[i].d);
  3775. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3776. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3777. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3778. }
  3779. *s = hsum_float_8(acc) + summs;
  3780. #elif defined(__riscv_v_intrinsic)
  3781. float sumf = 0.0;
  3782. uint32_t qh;
  3783. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3784. // temporary registers for shift operations
  3785. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3786. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3787. for (int i = 0; i < nb; i++) {
  3788. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3789. // load qh
  3790. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3791. // ((qh >> (j + 0)) << 4) & 0x10;
  3792. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3793. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3794. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3795. // ((qh >> (j + 12)) ) & 0x10;
  3796. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3797. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3798. // narrowing
  3799. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3800. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3801. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3802. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3803. // load
  3804. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3805. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3806. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3807. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3808. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3809. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3810. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3811. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3812. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3813. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3814. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3815. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3816. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3817. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3818. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3819. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3820. }
  3821. *s = sumf;
  3822. #else
  3823. // scalar
  3824. float sumf = 0.0;
  3825. for (int i = 0; i < nb; i++) {
  3826. uint32_t qh;
  3827. memcpy(&qh, x[i].qh, sizeof(qh));
  3828. int sumi = 0;
  3829. for (int j = 0; j < qk/2; ++j) {
  3830. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3831. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3832. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3833. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3834. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3835. }
  3836. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3837. }
  3838. *s = sumf;
  3839. #endif
  3840. }
  3841. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3842. const int qk = QK8_0;
  3843. const int nb = n / qk;
  3844. assert(n % qk == 0);
  3845. const block_q8_0 * restrict x = vx;
  3846. const block_q8_0 * restrict y = vy;
  3847. #if defined(__ARM_NEON)
  3848. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3849. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3850. assert(nb % 2 == 0); // TODO: handle odd nb
  3851. for (int i = 0; i < nb; i += 2) {
  3852. const block_q8_0 * restrict x0 = &x[i + 0];
  3853. const block_q8_0 * restrict x1 = &x[i + 1];
  3854. const block_q8_0 * restrict y0 = &y[i + 0];
  3855. const block_q8_0 * restrict y1 = &y[i + 1];
  3856. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3857. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3858. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3859. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3860. // load y
  3861. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3862. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3863. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3864. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3865. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3866. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3867. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3868. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3869. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3870. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3871. }
  3872. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3873. #elif defined(__AVX2__) || defined(__AVX__)
  3874. // Initialize accumulator with zeros
  3875. __m256 acc = _mm256_setzero_ps();
  3876. // Main loop
  3877. for (int i = 0; i < nb; ++i) {
  3878. // Compute combined scale for the block
  3879. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3880. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3881. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3882. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3883. // Multiply q with scale and accumulate
  3884. #if defined(__AVX2__)
  3885. acc = _mm256_fmadd_ps( d, q, acc );
  3886. #else
  3887. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3888. #endif
  3889. }
  3890. *s = hsum_float_8(acc);
  3891. #elif defined(__riscv_v_intrinsic)
  3892. float sumf = 0.0;
  3893. size_t vl = __riscv_vsetvl_e8m1(qk);
  3894. for (int i = 0; i < nb; i++) {
  3895. // load elements
  3896. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3897. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3898. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3899. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3900. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3901. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3902. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3903. }
  3904. *s = sumf;
  3905. #else
  3906. // scalar
  3907. float sumf = 0.0;
  3908. for (int i = 0; i < nb; i++) {
  3909. int sumi = 0;
  3910. for (int j = 0; j < qk; j++) {
  3911. sumi += x[i].qs[j]*y[i].qs[j];
  3912. }
  3913. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3914. }
  3915. *s = sumf;
  3916. #endif
  3917. }
  3918. #if QK_K == 256
  3919. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3920. const block_q2_K * restrict x = vx;
  3921. const block_q8_K * restrict y = vy;
  3922. const int nb = n / QK_K;
  3923. #ifdef __ARM_NEON
  3924. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3925. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3926. const int32x4_t vzero = vdupq_n_s32(0);
  3927. ggml_int8x16x2_t q2bytes;
  3928. uint8_t aux[16];
  3929. float sum = 0;
  3930. for (int i = 0; i < nb; ++i) {
  3931. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3932. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3933. const uint8_t * restrict q2 = x[i].qs;
  3934. const int8_t * restrict q8 = y[i].qs;
  3935. const uint8_t * restrict sc = x[i].scales;
  3936. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3937. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3938. vst1q_u8(aux, scales);
  3939. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3940. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3941. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3942. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3943. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3944. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3945. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3946. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3947. int isum = 0;
  3948. int is = 0;
  3949. // We use this macro instead of a function call because for some reason
  3950. // the code runs 2-3% slower, even if the function is declared inline
  3951. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3952. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3953. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3954. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3955. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3956. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3957. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3958. MULTIPLY_ACCUM_WITH_SCALE((index));
  3959. for (int j = 0; j < QK_K/128; ++j) {
  3960. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3961. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3962. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3963. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3964. MULTIPLY_ACCUM_WITH_SCALE(0);
  3965. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3966. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3967. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3968. is += 8;
  3969. }
  3970. sum += d * isum;
  3971. }
  3972. *s = sum;
  3973. #elif defined __AVX2__
  3974. const __m256i m3 = _mm256_set1_epi8(3);
  3975. const __m128i m4 = _mm_set1_epi8(0xF);
  3976. __m256 acc = _mm256_setzero_ps();
  3977. for (int i = 0; i < nb; ++i) {
  3978. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3979. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3980. const uint8_t * restrict q2 = x[i].qs;
  3981. const int8_t * restrict q8 = y[i].qs;
  3982. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3983. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3984. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3985. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3986. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3987. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3988. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3989. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3990. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3991. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3992. __m256i sumi = _mm256_setzero_si256();
  3993. for (int j = 0; j < QK_K/128; ++j) {
  3994. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  3995. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3996. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3997. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3998. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3999. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4000. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4001. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4002. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4003. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4004. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4005. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4006. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4007. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4008. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4009. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4010. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4011. p0 = _mm256_add_epi32(p0, p1);
  4012. p2 = _mm256_add_epi32(p2, p3);
  4013. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4014. }
  4015. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4016. }
  4017. *s = hsum_float_8(acc);
  4018. #elif defined __AVX__
  4019. const __m128i m3 = _mm_set1_epi8(0x3);
  4020. const __m128i m4 = _mm_set1_epi8(0xF);
  4021. const __m128i m2 = _mm_set1_epi8(0x2);
  4022. __m256 acc = _mm256_setzero_ps();
  4023. for (int i = 0; i < nb; ++i) {
  4024. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4025. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4026. const uint8_t * restrict q2 = x[i].qs;
  4027. const int8_t * restrict q8 = y[i].qs;
  4028. // load mins and scales from block_q2_K.scales[QK_K/16]
  4029. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4030. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4031. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4032. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4033. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4034. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4035. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4036. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4037. // sumf += -dmin * summs in 32bits*8
  4038. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4039. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4040. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4041. const __m128i scales[2] = { scales_0, scales_1 };
  4042. __m128i sumi_0 = _mm_setzero_si128();
  4043. __m128i sumi_1 = _mm_setzero_si128();
  4044. for (int j = 0; j < QK_K/128; ++j) {
  4045. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4046. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4047. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4048. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4049. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4050. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4051. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4052. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4053. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4054. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4055. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4056. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4057. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4058. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4059. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4060. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4061. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4062. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4063. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4064. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4065. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4066. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4067. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4068. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4069. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4070. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4071. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4072. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4073. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4074. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4075. __m128i shuffle = _mm_set1_epi16(0x0100);
  4076. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4077. shuffle = _mm_add_epi16(shuffle, m2);
  4078. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4079. shuffle = _mm_add_epi16(shuffle, m2);
  4080. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4081. shuffle = _mm_add_epi16(shuffle, m2);
  4082. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4083. shuffle = _mm_add_epi16(shuffle, m2);
  4084. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4085. shuffle = _mm_add_epi16(shuffle, m2);
  4086. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4087. shuffle = _mm_add_epi16(shuffle, m2);
  4088. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4089. shuffle = _mm_add_epi16(shuffle, m2);
  4090. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4091. p0 = _mm_add_epi32(p0, p1);
  4092. p2 = _mm_add_epi32(p2, p3);
  4093. p4 = _mm_add_epi32(p4, p5);
  4094. p6 = _mm_add_epi32(p6, p7);
  4095. // isum in 32bits*4*2
  4096. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4097. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4098. }
  4099. // sumf += dall * isum - dmin * summs in 32bits
  4100. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4101. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4102. }
  4103. *s = hsum_float_8(acc);
  4104. #elif defined __riscv_v_intrinsic
  4105. float sumf = 0;
  4106. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4107. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4108. for (int i = 0; i < nb; ++i) {
  4109. const uint8_t * q2 = x[i].qs;
  4110. const int8_t * q8 = y[i].qs;
  4111. const uint8_t * sc = x[i].scales;
  4112. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4113. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4114. size_t vl = 16;
  4115. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4116. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4117. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4118. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4119. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4120. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4121. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4122. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4123. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4124. vl = 32;
  4125. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4126. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4127. uint8_t is=0;
  4128. int isum=0;
  4129. for (int j = 0; j < QK_K/128; ++j) {
  4130. // load Q2
  4131. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4132. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4133. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4134. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4135. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4136. // duplicate scale elements for product
  4137. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4138. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4139. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4140. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4141. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4142. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4143. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4144. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4145. // load Q8
  4146. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4147. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4148. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4149. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4150. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4151. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4152. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4153. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4154. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4155. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4156. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4157. q2+=32; q8+=128; is=8;
  4158. }
  4159. sumf += dall * isum;
  4160. }
  4161. *s = sumf;
  4162. #else
  4163. float sumf = 0;
  4164. for (int i = 0; i < nb; ++i) {
  4165. const uint8_t * q2 = x[i].qs;
  4166. const int8_t * q8 = y[i].qs;
  4167. const uint8_t * sc = x[i].scales;
  4168. int summs = 0;
  4169. for (int j = 0; j < 16; ++j) {
  4170. summs += y[i].bsums[j] * (sc[j] >> 4);
  4171. }
  4172. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4173. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4174. int isum = 0;
  4175. int is = 0;
  4176. int d;
  4177. for (int k = 0; k < QK_K/128; ++k) {
  4178. int shift = 0;
  4179. for (int j = 0; j < 4; ++j) {
  4180. d = sc[is++] & 0xF;
  4181. int isuml = 0;
  4182. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4183. isum += d * isuml;
  4184. d = sc[is++] & 0xF;
  4185. isuml = 0;
  4186. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4187. isum += d * isuml;
  4188. shift += 2;
  4189. q8 += 32;
  4190. }
  4191. q2 += 32;
  4192. }
  4193. sumf += dall * isum - dmin * summs;
  4194. }
  4195. *s = sumf;
  4196. #endif
  4197. }
  4198. #else
  4199. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4200. const block_q2_K * restrict x = vx;
  4201. const block_q8_K * restrict y = vy;
  4202. const int nb = n / QK_K;
  4203. #ifdef __ARM_NEON
  4204. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4205. const int32x4_t vzero = vdupq_n_s32(0);
  4206. ggml_int8x16x4_t q2bytes;
  4207. uint32_t aux32[2];
  4208. const uint8_t * scales = (const uint8_t *)aux32;
  4209. float sum = 0;
  4210. for (int i = 0; i < nb; ++i) {
  4211. const float d = y[i].d * (float)x[i].d;
  4212. const float dmin = -y[i].d * (float)x[i].dmin;
  4213. const uint8_t * restrict q2 = x[i].qs;
  4214. const int8_t * restrict q8 = y[i].qs;
  4215. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4216. aux32[0] = sc[0] & 0x0f0f0f0f;
  4217. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4218. 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]);
  4219. int isum1 = 0, isum2 = 0;
  4220. const uint8x16_t q2bits = vld1q_u8(q2);
  4221. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4222. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4223. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4224. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4225. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4226. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4227. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4228. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4229. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4230. sum += d * (isum1 + isum2);
  4231. }
  4232. *s = sum;
  4233. #elif defined __AVX2__
  4234. const __m256i m3 = _mm256_set1_epi8(3);
  4235. __m256 acc = _mm256_setzero_ps();
  4236. uint32_t ud, um;
  4237. const uint8_t * restrict db = (const uint8_t *)&ud;
  4238. const uint8_t * restrict mb = (const uint8_t *)&um;
  4239. float summs = 0;
  4240. // TODO: optimize this
  4241. for (int i = 0; i < nb; ++i) {
  4242. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4243. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4244. const uint8_t * restrict q2 = x[i].qs;
  4245. const int8_t * restrict q8 = y[i].qs;
  4246. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4247. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4248. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4249. 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];
  4250. summs += dmin * smin;
  4251. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4252. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4253. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4254. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4255. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4256. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4257. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4258. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4259. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4260. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4261. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4262. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4263. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4264. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4265. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4266. }
  4267. *s = hsum_float_8(acc) + summs;
  4268. #elif defined __AVX__
  4269. const __m128i m3 = _mm_set1_epi8(3);
  4270. __m256 acc = _mm256_setzero_ps();
  4271. uint32_t ud, um;
  4272. const uint8_t * restrict db = (const uint8_t *)&ud;
  4273. const uint8_t * restrict mb = (const uint8_t *)&um;
  4274. float summs = 0;
  4275. // TODO: optimize this
  4276. for (int i = 0; i < nb; ++i) {
  4277. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4278. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4279. const uint8_t * restrict q2 = x[i].qs;
  4280. const int8_t * restrict q8 = y[i].qs;
  4281. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4282. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4283. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4284. 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];
  4285. summs += dmin * smin;
  4286. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4287. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4288. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4289. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4290. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4291. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4292. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4293. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4294. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4295. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4296. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4297. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4298. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4299. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4300. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4301. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4302. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4303. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4304. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4305. }
  4306. *s = hsum_float_8(acc) + summs;
  4307. #elif defined __riscv_v_intrinsic
  4308. uint32_t aux32[2];
  4309. const uint8_t * scales = (const uint8_t *)aux32;
  4310. float sumf = 0;
  4311. for (int i = 0; i < nb; ++i) {
  4312. const float d = y[i].d * (float)x[i].d;
  4313. const float dmin = -y[i].d * (float)x[i].dmin;
  4314. const uint8_t * restrict q2 = x[i].qs;
  4315. const int8_t * restrict q8 = y[i].qs;
  4316. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4317. aux32[0] = sc[0] & 0x0f0f0f0f;
  4318. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4319. 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]);
  4320. int isum1 = 0;
  4321. int isum2 = 0;
  4322. size_t vl = 16;
  4323. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4324. // load Q2
  4325. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4326. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4327. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4328. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4329. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4330. // load Q8, and take product with Q2
  4331. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4332. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4333. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4334. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4335. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4336. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4337. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4338. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4339. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4340. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4341. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4342. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4343. sumf += d * (isum1 + isum2);
  4344. }
  4345. *s = sumf;
  4346. #else
  4347. float sumf = 0;
  4348. int isum[4];
  4349. for (int i = 0; i < nb; ++i) {
  4350. const uint8_t * q2 = x[i].qs;
  4351. const int8_t * q8 = y[i].qs;
  4352. const uint8_t * sc = x[i].scales;
  4353. int summs = 0;
  4354. for (int j = 0; j < QK_K/16; ++j) {
  4355. summs += y[i].bsums[j] * (sc[j] >> 4);
  4356. }
  4357. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4358. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4359. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  4360. for (int l = 0; l < 16; ++l) {
  4361. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4362. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4363. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4364. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4365. }
  4366. for (int l = 0; l < 4; ++l) {
  4367. isum[l] *= (sc[l] & 0xF);
  4368. }
  4369. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4370. }
  4371. *s = sumf;
  4372. #endif
  4373. }
  4374. #endif
  4375. #if QK_K == 256
  4376. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4377. assert(n % QK_K == 0);
  4378. const uint32_t kmask1 = 0x03030303;
  4379. const uint32_t kmask2 = 0x0f0f0f0f;
  4380. const block_q3_K * restrict x = vx;
  4381. const block_q8_K * restrict y = vy;
  4382. const int nb = n / QK_K;
  4383. #ifdef __ARM_NEON
  4384. uint32_t aux[3];
  4385. uint32_t utmp[4];
  4386. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4387. const int32x4_t vzero = vdupq_n_s32(0);
  4388. const uint8x16_t m0 = vdupq_n_u8(1);
  4389. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4390. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4391. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4392. const int8_t m32 = 32;
  4393. ggml_int8x16x4_t q3bytes;
  4394. float sum = 0;
  4395. for (int i = 0; i < nb; ++i) {
  4396. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4397. const uint8_t * restrict q3 = x[i].qs;
  4398. const uint8_t * restrict qh = x[i].hmask;
  4399. const int8_t * restrict q8 = y[i].qs;
  4400. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4401. ggml_uint8x16x4_t q3h;
  4402. int32_t isum = 0;
  4403. // Set up scales
  4404. memcpy(aux, x[i].scales, 12);
  4405. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4406. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4407. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4408. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4409. int8_t * scale = (int8_t *)utmp;
  4410. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4411. for (int j = 0; j < QK_K/128; ++j) {
  4412. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4413. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4414. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4415. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4416. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4417. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4418. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4419. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4420. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4421. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4422. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4423. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4424. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4425. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4426. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4427. scale += 4;
  4428. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4429. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4430. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4431. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4432. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4433. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4434. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4435. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4436. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4437. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4438. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4439. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4440. scale += 4;
  4441. if (j == 0) {
  4442. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4443. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4444. }
  4445. }
  4446. sum += d * isum;
  4447. }
  4448. *s = sum;
  4449. #elif defined __AVX2__
  4450. const __m256i m3 = _mm256_set1_epi8(3);
  4451. const __m256i mone = _mm256_set1_epi8(1);
  4452. const __m128i m32 = _mm_set1_epi8(32);
  4453. __m256 acc = _mm256_setzero_ps();
  4454. uint32_t aux[3];
  4455. for (int i = 0; i < nb; ++i) {
  4456. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4457. const uint8_t * restrict q3 = x[i].qs;
  4458. const int8_t * restrict q8 = y[i].qs;
  4459. // Set up scales
  4460. memcpy(aux, x[i].scales, 12);
  4461. __m128i scales128 = _mm_set_epi32(
  4462. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4463. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4464. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4465. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4466. scales128 = _mm_sub_epi8(scales128, m32);
  4467. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4468. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4469. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4470. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4471. // high bit
  4472. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4473. // integer accumulator
  4474. __m256i sumi = _mm256_setzero_si256();
  4475. int bit = 0;
  4476. int is = 0;
  4477. for (int j = 0; j < QK_K/128; ++j) {
  4478. // load low 2 bits
  4479. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4480. // prepare low and high bits
  4481. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4482. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4483. ++bit;
  4484. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4485. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4486. ++bit;
  4487. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4488. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4489. ++bit;
  4490. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4491. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4492. ++bit;
  4493. // load Q8 quants
  4494. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4495. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4496. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4497. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4498. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4499. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4500. // and 2 if the high bit was set)
  4501. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4502. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4503. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4504. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4505. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4506. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4507. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4508. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4509. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4510. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4511. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4512. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4513. // multiply with scales
  4514. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4515. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4516. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4517. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4518. // accumulate
  4519. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4520. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4521. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4522. }
  4523. // multiply with block scale and accumulate
  4524. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4525. }
  4526. *s = hsum_float_8(acc);
  4527. #elif defined __AVX__
  4528. const __m128i m3 = _mm_set1_epi8(3);
  4529. const __m128i mone = _mm_set1_epi8(1);
  4530. const __m128i m32 = _mm_set1_epi8(32);
  4531. const __m128i m2 = _mm_set1_epi8(2);
  4532. __m256 acc = _mm256_setzero_ps();
  4533. const uint32_t *aux;
  4534. for (int i = 0; i < nb; ++i) {
  4535. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4536. const uint8_t * restrict q3 = x[i].qs;
  4537. const int8_t * restrict q8 = y[i].qs;
  4538. // Set up scales
  4539. aux = (const uint32_t *)x[i].scales;
  4540. __m128i scales128 = _mm_set_epi32(
  4541. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4542. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4543. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4544. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4545. scales128 = _mm_sub_epi8(scales128, m32);
  4546. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4547. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4548. const __m128i scales[2] = { scales_0, scales_1 };
  4549. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4550. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4551. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4552. // integer accumulator
  4553. __m128i sumi_0 = _mm_setzero_si128();
  4554. __m128i sumi_1 = _mm_setzero_si128();
  4555. for (int j = 0; j < QK_K/128; ++j) {
  4556. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4557. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4558. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4559. // prepare low and high bits
  4560. const int bit = j << 2;
  4561. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4562. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4563. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4564. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4565. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4566. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4567. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4568. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4569. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4570. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4571. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4572. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4573. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4574. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4575. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4576. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4577. // load Q8 quants from block_q8_K.qs[QK_K]
  4578. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4579. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4580. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4581. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4582. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4583. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4584. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4585. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4586. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4587. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4588. // and 2 if the high bit was set)
  4589. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4590. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4591. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4592. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4593. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4594. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4595. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4596. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4597. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4598. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4599. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4600. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4601. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4602. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4603. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4604. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4605. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4606. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4607. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4608. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4609. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4610. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4611. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4612. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4613. // multiply with scales
  4614. __m128i shuffle = _mm_set1_epi16(0x0100);
  4615. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4616. shuffle = _mm_add_epi16(shuffle, m2);
  4617. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4618. shuffle = _mm_add_epi16(shuffle, m2);
  4619. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4620. shuffle = _mm_add_epi16(shuffle, m2);
  4621. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4622. shuffle = _mm_add_epi16(shuffle, m2);
  4623. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4624. shuffle = _mm_add_epi16(shuffle, m2);
  4625. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4626. shuffle = _mm_add_epi16(shuffle, m2);
  4627. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4628. shuffle = _mm_add_epi16(shuffle, m2);
  4629. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4630. // accumulate
  4631. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4632. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4633. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4634. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4635. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4636. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4637. }
  4638. // multiply with block scale and accumulate
  4639. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4640. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4641. }
  4642. *s = hsum_float_8(acc);
  4643. #elif defined __riscv_v_intrinsic
  4644. uint32_t aux[3];
  4645. uint32_t utmp[4];
  4646. float sumf = 0;
  4647. for (int i = 0; i < nb; ++i) {
  4648. const uint8_t * restrict q3 = x[i].qs;
  4649. const uint8_t * restrict qh = x[i].hmask;
  4650. const int8_t * restrict q8 = y[i].qs;
  4651. memcpy(aux, x[i].scales, 12);
  4652. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4653. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4654. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4655. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4656. int8_t * scale = (int8_t *)utmp;
  4657. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4658. size_t vl = 32;
  4659. uint8_t m = 1;
  4660. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4661. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4662. int sum_t = 0;
  4663. for (int j = 0; j < QK_K; j += 128) {
  4664. vl = 32;
  4665. // load Q3
  4666. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4667. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4668. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4669. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4670. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4671. // compute mask for subtraction
  4672. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4673. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4674. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4675. m <<= 1;
  4676. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4677. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4678. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4679. m <<= 1;
  4680. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4681. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4682. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4683. m <<= 1;
  4684. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4685. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4686. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4687. m <<= 1;
  4688. // load Q8 and take product with Q3
  4689. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4690. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4691. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4692. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4693. vl = 16;
  4694. // retrieve lane to multiply with scale
  4695. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4696. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4697. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4698. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4699. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4700. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4701. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4702. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4703. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4704. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4705. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4706. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4707. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4708. q3 += 32; q8 += 128; scale += 8;
  4709. }
  4710. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4711. sumf += d*sum_t;
  4712. }
  4713. *s = sumf;
  4714. #else
  4715. // scalar version
  4716. // This function is written like this so the compiler can manage to vectorize most of it
  4717. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4718. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4719. // The ideal situation would be if we could just write the code once, and the compiler would
  4720. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4721. // write vectorized versions for AVX, ARM_NEON, etc.
  4722. int8_t aux8[QK_K];
  4723. int16_t aux16[8];
  4724. float sums [8];
  4725. int32_t aux32[8];
  4726. memset(sums, 0, 8*sizeof(float));
  4727. uint32_t auxs[4];
  4728. const int8_t * scales = (const int8_t*)auxs;
  4729. float sumf = 0;
  4730. for (int i = 0; i < nb; ++i) {
  4731. const uint8_t * restrict q3 = x[i].qs;
  4732. const uint8_t * restrict hm = x[i].hmask;
  4733. const int8_t * restrict q8 = y[i].qs;
  4734. memset(aux32, 0, 8*sizeof(int32_t));
  4735. int8_t * restrict a = aux8;
  4736. uint8_t m = 1;
  4737. for (int j = 0; j < QK_K; j += 128) {
  4738. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4739. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4740. a += 32; m <<= 1;
  4741. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4742. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4743. a += 32; m <<= 1;
  4744. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4745. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4746. a += 32; m <<= 1;
  4747. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4748. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4749. a += 32; m <<= 1;
  4750. q3 += 32;
  4751. }
  4752. a = aux8;
  4753. memcpy(auxs, x[i].scales, 12);
  4754. uint32_t tmp = auxs[2];
  4755. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4756. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4757. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4758. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4759. for (int j = 0; j < QK_K/16; ++j) {
  4760. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4761. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4762. q8 += 8; a += 8;
  4763. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4764. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4765. q8 += 8; a += 8;
  4766. }
  4767. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4768. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4769. }
  4770. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4771. *s = sumf;
  4772. #endif
  4773. }
  4774. #else
  4775. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4776. assert(n % QK_K == 0);
  4777. const block_q3_K * restrict x = vx;
  4778. const block_q8_K * restrict y = vy;
  4779. const int nb = n / QK_K;
  4780. #ifdef __ARM_NEON
  4781. const int32x4_t vzero = vdupq_n_s32(0);
  4782. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4783. const uint8x16_t mh = vdupq_n_u8(4);
  4784. ggml_int8x16x4_t q3bytes;
  4785. uint16_t aux16[2];
  4786. int8_t * scales = (int8_t *)aux16;
  4787. float sum = 0;
  4788. for (int i = 0; i < nb; ++i) {
  4789. ggml_uint8x16x4_t q3h;
  4790. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4791. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4792. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4793. const uint16_t a = *(const uint16_t *)x[i].scales;
  4794. aux16[0] = a & 0x0f0f;
  4795. aux16[1] = (a >> 4) & 0x0f0f;
  4796. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4797. 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]);
  4798. const float d = y[i].d * (float)x[i].d;
  4799. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4800. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4801. q3h.val[1] = vandq_u8(mh, htmp);
  4802. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4803. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4804. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4805. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4806. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4807. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4808. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4809. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4810. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4811. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4812. sum += d * isum;
  4813. }
  4814. *s = sum;
  4815. #elif defined __AVX2__
  4816. const __m256i m3 = _mm256_set1_epi8(3);
  4817. const __m256i m1 = _mm256_set1_epi8(1);
  4818. __m256 acc = _mm256_setzero_ps();
  4819. uint64_t aux64;
  4820. uint16_t aux16[2];
  4821. const int8_t * aux8 = (const int8_t *)aux16;
  4822. for (int i = 0; i < nb; ++i) {
  4823. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4824. const uint8_t * restrict q3 = x[i].qs;
  4825. const int8_t * restrict q8 = y[i].qs;
  4826. const uint16_t a = *(const uint16_t *)x[i].scales;
  4827. aux16[0] = a & 0x0f0f;
  4828. aux16[1] = (a >> 4) & 0x0f0f;
  4829. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4830. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4831. memcpy(&aux64, x[i].hmask, 8);
  4832. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4833. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4834. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4835. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4836. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4837. // load low 2 bits
  4838. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4839. // prepare low and high bits
  4840. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4841. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4842. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4843. // load Q8 quants
  4844. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4845. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4846. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4847. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4848. // and 2 if the high bit was set)
  4849. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4850. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4851. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4852. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4853. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4854. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4855. // multiply with scales
  4856. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4857. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4858. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4859. // multiply with block scale and accumulate
  4860. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4861. }
  4862. *s = hsum_float_8(acc);
  4863. #elif defined __AVX__
  4864. const __m128i m3 = _mm_set1_epi8(3);
  4865. const __m128i m1 = _mm_set1_epi8(1);
  4866. __m256 acc = _mm256_setzero_ps();
  4867. uint64_t aux64;
  4868. uint16_t aux16[2];
  4869. const int8_t * aux8 = (const int8_t *)aux16;
  4870. for (int i = 0; i < nb; ++i) {
  4871. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4872. const uint8_t * restrict q3 = x[i].qs;
  4873. const int8_t * restrict q8 = y[i].qs;
  4874. const uint16_t a = *(const uint16_t *)x[i].scales;
  4875. aux16[0] = a & 0x0f0f;
  4876. aux16[1] = (a >> 4) & 0x0f0f;
  4877. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4878. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4879. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4880. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4881. memcpy(&aux64, x[i].hmask, 8);
  4882. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4883. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4884. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4885. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4886. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4887. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4888. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4889. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4890. // load low 2 bits
  4891. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4892. // prepare low and high bits
  4893. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4894. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4895. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4896. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4897. // load Q8 quants
  4898. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4899. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4900. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4901. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4902. // and 2 if the high bit was set)
  4903. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4904. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4905. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4906. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4907. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4908. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4909. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4910. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4911. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4912. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4913. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4914. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4915. // multiply with scales
  4916. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4917. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4918. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4919. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4920. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4921. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4922. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4923. // multiply with block scale and accumulate
  4924. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4925. }
  4926. *s = hsum_float_8(acc);
  4927. #elif defined __riscv_v_intrinsic
  4928. uint16_t aux16[2];
  4929. int8_t * scales = (int8_t *)aux16;
  4930. float sumf = 0;
  4931. for (int i = 0; i < nb; ++i) {
  4932. const uint8_t * restrict q3 = x[i].qs;
  4933. const int8_t * restrict q8 = y[i].qs;
  4934. const uint16_t a = *(const uint16_t *)x[i].scales;
  4935. aux16[0] = a & 0x0f0f;
  4936. aux16[1] = (a >> 4) & 0x0f0f;
  4937. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4938. 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]);
  4939. const float d = y[i].d * (float)x[i].d;
  4940. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4941. // load qh
  4942. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4943. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4944. size_t vl = 16;
  4945. // extend and combine both qh_x1 and qh_x2
  4946. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4947. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4948. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4949. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4950. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4951. // load Q3
  4952. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4953. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4954. 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);
  4955. 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);
  4956. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4957. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4958. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4959. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4960. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4961. // load Q8 and take product with Q3
  4962. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4963. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4964. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4965. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4966. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4967. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4968. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4969. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4970. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4971. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4972. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4973. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4974. sumf += d * isum;
  4975. }
  4976. *s = sumf;
  4977. #else
  4978. int8_t aux8[QK_K];
  4979. int16_t aux16[8];
  4980. float sums [8];
  4981. int32_t aux32[8];
  4982. int32_t scales[4];
  4983. memset(sums, 0, 8*sizeof(float));
  4984. float sumf = 0;
  4985. for (int i = 0; i < nb; ++i) {
  4986. const uint8_t * restrict q3 = x[i].qs;
  4987. const uint8_t * restrict hm = x[i].hmask;
  4988. const int8_t * restrict q8 = y[i].qs;
  4989. int8_t * restrict a = aux8;
  4990. for (int l = 0; l < 8; ++l) {
  4991. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  4992. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  4993. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  4994. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  4995. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  4996. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  4997. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  4998. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  4999. }
  5000. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5001. scales[1] = (x[i].scales[0] >> 4) - 8;
  5002. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5003. scales[3] = (x[i].scales[1] >> 4) - 8;
  5004. memset(aux32, 0, 8*sizeof(int32_t));
  5005. for (int j = 0; j < QK_K/16; ++j) {
  5006. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5007. q8 += 8; a += 8;
  5008. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5009. q8 += 8; a += 8;
  5010. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5011. }
  5012. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5013. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5014. }
  5015. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5016. *s = sumf;
  5017. #endif
  5018. }
  5019. #endif
  5020. #if QK_K == 256
  5021. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5022. assert(n % QK_K == 0);
  5023. const block_q4_K * restrict x = vx;
  5024. const block_q8_K * restrict y = vy;
  5025. const int nb = n / QK_K;
  5026. static const uint32_t kmask1 = 0x3f3f3f3f;
  5027. static const uint32_t kmask2 = 0x0f0f0f0f;
  5028. static const uint32_t kmask3 = 0x03030303;
  5029. uint32_t utmp[4];
  5030. #ifdef __ARM_NEON
  5031. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5032. const int32x4_t mzero = vdupq_n_s32(0);
  5033. ggml_int8x16x2_t q4bytes;
  5034. ggml_int8x16x2_t q8bytes;
  5035. float sumf = 0;
  5036. for (int i = 0; i < nb; ++i) {
  5037. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5038. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5039. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5040. memcpy(utmp, x[i].scales, 12);
  5041. uint32x2_t mins8 = { 0 };
  5042. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5043. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5044. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5045. utmp[0] &= kmask1;
  5046. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5047. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5048. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5049. sumf -= dmin * vaddvq_s32(prod);
  5050. const uint8_t * scales = (const uint8_t *)utmp;
  5051. const uint8_t * restrict q4 = x[i].qs;
  5052. const int8_t * restrict q8 = y[i].qs;
  5053. int32_t sumi1 = 0;
  5054. int32_t sumi2 = 0;
  5055. for (int j = 0; j < QK_K/64; ++j) {
  5056. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5057. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5058. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5059. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5060. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5061. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5062. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5063. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5064. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5065. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5066. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5067. }
  5068. sumf += d * (sumi1 + sumi2);
  5069. }
  5070. *s = sumf;
  5071. #elif defined __AVX2__
  5072. const __m256i m4 = _mm256_set1_epi8(0xF);
  5073. __m256 acc = _mm256_setzero_ps();
  5074. __m128 acc_m = _mm_setzero_ps();
  5075. for (int i = 0; i < nb; ++i) {
  5076. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5077. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5078. memcpy(utmp, x[i].scales, 12);
  5079. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5080. const uint32_t uaux = utmp[1] & kmask1;
  5081. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5082. utmp[2] = uaux;
  5083. utmp[0] &= kmask1;
  5084. const uint8_t * restrict q4 = x[i].qs;
  5085. const int8_t * restrict q8 = y[i].qs;
  5086. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5087. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5088. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5089. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5090. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5091. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5092. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5093. __m256i sumi = _mm256_setzero_si256();
  5094. for (int j = 0; j < QK_K/64; ++j) {
  5095. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5096. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5097. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5098. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5099. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5100. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5101. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5102. p16l = _mm256_madd_epi16(scale_l, p16l);
  5103. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5104. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5105. p16h = _mm256_madd_epi16(scale_h, p16h);
  5106. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5107. sumi = _mm256_add_epi32(sumi, sumj);
  5108. }
  5109. __m256 vd = _mm256_set1_ps(d);
  5110. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5111. }
  5112. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5113. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5114. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5115. #elif defined __AVX__
  5116. const __m128i m4 = _mm_set1_epi8(0xF);
  5117. const __m128i m2 = _mm_set1_epi8(0x2);
  5118. __m256 acc = _mm256_setzero_ps();
  5119. __m128 acc_m = _mm_setzero_ps();
  5120. for (int i = 0; i < nb; ++i) {
  5121. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5122. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5123. const uint8_t * restrict q4 = x[i].qs;
  5124. const int8_t * restrict q8 = y[i].qs;
  5125. memcpy(utmp, x[i].scales, 12);
  5126. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5127. const uint32_t uaux = utmp[1] & kmask1;
  5128. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5129. utmp[2] = uaux;
  5130. utmp[0] &= kmask1;
  5131. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5132. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5133. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5134. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5135. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5136. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5137. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5138. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5139. __m128i sumi_0 = _mm_setzero_si128();
  5140. __m128i sumi_1 = _mm_setzero_si128();
  5141. __m128i shuffle = _mm_set1_epi16(0x0100);
  5142. for (int j = 0; j < QK_K/64; ++j) {
  5143. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5144. shuffle = _mm_add_epi16(shuffle, m2);
  5145. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5146. shuffle = _mm_add_epi16(shuffle, m2);
  5147. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5148. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5149. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5150. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5151. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5152. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5153. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5154. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5155. p16l = _mm_madd_epi16(scale_l, p16l);
  5156. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5157. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5158. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5159. p16l = _mm_madd_epi16(scale_l, p16l);
  5160. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5161. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5162. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5163. p16h = _mm_madd_epi16(scale_h, p16h);
  5164. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5165. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5166. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5167. p16h = _mm_madd_epi16(scale_h, p16h);
  5168. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5169. }
  5170. __m256 vd = _mm256_set1_ps(d);
  5171. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5172. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5173. }
  5174. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5175. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5176. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5177. #elif defined __riscv_v_intrinsic
  5178. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5179. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5180. float sumf = 0;
  5181. for (int i = 0; i < nb; ++i) {
  5182. size_t vl = 8;
  5183. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5184. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5185. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5186. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5187. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5188. memcpy(utmp, x[i].scales, 12);
  5189. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5190. const uint32_t uaux = utmp[1] & kmask1;
  5191. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5192. utmp[2] = uaux;
  5193. utmp[0] &= kmask1;
  5194. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5195. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5196. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5197. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5198. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5199. const uint8_t * restrict q4 = x[i].qs;
  5200. const int8_t * restrict q8 = y[i].qs;
  5201. vl = 32;
  5202. int32_t sum_1 = 0;
  5203. int32_t sum_2 = 0;
  5204. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5205. for (int j = 0; j < QK_K/64; ++j) {
  5206. // load Q4
  5207. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5208. // load Q8 and multiply it with lower Q4 nibble
  5209. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5210. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5211. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5212. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5213. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5214. // load Q8 and multiply it with upper Q4 nibble
  5215. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5216. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5217. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5218. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5219. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5220. q4 += 32; q8 += 64;
  5221. }
  5222. sumf += d*(sum_1 + sum_2);
  5223. }
  5224. *s = sumf;
  5225. #else
  5226. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5227. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5228. int8_t aux8[QK_K];
  5229. int16_t aux16[8];
  5230. float sums [8];
  5231. int32_t aux32[8];
  5232. memset(sums, 0, 8*sizeof(float));
  5233. float sumf = 0;
  5234. for (int i = 0; i < nb; ++i) {
  5235. const uint8_t * restrict q4 = x[i].qs;
  5236. const int8_t * restrict q8 = y[i].qs;
  5237. memset(aux32, 0, 8*sizeof(int32_t));
  5238. int8_t * restrict a = aux8;
  5239. for (int j = 0; j < QK_K/64; ++j) {
  5240. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5241. a += 32;
  5242. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5243. a += 32; q4 += 32;
  5244. }
  5245. memcpy(utmp, x[i].scales, 12);
  5246. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5247. const uint32_t uaux = utmp[1] & kmask1;
  5248. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5249. utmp[2] = uaux;
  5250. utmp[0] &= kmask1;
  5251. int sumi = 0;
  5252. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5253. a = aux8;
  5254. int is = 0;
  5255. for (int j = 0; j < QK_K/32; ++j) {
  5256. int32_t scale = scales[is++];
  5257. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5258. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5259. q8 += 8; a += 8;
  5260. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5261. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5262. q8 += 8; a += 8;
  5263. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5264. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5265. q8 += 8; a += 8;
  5266. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5267. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5268. q8 += 8; a += 8;
  5269. }
  5270. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5271. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5272. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5273. sumf -= dmin * sumi;
  5274. }
  5275. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5276. *s = sumf;
  5277. #endif
  5278. }
  5279. #else
  5280. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5281. assert(n % QK_K == 0);
  5282. const block_q4_K * restrict x = vx;
  5283. const block_q8_K * restrict y = vy;
  5284. const int nb = n / QK_K;
  5285. #ifdef __ARM_NEON
  5286. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5287. const int32x4_t mzero = vdupq_n_s32(0);
  5288. float sumf = 0;
  5289. ggml_int8x16x2_t q4bytes;
  5290. ggml_int8x16x4_t q8bytes;
  5291. float sum_mins = 0.f;
  5292. uint16_t aux16[2];
  5293. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5294. for (int i = 0; i < nb; ++i) {
  5295. const uint8_t * restrict q4 = x[i].qs;
  5296. const int8_t * restrict q8 = y[i].qs;
  5297. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5298. aux16[0] = a[0] & 0x0f0f;
  5299. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5300. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5301. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  5302. const float d = y[i].d * (float)x[i].d[0];
  5303. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5304. q8bytes = ggml_vld1q_s8_x4(q8);
  5305. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5306. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5307. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5308. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5309. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5310. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5311. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5312. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5313. sumf += d * (sumi1 + sumi2);
  5314. }
  5315. *s = sumf - sum_mins;
  5316. #elif defined __AVX2__
  5317. const __m256i m4 = _mm256_set1_epi8(0xF);
  5318. __m256 acc = _mm256_setzero_ps();
  5319. float summs = 0;
  5320. uint16_t aux16[2];
  5321. const uint8_t * scales = (const uint8_t *)aux16;
  5322. for (int i = 0; i < nb; ++i) {
  5323. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5324. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5325. const __m256 vd = _mm256_set1_ps(d);
  5326. const uint16_t * a = (const uint16_t *)x[i].scales;
  5327. aux16[0] = a[0] & 0x0f0f;
  5328. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5329. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5330. const uint8_t * restrict q4 = x[i].qs;
  5331. const int8_t * restrict q8 = y[i].qs;
  5332. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5333. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5334. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5335. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5336. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5337. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5338. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5339. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5340. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5341. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5342. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5343. }
  5344. *s = hsum_float_8(acc) - summs;
  5345. #elif defined __AVX__
  5346. const __m128i m4 = _mm_set1_epi8(0xF);
  5347. __m256 acc = _mm256_setzero_ps();
  5348. float summs = 0;
  5349. uint16_t aux16[2];
  5350. const uint8_t * scales = (const uint8_t *)aux16;
  5351. for (int i = 0; i < nb; ++i) {
  5352. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5353. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5354. const __m256 vd = _mm256_set1_ps(d);
  5355. const uint16_t * a = (const uint16_t *)x[i].scales;
  5356. aux16[0] = a[0] & 0x0f0f;
  5357. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5358. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5359. const uint8_t * restrict q4 = x[i].qs;
  5360. const int8_t * restrict q8 = y[i].qs;
  5361. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5362. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5363. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5364. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5365. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5366. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5367. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5368. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5369. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5370. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5371. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5372. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5373. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5374. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5375. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5376. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5377. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5378. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5379. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5380. }
  5381. *s = hsum_float_8(acc) - summs;
  5382. #elif defined __riscv_v_intrinsic
  5383. uint16_t s16[2];
  5384. const uint8_t * restrict scales = (const uint8_t *)s16;
  5385. float sumf = 0;
  5386. for (int i = 0; i < nb; ++i) {
  5387. const uint8_t * restrict q4 = x[i].qs;
  5388. const int8_t * restrict q8 = y[i].qs;
  5389. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5390. s16[0] = b[0] & 0x0f0f;
  5391. s16[1] = (b[0] >> 4) & 0x0f0f;
  5392. 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]));
  5393. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5394. size_t vl = 32;
  5395. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5396. // load Q4
  5397. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5398. // load Q8 and multiply it with lower Q4 nibble
  5399. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5400. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5401. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5402. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5403. // load Q8 and multiply it with upper Q4 nibble
  5404. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5405. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5406. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5407. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5408. }
  5409. *s = sumf;
  5410. #else
  5411. uint8_t aux8[QK_K];
  5412. int16_t aux16[16];
  5413. float sums [8];
  5414. memset(sums, 0, 8*sizeof(float));
  5415. uint16_t s16[2];
  5416. const uint8_t * restrict scales = (const uint8_t *)s16;
  5417. float sumf = 0;
  5418. for (int i = 0; i < nb; ++i) {
  5419. const uint8_t * restrict q4 = x[i].qs;
  5420. const int8_t * restrict q8 = y[i].qs;
  5421. uint8_t * restrict a = aux8;
  5422. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5423. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5424. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5425. s16[0] = b[0] & 0x0f0f;
  5426. s16[1] = (b[0] >> 4) & 0x0f0f;
  5427. 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]));
  5428. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5429. for (int j = 0; j < QK_K/32; ++j) {
  5430. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5431. q8 += 16; a += 16;
  5432. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5433. q8 += 16; a += 16;
  5434. const float dl = d * scales[j];
  5435. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5436. }
  5437. }
  5438. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5439. *s = sumf;
  5440. #endif
  5441. }
  5442. #endif
  5443. #if QK_K == 256
  5444. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5445. assert(n % QK_K == 0);
  5446. const block_q5_K * restrict x = vx;
  5447. const block_q8_K * restrict y = vy;
  5448. const int nb = n / QK_K;
  5449. static const uint32_t kmask1 = 0x3f3f3f3f;
  5450. static const uint32_t kmask2 = 0x0f0f0f0f;
  5451. static const uint32_t kmask3 = 0x03030303;
  5452. uint32_t utmp[4];
  5453. #ifdef __ARM_NEON
  5454. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5455. const uint8x16_t mone = vdupq_n_u8(1);
  5456. const uint8x16_t mtwo = vdupq_n_u8(2);
  5457. const int32x4_t mzero = vdupq_n_s32(0);
  5458. ggml_int8x16x4_t q5bytes;
  5459. float sumf = 0;
  5460. for (int i = 0; i < nb; ++i) {
  5461. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5462. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5463. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5464. memcpy(utmp, x[i].scales, 12);
  5465. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5466. const uint32_t uaux = utmp[1] & kmask1;
  5467. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5468. utmp[2] = uaux;
  5469. utmp[0] &= kmask1;
  5470. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5471. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5472. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5473. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5474. int32_t sumi_mins = vaddvq_s32(prod);
  5475. const uint8_t * scales = (const uint8_t *)utmp;
  5476. const uint8_t * restrict q5 = x[i].qs;
  5477. const uint8_t * restrict qh = x[i].qh;
  5478. const int8_t * restrict q8 = y[i].qs;
  5479. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5480. ggml_uint8x16x4_t q5h;
  5481. int32_t sumi = 0;
  5482. for (int j = 0; j < QK_K/64; ++j) {
  5483. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5484. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5485. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5486. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5487. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5488. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5489. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5490. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5491. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5492. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5493. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5494. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5495. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5496. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5497. }
  5498. sumf += d * sumi - dmin * sumi_mins;
  5499. }
  5500. *s = sumf;
  5501. #elif defined __AVX2__
  5502. const __m256i m4 = _mm256_set1_epi8(0xF);
  5503. const __m128i mzero = _mm_setzero_si128();
  5504. const __m256i mone = _mm256_set1_epi8(1);
  5505. __m256 acc = _mm256_setzero_ps();
  5506. float summs = 0.f;
  5507. for (int i = 0; i < nb; ++i) {
  5508. const uint8_t * restrict q5 = x[i].qs;
  5509. const int8_t * restrict q8 = y[i].qs;
  5510. #if QK_K == 256
  5511. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5512. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5513. memcpy(utmp, x[i].scales, 12);
  5514. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5515. const uint32_t uaux = utmp[1] & kmask1;
  5516. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5517. utmp[2] = uaux;
  5518. utmp[0] &= kmask1;
  5519. #else
  5520. // TODO
  5521. const float d = 0, dmin = 0;
  5522. #endif
  5523. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5524. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5525. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5526. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5527. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5528. summs += dmin * _mm_extract_epi32(hsum, 0);
  5529. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5530. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5531. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5532. __m256i hmask = mone;
  5533. __m256i sumi = _mm256_setzero_si256();
  5534. int bit = 0;
  5535. for (int j = 0; j < QK_K/64; ++j) {
  5536. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5537. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5538. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5539. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5540. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5541. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5542. hmask = _mm256_slli_epi16(hmask, 1);
  5543. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5544. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5545. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5546. hmask = _mm256_slli_epi16(hmask, 1);
  5547. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5548. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5549. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5550. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5551. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5552. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5553. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5554. }
  5555. __m256 vd = _mm256_set1_ps(d);
  5556. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5557. }
  5558. *s = hsum_float_8(acc) + summs;
  5559. #elif defined __AVX__
  5560. const __m128i m4 = _mm_set1_epi8(0xF);
  5561. const __m128i mzero = _mm_setzero_si128();
  5562. const __m128i mone = _mm_set1_epi8(1);
  5563. const __m128i m2 = _mm_set1_epi8(2);
  5564. __m256 acc = _mm256_setzero_ps();
  5565. float summs = 0.f;
  5566. for (int i = 0; i < nb; ++i) {
  5567. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5568. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5569. const uint8_t * restrict q5 = x[i].qs;
  5570. const int8_t * restrict q8 = y[i].qs;
  5571. memcpy(utmp, x[i].scales, 12);
  5572. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5573. const uint32_t uaux = utmp[1] & kmask1;
  5574. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5575. utmp[2] = uaux;
  5576. utmp[0] &= kmask1;
  5577. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5578. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5579. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5580. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5581. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5582. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5583. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5584. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5585. summs += dmin * _mm_extract_epi32(hsum, 0);
  5586. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5587. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5588. __m128i hmask = mone;
  5589. __m128i sumi_0 = _mm_setzero_si128();
  5590. __m128i sumi_1 = _mm_setzero_si128();
  5591. int bit = 0;
  5592. __m128i shuffle = _mm_set1_epi16(0x0100);
  5593. for (int j = 0; j < QK_K/64; ++j) {
  5594. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5595. shuffle = _mm_add_epi16(shuffle, m2);
  5596. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5597. shuffle = _mm_add_epi16(shuffle, m2);
  5598. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5599. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5600. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5601. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5602. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5603. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5604. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5605. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5606. hmask = _mm_slli_epi16(hmask, 1);
  5607. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5608. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5609. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5610. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5611. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5612. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5613. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5614. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5615. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5616. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5617. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5618. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5619. hmask = _mm_slli_epi16(hmask, 1);
  5620. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5621. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5622. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5623. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5624. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5625. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5626. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5627. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5628. }
  5629. __m256 vd = _mm256_set1_ps(d);
  5630. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5631. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5632. }
  5633. *s = hsum_float_8(acc) + summs;
  5634. #elif defined __riscv_v_intrinsic
  5635. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5636. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5637. float sumf = 0;
  5638. float sums = 0.0;
  5639. size_t vl;
  5640. for (int i = 0; i < nb; ++i) {
  5641. vl = 8;
  5642. const uint8_t * restrict q5 = x[i].qs;
  5643. const uint8_t * restrict hm = x[i].qh;
  5644. const int8_t * restrict q8 = y[i].qs;
  5645. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5646. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5647. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5648. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5649. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5650. memcpy(utmp, x[i].scales, 12);
  5651. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5652. const uint32_t uaux = utmp[1] & kmask1;
  5653. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5654. utmp[2] = uaux;
  5655. utmp[0] &= kmask1;
  5656. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5657. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5658. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5659. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5660. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5661. vl = 32;
  5662. int32_t aux32 = 0;
  5663. int is = 0;
  5664. uint8_t m = 1;
  5665. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5666. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5667. for (int j = 0; j < QK_K/64; ++j) {
  5668. // load Q5 and Q8
  5669. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5670. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5671. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5672. // compute mask for addition
  5673. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5674. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5675. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5676. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5677. m <<= 1;
  5678. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5679. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5680. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5681. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5682. m <<= 1;
  5683. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5684. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5685. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5686. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5687. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5688. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5689. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5690. q5 += 32; q8 += 64;
  5691. }
  5692. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5693. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5694. }
  5695. *s = sumf+sums;
  5696. #else
  5697. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5698. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5699. int8_t aux8[QK_K];
  5700. int16_t aux16[8];
  5701. float sums [8];
  5702. int32_t aux32[8];
  5703. memset(sums, 0, 8*sizeof(float));
  5704. float sumf = 0;
  5705. for (int i = 0; i < nb; ++i) {
  5706. const uint8_t * restrict q4 = x[i].qs;
  5707. const uint8_t * restrict hm = x[i].qh;
  5708. const int8_t * restrict q8 = y[i].qs;
  5709. memset(aux32, 0, 8*sizeof(int32_t));
  5710. int8_t * restrict a = aux8;
  5711. uint8_t m = 1;
  5712. for (int j = 0; j < QK_K/64; ++j) {
  5713. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5714. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5715. a += 32; m <<= 1;
  5716. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5717. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5718. a += 32; m <<= 1;
  5719. q4 += 32;
  5720. }
  5721. memcpy(utmp, x[i].scales, 12);
  5722. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5723. const uint32_t uaux = utmp[1] & kmask1;
  5724. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5725. utmp[2] = uaux;
  5726. utmp[0] &= kmask1;
  5727. int sumi = 0;
  5728. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5729. a = aux8;
  5730. int is = 0;
  5731. for (int j = 0; j < QK_K/32; ++j) {
  5732. int32_t scale = scales[is++];
  5733. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5734. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5735. q8 += 8; a += 8;
  5736. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5737. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5738. q8 += 8; a += 8;
  5739. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5740. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5741. q8 += 8; a += 8;
  5742. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5743. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5744. q8 += 8; a += 8;
  5745. }
  5746. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5747. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5748. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5749. sumf -= dmin * sumi;
  5750. }
  5751. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5752. *s = sumf;
  5753. #endif
  5754. }
  5755. #else
  5756. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5757. assert(n % QK_K == 0);
  5758. const block_q5_K * restrict x = vx;
  5759. const block_q8_K * restrict y = vy;
  5760. const int nb = n / QK_K;
  5761. #ifdef __ARM_NEON
  5762. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5763. const uint8x16_t mh = vdupq_n_u8(16);
  5764. const int32x4_t mzero = vdupq_n_s32(0);
  5765. ggml_int8x16x4_t q5bytes;
  5766. ggml_uint8x16x4_t q5h;
  5767. float sumf = 0;
  5768. for (int i = 0; i < nb; ++i) {
  5769. const float d = y[i].d * (float)x[i].d;
  5770. const int8_t * sc = x[i].scales;
  5771. const uint8_t * restrict q5 = x[i].qs;
  5772. const uint8_t * restrict qh = x[i].qh;
  5773. const int8_t * restrict q8 = y[i].qs;
  5774. const uint8x8_t qhbits = vld1_u8(qh);
  5775. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5776. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5777. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5778. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5779. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5780. q5h.val[2] = vbicq_u8(mh, htmp);
  5781. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5782. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5783. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5784. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5785. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5786. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5787. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5788. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5789. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5790. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5791. }
  5792. *s = sumf;
  5793. #elif defined __AVX2__
  5794. const __m256i m4 = _mm256_set1_epi8(0xF);
  5795. const __m256i mone = _mm256_set1_epi8(1);
  5796. __m256 acc = _mm256_setzero_ps();
  5797. for (int i = 0; i < nb; ++i) {
  5798. const uint8_t * restrict q5 = x[i].qs;
  5799. const int8_t * restrict q8 = y[i].qs;
  5800. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5801. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5802. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5803. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5804. int64_t aux64;
  5805. memcpy(&aux64, x[i].qh, 8);
  5806. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5807. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5808. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5809. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5810. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5811. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5812. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5813. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5814. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5815. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5816. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5817. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5818. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5819. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5820. }
  5821. *s = hsum_float_8(acc);
  5822. #elif defined __AVX__
  5823. const __m128i m4 = _mm_set1_epi8(0xF);
  5824. const __m128i mone = _mm_set1_epi8(1);
  5825. __m256 acc = _mm256_setzero_ps();
  5826. for (int i = 0; i < nb; ++i) {
  5827. const uint8_t * restrict q5 = x[i].qs;
  5828. const int8_t * restrict q8 = y[i].qs;
  5829. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5830. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5831. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5832. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5833. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5834. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5835. int64_t aux64;
  5836. memcpy(&aux64, x[i].qh, 8);
  5837. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5838. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5839. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5840. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5841. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5842. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5843. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5844. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5845. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5846. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5847. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5848. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5849. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5850. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5851. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5852. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5853. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5854. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5855. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5856. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5857. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5858. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5859. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5860. }
  5861. *s = hsum_float_8(acc);
  5862. #elif defined __riscv_v_intrinsic
  5863. float sumf = 0;
  5864. for (int i = 0; i < nb; ++i) {
  5865. const float d = y[i].d * (float)x[i].d;
  5866. const int8_t * sc = x[i].scales;
  5867. const uint8_t * restrict q5 = x[i].qs;
  5868. const uint8_t * restrict qh = x[i].qh;
  5869. const int8_t * restrict q8 = y[i].qs;
  5870. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5871. // load qh
  5872. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5873. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5874. size_t vl = 16;
  5875. // combine both qh_1 and qh_2
  5876. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5877. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5878. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5879. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5880. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5881. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5882. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5883. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5884. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5885. // load q5
  5886. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5887. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5888. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5889. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5890. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5891. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5892. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5893. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5894. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5895. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5896. // load Q8 and multiply it with Q5
  5897. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5898. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5899. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5900. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5901. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5902. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5903. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5904. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5905. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5906. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5907. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5908. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5909. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5910. }
  5911. *s = sumf;
  5912. #else
  5913. int8_t aux8[QK_K];
  5914. int16_t aux16[16];
  5915. float sums [8];
  5916. memset(sums, 0, 8*sizeof(float));
  5917. float sumf = 0;
  5918. for (int i = 0; i < nb; ++i) {
  5919. const uint8_t * restrict q4 = x[i].qs;
  5920. const uint8_t * restrict hm = x[i].qh;
  5921. const int8_t * restrict q8 = y[i].qs;
  5922. int8_t * restrict a = aux8;
  5923. for (int l = 0; l < 32; ++l) {
  5924. a[l+ 0] = q4[l] & 0xF;
  5925. a[l+32] = q4[l] >> 4;
  5926. }
  5927. for (int is = 0; is < 8; ++is) {
  5928. uint8_t m = 1 << is;
  5929. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5930. }
  5931. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5932. const int8_t * restrict sc = x[i].scales;
  5933. for (int j = 0; j < QK_K/16; ++j) {
  5934. const float dl = d * sc[j];
  5935. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5936. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5937. q8 += 16; a += 16;
  5938. }
  5939. }
  5940. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5941. *s = sumf;
  5942. #endif
  5943. }
  5944. #endif
  5945. #if QK_K == 256
  5946. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5947. assert(n % QK_K == 0);
  5948. const block_q6_K * restrict x = vx;
  5949. const block_q8_K * restrict y = vy;
  5950. const int nb = n / QK_K;
  5951. #ifdef __ARM_NEON
  5952. float sum = 0;
  5953. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5954. const int32x4_t vzero = vdupq_n_s32(0);
  5955. //const int8x16_t m32s = vdupq_n_s8(32);
  5956. const uint8x16_t mone = vdupq_n_u8(3);
  5957. ggml_int8x16x4_t q6bytes;
  5958. ggml_uint8x16x4_t q6h;
  5959. for (int i = 0; i < nb; ++i) {
  5960. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5961. const uint8_t * restrict q6 = x[i].ql;
  5962. const uint8_t * restrict qh = x[i].qh;
  5963. const int8_t * restrict q8 = y[i].qs;
  5964. const int8_t * restrict scale = x[i].scales;
  5965. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  5966. const int8x16_t scales = vld1q_s8(scale);
  5967. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  5968. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5969. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5970. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5971. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5972. int32_t isum_mins = vaddvq_s32(prod);
  5973. int32_t isum = 0;
  5974. for (int j = 0; j < QK_K/128; ++j) {
  5975. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  5976. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  5977. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5978. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5979. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5980. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5981. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5982. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5983. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5984. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5985. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5986. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5987. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5988. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5989. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  5990. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  5991. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  5992. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5993. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5994. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5995. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5996. scale += 4;
  5997. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5998. shifted = vshrq_n_u8(qhbits.val[0], 4);
  5999. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6000. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6001. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6002. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6003. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6004. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6005. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6006. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6007. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6008. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6009. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6010. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6011. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6012. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6013. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6014. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6015. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6016. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6017. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6018. scale += 4;
  6019. }
  6020. //sum += isum * d_all * y[i].d;
  6021. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6022. }
  6023. *s = sum;
  6024. #elif defined __AVX2__
  6025. const __m256i m4 = _mm256_set1_epi8(0xF);
  6026. const __m256i m2 = _mm256_set1_epi8(3);
  6027. const __m256i m32s = _mm256_set1_epi8(32);
  6028. __m256 acc = _mm256_setzero_ps();
  6029. for (int i = 0; i < nb; ++i) {
  6030. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6031. const uint8_t * restrict q4 = x[i].ql;
  6032. const uint8_t * restrict qh = x[i].qh;
  6033. const int8_t * restrict q8 = y[i].qs;
  6034. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6035. __m256i sumi = _mm256_setzero_si256();
  6036. int is = 0;
  6037. for (int j = 0; j < QK_K/128; ++j) {
  6038. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6039. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6040. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6041. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6042. is += 4;
  6043. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6044. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6045. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6046. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6047. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6048. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6049. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6050. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6051. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6052. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6053. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6054. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6055. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6056. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6057. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6058. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6059. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6060. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6061. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6062. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6063. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6064. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6065. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6066. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6067. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6068. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6069. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6070. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6071. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6072. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6073. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6074. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6075. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6076. }
  6077. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6078. }
  6079. *s = hsum_float_8(acc);
  6080. #elif defined __AVX__
  6081. const __m128i m4 = _mm_set1_epi8(0xF);
  6082. const __m128i m3 = _mm_set1_epi8(3);
  6083. const __m128i m32s = _mm_set1_epi8(32);
  6084. const __m128i m2 = _mm_set1_epi8(2);
  6085. __m256 acc = _mm256_setzero_ps();
  6086. for (int i = 0; i < nb; ++i) {
  6087. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6088. const uint8_t * restrict q4 = x[i].ql;
  6089. const uint8_t * restrict qh = x[i].qh;
  6090. const int8_t * restrict q8 = y[i].qs;
  6091. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6092. __m128i sumi_0 = _mm_setzero_si128();
  6093. __m128i sumi_1 = _mm_setzero_si128();
  6094. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6095. for (int j = 0; j < QK_K/128; ++j) {
  6096. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6097. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6098. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6099. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6100. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6101. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6102. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6103. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6104. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6105. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6106. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6107. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6108. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6109. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6110. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6111. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6112. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6113. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6114. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6115. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6116. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6117. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6118. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6119. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6120. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6121. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6122. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6123. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6124. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6125. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6126. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6127. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6128. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6129. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6130. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6131. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6132. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6133. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6134. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6135. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6136. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6137. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6138. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6139. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6140. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6141. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6142. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6143. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6144. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6145. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6146. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6147. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6148. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6149. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6150. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6151. shuffle = _mm_add_epi8(shuffle, m2);
  6152. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6153. shuffle = _mm_add_epi8(shuffle, m2);
  6154. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6155. shuffle = _mm_add_epi8(shuffle, m2);
  6156. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6157. shuffle = _mm_add_epi8(shuffle, m2);
  6158. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6159. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6160. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6161. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6162. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6163. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6164. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6165. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6166. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6167. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6168. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6169. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6170. }
  6171. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6172. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6173. }
  6174. *s = hsum_float_8(acc);
  6175. #elif defined __riscv_v_intrinsic
  6176. float sumf = 0;
  6177. for (int i = 0; i < nb; ++i) {
  6178. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6179. const uint8_t * restrict q6 = x[i].ql;
  6180. const uint8_t * restrict qh = x[i].qh;
  6181. const int8_t * restrict q8 = y[i].qs;
  6182. const int8_t * restrict scale = x[i].scales;
  6183. size_t vl;
  6184. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6185. int sum_t = 0;
  6186. int is = 0;
  6187. for (int j = 0; j < QK_K/128; ++j) {
  6188. vl = 32;
  6189. // load qh
  6190. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6191. // load Q6
  6192. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6193. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6194. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6195. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6196. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6197. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6198. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6199. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6200. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6201. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6202. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6203. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6204. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6205. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6206. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6207. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6208. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6209. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6210. // load Q8 and take product
  6211. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6212. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6213. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6214. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6215. vl = 16;
  6216. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6217. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6218. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6219. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6220. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6221. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6222. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6223. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6224. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6225. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6226. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6227. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6228. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6229. q6 += 64; qh += 32; q8 += 128; is=8;
  6230. }
  6231. sumf += d * sum_t;
  6232. }
  6233. *s = sumf;
  6234. #else
  6235. int8_t aux8[QK_K];
  6236. int16_t aux16[8];
  6237. float sums [8];
  6238. int32_t aux32[8];
  6239. memset(sums, 0, 8*sizeof(float));
  6240. float sumf = 0;
  6241. for (int i = 0; i < nb; ++i) {
  6242. const uint8_t * restrict q4 = x[i].ql;
  6243. const uint8_t * restrict qh = x[i].qh;
  6244. const int8_t * restrict q8 = y[i].qs;
  6245. memset(aux32, 0, 8*sizeof(int32_t));
  6246. int8_t * restrict a = aux8;
  6247. for (int j = 0; j < QK_K; j += 128) {
  6248. for (int l = 0; l < 32; ++l) {
  6249. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6250. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6251. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6252. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6253. }
  6254. a += 128;
  6255. q4 += 64;
  6256. qh += 32;
  6257. }
  6258. a = aux8;
  6259. int is = 0;
  6260. for (int j = 0; j < QK_K/16; ++j) {
  6261. int scale = x[i].scales[is++];
  6262. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6263. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6264. q8 += 8; a += 8;
  6265. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6266. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6267. q8 += 8; a += 8;
  6268. }
  6269. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6270. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6271. }
  6272. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6273. *s = sumf;
  6274. #endif
  6275. }
  6276. #else
  6277. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6278. assert(n % QK_K == 0);
  6279. const block_q6_K * restrict x = vx;
  6280. const block_q8_K * restrict y = vy;
  6281. const int nb = n / QK_K;
  6282. #ifdef __ARM_NEON
  6283. float sum = 0;
  6284. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6285. const int8x16_t m32s = vdupq_n_s8(32);
  6286. const int32x4_t vzero = vdupq_n_s32(0);
  6287. const uint8x16_t mone = vdupq_n_u8(3);
  6288. ggml_int8x16x4_t q6bytes;
  6289. ggml_uint8x16x4_t q6h;
  6290. for (int i = 0; i < nb; ++i) {
  6291. const float d_all = (float)x[i].d;
  6292. const uint8_t * restrict q6 = x[i].ql;
  6293. const uint8_t * restrict qh = x[i].qh;
  6294. const int8_t * restrict q8 = y[i].qs;
  6295. const int8_t * restrict scale = x[i].scales;
  6296. int32_t isum = 0;
  6297. uint8x16_t qhbits = vld1q_u8(qh);
  6298. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6299. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6300. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6301. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6302. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6303. shifted = vshrq_n_u8(qhbits, 4);
  6304. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6305. shifted = vshrq_n_u8(qhbits, 6);
  6306. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6307. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6308. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6309. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6310. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6311. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6312. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6313. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6314. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6315. sum += isum * d_all * y[i].d;
  6316. }
  6317. *s = sum;
  6318. #elif defined __AVX2__
  6319. const __m256i m4 = _mm256_set1_epi8(0xF);
  6320. const __m256i m2 = _mm256_set1_epi8(3);
  6321. const __m256i m32s = _mm256_set1_epi8(32);
  6322. __m256 acc = _mm256_setzero_ps();
  6323. for (int i = 0; i < nb; ++i) {
  6324. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6325. const uint8_t * restrict q4 = x[i].ql;
  6326. const uint8_t * restrict qh = x[i].qh;
  6327. const int8_t * restrict q8 = y[i].qs;
  6328. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6329. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6330. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6331. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6332. __m256i sumi = _mm256_setzero_si256();
  6333. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6334. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6335. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6336. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6337. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6338. 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);
  6339. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6340. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6341. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6342. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6343. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6344. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6345. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6346. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6347. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6348. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6349. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6350. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6351. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6352. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6353. }
  6354. *s = hsum_float_8(acc);
  6355. #elif defined __AVX__
  6356. const __m128i m4 = _mm_set1_epi8(0xF);
  6357. const __m128i m2 = _mm_set1_epi8(3);
  6358. const __m128i m32s = _mm_set1_epi8(32);
  6359. __m256 acc = _mm256_setzero_ps();
  6360. for (int i = 0; i < nb; ++i) {
  6361. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6362. const uint8_t * restrict q4 = x[i].ql;
  6363. const uint8_t * restrict qh = x[i].qh;
  6364. const int8_t * restrict q8 = y[i].qs;
  6365. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6366. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6367. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6368. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6369. __m128i sumi_0 = _mm_setzero_si128();
  6370. __m128i sumi_1 = _mm_setzero_si128();
  6371. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6372. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6373. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6374. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6375. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6376. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6377. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6378. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6379. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6380. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6381. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6382. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6383. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6384. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6385. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6386. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6387. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6388. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6389. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6390. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6391. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6392. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6393. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6394. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6395. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6396. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6397. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6398. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6399. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6400. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6401. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6402. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6403. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6404. }
  6405. *s = hsum_float_8(acc);
  6406. #elif defined __riscv_v_intrinsic
  6407. float sumf = 0;
  6408. for (int i = 0; i < nb; ++i) {
  6409. const float d_all = (float)x[i].d;
  6410. const uint8_t * restrict q6 = x[i].ql;
  6411. const uint8_t * restrict qh = x[i].qh;
  6412. const int8_t * restrict q8 = y[i].qs;
  6413. const int8_t * restrict scale = x[i].scales;
  6414. int32_t isum = 0;
  6415. size_t vl = 16;
  6416. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6417. // load Q6
  6418. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6419. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6420. // load qh
  6421. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6422. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6423. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6424. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6425. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6426. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6427. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6428. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6429. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6430. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6431. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6432. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6433. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6434. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6435. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6436. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6437. // load Q8 and take product
  6438. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6439. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6440. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6441. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6442. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6443. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6444. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6445. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6446. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6447. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6448. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6449. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6450. sumf += isum * d_all * y[i].d;
  6451. }
  6452. *s = sumf;
  6453. #else
  6454. int8_t aux8[QK_K];
  6455. int16_t aux16[8];
  6456. float sums [8];
  6457. int32_t aux32[8];
  6458. memset(sums, 0, 8*sizeof(float));
  6459. float sumf = 0;
  6460. for (int i = 0; i < nb; ++i) {
  6461. const uint8_t * restrict q4 = x[i].ql;
  6462. const uint8_t * restrict qh = x[i].qh;
  6463. const int8_t * restrict q8 = y[i].qs;
  6464. memset(aux32, 0, 8*sizeof(int32_t));
  6465. int8_t * restrict a = aux8;
  6466. for (int l = 0; l < 16; ++l) {
  6467. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6468. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6469. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6470. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6471. }
  6472. int is = 0;
  6473. for (int j = 0; j < QK_K/16; ++j) {
  6474. int scale = x[i].scales[is++];
  6475. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6476. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6477. q8 += 8; a += 8;
  6478. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6479. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6480. q8 += 8; a += 8;
  6481. }
  6482. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6483. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6484. }
  6485. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6486. *s = sumf;
  6487. #endif
  6488. }
  6489. #endif
  6490. static const int8_t keven_signs_q2xs[1024] = {
  6491. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6492. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6493. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6494. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6495. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6496. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6497. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6498. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6499. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6500. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6501. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6502. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6503. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6504. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6505. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6506. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6507. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6508. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6509. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6510. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6511. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6512. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6513. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6514. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6515. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6516. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6517. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6518. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6519. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6520. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6521. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6522. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6523. };
  6524. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6525. assert(n % QK_K == 0);
  6526. const block_iq2_xxs * restrict x = vx;
  6527. const block_q8_K * restrict y = vy;
  6528. const int nb = n / QK_K;
  6529. #if defined(__ARM_NEON)
  6530. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6531. uint32_t aux32[4];
  6532. const uint8_t * aux8 = (const uint8_t *)aux32;
  6533. ggml_int8x16x4_t q2u;
  6534. ggml_int8x16x4_t q2s;
  6535. ggml_int8x16x4_t q8b;
  6536. float sumf = 0;
  6537. for (int i = 0; i < nb; ++i) {
  6538. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6539. const uint16_t * restrict q2 = x[i].qs;
  6540. const int8_t * restrict q8 = y[i].qs;
  6541. float sumf1 = 0, sumf2 = 0;
  6542. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6543. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6544. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6545. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6546. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6547. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6548. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6549. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6550. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6551. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6552. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6553. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6554. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6555. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6556. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6557. 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]);
  6558. 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]);
  6559. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6560. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6561. }
  6562. sumf += d*(sumf1 + sumf2);
  6563. }
  6564. *s = 0.25f * sumf;
  6565. #elif defined(__AVX2__)
  6566. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6567. uint32_t aux32[4];
  6568. const uint8_t * aux8 = (const uint8_t *)aux32;
  6569. __m256 accumf = _mm256_setzero_ps();
  6570. for (int i = 0; i < nb; ++i) {
  6571. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6572. const uint16_t * restrict q2 = x[i].qs;
  6573. const int8_t * restrict q8 = y[i].qs;
  6574. __m256i sumi1 = _mm256_setzero_si256();
  6575. __m256i sumi2 = _mm256_setzero_si256();
  6576. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6577. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6578. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6579. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6580. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6581. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6582. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6583. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6584. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6585. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6586. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6587. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6588. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6589. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6590. const uint16_t ls1 = aux32[1] >> 28;
  6591. const uint16_t ls2 = aux32[3] >> 28;
  6592. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6593. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6594. sumi1 = _mm256_add_epi32(sumi1, p1);
  6595. sumi2 = _mm256_add_epi32(sumi2, p2);
  6596. }
  6597. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6598. }
  6599. *s = 0.125f * hsum_float_8(accumf);
  6600. #else
  6601. uint32_t aux32[2];
  6602. const uint8_t * aux8 = (const uint8_t *)aux32;
  6603. float sumf = 0.f;
  6604. for (int i = 0; i < nb; ++i) {
  6605. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6606. const uint16_t * restrict q2 = x[i].qs;
  6607. const int8_t * restrict q8 = y[i].qs;
  6608. int32_t bsum = 0;
  6609. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6610. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6611. q2 += 4;
  6612. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6613. int32_t sumi = 0;
  6614. for (int l = 0; l < 4; ++l) {
  6615. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6616. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6617. for (int j = 0; j < 8; ++j) {
  6618. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6619. }
  6620. q8 += 8;
  6621. }
  6622. bsum += sumi * ls;
  6623. }
  6624. sumf += d * bsum;
  6625. }
  6626. *s = 0.125f * sumf;
  6627. #endif
  6628. }
  6629. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6630. assert(n % QK_K == 0);
  6631. const block_iq2_xs * restrict x = vx;
  6632. const block_q8_K * restrict y = vy;
  6633. const int nb = n / QK_K;
  6634. #if defined(__ARM_NEON)
  6635. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6636. ggml_int8x16x4_t q2u;
  6637. ggml_int8x16x4_t q2s;
  6638. ggml_int8x16x4_t q8b;
  6639. int32x4x4_t scales32;
  6640. float sumf = 0;
  6641. for (int i = 0; i < nb; ++i) {
  6642. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6643. const uint16_t * restrict q2 = x[i].qs;
  6644. const int8_t * restrict q8 = y[i].qs;
  6645. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6646. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6647. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6648. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6649. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6650. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6651. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6652. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6653. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6654. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6655. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6656. int32x4_t sumi = vdupq_n_s32(0);
  6657. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6658. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6659. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6660. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6661. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6662. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6663. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6664. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6665. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6666. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6667. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6668. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6669. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6670. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6671. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6672. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6673. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6674. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6675. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6676. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6677. q2 += 8;
  6678. }
  6679. sumf += d*vaddvq_s32(sumi);
  6680. }
  6681. *s = 0.125f * sumf;
  6682. #elif defined(__AVX2__)
  6683. const __m128i m4 = _mm_set1_epi8(0xf);
  6684. const __m128i m1 = _mm_set1_epi8(1);
  6685. const __m128i m511 = _mm_set1_epi16(511);
  6686. const __m128i m127 = _mm_set1_epi16(127);
  6687. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6688. uint64_t aux64;
  6689. // somewhat hacky, but gives a significant boost in performance
  6690. __m128i aux_gindex, aux_sindex;
  6691. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6692. const uint16_t * sindex = (const uint16_t *)&aux_sindex;
  6693. __m256 accumf = _mm256_setzero_ps();
  6694. for (int i = 0; i < nb; ++i) {
  6695. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6696. const uint16_t * restrict q2 = x[i].qs;
  6697. const int8_t * restrict q8 = y[i].qs;
  6698. memcpy(&aux64, x[i].scales, 8);
  6699. __m128i stmp = _mm_set1_epi64x(aux64);
  6700. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6701. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6702. __m256i sumi1 = _mm256_setzero_si256();
  6703. __m256i sumi2 = _mm256_setzero_si256();
  6704. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6705. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6706. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6707. const __m128i q2_data = _mm_loadu_si128((const __m128i*)q2); q2 += 8;
  6708. aux_gindex = _mm_and_si128(q2_data, m511);
  6709. aux_sindex = _mm_and_si128(_mm_srli_epi16(q2_data, 9), m127);
  6710. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]], iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
  6711. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]], iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
  6712. const __m256i s2_1 = _mm256_set_epi64x(signs64[sindex[3]], signs64[sindex[2]], signs64[sindex[1]], signs64[sindex[0]]);
  6713. const __m256i s2_2 = _mm256_set_epi64x(signs64[sindex[7]], signs64[sindex[6]], signs64[sindex[5]], signs64[sindex[4]]);
  6714. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6715. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6716. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6717. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6718. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6719. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6720. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6721. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6722. }
  6723. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6724. }
  6725. *s = 0.125f * hsum_float_8(accumf);
  6726. #else
  6727. float sumf = 0.f;
  6728. for (int i = 0; i < nb; ++i) {
  6729. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6730. const uint16_t * restrict q2 = x[i].qs;
  6731. const uint8_t * restrict sc = x[i].scales;
  6732. const int8_t * restrict q8 = y[i].qs;
  6733. int32_t bsum = 0;
  6734. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6735. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6736. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6737. int32_t sumi = 0;
  6738. for (int l = 0; l < 2; ++l) {
  6739. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6740. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6741. for (int j = 0; j < 8; ++j) {
  6742. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6743. }
  6744. q8 += 8;
  6745. }
  6746. bsum += sumi * ls1;
  6747. sumi = 0;
  6748. for (int l = 2; l < 4; ++l) {
  6749. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6750. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6751. for (int j = 0; j < 8; ++j) {
  6752. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6753. }
  6754. q8 += 8;
  6755. }
  6756. bsum += sumi * ls2;
  6757. q2 += 4;
  6758. }
  6759. sumf += d * bsum;
  6760. }
  6761. *s = 0.125f * sumf;
  6762. #endif
  6763. }
  6764. // ================================ IQ2 quantization =============================================
  6765. typedef struct {
  6766. uint64_t * grid;
  6767. int * map;
  6768. uint16_t * neighbours;
  6769. } iq2_entry_t;
  6770. static iq2_entry_t iq2_data[2] = {
  6771. {NULL, NULL, NULL},
  6772. {NULL, NULL, NULL},
  6773. };
  6774. static inline int iq2_data_index(int grid_size) {
  6775. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  6776. return grid_size == 256 ? 0 : 1;
  6777. }
  6778. static int iq2_compare_func(const void * left, const void * right) {
  6779. const int * l = (const int *)left;
  6780. const int * r = (const int *)right;
  6781. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  6782. }
  6783. static void q2xs_init_impl(int grid_size) {
  6784. const int gindex = iq2_data_index(grid_size);
  6785. if (iq2_data[gindex].grid) {
  6786. return;
  6787. }
  6788. static const uint16_t kgrid_256[256] = {
  6789. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  6790. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  6791. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  6792. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  6793. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  6794. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  6795. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  6796. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  6797. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  6798. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  6799. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  6800. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  6801. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  6802. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  6803. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  6804. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  6805. };
  6806. static const uint16_t kgrid_512[512] = {
  6807. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  6808. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  6809. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  6810. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  6811. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  6812. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  6813. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  6814. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  6815. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  6816. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  6817. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  6818. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  6819. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  6820. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  6821. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  6822. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  6823. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  6824. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  6825. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  6826. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  6827. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  6828. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  6829. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  6830. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  6831. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  6832. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  6833. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  6834. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  6835. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  6836. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  6837. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  6838. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  6839. };
  6840. const int kmap_size = 43692;
  6841. const int nwant = 2;
  6842. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  6843. uint64_t * kgrid_q2xs;
  6844. int * kmap_q2xs;
  6845. uint16_t * kneighbors_q2xs;
  6846. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  6847. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  6848. for (int k = 0; k < grid_size; ++k) {
  6849. int8_t * pos = (int8_t *)(the_grid + k);
  6850. for (int i = 0; i < 8; ++i) {
  6851. int l = (kgrid[k] >> 2*i) & 0x3;
  6852. pos[i] = 2*l + 1;
  6853. }
  6854. }
  6855. kgrid_q2xs = the_grid;
  6856. iq2_data[gindex].grid = the_grid;
  6857. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  6858. iq2_data[gindex].map = kmap_q2xs;
  6859. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  6860. uint64_t aux64;
  6861. uint8_t * aux8 = (uint8_t *)&aux64;
  6862. for (int i = 0; i < grid_size; ++i) {
  6863. aux64 = kgrid_q2xs[i];
  6864. uint16_t index = 0;
  6865. for (int k=0; k<8; ++k) {
  6866. uint16_t q = (aux8[k] - 1)/2;
  6867. index |= (q << 2*k);
  6868. }
  6869. kmap_q2xs[index] = i;
  6870. }
  6871. int8_t pos[8];
  6872. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  6873. int num_neighbors = 0, num_not_in_map = 0;
  6874. for (int i = 0; i < kmap_size; ++i) {
  6875. if (kmap_q2xs[i] >= 0) continue;
  6876. ++num_not_in_map;
  6877. for (int k = 0; k < 8; ++k) {
  6878. int l = (i >> 2*k) & 0x3;
  6879. pos[k] = 2*l + 1;
  6880. }
  6881. for (int j = 0; j < grid_size; ++j) {
  6882. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6883. int d2 = 0;
  6884. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6885. dist2[2*j+0] = d2;
  6886. dist2[2*j+1] = j;
  6887. }
  6888. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6889. int n = 0; int d2 = dist2[0];
  6890. int nhave = 1;
  6891. for (int j = 0; j < grid_size; ++j) {
  6892. if (dist2[2*j] > d2) {
  6893. if (nhave == nwant) break;
  6894. d2 = dist2[2*j];
  6895. ++nhave;
  6896. }
  6897. ++n;
  6898. }
  6899. num_neighbors += n;
  6900. }
  6901. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  6902. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  6903. iq2_data[gindex].neighbours = kneighbors_q2xs;
  6904. int counter = 0;
  6905. for (int i = 0; i < kmap_size; ++i) {
  6906. if (kmap_q2xs[i] >= 0) continue;
  6907. for (int k = 0; k < 8; ++k) {
  6908. int l = (i >> 2*k) & 0x3;
  6909. pos[k] = 2*l + 1;
  6910. }
  6911. for (int j = 0; j < grid_size; ++j) {
  6912. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6913. int d2 = 0;
  6914. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6915. dist2[2*j+0] = d2;
  6916. dist2[2*j+1] = j;
  6917. }
  6918. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6919. kmap_q2xs[i] = -(counter + 1);
  6920. int d2 = dist2[0];
  6921. uint16_t * start = &kneighbors_q2xs[counter++];
  6922. int n = 0, nhave = 1;
  6923. for (int j = 0; j < grid_size; ++j) {
  6924. if (dist2[2*j] > d2) {
  6925. if (nhave == nwant) break;
  6926. d2 = dist2[2*j];
  6927. ++nhave;
  6928. }
  6929. kneighbors_q2xs[counter++] = dist2[2*j+1];
  6930. ++n;
  6931. }
  6932. *start = n;
  6933. }
  6934. free(dist2);
  6935. }
  6936. void ggml_init_iq2_quantization(enum ggml_type type) {
  6937. if (type == GGML_TYPE_IQ2_XXS) {
  6938. q2xs_init_impl(256);
  6939. }
  6940. else if (type == GGML_TYPE_IQ2_XS) {
  6941. q2xs_init_impl(512);
  6942. }
  6943. else {
  6944. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6945. }
  6946. }
  6947. static void q2xs_deinit_impl(int grid_size) {
  6948. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  6949. const int gindex = iq2_data_index(grid_size);
  6950. if (iq2_data[gindex].grid) {
  6951. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  6952. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  6953. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  6954. }
  6955. }
  6956. void ggml_deinit_iq2_quantization(enum ggml_type type) {
  6957. if (type == GGML_TYPE_IQ2_XXS) {
  6958. q2xs_deinit_impl(256);
  6959. }
  6960. else if (type == GGML_TYPE_IQ2_XS) {
  6961. q2xs_deinit_impl(512);
  6962. }
  6963. else {
  6964. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6965. }
  6966. }
  6967. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  6968. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  6969. int num_neighbors = neighbours[0];
  6970. GGML_ASSERT(num_neighbors > 0);
  6971. float best_d2 = FLT_MAX;
  6972. int grid_index = -1;
  6973. for (int j = 1; j <= num_neighbors; ++j) {
  6974. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  6975. float d2 = 0;
  6976. for (int i = 0; i < 8; ++i) {
  6977. float q = pg[i];
  6978. float diff = scale*q - xval[i];
  6979. d2 += weight[i]*diff*diff;
  6980. }
  6981. if (d2 < best_d2) {
  6982. best_d2 = d2; grid_index = neighbours[j];
  6983. }
  6984. }
  6985. GGML_ASSERT(grid_index >= 0);
  6986. const int8_t * pg = (const int8_t *)(grid + grid_index);
  6987. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  6988. return grid_index;
  6989. }
  6990. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  6991. const int gindex = iq2_data_index(256);
  6992. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  6993. const int * kmap_q2xs = iq2_data[gindex].map;
  6994. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  6995. GGML_ASSERT(quant_weights);
  6996. GGML_ASSERT(kgrid_q2xs);
  6997. GGML_ASSERT(kmap_q2xs);
  6998. GGML_ASSERT(kneighbors_q2xs);
  6999. GGML_ASSERT(n%QK_K == 0);
  7000. const int kMaxQ = 3;
  7001. const int nbl = n/256;
  7002. block_iq2_xxs * y = vy;
  7003. float scales[QK_K/32];
  7004. float weight[32];
  7005. float xval[32];
  7006. int8_t L[32];
  7007. int8_t Laux[32];
  7008. float waux[32];
  7009. bool is_on_grid[4];
  7010. bool is_on_grid_aux[4];
  7011. uint8_t block_signs[4];
  7012. uint32_t q2[2*(QK_K/32)];
  7013. for (int ibl = 0; ibl < nbl; ++ibl) {
  7014. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7015. memset(q2, 0, QK_K/4);
  7016. float max_scale = 0;
  7017. const float * xbl = x + QK_K*ibl;
  7018. float sumx2 = 0;
  7019. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7020. float sigma2 = sumx2/QK_K;
  7021. for (int ib = 0; ib < QK_K/32; ++ib) {
  7022. const float * xb = xbl + 32*ib;
  7023. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7024. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7025. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7026. for (int k = 0; k < 4; ++k) {
  7027. int nflip = 0;
  7028. uint8_t s = 0;
  7029. for (int i = 0; i < 8; ++i) {
  7030. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7031. else {
  7032. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7033. }
  7034. }
  7035. if (nflip%2) {
  7036. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7037. for (int i = 1; i < 8; ++i) {
  7038. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7039. if (ax < min) {
  7040. min = ax; imin = i;
  7041. }
  7042. }
  7043. xval[8*k+imin] = -xval[8*k+imin];
  7044. s ^= (1 << imin);
  7045. }
  7046. block_signs[k] = s & 127;
  7047. }
  7048. float max = xval[0];
  7049. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7050. if (!max) {
  7051. scales[ib] = 0;
  7052. memset(L, 0, 32);
  7053. continue;
  7054. }
  7055. float best = 0;
  7056. float scale = max/(2*kMaxQ-1);
  7057. for (int is = -9; is <= 9; ++is) {
  7058. float id = (2*kMaxQ-1+is*0.1f)/max;
  7059. float this_scale = 1/id;
  7060. for (int k = 0; k < 4; ++k) {
  7061. for (int i = 0; i < 8; ++i) {
  7062. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7063. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7064. }
  7065. uint16_t u = 0;
  7066. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7067. int grid_index = kmap_q2xs[u];
  7068. is_on_grid_aux[k] = true;
  7069. if (grid_index < 0) {
  7070. is_on_grid_aux[k] = false;
  7071. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7072. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7073. }
  7074. }
  7075. float sumqx = 0, sumq2 = 0;
  7076. for (int i = 0; i < 32; ++i) {
  7077. float w = weight[i];
  7078. float q = 2*Laux[i] + 1;
  7079. sumqx += w*xval[i]*q;
  7080. sumq2 += w*q*q;
  7081. }
  7082. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7083. scale = sumqx/sumq2; best = scale*sumqx;
  7084. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  7085. for (int k = 0; k < 4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7086. }
  7087. }
  7088. int n_not_ongrid = 0;
  7089. for (int k = 0; k < 4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7090. if (n_not_ongrid > 0 && scale > 0) {
  7091. float id = 1/scale;
  7092. for (int k = 0; k < 4; ++k) {
  7093. if (is_on_grid[k]) continue;
  7094. uint16_t u = 0;
  7095. for (int i = 0; i < 8; ++i) {
  7096. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7097. l = MAX(0, MIN(kMaxQ-1, l));
  7098. u |= (l << 2*i);
  7099. }
  7100. int grid_index = kmap_q2xs[u];
  7101. if (grid_index < 0) {
  7102. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7103. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7104. }
  7105. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  7106. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  7107. }
  7108. float sumqx = 0, sumq2 = 0;
  7109. for (int i = 0; i < 32; ++i) {
  7110. float w = weight[i];
  7111. float q = 2*L[i] + 1;
  7112. sumqx += w*xval[i]*q;
  7113. sumq2 += w*q*q;
  7114. }
  7115. if (sumq2 > 0) scale = sumqx/sumq2;
  7116. }
  7117. if (scale < 0) {
  7118. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7119. // and correspondingly flip quant signs.
  7120. scale = -scale;
  7121. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7122. }
  7123. for (int k = 0; k < 4; ++k) {
  7124. uint16_t u = 0;
  7125. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7126. int grid_index = kmap_q2xs[u];
  7127. if (grid_index < 0) {
  7128. printf("Oops: found point %u not on grid:", u);
  7129. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7130. printf("\n");
  7131. GGML_ASSERT(false);
  7132. }
  7133. q2[2*ib+0] |= (grid_index << 8*k);
  7134. q2[2*ib+1] |= (block_signs[k] << 7*k);
  7135. }
  7136. GGML_ASSERT(scale >= 0);
  7137. scales[ib] = scale;
  7138. max_scale = MAX(max_scale, scale);
  7139. }
  7140. if (!max_scale) {
  7141. memset(y[ibl].qs, 0, QK_K/4);
  7142. continue;
  7143. }
  7144. float d = max_scale/31;
  7145. y[ibl].d = GGML_FP32_TO_FP16(d);
  7146. float id = 1/d;
  7147. float sumqx = 0, sumq2 = 0;
  7148. for (int ib = 0; ib < QK_K/32; ++ib) {
  7149. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7150. l = MAX(0, MIN(15, l));
  7151. q2[2*ib+1] |= ((uint32_t)l << 28);
  7152. const float * xb = xbl + 32*ib;
  7153. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7154. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7155. const uint8_t * aux8 = (const uint8_t *)(q2 + 2*ib);
  7156. const float db = d * (1 + 2*l);
  7157. uint32_t u = 0;
  7158. for (int k = 0; k < 4; ++k) {
  7159. const int8_t * signs = keven_signs_q2xs + 8*((q2[2*ib+1] >> 7*k) & 127);
  7160. const float * xk = xb + 8*k;
  7161. const float * wk = weight + 8*k;
  7162. const uint8_t * grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7163. float best_mse = 0; int best_index = aux8[k];
  7164. for (int j = 0; j < 8; ++j) {
  7165. float diff = db * grid[j] * signs[j] - xk[j];
  7166. best_mse += wk[j] * diff * diff;
  7167. }
  7168. for (int idx = 0; idx < 256; ++idx) {
  7169. grid = (const uint8_t *)(kgrid_q2xs + idx);
  7170. float mse = 0;
  7171. for (int j = 0; j < 8; ++j) {
  7172. float diff = db * grid[j] * signs[j] - xk[j];
  7173. mse += wk[j] * diff * diff;
  7174. }
  7175. if (mse < best_mse) {
  7176. best_mse = mse; best_index = idx;
  7177. }
  7178. }
  7179. u |= (best_index << 8*k);
  7180. grid = (const uint8_t *)(kgrid_q2xs + best_index);
  7181. //grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7182. for (int j = 0; j < 8; ++j) {
  7183. float q = db * grid[j] * signs[j];
  7184. sumqx += wk[j] * q * xk[j];
  7185. sumq2 += wk[j] * q * q;
  7186. }
  7187. }
  7188. q2[2*ib] = u;
  7189. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  7190. }
  7191. memcpy(y[ibl].qs, q2, QK_K/4);
  7192. }
  7193. }
  7194. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7195. const int gindex = iq2_data_index(512);
  7196. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7197. const int * kmap_q2xs = iq2_data[gindex].map;
  7198. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7199. GGML_ASSERT(quant_weights);
  7200. GGML_ASSERT(kmap_q2xs);
  7201. GGML_ASSERT(kgrid_q2xs);
  7202. GGML_ASSERT(kneighbors_q2xs);
  7203. GGML_ASSERT(n%QK_K == 0);
  7204. const int kMaxQ = 3;
  7205. const int nbl = n/256;
  7206. block_iq2_xs * y = vy;
  7207. float scales[QK_K/16];
  7208. float weight[16];
  7209. float xval[16];
  7210. int8_t L[16];
  7211. int8_t Laux[16];
  7212. float waux[16];
  7213. bool is_on_grid[2];
  7214. bool is_on_grid_aux[2];
  7215. uint8_t block_signs[2];
  7216. uint16_t q2[2*(QK_K/16)];
  7217. for (int ibl = 0; ibl < nbl; ++ibl) {
  7218. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7219. memset(q2, 0, QK_K/4);
  7220. memset(y[ibl].scales, 0, QK_K/32);
  7221. float max_scale = 0;
  7222. const float * xbl = x + QK_K*ibl;
  7223. float sumx2 = 0;
  7224. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7225. float sigma2 = sumx2/QK_K;
  7226. for (int ib = 0; ib < QK_K/16; ++ib) {
  7227. const float * xb = xbl + 16*ib;
  7228. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  7229. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7230. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  7231. for (int k = 0; k < 2; ++k) {
  7232. int nflip = 0;
  7233. uint8_t s = 0;
  7234. for (int i = 0; i < 8; ++i) {
  7235. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7236. else {
  7237. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7238. }
  7239. }
  7240. if (nflip%2) {
  7241. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7242. for (int i = 1; i < 8; ++i) {
  7243. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7244. if (ax < min) {
  7245. min = ax; imin = i;
  7246. }
  7247. }
  7248. xval[8*k+imin] = -xval[8*k+imin];
  7249. s ^= (1 << imin);
  7250. }
  7251. block_signs[k] = s & 127;
  7252. }
  7253. float max = xval[0];
  7254. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  7255. if (!max) {
  7256. scales[ib] = 0;
  7257. memset(L, 0, 16);
  7258. continue;
  7259. }
  7260. float best = 0;
  7261. float scale = max/(2*kMaxQ-1);
  7262. is_on_grid[0] = is_on_grid[1] = true;
  7263. for (int is = -9; is <= 9; ++is) {
  7264. float id = (2*kMaxQ-1+is*0.1f)/max;
  7265. float this_scale = 1/id;
  7266. for (int k = 0; k < 2; ++k) {
  7267. for (int i = 0; i < 8; ++i) {
  7268. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7269. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7270. }
  7271. uint16_t u = 0;
  7272. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7273. int grid_index = kmap_q2xs[u];
  7274. is_on_grid_aux[k] = true;
  7275. if (grid_index < 0) {
  7276. is_on_grid_aux[k] = false;
  7277. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7278. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7279. }
  7280. }
  7281. float sumqx = 0, sumq2 = 0;
  7282. for (int i = 0; i < 16; ++i) {
  7283. float w = weight[i];
  7284. float q = 2*Laux[i] + 1;
  7285. sumqx += w*xval[i]*q;
  7286. sumq2 += w*q*q;
  7287. }
  7288. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7289. scale = sumqx/sumq2; best = scale*sumqx;
  7290. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  7291. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7292. }
  7293. }
  7294. int n_not_ongrid = 0;
  7295. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7296. if (n_not_ongrid > 0 && scale > 0) {
  7297. float id = 1/scale;
  7298. for (int k = 0; k < 2; ++k) {
  7299. if (is_on_grid[k]) continue;
  7300. uint16_t u = 0;
  7301. for (int i = 0; i < 8; ++i) {
  7302. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7303. l = MAX(0, MIN(kMaxQ-1, l));
  7304. u |= (l << 2*i);
  7305. L[8*k + i] = l;
  7306. }
  7307. int grid_index = kmap_q2xs[u];
  7308. if (grid_index < 0) {
  7309. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7310. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7311. }
  7312. }
  7313. float sumqx = 0, sumq2 = 0;
  7314. for (int i = 0; i < 16; ++i) {
  7315. float w = weight[i];
  7316. float q = 2*L[i] + 1;
  7317. sumqx += w*xval[i]*q;
  7318. sumq2 += w*q*q;
  7319. }
  7320. if (sumq2 > 0) scale = sumqx/sumq2;
  7321. }
  7322. if (scale < 0) {
  7323. scale = -scale;
  7324. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7325. }
  7326. for (int k = 0; k < 2; ++k) {
  7327. uint16_t u = 0;
  7328. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7329. int grid_index = kmap_q2xs[u];
  7330. if (grid_index < 0) {
  7331. printf("Oops: found point %u not on grid:", u);
  7332. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7333. printf("\n");
  7334. GGML_ASSERT(false);
  7335. }
  7336. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  7337. }
  7338. GGML_ASSERT(scale >= 0);
  7339. scales[ib] = scale;
  7340. max_scale = MAX(max_scale, scale);
  7341. }
  7342. if (!max_scale) {
  7343. memset(y[ibl].qs, 0, QK_K/4);
  7344. continue;
  7345. }
  7346. float d = max_scale/31;
  7347. y[ibl].d = GGML_FP32_TO_FP16(d);
  7348. float id = 1/d;
  7349. for (int ib = 0; ib < QK_K/16; ++ib) {
  7350. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7351. l = MAX(0, MIN(15, l));
  7352. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  7353. else y[ibl].scales[ib/2] |= (l << 4);
  7354. }
  7355. memcpy(y[ibl].qs, q2, QK_K/4);
  7356. }
  7357. }
  7358. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7359. (void)hist;
  7360. GGML_ASSERT(n_per_row%QK_K == 0);
  7361. int nblock = n_per_row/QK_K;
  7362. char * qrow = (char *)dst;
  7363. for (int row = 0; row < nrow; ++row) {
  7364. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  7365. src += n_per_row;
  7366. qrow += nblock*sizeof(block_iq2_xxs);
  7367. }
  7368. return nrow * nblock * sizeof(block_iq2_xxs);
  7369. }
  7370. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7371. (void)hist;
  7372. GGML_ASSERT(n_per_row%QK_K == 0);
  7373. int nblock = n_per_row/QK_K;
  7374. char * qrow = (char *)dst;
  7375. for (int row = 0; row < nrow; ++row) {
  7376. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  7377. src += n_per_row;
  7378. qrow += nblock*sizeof(block_iq2_xs);
  7379. }
  7380. return nrow * nblock * sizeof(block_iq2_xs);
  7381. }