ggml-quants.c 282 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282
  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. #ifdef __ARM_NEON
  8. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  9. //
  10. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  11. //
  12. #include <arm_neon.h>
  13. #if !defined(__aarch64__)
  14. inline static int32_t vaddvq_s16(int16x8_t v) {
  15. return
  16. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  17. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  18. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  19. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  20. }
  21. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  22. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  23. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  24. return vcombine_s16(a0, b0);
  25. }
  26. inline static int32_t vaddvq_s32(int32x4_t v) {
  27. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  28. }
  29. #endif
  30. #else
  31. #ifdef __wasm_simd128__
  32. #include <wasm_simd128.h>
  33. #else
  34. #ifdef __POWER9_VECTOR__
  35. #include <altivec.h>
  36. #undef bool
  37. #define bool _Bool
  38. #else
  39. #if defined(_MSC_VER) || defined(__MINGW32__)
  40. #include <intrin.h>
  41. #else
  42. #if !defined(__riscv) && !defined(__s390__)
  43. #include <immintrin.h>
  44. #endif
  45. #endif
  46. #endif
  47. #endif
  48. #endif
  49. #ifdef __riscv_v_intrinsic
  50. #include <riscv_vector.h>
  51. #endif
  52. #undef MIN
  53. #undef MAX
  54. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  55. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  56. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  57. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  58. // multiply int8_t, add results pairwise twice
  59. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  60. // Get absolute values of x vectors
  61. const __m128i ax = _mm_sign_epi8(x, x);
  62. // Sign the values of the y vectors
  63. const __m128i sy = _mm_sign_epi8(y, x);
  64. // Perform multiplication and create 16-bit values
  65. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  66. const __m128i ones = _mm_set1_epi16(1);
  67. return _mm_madd_epi16(ones, dot);
  68. }
  69. #if __AVX__ || __AVX2__ || __AVX512F__
  70. // horizontally add 8 floats
  71. static inline float hsum_float_8(const __m256 x) {
  72. __m128 res = _mm256_extractf128_ps(x, 1);
  73. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  74. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  75. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  76. return _mm_cvtss_f32(res);
  77. }
  78. // horizontally add 8 int32_t
  79. static inline int hsum_i32_8(const __m256i a) {
  80. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  81. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  82. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  83. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  84. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  85. }
  86. // horizontally add 4 int32_t
  87. static inline int hsum_i32_4(const __m128i a) {
  88. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  89. const __m128i sum64 = _mm_add_epi32(hi64, a);
  90. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  91. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  92. }
  93. #if defined(__AVX2__) || defined(__AVX512F__)
  94. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  95. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  96. uint32_t x32;
  97. memcpy(&x32, x, sizeof(uint32_t));
  98. const __m256i shuf_mask = _mm256_set_epi64x(
  99. 0x0303030303030303, 0x0202020202020202,
  100. 0x0101010101010101, 0x0000000000000000);
  101. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  102. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  103. bytes = _mm256_or_si256(bytes, bit_mask);
  104. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  105. }
  106. // Unpack 32 4-bit fields into 32 bytes
  107. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  108. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  109. {
  110. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  111. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  112. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  113. return _mm256_and_si256(lowMask, bytes);
  114. }
  115. // add int16_t pairwise and return as float vector
  116. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  117. const __m256i ones = _mm256_set1_epi16(1);
  118. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  119. return _mm256_cvtepi32_ps(summed_pairs);
  120. }
  121. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  122. #if __AVXVNNI__
  123. const __m256i zero = _mm256_setzero_si256();
  124. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  125. return _mm256_cvtepi32_ps(summed_pairs);
  126. #else
  127. // Perform multiplication and create 16-bit values
  128. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  129. return sum_i16_pairs_float(dot);
  130. #endif
  131. }
  132. // multiply int8_t, add results pairwise twice and return as float vector
  133. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  134. #if __AVXVNNIINT8__
  135. const __m256i zero = _mm256_setzero_si256();
  136. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  137. return _mm256_cvtepi32_ps(summed_pairs);
  138. #else
  139. // Get absolute values of x vectors
  140. const __m256i ax = _mm256_sign_epi8(x, x);
  141. // Sign the values of the y vectors
  142. const __m256i sy = _mm256_sign_epi8(y, x);
  143. return mul_sum_us8_pairs_float(ax, sy);
  144. #endif
  145. }
  146. static inline __m128i packNibbles( __m256i bytes )
  147. {
  148. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  149. #if __AVX512F__
  150. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  151. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  152. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  153. #else
  154. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  155. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  156. __m256i low = _mm256_and_si256( lowByte, bytes );
  157. high = _mm256_srli_epi16( high, 4 );
  158. bytes = _mm256_or_si256( low, high );
  159. // Compress uint16_t lanes into bytes
  160. __m128i r0 = _mm256_castsi256_si128( bytes );
  161. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  162. return _mm_packus_epi16( r0, r1 );
  163. #endif
  164. }
  165. #elif defined(__AVX__)
  166. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  167. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  168. uint32_t x32;
  169. memcpy(&x32, x, sizeof(uint32_t));
  170. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  171. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  172. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  173. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  174. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  175. bytesl = _mm_or_si128(bytesl, bit_mask);
  176. bytesh = _mm_or_si128(bytesh, bit_mask);
  177. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  178. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  179. return MM256_SET_M128I(bytesh, bytesl);
  180. }
  181. // Unpack 32 4-bit fields into 32 bytes
  182. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  183. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  184. {
  185. // Load 16 bytes from memory
  186. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  187. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  188. const __m128i lowMask = _mm_set1_epi8(0xF);
  189. tmpl = _mm_and_si128(lowMask, tmpl);
  190. tmph = _mm_and_si128(lowMask, tmph);
  191. return MM256_SET_M128I(tmph, tmpl);
  192. }
  193. // add int16_t pairwise and return as float vector
  194. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  195. const __m128i ones = _mm_set1_epi16(1);
  196. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  197. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  198. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  199. return _mm256_cvtepi32_ps(summed_pairs);
  200. }
  201. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  202. const __m128i axl = _mm256_castsi256_si128(ax);
  203. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  204. const __m128i syl = _mm256_castsi256_si128(sy);
  205. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  206. // Perform multiplication and create 16-bit values
  207. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  208. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  209. return sum_i16_pairs_float(doth, dotl);
  210. }
  211. // multiply int8_t, add results pairwise twice and return as float vector
  212. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  213. const __m128i xl = _mm256_castsi256_si128(x);
  214. const __m128i xh = _mm256_extractf128_si256(x, 1);
  215. const __m128i yl = _mm256_castsi256_si128(y);
  216. const __m128i yh = _mm256_extractf128_si256(y, 1);
  217. // Get absolute values of x vectors
  218. const __m128i axl = _mm_sign_epi8(xl, xl);
  219. const __m128i axh = _mm_sign_epi8(xh, xh);
  220. // Sign the values of the y vectors
  221. const __m128i syl = _mm_sign_epi8(yl, xl);
  222. const __m128i syh = _mm_sign_epi8(yh, xh);
  223. // Perform multiplication and create 16-bit values
  224. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  225. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  226. return sum_i16_pairs_float(doth, dotl);
  227. }
  228. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  229. {
  230. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  231. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  232. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  233. __m128i low = _mm_and_si128( lowByte, bytes1 );
  234. high = _mm_srli_epi16( high, 4 );
  235. bytes1 = _mm_or_si128( low, high );
  236. high = _mm_andnot_si128( lowByte, bytes2 );
  237. low = _mm_and_si128( lowByte, bytes2 );
  238. high = _mm_srli_epi16( high, 4 );
  239. bytes2 = _mm_or_si128( low, high );
  240. return _mm_packus_epi16( bytes1, bytes2);
  241. }
  242. #endif
  243. #elif defined(__SSSE3__)
  244. // horizontally add 4x4 floats
  245. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  246. __m128 res_0 =_mm_hadd_ps(a, b);
  247. __m128 res_1 =_mm_hadd_ps(c, d);
  248. __m128 res =_mm_hadd_ps(res_0, res_1);
  249. res =_mm_hadd_ps(res, res);
  250. res =_mm_hadd_ps(res, res);
  251. return _mm_cvtss_f32(res);
  252. }
  253. #endif // __AVX__ || __AVX2__ || __AVX512F__
  254. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  255. #if defined(__ARM_NEON)
  256. #if !defined(__aarch64__)
  257. inline static int32_t vaddvq_s32(int32x4_t v) {
  258. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  259. }
  260. inline static float vaddvq_f32(float32x4_t v) {
  261. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  262. }
  263. inline static float vmaxvq_f32(float32x4_t v) {
  264. return
  265. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  266. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  267. }
  268. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  269. int32x4_t res;
  270. res[0] = roundf(vgetq_lane_f32(v, 0));
  271. res[1] = roundf(vgetq_lane_f32(v, 1));
  272. res[2] = roundf(vgetq_lane_f32(v, 2));
  273. res[3] = roundf(vgetq_lane_f32(v, 3));
  274. return res;
  275. }
  276. #endif
  277. #endif
  278. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  279. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  280. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  281. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  282. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  283. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  284. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  285. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  286. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  287. // precomputed tables for expanding 8bits to 8 bytes:
  288. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  289. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  290. #endif
  291. // reference implementation for deterministic creation of model files
  292. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  293. static const int qk = QK4_0;
  294. assert(k % qk == 0);
  295. const int nb = k / qk;
  296. for (int i = 0; i < nb; i++) {
  297. float amax = 0.0f; // absolute max
  298. float max = 0.0f;
  299. for (int j = 0; j < qk; j++) {
  300. const float v = x[i*qk + j];
  301. if (amax < fabsf(v)) {
  302. amax = fabsf(v);
  303. max = v;
  304. }
  305. }
  306. const float d = max / -8;
  307. const float id = d ? 1.0f/d : 0.0f;
  308. y[i].d = GGML_FP32_TO_FP16(d);
  309. for (int j = 0; j < qk/2; ++j) {
  310. const float x0 = x[i*qk + 0 + j]*id;
  311. const float x1 = x[i*qk + qk/2 + j]*id;
  312. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  313. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  314. y[i].qs[j] = xi0;
  315. y[i].qs[j] |= xi1 << 4;
  316. }
  317. }
  318. }
  319. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  320. quantize_row_q4_0_reference(x, y, k);
  321. }
  322. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  323. const int qk = QK4_1;
  324. assert(k % qk == 0);
  325. const int nb = k / qk;
  326. for (int i = 0; i < nb; i++) {
  327. float min = FLT_MAX;
  328. float max = -FLT_MAX;
  329. for (int j = 0; j < qk; j++) {
  330. const float v = x[i*qk + j];
  331. if (v < min) min = v;
  332. if (v > max) max = v;
  333. }
  334. const float d = (max - min) / ((1 << 4) - 1);
  335. const float id = d ? 1.0f/d : 0.0f;
  336. y[i].d = GGML_FP32_TO_FP16(d);
  337. y[i].m = GGML_FP32_TO_FP16(min);
  338. for (int j = 0; j < qk/2; ++j) {
  339. const float x0 = (x[i*qk + 0 + j] - min)*id;
  340. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  341. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  342. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  343. y[i].qs[j] = xi0;
  344. y[i].qs[j] |= xi1 << 4;
  345. }
  346. }
  347. }
  348. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  349. quantize_row_q4_1_reference(x, y, k);
  350. }
  351. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  352. static const int qk = QK5_0;
  353. assert(k % qk == 0);
  354. const int nb = k / qk;
  355. for (int i = 0; i < nb; i++) {
  356. float amax = 0.0f; // absolute max
  357. float max = 0.0f;
  358. for (int j = 0; j < qk; j++) {
  359. const float v = x[i*qk + j];
  360. if (amax < fabsf(v)) {
  361. amax = fabsf(v);
  362. max = v;
  363. }
  364. }
  365. const float d = max / -16;
  366. const float id = d ? 1.0f/d : 0.0f;
  367. y[i].d = GGML_FP32_TO_FP16(d);
  368. uint32_t qh = 0;
  369. for (int j = 0; j < qk/2; ++j) {
  370. const float x0 = x[i*qk + 0 + j]*id;
  371. const float x1 = x[i*qk + qk/2 + j]*id;
  372. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  373. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  374. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  375. // get the 5-th bit and store it in qh at the right position
  376. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  377. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  378. }
  379. memcpy(&y[i].qh, &qh, sizeof(qh));
  380. }
  381. }
  382. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  383. quantize_row_q5_0_reference(x, y, k);
  384. }
  385. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  386. const int qk = QK5_1;
  387. assert(k % qk == 0);
  388. const int nb = k / qk;
  389. for (int i = 0; i < nb; i++) {
  390. float min = FLT_MAX;
  391. float max = -FLT_MAX;
  392. for (int j = 0; j < qk; j++) {
  393. const float v = x[i*qk + j];
  394. if (v < min) min = v;
  395. if (v > max) max = v;
  396. }
  397. const float d = (max - min) / ((1 << 5) - 1);
  398. const float id = d ? 1.0f/d : 0.0f;
  399. y[i].d = GGML_FP32_TO_FP16(d);
  400. y[i].m = GGML_FP32_TO_FP16(min);
  401. uint32_t qh = 0;
  402. for (int j = 0; j < qk/2; ++j) {
  403. const float x0 = (x[i*qk + 0 + j] - min)*id;
  404. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  405. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  406. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  407. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  408. // get the 5-th bit and store it in qh at the right position
  409. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  410. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  411. }
  412. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  413. }
  414. }
  415. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  416. quantize_row_q5_1_reference(x, y, k);
  417. }
  418. // reference implementation for deterministic creation of model files
  419. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  420. assert(k % QK8_0 == 0);
  421. const int nb = k / QK8_0;
  422. for (int i = 0; i < nb; i++) {
  423. float amax = 0.0f; // absolute max
  424. for (int j = 0; j < QK8_0; j++) {
  425. const float v = x[i*QK8_0 + j];
  426. amax = MAX(amax, fabsf(v));
  427. }
  428. const float d = amax / ((1 << 7) - 1);
  429. const float id = d ? 1.0f/d : 0.0f;
  430. y[i].d = GGML_FP32_TO_FP16(d);
  431. for (int j = 0; j < QK8_0; ++j) {
  432. const float x0 = x[i*QK8_0 + j]*id;
  433. y[i].qs[j] = roundf(x0);
  434. }
  435. }
  436. }
  437. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  438. assert(QK8_0 == 32);
  439. assert(k % QK8_0 == 0);
  440. const int nb = k / QK8_0;
  441. block_q8_0 * restrict y = vy;
  442. #if defined(__ARM_NEON)
  443. for (int i = 0; i < nb; i++) {
  444. float32x4_t srcv [8];
  445. float32x4_t asrcv[8];
  446. float32x4_t amaxv[8];
  447. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  448. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  449. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  450. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  451. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  452. const float amax = vmaxvq_f32(amaxv[0]);
  453. const float d = amax / ((1 << 7) - 1);
  454. const float id = d ? 1.0f/d : 0.0f;
  455. y[i].d = GGML_FP32_TO_FP16(d);
  456. for (int j = 0; j < 8; j++) {
  457. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  458. const int32x4_t vi = vcvtnq_s32_f32(v);
  459. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  460. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  461. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  462. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  463. }
  464. }
  465. #elif defined(__wasm_simd128__)
  466. for (int i = 0; i < nb; i++) {
  467. v128_t srcv [8];
  468. v128_t asrcv[8];
  469. v128_t amaxv[8];
  470. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  471. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  472. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  473. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  474. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  475. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  476. wasm_f32x4_extract_lane(amaxv[0], 1)),
  477. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  478. wasm_f32x4_extract_lane(amaxv[0], 3)));
  479. const float d = amax / ((1 << 7) - 1);
  480. const float id = d ? 1.0f/d : 0.0f;
  481. y[i].d = GGML_FP32_TO_FP16(d);
  482. for (int j = 0; j < 8; j++) {
  483. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  484. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  485. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  486. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  487. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  488. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  489. }
  490. }
  491. #elif defined(__AVX2__) || defined(__AVX__)
  492. for (int i = 0; i < nb; i++) {
  493. // Load elements into 4 AVX vectors
  494. __m256 v0 = _mm256_loadu_ps( x );
  495. __m256 v1 = _mm256_loadu_ps( x + 8 );
  496. __m256 v2 = _mm256_loadu_ps( x + 16 );
  497. __m256 v3 = _mm256_loadu_ps( x + 24 );
  498. x += 32;
  499. // Compute max(abs(e)) for the block
  500. const __m256 signBit = _mm256_set1_ps( -0.0f );
  501. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  502. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  503. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  504. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  505. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  506. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  507. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  508. const float maxScalar = _mm_cvtss_f32( max4 );
  509. // Quantize these floats
  510. const float d = maxScalar / 127.f;
  511. y[i].d = GGML_FP32_TO_FP16(d);
  512. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  513. const __m256 mul = _mm256_set1_ps( id );
  514. // Apply the multiplier
  515. v0 = _mm256_mul_ps( v0, mul );
  516. v1 = _mm256_mul_ps( v1, mul );
  517. v2 = _mm256_mul_ps( v2, mul );
  518. v3 = _mm256_mul_ps( v3, mul );
  519. // Round to nearest integer
  520. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  521. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  522. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  523. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  524. // Convert floats to integers
  525. __m256i i0 = _mm256_cvtps_epi32( v0 );
  526. __m256i i1 = _mm256_cvtps_epi32( v1 );
  527. __m256i i2 = _mm256_cvtps_epi32( v2 );
  528. __m256i i3 = _mm256_cvtps_epi32( v3 );
  529. #if defined(__AVX2__)
  530. // Convert int32 to int16
  531. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  532. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  533. // Convert int16 to int8
  534. 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
  535. // We got our precious signed bytes, but the order is now wrong
  536. // These AVX2 pack instructions process 16-byte pieces independently
  537. // The following instruction is fixing the order
  538. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  539. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  540. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  541. #else
  542. // Since we don't have in AVX some necessary functions,
  543. // we split the registers in half and call AVX2 analogs from SSE
  544. __m128i ni0 = _mm256_castsi256_si128( i0 );
  545. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  546. __m128i ni2 = _mm256_castsi256_si128( i1 );
  547. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  548. __m128i ni4 = _mm256_castsi256_si128( i2 );
  549. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  550. __m128i ni6 = _mm256_castsi256_si128( i3 );
  551. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  552. // Convert int32 to int16
  553. ni0 = _mm_packs_epi32( ni0, ni1 );
  554. ni2 = _mm_packs_epi32( ni2, ni3 );
  555. ni4 = _mm_packs_epi32( ni4, ni5 );
  556. ni6 = _mm_packs_epi32( ni6, ni7 );
  557. // Convert int16 to int8
  558. ni0 = _mm_packs_epi16( ni0, ni2 );
  559. ni4 = _mm_packs_epi16( ni4, ni6 );
  560. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  561. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  562. #endif
  563. }
  564. #elif defined(__riscv_v_intrinsic)
  565. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  566. for (int i = 0; i < nb; i++) {
  567. // load elements
  568. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  569. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  570. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  571. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  572. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  573. const float d = amax / ((1 << 7) - 1);
  574. const float id = d ? 1.0f/d : 0.0f;
  575. y[i].d = GGML_FP32_TO_FP16(d);
  576. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  577. // convert to integer
  578. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  579. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  580. // store result
  581. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  582. }
  583. #else
  584. GGML_UNUSED(nb);
  585. // scalar
  586. quantize_row_q8_0_reference(x, y, k);
  587. #endif
  588. }
  589. // reference implementation for deterministic creation of model files
  590. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  591. assert(QK8_1 == 32);
  592. assert(k % QK8_1 == 0);
  593. const int nb = k / QK8_1;
  594. for (int i = 0; i < nb; i++) {
  595. float amax = 0.0f; // absolute max
  596. for (int j = 0; j < QK8_1; j++) {
  597. const float v = x[i*QK8_1 + j];
  598. amax = MAX(amax, fabsf(v));
  599. }
  600. const float d = amax / ((1 << 7) - 1);
  601. const float id = d ? 1.0f/d : 0.0f;
  602. y[i].d = d;
  603. int sum = 0;
  604. for (int j = 0; j < QK8_1/2; ++j) {
  605. const float v0 = x[i*QK8_1 + j]*id;
  606. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  607. y[i].qs[ j] = roundf(v0);
  608. y[i].qs[QK8_1/2 + j] = roundf(v1);
  609. sum += y[i].qs[ j];
  610. sum += y[i].qs[QK8_1/2 + j];
  611. }
  612. y[i].s = sum*d;
  613. }
  614. }
  615. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  616. assert(k % QK8_1 == 0);
  617. const int nb = k / QK8_1;
  618. block_q8_1 * restrict y = vy;
  619. #if defined(__ARM_NEON)
  620. for (int i = 0; i < nb; i++) {
  621. float32x4_t srcv [8];
  622. float32x4_t asrcv[8];
  623. float32x4_t amaxv[8];
  624. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  625. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  626. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  627. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  628. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  629. const float amax = vmaxvq_f32(amaxv[0]);
  630. const float d = amax / ((1 << 7) - 1);
  631. const float id = d ? 1.0f/d : 0.0f;
  632. y[i].d = d;
  633. int32x4_t accv = vdupq_n_s32(0);
  634. for (int j = 0; j < 8; j++) {
  635. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  636. const int32x4_t vi = vcvtnq_s32_f32(v);
  637. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  638. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  639. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  640. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  641. accv = vaddq_s32(accv, vi);
  642. }
  643. y[i].s = d * vaddvq_s32(accv);
  644. }
  645. #elif defined(__wasm_simd128__)
  646. for (int i = 0; i < nb; i++) {
  647. v128_t srcv [8];
  648. v128_t asrcv[8];
  649. v128_t amaxv[8];
  650. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  651. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  652. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  653. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  654. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  655. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  656. wasm_f32x4_extract_lane(amaxv[0], 1)),
  657. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  658. wasm_f32x4_extract_lane(amaxv[0], 3)));
  659. const float d = amax / ((1 << 7) - 1);
  660. const float id = d ? 1.0f/d : 0.0f;
  661. y[i].d = d;
  662. v128_t accv = wasm_i32x4_splat(0);
  663. for (int j = 0; j < 8; j++) {
  664. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  665. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  666. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  667. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  668. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  669. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  670. accv = wasm_i32x4_add(accv, vi);
  671. }
  672. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  673. wasm_i32x4_extract_lane(accv, 1) +
  674. wasm_i32x4_extract_lane(accv, 2) +
  675. wasm_i32x4_extract_lane(accv, 3));
  676. }
  677. #elif defined(__AVX2__) || defined(__AVX__)
  678. for (int i = 0; i < nb; i++) {
  679. // Load elements into 4 AVX vectors
  680. __m256 v0 = _mm256_loadu_ps( x );
  681. __m256 v1 = _mm256_loadu_ps( x + 8 );
  682. __m256 v2 = _mm256_loadu_ps( x + 16 );
  683. __m256 v3 = _mm256_loadu_ps( x + 24 );
  684. x += 32;
  685. // Compute max(abs(e)) for the block
  686. const __m256 signBit = _mm256_set1_ps( -0.0f );
  687. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  688. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  689. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  690. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  691. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  692. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  693. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  694. const float maxScalar = _mm_cvtss_f32( max4 );
  695. // Quantize these floats
  696. const float d = maxScalar / 127.f;
  697. y[i].d = d;
  698. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  699. const __m256 mul = _mm256_set1_ps( id );
  700. // Apply the multiplier
  701. v0 = _mm256_mul_ps( v0, mul );
  702. v1 = _mm256_mul_ps( v1, mul );
  703. v2 = _mm256_mul_ps( v2, mul );
  704. v3 = _mm256_mul_ps( v3, mul );
  705. // Round to nearest integer
  706. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  707. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  708. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  709. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  710. // Convert floats to integers
  711. __m256i i0 = _mm256_cvtps_epi32( v0 );
  712. __m256i i1 = _mm256_cvtps_epi32( v1 );
  713. __m256i i2 = _mm256_cvtps_epi32( v2 );
  714. __m256i i3 = _mm256_cvtps_epi32( v3 );
  715. #if defined(__AVX2__)
  716. // Compute the sum of the quants and set y[i].s
  717. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  718. // Convert int32 to int16
  719. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  720. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  721. // Convert int16 to int8
  722. 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
  723. // We got our precious signed bytes, but the order is now wrong
  724. // These AVX2 pack instructions process 16-byte pieces independently
  725. // The following instruction is fixing the order
  726. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  727. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  728. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  729. #else
  730. // Since we don't have in AVX some necessary functions,
  731. // we split the registers in half and call AVX2 analogs from SSE
  732. __m128i ni0 = _mm256_castsi256_si128( i0 );
  733. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  734. __m128i ni2 = _mm256_castsi256_si128( i1 );
  735. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  736. __m128i ni4 = _mm256_castsi256_si128( i2 );
  737. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  738. __m128i ni6 = _mm256_castsi256_si128( i3 );
  739. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  740. // Compute the sum of the quants and set y[i].s
  741. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  742. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  743. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  744. // Convert int32 to int16
  745. ni0 = _mm_packs_epi32( ni0, ni1 );
  746. ni2 = _mm_packs_epi32( ni2, ni3 );
  747. ni4 = _mm_packs_epi32( ni4, ni5 );
  748. ni6 = _mm_packs_epi32( ni6, ni7 );
  749. // Convert int16 to int8
  750. ni0 = _mm_packs_epi16( ni0, ni2 );
  751. ni4 = _mm_packs_epi16( ni4, ni6 );
  752. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  753. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  754. #endif
  755. }
  756. #elif defined(__riscv_v_intrinsic)
  757. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  758. for (int i = 0; i < nb; i++) {
  759. // load elements
  760. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  761. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  762. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  763. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  764. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  765. const float d = amax / ((1 << 7) - 1);
  766. const float id = d ? 1.0f/d : 0.0f;
  767. y[i].d = d;
  768. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  769. // convert to integer
  770. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  771. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  772. // store result
  773. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  774. // compute sum for y[i].s
  775. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  776. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  777. // set y[i].s
  778. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  779. y[i].s = sum*d;
  780. }
  781. #else
  782. GGML_UNUSED(nb);
  783. // scalar
  784. quantize_row_q8_1_reference(x, y, k);
  785. #endif
  786. }
  787. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  788. static const int qk = QK4_0;
  789. assert(k % qk == 0);
  790. const int nb = k / qk;
  791. for (int i = 0; i < nb; i++) {
  792. const float d = GGML_FP16_TO_FP32(x[i].d);
  793. for (int j = 0; j < qk/2; ++j) {
  794. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  795. const int x1 = (x[i].qs[j] >> 4) - 8;
  796. y[i*qk + j + 0 ] = x0*d;
  797. y[i*qk + j + qk/2] = x1*d;
  798. }
  799. }
  800. }
  801. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  802. static const int qk = QK4_1;
  803. assert(k % qk == 0);
  804. const int nb = k / qk;
  805. for (int i = 0; i < nb; i++) {
  806. const float d = GGML_FP16_TO_FP32(x[i].d);
  807. const float m = GGML_FP16_TO_FP32(x[i].m);
  808. for (int j = 0; j < qk/2; ++j) {
  809. const int x0 = (x[i].qs[j] & 0x0F);
  810. const int x1 = (x[i].qs[j] >> 4);
  811. y[i*qk + j + 0 ] = x0*d + m;
  812. y[i*qk + j + qk/2] = x1*d + m;
  813. }
  814. }
  815. }
  816. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  817. static const int qk = QK5_0;
  818. assert(k % qk == 0);
  819. const int nb = k / qk;
  820. for (int i = 0; i < nb; i++) {
  821. const float d = GGML_FP16_TO_FP32(x[i].d);
  822. uint32_t qh;
  823. memcpy(&qh, x[i].qh, sizeof(qh));
  824. for (int j = 0; j < qk/2; ++j) {
  825. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  826. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  827. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  828. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  829. y[i*qk + j + 0 ] = x0*d;
  830. y[i*qk + j + qk/2] = x1*d;
  831. }
  832. }
  833. }
  834. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  835. static const int qk = QK5_1;
  836. assert(k % qk == 0);
  837. const int nb = k / qk;
  838. for (int i = 0; i < nb; i++) {
  839. const float d = GGML_FP16_TO_FP32(x[i].d);
  840. const float m = GGML_FP16_TO_FP32(x[i].m);
  841. uint32_t qh;
  842. memcpy(&qh, x[i].qh, sizeof(qh));
  843. for (int j = 0; j < qk/2; ++j) {
  844. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  845. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  846. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  847. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  848. y[i*qk + j + 0 ] = x0*d + m;
  849. y[i*qk + j + qk/2] = x1*d + m;
  850. }
  851. }
  852. }
  853. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  854. static const int qk = QK8_0;
  855. assert(k % qk == 0);
  856. const int nb = k / qk;
  857. for (int i = 0; i < nb; i++) {
  858. const float d = GGML_FP16_TO_FP32(x[i].d);
  859. for (int j = 0; j < qk; ++j) {
  860. y[i*qk + j] = x[i].qs[j]*d;
  861. }
  862. }
  863. }
  864. //
  865. // 2-6 bit quantization in super-blocks
  866. //
  867. //
  868. // ===================== Helper functions
  869. //
  870. static inline int nearest_int(float fval) {
  871. assert(fval <= 4194303.f);
  872. float val = fval + 12582912.f;
  873. int i; memcpy(&i, &val, sizeof(int));
  874. return (i & 0x007fffff) - 0x00400000;
  875. }
  876. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type) {
  877. float max = 0;
  878. float amax = 0;
  879. for (int i = 0; i < n; ++i) {
  880. float ax = fabsf(x[i]);
  881. if (ax > amax) { amax = ax; max = x[i]; }
  882. }
  883. if (amax < 1e-30f) { // all zero
  884. for (int i = 0; i < n; ++i) {
  885. L[i] = 0;
  886. }
  887. return 0.f;
  888. }
  889. float iscale = -nmax / max;
  890. if (rmse_type == 0) {
  891. for (int i = 0; i < n; ++i) {
  892. int l = nearest_int(iscale * x[i]);
  893. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  894. }
  895. return 1/iscale;
  896. }
  897. bool return_early = false;
  898. if (rmse_type < 0) {
  899. rmse_type = -rmse_type;
  900. return_early = true;
  901. }
  902. int weight_type = rmse_type%2;
  903. float sumlx = 0;
  904. float suml2 = 0;
  905. for (int i = 0; i < n; ++i) {
  906. int l = nearest_int(iscale * x[i]);
  907. l = MAX(-nmax, MIN(nmax-1, l));
  908. L[i] = l + nmax;
  909. float w = weight_type == 1 ? x[i] * x[i] : 1;
  910. sumlx += w*x[i]*l;
  911. suml2 += w*l*l;
  912. }
  913. float scale = sumlx/suml2;
  914. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  915. float best = scale * sumlx;
  916. for (int is = -9; is <= 9; ++is) {
  917. if (is == 0) {
  918. continue;
  919. }
  920. iscale = -(nmax + 0.1f*is) / max;
  921. sumlx = suml2 = 0;
  922. for (int i = 0; i < n; ++i) {
  923. int l = nearest_int(iscale * x[i]);
  924. l = MAX(-nmax, MIN(nmax-1, l));
  925. float w = weight_type == 1 ? x[i] * x[i] : 1;
  926. sumlx += w*x[i]*l;
  927. suml2 += w*l*l;
  928. }
  929. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  930. for (int i = 0; i < n; ++i) {
  931. int l = nearest_int(iscale * x[i]);
  932. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  933. }
  934. scale = sumlx/suml2; best = scale*sumlx;
  935. }
  936. }
  937. return scale;
  938. }
  939. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  940. float max = 0;
  941. float amax = 0;
  942. for (int i = 0; i < n; ++i) {
  943. float ax = fabsf(x[i]);
  944. if (ax > amax) { amax = ax; max = x[i]; }
  945. }
  946. if (!amax) { // all zero
  947. for (int i = 0; i < n; ++i) { L[i] = 0; }
  948. return 0.f;
  949. }
  950. float iscale = -nmax / max;
  951. if (do_rmse) {
  952. float sumlx = 0;
  953. float suml2 = 0;
  954. for (int i = 0; i < n; ++i) {
  955. int l = nearest_int(iscale * x[i]);
  956. l = MAX(-nmax, MIN(nmax-1, l));
  957. L[i] = l;
  958. float w = x[i]*x[i];
  959. sumlx += w*x[i]*l;
  960. suml2 += w*l*l;
  961. }
  962. for (int itry = 0; itry < 5; ++itry) {
  963. int n_changed = 0;
  964. for (int i = 0; i < n; ++i) {
  965. float w = x[i]*x[i];
  966. float slx = sumlx - w*x[i]*L[i];
  967. if (slx > 0) {
  968. float sl2 = suml2 - w*L[i]*L[i];
  969. int new_l = nearest_int(x[i] * sl2 / slx);
  970. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  971. if (new_l != L[i]) {
  972. slx += w*x[i]*new_l;
  973. sl2 += w*new_l*new_l;
  974. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  975. L[i] = new_l; sumlx = slx; suml2 = sl2;
  976. ++n_changed;
  977. }
  978. }
  979. }
  980. }
  981. if (!n_changed) {
  982. break;
  983. }
  984. }
  985. for (int i = 0; i < n; ++i) {
  986. L[i] += nmax;
  987. }
  988. return sumlx / suml2;
  989. }
  990. for (int i = 0; i < n; ++i) {
  991. int l = nearest_int(iscale * x[i]);
  992. l = MAX(-nmax, MIN(nmax-1, l));
  993. L[i] = l + nmax;
  994. }
  995. return 1/iscale;
  996. }
  997. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  998. int ntry, float alpha) {
  999. float min = x[0];
  1000. float max = x[0];
  1001. for (int i = 1; i < n; ++i) {
  1002. if (x[i] < min) min = x[i];
  1003. if (x[i] > max) max = x[i];
  1004. }
  1005. if (max == min) {
  1006. for (int i = 0; i < n; ++i) L[i] = 0;
  1007. *the_min = 0;
  1008. return 0.f;
  1009. }
  1010. if (min > 0) min = 0;
  1011. float iscale = nmax/(max - min);
  1012. float scale = 1/iscale;
  1013. for (int itry = 0; itry < ntry; ++itry) {
  1014. float sumlx = 0; int suml2 = 0;
  1015. bool did_change = false;
  1016. for (int i = 0; i < n; ++i) {
  1017. int l = nearest_int(iscale*(x[i] - min));
  1018. l = MAX(0, MIN(nmax, l));
  1019. if (l != L[i]) {
  1020. L[i] = l;
  1021. did_change = true;
  1022. }
  1023. sumlx += (x[i] - min)*l;
  1024. suml2 += l*l;
  1025. }
  1026. scale = sumlx/suml2;
  1027. float sum = 0;
  1028. for (int i = 0; i < n; ++i) {
  1029. sum += x[i] - scale*L[i];
  1030. }
  1031. min = alpha*min + (1 - alpha)*sum/n;
  1032. if (min > 0) min = 0;
  1033. iscale = 1/scale;
  1034. if (!did_change) break;
  1035. }
  1036. *the_min = -min;
  1037. return scale;
  1038. }
  1039. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1040. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1041. float rmin, float rdelta, int nstep, bool use_mad) {
  1042. float min = x[0];
  1043. float max = x[0];
  1044. float sum_w = weights[0];
  1045. float sum_x = sum_w * x[0];
  1046. for (int i = 1; i < n; ++i) {
  1047. if (x[i] < min) min = x[i];
  1048. if (x[i] > max) max = x[i];
  1049. float w = weights[i];
  1050. sum_w += w;
  1051. sum_x += w * x[i];
  1052. }
  1053. if (min > 0) min = 0;
  1054. if (max == min) {
  1055. for (int i = 0; i < n; ++i) L[i] = 0;
  1056. *the_min = -min;
  1057. return 0.f;
  1058. }
  1059. float iscale = nmax/(max - min);
  1060. float scale = 1/iscale;
  1061. float best_mad = 0;
  1062. for (int i = 0; i < n; ++i) {
  1063. int l = nearest_int(iscale*(x[i] - min));
  1064. L[i] = MAX(0, MIN(nmax, l));
  1065. float diff = scale * L[i] + min - x[i];
  1066. diff = use_mad ? fabsf(diff) : diff * diff;
  1067. float w = weights[i];
  1068. best_mad += w * diff;
  1069. }
  1070. if (nstep < 1) {
  1071. *the_min = -min;
  1072. return scale;
  1073. }
  1074. for (int is = 0; is <= nstep; ++is) {
  1075. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1076. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1077. for (int i = 0; i < n; ++i) {
  1078. int l = nearest_int(iscale*(x[i] - min));
  1079. l = MAX(0, MIN(nmax, l));
  1080. Laux[i] = l;
  1081. float w = weights[i];
  1082. sum_l += w*l;
  1083. sum_l2 += w*l*l;
  1084. sum_xl += w*l*x[i];
  1085. }
  1086. float D = sum_w * sum_l2 - sum_l * sum_l;
  1087. if (D > 0) {
  1088. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1089. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1090. if (this_min > 0) {
  1091. this_min = 0;
  1092. this_scale = sum_xl / sum_l2;
  1093. }
  1094. float mad = 0;
  1095. for (int i = 0; i < n; ++i) {
  1096. float diff = this_scale * Laux[i] + this_min - x[i];
  1097. diff = use_mad ? fabsf(diff) : diff * diff;
  1098. float w = weights[i];
  1099. mad += w * diff;
  1100. }
  1101. if (mad < best_mad) {
  1102. for (int i = 0; i < n; ++i) {
  1103. L[i] = Laux[i];
  1104. }
  1105. best_mad = mad;
  1106. scale = this_scale;
  1107. min = this_min;
  1108. }
  1109. }
  1110. }
  1111. *the_min = -min;
  1112. return scale;
  1113. }
  1114. #if QK_K == 256
  1115. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1116. if (j < 4) {
  1117. *d = q[j] & 63; *m = q[j + 4] & 63;
  1118. } else {
  1119. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1120. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1121. }
  1122. }
  1123. #endif
  1124. //========================- 2-bit (de)-quantization
  1125. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1126. assert(k % QK_K == 0);
  1127. const int nb = k / QK_K;
  1128. uint8_t L[QK_K];
  1129. uint8_t Laux[16];
  1130. float weights[16];
  1131. float mins[QK_K/16];
  1132. float scales[QK_K/16];
  1133. const float q4scale = 15.f;
  1134. for (int i = 0; i < nb; i++) {
  1135. float max_scale = 0; // as we are deducting the min, scales are always positive
  1136. float max_min = 0;
  1137. for (int j = 0; j < QK_K/16; ++j) {
  1138. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1139. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1140. float scale = scales[j];
  1141. if (scale > max_scale) {
  1142. max_scale = scale;
  1143. }
  1144. float min = mins[j];
  1145. if (min > max_min) {
  1146. max_min = min;
  1147. }
  1148. }
  1149. if (max_scale > 0) {
  1150. float iscale = q4scale/max_scale;
  1151. for (int j = 0; j < QK_K/16; ++j) {
  1152. int l = nearest_int(iscale*scales[j]);
  1153. y[i].scales[j] = l;
  1154. }
  1155. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1156. } else {
  1157. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1158. y[i].d = GGML_FP32_TO_FP16(0.f);
  1159. }
  1160. if (max_min > 0) {
  1161. float iscale = q4scale/max_min;
  1162. for (int j = 0; j < QK_K/16; ++j) {
  1163. int l = nearest_int(iscale*mins[j]);
  1164. y[i].scales[j] |= (l << 4);
  1165. }
  1166. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1167. } else {
  1168. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1169. }
  1170. for (int j = 0; j < QK_K/16; ++j) {
  1171. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1172. if (!d) continue;
  1173. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1174. for (int ii = 0; ii < 16; ++ii) {
  1175. int l = nearest_int((x[16*j + ii] + dm)/d);
  1176. l = MAX(0, MIN(3, l));
  1177. L[16*j + ii] = l;
  1178. }
  1179. }
  1180. #if QK_K == 256
  1181. for (int j = 0; j < QK_K; j += 128) {
  1182. for (int l = 0; l < 32; ++l) {
  1183. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1184. }
  1185. }
  1186. #else
  1187. for (int l = 0; l < 16; ++l) {
  1188. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1189. }
  1190. #endif
  1191. x += QK_K;
  1192. }
  1193. }
  1194. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1195. assert(k % QK_K == 0);
  1196. const int nb = k / QK_K;
  1197. for (int i = 0; i < nb; i++) {
  1198. const float d = GGML_FP16_TO_FP32(x[i].d);
  1199. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1200. const uint8_t * q = x[i].qs;
  1201. #if QK_K == 256
  1202. int is = 0;
  1203. float dl, ml;
  1204. for (int n = 0; n < QK_K; n += 128) {
  1205. int shift = 0;
  1206. for (int j = 0; j < 4; ++j) {
  1207. uint8_t sc = x[i].scales[is++];
  1208. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1209. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1210. sc = x[i].scales[is++];
  1211. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1212. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1213. shift += 2;
  1214. }
  1215. q += 32;
  1216. }
  1217. #else
  1218. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1219. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1220. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1221. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1222. for (int l = 0; l < 16; ++l) {
  1223. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1224. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1225. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1226. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1227. }
  1228. y += QK_K;
  1229. #endif
  1230. }
  1231. }
  1232. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1233. quantize_row_q2_K_reference(x, vy, k);
  1234. }
  1235. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1236. (void)hist; // TODO: collect histograms
  1237. for (int j = 0; j < n; j += k) {
  1238. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1239. quantize_row_q2_K_reference(src + j, y, k);
  1240. }
  1241. return (n/QK_K*sizeof(block_q2_K));
  1242. }
  1243. //========================= 3-bit (de)-quantization
  1244. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1245. assert(k % QK_K == 0);
  1246. const int nb = k / QK_K;
  1247. int8_t L[QK_K];
  1248. float scales[QK_K / 16];
  1249. for (int i = 0; i < nb; i++) {
  1250. float max_scale = 0;
  1251. float amax = 0;
  1252. for (int j = 0; j < QK_K/16; ++j) {
  1253. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1254. float scale = fabsf(scales[j]);
  1255. if (scale > amax) {
  1256. amax = scale; max_scale = scales[j];
  1257. }
  1258. }
  1259. #if QK_K == 256
  1260. memset(y[i].scales, 0, 12);
  1261. if (max_scale) {
  1262. float iscale = -32.f/max_scale;
  1263. for (int j = 0; j < QK_K/16; ++j) {
  1264. int8_t l = nearest_int(iscale*scales[j]);
  1265. l = MAX(-32, MIN(31, l)) + 32;
  1266. if (j < 8) {
  1267. y[i].scales[j] = l & 0xF;
  1268. } else {
  1269. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1270. }
  1271. l >>= 4;
  1272. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1273. }
  1274. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1275. } else {
  1276. y[i].d = GGML_FP32_TO_FP16(0.f);
  1277. }
  1278. int8_t sc;
  1279. for (int j = 0; j < QK_K/16; ++j) {
  1280. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1281. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1282. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1283. if (!d) {
  1284. continue;
  1285. }
  1286. for (int ii = 0; ii < 16; ++ii) {
  1287. int l = nearest_int(x[16*j + ii]/d);
  1288. l = MAX(-4, MIN(3, l));
  1289. L[16*j + ii] = l + 4;
  1290. }
  1291. }
  1292. #else
  1293. if (max_scale) {
  1294. float iscale = -8.f/max_scale;
  1295. for (int j = 0; j < QK_K/16; j+=2) {
  1296. int l1 = nearest_int(iscale*scales[j]);
  1297. l1 = 8 + MAX(-8, MIN(7, l1));
  1298. int l2 = nearest_int(iscale*scales[j+1]);
  1299. l2 = 8 + MAX(-8, MIN(7, l2));
  1300. y[i].scales[j/2] = l1 | (l2 << 4);
  1301. }
  1302. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1303. } else {
  1304. for (int j = 0; j < QK_K/16; j+=2) {
  1305. y[i].scales[j/2] = 0;
  1306. }
  1307. y[i].d = GGML_FP32_TO_FP16(0.f);
  1308. }
  1309. for (int j = 0; j < QK_K/16; ++j) {
  1310. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1311. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1312. if (!d) {
  1313. continue;
  1314. }
  1315. for (int ii = 0; ii < 16; ++ii) {
  1316. int l = nearest_int(x[16*j + ii]/d);
  1317. l = MAX(-4, MIN(3, l));
  1318. L[16*j + ii] = l + 4;
  1319. }
  1320. }
  1321. #endif
  1322. memset(y[i].hmask, 0, QK_K/8);
  1323. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1324. int m = 0;
  1325. uint8_t hm = 1;
  1326. for (int j = 0; j < QK_K; ++j) {
  1327. if (L[j] > 3) {
  1328. y[i].hmask[m] |= hm;
  1329. L[j] -= 4;
  1330. }
  1331. if (++m == QK_K/8) {
  1332. m = 0; hm <<= 1;
  1333. }
  1334. }
  1335. #if QK_K == 256
  1336. for (int j = 0; j < QK_K; j += 128) {
  1337. for (int l = 0; l < 32; ++l) {
  1338. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1339. }
  1340. }
  1341. #else
  1342. for (int l = 0; l < 16; ++l) {
  1343. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1344. }
  1345. #endif
  1346. x += QK_K;
  1347. }
  1348. }
  1349. #if QK_K == 256
  1350. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1351. assert(k % QK_K == 0);
  1352. const int nb = k / QK_K;
  1353. const uint32_t kmask1 = 0x03030303;
  1354. const uint32_t kmask2 = 0x0f0f0f0f;
  1355. uint32_t aux[4];
  1356. const int8_t * scales = (const int8_t*)aux;
  1357. for (int i = 0; i < nb; i++) {
  1358. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1359. const uint8_t * restrict q = x[i].qs;
  1360. const uint8_t * restrict hm = x[i].hmask;
  1361. uint8_t m = 1;
  1362. memcpy(aux, x[i].scales, 12);
  1363. uint32_t tmp = aux[2];
  1364. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1365. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1366. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1367. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1368. int is = 0;
  1369. float dl;
  1370. for (int n = 0; n < QK_K; n += 128) {
  1371. int shift = 0;
  1372. for (int j = 0; j < 4; ++j) {
  1373. dl = d_all * (scales[is++] - 32);
  1374. for (int l = 0; l < 16; ++l) {
  1375. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1376. }
  1377. dl = d_all * (scales[is++] - 32);
  1378. for (int l = 0; l < 16; ++l) {
  1379. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1380. }
  1381. shift += 2;
  1382. m <<= 1;
  1383. }
  1384. q += 32;
  1385. }
  1386. }
  1387. }
  1388. #else
  1389. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1390. assert(k % QK_K == 0);
  1391. assert(QK_K == 64);
  1392. const int nb = k / QK_K;
  1393. for (int i = 0; i < nb; i++) {
  1394. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1395. const uint8_t * restrict q = x[i].qs;
  1396. const uint8_t * restrict hm = x[i].hmask;
  1397. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1398. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1399. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1400. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1401. for (int l=0; l<8; ++l) {
  1402. uint8_t h = hm[l];
  1403. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1404. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1405. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1406. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1407. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1408. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1409. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1410. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1411. }
  1412. y += QK_K;
  1413. }
  1414. }
  1415. #endif
  1416. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1417. quantize_row_q3_K_reference(x, vy, k);
  1418. }
  1419. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1420. (void)hist; // TODO: collect histograms
  1421. for (int j = 0; j < n; j += k) {
  1422. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1423. quantize_row_q3_K_reference(src + j, y, k);
  1424. }
  1425. return (n/QK_K*sizeof(block_q3_K));
  1426. }
  1427. // ====================== 4-bit (de)-quantization
  1428. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1429. assert(k % QK_K == 0);
  1430. const int nb = k / QK_K;
  1431. uint8_t L[QK_K];
  1432. uint8_t Laux[32];
  1433. float weights[32];
  1434. float mins[QK_K/32];
  1435. float scales[QK_K/32];
  1436. for (int i = 0; i < nb; i++) {
  1437. float max_scale = 0; // as we are deducting the min, scales are always positive
  1438. float max_min = 0;
  1439. for (int j = 0; j < QK_K/32; ++j) {
  1440. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1441. float sum_x2 = 0;
  1442. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1443. float av_x = sqrtf(sum_x2/32);
  1444. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1445. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1446. float scale = scales[j];
  1447. if (scale > max_scale) {
  1448. max_scale = scale;
  1449. }
  1450. float min = mins[j];
  1451. if (min > max_min) {
  1452. max_min = min;
  1453. }
  1454. }
  1455. #if QK_K == 256
  1456. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1457. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1458. for (int j = 0; j < QK_K/32; ++j) {
  1459. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1460. uint8_t lm = nearest_int(inv_min*mins[j]);
  1461. ls = MIN(63, ls);
  1462. lm = MIN(63, lm);
  1463. if (j < 4) {
  1464. y[i].scales[j] = ls;
  1465. y[i].scales[j+4] = lm;
  1466. } else {
  1467. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1468. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1469. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1470. }
  1471. }
  1472. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1473. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1474. uint8_t sc, m;
  1475. for (int j = 0; j < QK_K/32; ++j) {
  1476. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1477. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1478. if (!d) continue;
  1479. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1480. for (int ii = 0; ii < 32; ++ii) {
  1481. int l = nearest_int((x[32*j + ii] + dm)/d);
  1482. l = MAX(0, MIN(15, l));
  1483. L[32*j + ii] = l;
  1484. }
  1485. }
  1486. #else
  1487. const float s_factor = 15.f;
  1488. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1489. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1490. int d1 = nearest_int(inv_scale*scales[0]);
  1491. int m1 = nearest_int(inv_min*mins[0]);
  1492. int d2 = nearest_int(inv_scale*scales[1]);
  1493. int m2 = nearest_int(inv_min*mins[1]);
  1494. y[i].scales[0] = d1 | (m1 << 4);
  1495. y[i].scales[1] = d2 | (m2 << 4);
  1496. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1497. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1498. float sumlx = 0;
  1499. int suml2 = 0;
  1500. for (int j = 0; j < QK_K/32; ++j) {
  1501. const uint8_t sd = y[i].scales[j] & 0xF;
  1502. const uint8_t sm = y[i].scales[j] >> 4;
  1503. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1504. if (!d) continue;
  1505. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1506. for (int ii = 0; ii < 32; ++ii) {
  1507. int l = nearest_int((x[32*j + ii] + m)/d);
  1508. l = MAX(0, MIN(15, l));
  1509. L[32*j + ii] = l;
  1510. sumlx += (x[32*j + ii] + m)*l*sd;
  1511. suml2 += l*l*sd*sd;
  1512. }
  1513. }
  1514. if (suml2) {
  1515. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1516. }
  1517. #endif
  1518. uint8_t * q = y[i].qs;
  1519. for (int j = 0; j < QK_K; j += 64) {
  1520. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1521. q += 32;
  1522. }
  1523. x += QK_K;
  1524. }
  1525. }
  1526. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1527. assert(k % QK_K == 0);
  1528. const int nb = k / QK_K;
  1529. for (int i = 0; i < nb; i++) {
  1530. const uint8_t * q = x[i].qs;
  1531. #if QK_K == 256
  1532. const float d = GGML_FP16_TO_FP32(x[i].d);
  1533. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1534. int is = 0;
  1535. uint8_t sc, m;
  1536. for (int j = 0; j < QK_K; j += 64) {
  1537. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1538. const float d1 = d * sc; const float m1 = min * m;
  1539. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1540. const float d2 = d * sc; const float m2 = min * m;
  1541. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1542. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1543. q += 32; is += 2;
  1544. }
  1545. #else
  1546. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1547. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1548. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1549. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1550. for (int l = 0; l < 32; ++l) {
  1551. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1552. y[l+32] = d2 * (q[l] >> 4) - m2;
  1553. }
  1554. y += QK_K;
  1555. #endif
  1556. }
  1557. }
  1558. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1559. assert(k % QK_K == 0);
  1560. block_q4_K * restrict y = vy;
  1561. quantize_row_q4_K_reference(x, y, k);
  1562. }
  1563. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1564. assert(k % QK_K == 0);
  1565. (void)hist; // TODO: collect histograms
  1566. for (int j = 0; j < n; j += k) {
  1567. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  1568. quantize_row_q4_K_reference(src + j, y, k);
  1569. }
  1570. return (n/QK_K*sizeof(block_q4_K));
  1571. }
  1572. // ====================== 5-bit (de)-quantization
  1573. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  1574. assert(k % QK_K == 0);
  1575. const int nb = k / QK_K;
  1576. #if QK_K == 256
  1577. uint8_t L[QK_K];
  1578. float mins[QK_K/32];
  1579. float scales[QK_K/32];
  1580. float weights[32];
  1581. uint8_t Laux[32];
  1582. #else
  1583. int8_t L[QK_K];
  1584. float scales[QK_K/16];
  1585. #endif
  1586. for (int i = 0; i < nb; i++) {
  1587. #if QK_K == 256
  1588. float max_scale = 0; // as we are deducting the min, scales are always positive
  1589. float max_min = 0;
  1590. for (int j = 0; j < QK_K/32; ++j) {
  1591. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1592. float sum_x2 = 0;
  1593. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1594. float av_x = sqrtf(sum_x2/32);
  1595. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1596. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1597. float scale = scales[j];
  1598. if (scale > max_scale) {
  1599. max_scale = scale;
  1600. }
  1601. float min = mins[j];
  1602. if (min > max_min) {
  1603. max_min = min;
  1604. }
  1605. }
  1606. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1607. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1608. for (int j = 0; j < QK_K/32; ++j) {
  1609. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1610. uint8_t lm = nearest_int(inv_min*mins[j]);
  1611. ls = MIN(63, ls);
  1612. lm = MIN(63, lm);
  1613. if (j < 4) {
  1614. y[i].scales[j] = ls;
  1615. y[i].scales[j+4] = lm;
  1616. } else {
  1617. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1618. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1619. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1620. }
  1621. }
  1622. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1623. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1624. uint8_t sc, m;
  1625. for (int j = 0; j < QK_K/32; ++j) {
  1626. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1627. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1628. if (!d) continue;
  1629. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1630. for (int ii = 0; ii < 32; ++ii) {
  1631. int l = nearest_int((x[32*j + ii] + dm)/d);
  1632. l = MAX(0, MIN(31, l));
  1633. L[32*j + ii] = l;
  1634. }
  1635. }
  1636. uint8_t * restrict qh = y[i].qh;
  1637. uint8_t * restrict ql = y[i].qs;
  1638. memset(qh, 0, QK_K/8);
  1639. uint8_t m1 = 1, m2 = 2;
  1640. for (int n = 0; n < QK_K; n += 64) {
  1641. for (int j = 0; j < 32; ++j) {
  1642. int l1 = L[n + j];
  1643. if (l1 > 15) {
  1644. l1 -= 16; qh[j] |= m1;
  1645. }
  1646. int l2 = L[n + j + 32];
  1647. if (l2 > 15) {
  1648. l2 -= 16; qh[j] |= m2;
  1649. }
  1650. ql[j] = l1 | (l2 << 4);
  1651. }
  1652. m1 <<= 2; m2 <<= 2;
  1653. ql += 32;
  1654. }
  1655. #else
  1656. float max_scale = 0, amax = 0;
  1657. for (int j = 0; j < QK_K/16; ++j) {
  1658. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1);
  1659. float abs_scale = fabsf(scales[j]);
  1660. if (abs_scale > amax) {
  1661. amax = abs_scale;
  1662. max_scale = scales[j];
  1663. }
  1664. }
  1665. float iscale = -128.f/max_scale;
  1666. for (int j = 0; j < QK_K/16; ++j) {
  1667. int l = nearest_int(iscale*scales[j]);
  1668. y[i].scales[j] = MAX(-128, MIN(127, l));
  1669. }
  1670. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1671. for (int j = 0; j < QK_K/16; ++j) {
  1672. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1673. if (!d) continue;
  1674. for (int ii = 0; ii < 16; ++ii) {
  1675. int l = nearest_int(x[16*j + ii]/d);
  1676. l = MAX(-16, MIN(15, l));
  1677. L[16*j + ii] = l + 16;
  1678. }
  1679. }
  1680. uint8_t * restrict qh = y[i].qh;
  1681. uint8_t * restrict ql = y[i].qs;
  1682. memset(qh, 0, QK_K/8);
  1683. for (int j = 0; j < 32; ++j) {
  1684. int jm = j%8;
  1685. int is = j/8;
  1686. int l1 = L[j];
  1687. if (l1 > 15) {
  1688. l1 -= 16; qh[jm] |= (1 << is);
  1689. }
  1690. int l2 = L[j + 32];
  1691. if (l2 > 15) {
  1692. l2 -= 16; qh[jm] |= (1 << (4 + is));
  1693. }
  1694. ql[j] = l1 | (l2 << 4);
  1695. }
  1696. #endif
  1697. x += QK_K;
  1698. }
  1699. }
  1700. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  1701. assert(k % QK_K == 0);
  1702. const int nb = k / QK_K;
  1703. for (int i = 0; i < nb; i++) {
  1704. const uint8_t * ql = x[i].qs;
  1705. const uint8_t * qh = x[i].qh;
  1706. #if QK_K == 256
  1707. const float d = GGML_FP16_TO_FP32(x[i].d);
  1708. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1709. int is = 0;
  1710. uint8_t sc, m;
  1711. uint8_t u1 = 1, u2 = 2;
  1712. for (int j = 0; j < QK_K; j += 64) {
  1713. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1714. const float d1 = d * sc; const float m1 = min * m;
  1715. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1716. const float d2 = d * sc; const float m2 = min * m;
  1717. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  1718. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  1719. ql += 32; is += 2;
  1720. u1 <<= 2; u2 <<= 2;
  1721. }
  1722. #else
  1723. float d = GGML_FP16_TO_FP32(x[i].d);
  1724. const int8_t * restrict s = x[i].scales;
  1725. for (int l = 0; l < 8; ++l) {
  1726. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  1727. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  1728. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  1729. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  1730. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  1731. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  1732. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  1733. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  1734. }
  1735. y += QK_K;
  1736. #endif
  1737. }
  1738. }
  1739. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  1740. assert(k % QK_K == 0);
  1741. block_q5_K * restrict y = vy;
  1742. quantize_row_q5_K_reference(x, y, k);
  1743. }
  1744. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1745. assert(k % QK_K == 0);
  1746. (void)hist; // TODO: collect histograms
  1747. for (int j = 0; j < n; j += k) {
  1748. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  1749. quantize_row_q5_K_reference(src + j, y, k);
  1750. }
  1751. return (n/QK_K*sizeof(block_q5_K));
  1752. }
  1753. // ====================== 6-bit (de)-quantization
  1754. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  1755. assert(k % QK_K == 0);
  1756. const int nb = k / QK_K;
  1757. int8_t L[QK_K];
  1758. float scales[QK_K/16];
  1759. for (int i = 0; i < nb; i++) {
  1760. float max_scale = 0;
  1761. float max_abs_scale = 0;
  1762. for (int ib = 0; ib < QK_K/16; ++ib) {
  1763. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1);
  1764. scales[ib] = scale;
  1765. const float abs_scale = fabsf(scale);
  1766. if (abs_scale > max_abs_scale) {
  1767. max_abs_scale = abs_scale;
  1768. max_scale = scale;
  1769. }
  1770. }
  1771. if (!max_abs_scale) {
  1772. memset(&y[i], 0, sizeof(block_q6_K));
  1773. y[i].d = GGML_FP32_TO_FP16(0.f);
  1774. x += QK_K;
  1775. continue;
  1776. }
  1777. float iscale = -128.f/max_scale;
  1778. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1779. for (int ib = 0; ib < QK_K/16; ++ib) {
  1780. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  1781. }
  1782. for (int j = 0; j < QK_K/16; ++j) {
  1783. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1784. if (!d) {
  1785. continue;
  1786. }
  1787. for (int ii = 0; ii < 16; ++ii) {
  1788. int l = nearest_int(x[16*j + ii]/d);
  1789. l = MAX(-32, MIN(31, l));
  1790. L[16*j + ii] = l + 32;
  1791. }
  1792. }
  1793. uint8_t * restrict ql = y[i].ql;
  1794. uint8_t * restrict qh = y[i].qh;
  1795. #if QK_K == 256
  1796. for (int j = 0; j < QK_K; j += 128) {
  1797. for (int l = 0; l < 32; ++l) {
  1798. const uint8_t q1 = L[j + l + 0] & 0xF;
  1799. const uint8_t q2 = L[j + l + 32] & 0xF;
  1800. const uint8_t q3 = L[j + l + 64] & 0xF;
  1801. const uint8_t q4 = L[j + l + 96] & 0xF;
  1802. ql[l+ 0] = q1 | (q3 << 4);
  1803. ql[l+32] = q2 | (q4 << 4);
  1804. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  1805. }
  1806. ql += 64;
  1807. qh += 32;
  1808. }
  1809. #else
  1810. for (int l = 0; l < 32; ++l) {
  1811. const uint8_t q1 = L[l + 0] & 0xF;
  1812. const uint8_t q2 = L[l + 32] & 0xF;
  1813. ql[l] = q1 | (q2 << 4);
  1814. }
  1815. for (int l = 0; l < 16; ++l) {
  1816. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  1817. }
  1818. #endif
  1819. x += QK_K;
  1820. }
  1821. }
  1822. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  1823. assert(k % QK_K == 0);
  1824. const int nb = k / QK_K;
  1825. for (int i = 0; i < nb; i++) {
  1826. const float d = GGML_FP16_TO_FP32(x[i].d);
  1827. const uint8_t * restrict ql = x[i].ql;
  1828. const uint8_t * restrict qh = x[i].qh;
  1829. const int8_t * restrict sc = x[i].scales;
  1830. #if QK_K == 256
  1831. for (int n = 0; n < QK_K; n += 128) {
  1832. for (int l = 0; l < 32; ++l) {
  1833. int is = l/16;
  1834. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1835. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1836. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1837. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1838. y[l + 0] = d * sc[is + 0] * q1;
  1839. y[l + 32] = d * sc[is + 2] * q2;
  1840. y[l + 64] = d * sc[is + 4] * q3;
  1841. y[l + 96] = d * sc[is + 6] * q4;
  1842. }
  1843. y += 128;
  1844. ql += 64;
  1845. qh += 32;
  1846. sc += 8;
  1847. }
  1848. #else
  1849. for (int l = 0; l < 16; ++l) {
  1850. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1851. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1852. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1853. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1854. y[l+ 0] = d * sc[0] * q1;
  1855. y[l+16] = d * sc[1] * q2;
  1856. y[l+32] = d * sc[2] * q3;
  1857. y[l+48] = d * sc[3] * q4;
  1858. }
  1859. y += 64;
  1860. #endif
  1861. }
  1862. }
  1863. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  1864. assert(k % QK_K == 0);
  1865. block_q6_K * restrict y = vy;
  1866. quantize_row_q6_K_reference(x, y, k);
  1867. }
  1868. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  1869. assert(k % QK_K == 0);
  1870. (void)hist; // TODO: collect histograms
  1871. for (int j = 0; j < n; j += k) {
  1872. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  1873. quantize_row_q6_K_reference(src + j, y, k);
  1874. }
  1875. return (n/QK_K*sizeof(block_q6_K));
  1876. }
  1877. //===================================== Q8_K ==============================================
  1878. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  1879. assert(k % QK_K == 0);
  1880. const int nb = k / QK_K;
  1881. for (int i = 0; i < nb; i++) {
  1882. float max = 0;
  1883. float amax = 0;
  1884. for (int j = 0; j < QK_K; ++j) {
  1885. float ax = fabsf(x[j]);
  1886. if (ax > amax) {
  1887. amax = ax; max = x[j];
  1888. }
  1889. }
  1890. if (!amax) {
  1891. y[i].d = 0;
  1892. memset(y[i].qs, 0, QK_K);
  1893. x += QK_K;
  1894. continue;
  1895. }
  1896. const float iscale = -128.f/max;
  1897. for (int j = 0; j < QK_K; ++j) {
  1898. int v = nearest_int(iscale*x[j]);
  1899. y[i].qs[j] = MIN(127, v);
  1900. }
  1901. for (int j = 0; j < QK_K/16; ++j) {
  1902. int sum = 0;
  1903. for (int ii = 0; ii < 16; ++ii) {
  1904. sum += y[i].qs[j*16 + ii];
  1905. }
  1906. y[i].bsums[j] = sum;
  1907. }
  1908. y[i].d = 1/iscale;
  1909. x += QK_K;
  1910. }
  1911. }
  1912. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  1913. assert(k % QK_K == 0);
  1914. const int nb = k / QK_K;
  1915. for (int i = 0; i < nb; i++) {
  1916. for (int j = 0; j < QK_K; ++j) {
  1917. *y++ = x[i].d * x[i].qs[j];
  1918. }
  1919. }
  1920. }
  1921. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  1922. quantize_row_q8_K_reference(x, y, k);
  1923. }
  1924. //===================================== Dot ptoducts =================================
  1925. //
  1926. // Helper functions
  1927. //
  1928. #if __AVX__ || __AVX2__ || __AVX512F__
  1929. // shuffles to pick the required scales in dot products
  1930. static inline __m256i get_scale_shuffle_q3k(int i) {
  1931. static const uint8_t k_shuffle[128] = {
  1932. 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,
  1933. 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,
  1934. 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,
  1935. 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,
  1936. };
  1937. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  1938. }
  1939. static inline __m256i get_scale_shuffle_k4(int i) {
  1940. static const uint8_t k_shuffle[256] = {
  1941. 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,
  1942. 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,
  1943. 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,
  1944. 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,
  1945. 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,
  1946. 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,
  1947. 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,
  1948. 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
  1949. };
  1950. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  1951. }
  1952. static inline __m128i get_scale_shuffle(int i) {
  1953. static const uint8_t k_shuffle[128] = {
  1954. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  1955. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  1956. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  1957. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  1958. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  1959. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  1960. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  1961. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  1962. };
  1963. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  1964. }
  1965. #endif
  1966. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  1967. const int qk = QK8_0;
  1968. const int nb = n / qk;
  1969. assert(n % qk == 0);
  1970. const block_q4_0 * restrict x = vx;
  1971. const block_q8_0 * restrict y = vy;
  1972. #if defined(__ARM_NEON)
  1973. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  1974. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  1975. assert(nb % 2 == 0); // TODO: handle odd nb
  1976. for (int i = 0; i < nb; i += 2) {
  1977. const block_q4_0 * restrict x0 = &x[i + 0];
  1978. const block_q4_0 * restrict x1 = &x[i + 1];
  1979. const block_q8_0 * restrict y0 = &y[i + 0];
  1980. const block_q8_0 * restrict y1 = &y[i + 1];
  1981. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  1982. const int8x16_t s8b = vdupq_n_s8(0x8);
  1983. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  1984. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  1985. // 4-bit -> 8-bit
  1986. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  1987. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  1988. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  1989. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  1990. // sub 8
  1991. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  1992. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  1993. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  1994. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  1995. // load y
  1996. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  1997. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  1998. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  1999. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2000. #if defined(__ARM_FEATURE_DOTPROD)
  2001. // dot product into int32x4_t
  2002. const int32x4_t p_0 = vdotq_s32(vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  2003. const int32x4_t p_1 = vdotq_s32(vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  2004. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2005. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2006. #else
  2007. const int16x8_t pl0l = vmull_s8(vget_low_s8 (v0_0ls), vget_low_s8 (v1_0l));
  2008. const int16x8_t pl0h = vmull_s8(vget_high_s8(v0_0ls), vget_high_s8(v1_0l));
  2009. const int16x8_t ph0l = vmull_s8(vget_low_s8 (v0_0hs), vget_low_s8 (v1_0h));
  2010. const int16x8_t ph0h = vmull_s8(vget_high_s8(v0_0hs), vget_high_s8(v1_0h));
  2011. const int16x8_t pl1l = vmull_s8(vget_low_s8 (v0_1ls), vget_low_s8 (v1_1l));
  2012. const int16x8_t pl1h = vmull_s8(vget_high_s8(v0_1ls), vget_high_s8(v1_1l));
  2013. const int16x8_t ph1l = vmull_s8(vget_low_s8 (v0_1hs), vget_low_s8 (v1_1h));
  2014. const int16x8_t ph1h = vmull_s8(vget_high_s8(v0_1hs), vget_high_s8(v1_1h));
  2015. const int32x4_t pl0 = vaddq_s32(vpaddlq_s16(pl0l), vpaddlq_s16(pl0h));
  2016. const int32x4_t ph0 = vaddq_s32(vpaddlq_s16(ph0l), vpaddlq_s16(ph0h));
  2017. const int32x4_t pl1 = vaddq_s32(vpaddlq_s16(pl1l), vpaddlq_s16(pl1h));
  2018. const int32x4_t ph1 = vaddq_s32(vpaddlq_s16(ph1l), vpaddlq_s16(ph1h));
  2019. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(pl0, ph0)), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2020. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(pl1, ph1)), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2021. #endif
  2022. }
  2023. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2024. #elif defined(__AVX2__)
  2025. // Initialize accumulator with zeros
  2026. __m256 acc = _mm256_setzero_ps();
  2027. // Main loop
  2028. for (int i = 0; i < nb; ++i) {
  2029. /* Compute combined scale for the block */
  2030. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2031. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2032. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  2033. const __m256i off = _mm256_set1_epi8( 8 );
  2034. bx = _mm256_sub_epi8( bx, off );
  2035. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2036. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2037. /* Multiply q with scale and accumulate */
  2038. acc = _mm256_fmadd_ps( d, q, acc );
  2039. }
  2040. *s = hsum_float_8(acc);
  2041. #elif defined(__AVX__)
  2042. // Initialize accumulator with zeros
  2043. __m256 acc = _mm256_setzero_ps();
  2044. // Main loop
  2045. for (int i = 0; i < nb; ++i) {
  2046. // Compute combined scale for the block
  2047. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2048. const __m128i lowMask = _mm_set1_epi8(0xF);
  2049. const __m128i off = _mm_set1_epi8(8);
  2050. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  2051. __m128i bx = _mm_and_si128(lowMask, tmp);
  2052. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  2053. bx = _mm_sub_epi8(bx, off);
  2054. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  2055. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  2056. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2057. bx = _mm_sub_epi8(bx, off);
  2058. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  2059. // Convert int32_t to float
  2060. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  2061. // Apply the scale, and accumulate
  2062. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  2063. }
  2064. *s = hsum_float_8(acc);
  2065. #elif defined(__SSSE3__)
  2066. // set constants
  2067. const __m128i lowMask = _mm_set1_epi8(0xF);
  2068. const __m128i off = _mm_set1_epi8(8);
  2069. // Initialize accumulator with zeros
  2070. __m128 acc_0 = _mm_setzero_ps();
  2071. __m128 acc_1 = _mm_setzero_ps();
  2072. __m128 acc_2 = _mm_setzero_ps();
  2073. __m128 acc_3 = _mm_setzero_ps();
  2074. // First round without accumulation
  2075. {
  2076. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  2077. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  2078. // Compute combined scale for the block 0 and 1
  2079. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  2080. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  2081. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2082. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  2083. bx_0 = _mm_sub_epi8(bx_0, off);
  2084. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2085. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2086. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  2087. bx_1 = _mm_sub_epi8(bx_1, off);
  2088. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2089. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  2090. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  2091. // Compute combined scale for the block 2 and 3
  2092. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  2093. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  2094. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2095. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  2096. bx_2 = _mm_sub_epi8(bx_2, off);
  2097. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2098. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2099. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  2100. bx_3 = _mm_sub_epi8(bx_3, off);
  2101. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2102. // Convert int32_t to float
  2103. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2104. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2105. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2106. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2107. // Apply the scale
  2108. acc_0 = _mm_mul_ps( d_0_1, p0 );
  2109. acc_1 = _mm_mul_ps( d_0_1, p1 );
  2110. acc_2 = _mm_mul_ps( d_2_3, p2 );
  2111. acc_3 = _mm_mul_ps( d_2_3, p3 );
  2112. }
  2113. assert(nb % 2 == 0); // TODO: handle odd nb
  2114. // Main loop
  2115. for (int i = 2; i < nb; i+=2) {
  2116. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  2117. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  2118. // Compute combined scale for the block 0 and 1
  2119. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2120. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  2121. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2122. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  2123. bx_0 = _mm_sub_epi8(bx_0, off);
  2124. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2125. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2126. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2127. bx_1 = _mm_sub_epi8(bx_1, off);
  2128. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2129. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  2130. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  2131. // Compute combined scale for the block 2 and 3
  2132. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  2133. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  2134. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2135. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  2136. bx_2 = _mm_sub_epi8(bx_2, off);
  2137. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2138. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2139. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  2140. bx_3 = _mm_sub_epi8(bx_3, off);
  2141. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2142. // Convert int32_t to float
  2143. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2144. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2145. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2146. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2147. // Apply the scale
  2148. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  2149. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  2150. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  2151. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  2152. // Acummulate
  2153. acc_0 = _mm_add_ps(p0_d, acc_0);
  2154. acc_1 = _mm_add_ps(p1_d, acc_1);
  2155. acc_2 = _mm_add_ps(p2_d, acc_2);
  2156. acc_3 = _mm_add_ps(p3_d, acc_3);
  2157. }
  2158. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  2159. #elif defined(__riscv_v_intrinsic)
  2160. float sumf = 0.0;
  2161. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2162. for (int i = 0; i < nb; i++) {
  2163. // load elements
  2164. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2165. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2166. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2167. // mask and store lower part of x, and then upper part
  2168. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2169. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2170. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2171. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2172. // subtract offset
  2173. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  2174. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  2175. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2176. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2177. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2178. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2179. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2180. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2181. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2182. }
  2183. *s = sumf;
  2184. #else
  2185. // scalar
  2186. float sumf = 0.0;
  2187. for (int i = 0; i < nb; i++) {
  2188. int sumi = 0;
  2189. for (int j = 0; j < qk/2; ++j) {
  2190. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  2191. const int v1 = (x[i].qs[j] >> 4) - 8;
  2192. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2193. }
  2194. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2195. }
  2196. *s = sumf;
  2197. #endif
  2198. }
  2199. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2200. const int qk = QK8_1;
  2201. const int nb = n / qk;
  2202. assert(n % qk == 0);
  2203. const block_q4_1 * restrict x = vx;
  2204. const block_q8_1 * restrict y = vy;
  2205. // TODO: add WASM SIMD
  2206. #if defined(__ARM_NEON)
  2207. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2208. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2209. float summs = 0;
  2210. assert(nb % 2 == 0); // TODO: handle odd nb
  2211. for (int i = 0; i < nb; i += 2) {
  2212. const block_q4_1 * restrict x0 = &x[i + 0];
  2213. const block_q4_1 * restrict x1 = &x[i + 1];
  2214. const block_q8_1 * restrict y0 = &y[i + 0];
  2215. const block_q8_1 * restrict y1 = &y[i + 1];
  2216. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  2217. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2218. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2219. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2220. // 4-bit -> 8-bit
  2221. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2222. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2223. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2224. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2225. // load y
  2226. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2227. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2228. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2229. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2230. #if defined(__ARM_FEATURE_DOTPROD)
  2231. // dot product into int32x4_t
  2232. const int32x4_t p_0 = vdotq_s32(vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  2233. const int32x4_t p_1 = vdotq_s32(vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  2234. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2235. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2236. #else
  2237. const int16x8_t pl0l = vmull_s8(vget_low_s8 (v0_0l), vget_low_s8 (v1_0l));
  2238. const int16x8_t pl0h = vmull_s8(vget_high_s8(v0_0l), vget_high_s8(v1_0l));
  2239. const int16x8_t ph0l = vmull_s8(vget_low_s8 (v0_0h), vget_low_s8 (v1_0h));
  2240. const int16x8_t ph0h = vmull_s8(vget_high_s8(v0_0h), vget_high_s8(v1_0h));
  2241. const int16x8_t pl1l = vmull_s8(vget_low_s8 (v0_1l), vget_low_s8 (v1_1l));
  2242. const int16x8_t pl1h = vmull_s8(vget_high_s8(v0_1l), vget_high_s8(v1_1l));
  2243. const int16x8_t ph1l = vmull_s8(vget_low_s8 (v0_1h), vget_low_s8 (v1_1h));
  2244. const int16x8_t ph1h = vmull_s8(vget_high_s8(v0_1h), vget_high_s8(v1_1h));
  2245. const int32x4_t pl0 = vaddq_s32(vpaddlq_s16(pl0l), vpaddlq_s16(pl0h));
  2246. const int32x4_t ph0 = vaddq_s32(vpaddlq_s16(ph0l), vpaddlq_s16(ph0h));
  2247. const int32x4_t pl1 = vaddq_s32(vpaddlq_s16(pl1l), vpaddlq_s16(pl1h));
  2248. const int32x4_t ph1 = vaddq_s32(vpaddlq_s16(ph1l), vpaddlq_s16(ph1h));
  2249. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(pl0, ph0)), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2250. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(pl1, ph1)), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2251. #endif
  2252. }
  2253. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  2254. #elif defined(__AVX2__) || defined(__AVX__)
  2255. // Initialize accumulator with zeros
  2256. __m256 acc = _mm256_setzero_ps();
  2257. float summs = 0;
  2258. // Main loop
  2259. for (int i = 0; i < nb; ++i) {
  2260. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  2261. const float d1 = y[i].d;
  2262. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2263. const __m256 d0v = _mm256_set1_ps( d0 );
  2264. const __m256 d1v = _mm256_set1_ps( d1 );
  2265. // Compute combined scales
  2266. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  2267. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  2268. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2269. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  2270. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  2271. // Accumulate d0*d1*x*y
  2272. #if defined(__AVX2__)
  2273. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  2274. #else
  2275. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  2276. #endif
  2277. }
  2278. *s = hsum_float_8(acc) + summs;
  2279. #elif defined(__riscv_v_intrinsic)
  2280. float sumf = 0.0;
  2281. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2282. for (int i = 0; i < nb; i++) {
  2283. // load elements
  2284. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2285. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2286. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2287. // mask and store lower part of x, and then upper part
  2288. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2289. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2290. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2291. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2292. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2293. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2294. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2295. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2296. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2297. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2298. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2299. }
  2300. *s = sumf;
  2301. #else
  2302. // scalar
  2303. float sumf = 0.0;
  2304. for (int i = 0; i < nb; i++) {
  2305. int sumi = 0;
  2306. for (int j = 0; j < qk/2; ++j) {
  2307. const int v0 = (x[i].qs[j] & 0x0F);
  2308. const int v1 = (x[i].qs[j] >> 4);
  2309. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2310. }
  2311. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2312. }
  2313. *s = sumf;
  2314. #endif
  2315. }
  2316. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2317. const int qk = QK8_0;
  2318. const int nb = n / qk;
  2319. assert(n % qk == 0);
  2320. assert(qk == QK5_0);
  2321. const block_q5_0 * restrict x = vx;
  2322. const block_q8_0 * restrict y = vy;
  2323. #if defined(__ARM_NEON)
  2324. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2325. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2326. uint32_t qh0;
  2327. uint32_t qh1;
  2328. uint64_t tmp0[4];
  2329. uint64_t tmp1[4];
  2330. assert(nb % 2 == 0); // TODO: handle odd nb
  2331. for (int i = 0; i < nb; i += 2) {
  2332. const block_q5_0 * restrict x0 = &x[i];
  2333. const block_q5_0 * restrict x1 = &x[i + 1];
  2334. const block_q8_0 * restrict y0 = &y[i];
  2335. const block_q8_0 * restrict y1 = &y[i + 1];
  2336. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2337. // extract the 5th bit via lookup table ((!b) << 4)
  2338. memcpy(&qh0, x0->qh, sizeof(qh0));
  2339. memcpy(&qh1, x1->qh, sizeof(qh1));
  2340. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  2341. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  2342. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  2343. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  2344. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  2345. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  2346. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  2347. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  2348. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2349. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2350. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2351. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2352. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2353. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2354. // 4-bit -> 8-bit
  2355. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2356. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2357. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2358. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2359. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2360. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  2361. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  2362. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  2363. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  2364. // load y
  2365. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2366. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2367. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2368. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2369. #if defined(__ARM_FEATURE_DOTPROD)
  2370. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2371. vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2372. vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2373. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2374. vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2375. vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2376. #else
  2377. const int16x8_t pl0l = vmull_s8(vget_low_s8 (v0_0lf), vget_low_s8 (v1_0l));
  2378. const int16x8_t pl0h = vmull_s8(vget_high_s8(v0_0lf), vget_high_s8(v1_0l));
  2379. const int16x8_t ph0l = vmull_s8(vget_low_s8 (v0_0hf), vget_low_s8 (v1_0h));
  2380. const int16x8_t ph0h = vmull_s8(vget_high_s8(v0_0hf), vget_high_s8(v1_0h));
  2381. const int16x8_t pl1l = vmull_s8(vget_low_s8 (v0_1lf), vget_low_s8 (v1_1l));
  2382. const int16x8_t pl1h = vmull_s8(vget_high_s8(v0_1lf), vget_high_s8(v1_1l));
  2383. const int16x8_t ph1l = vmull_s8(vget_low_s8 (v0_1hf), vget_low_s8 (v1_1h));
  2384. const int16x8_t ph1h = vmull_s8(vget_high_s8(v0_1hf), vget_high_s8(v1_1h));
  2385. const int32x4_t pl0 = vaddq_s32(vpaddlq_s16(pl0l), vpaddlq_s16(pl0h));
  2386. const int32x4_t ph0 = vaddq_s32(vpaddlq_s16(ph0l), vpaddlq_s16(ph0h));
  2387. const int32x4_t pl1 = vaddq_s32(vpaddlq_s16(pl1l), vpaddlq_s16(pl1h));
  2388. const int32x4_t ph1 = vaddq_s32(vpaddlq_s16(ph1l), vpaddlq_s16(ph1h));
  2389. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(pl0, ph0)), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2390. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(pl1, ph1)), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2391. #endif
  2392. }
  2393. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2394. #elif defined(__wasm_simd128__)
  2395. v128_t sumv = wasm_f32x4_splat(0.0f);
  2396. uint32_t qh;
  2397. uint64_t tmp[4];
  2398. // TODO: check if unrolling this is better
  2399. for (int i = 0; i < nb; ++i) {
  2400. const block_q5_0 * restrict x0 = &x[i];
  2401. const block_q8_0 * restrict y0 = &y[i];
  2402. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2403. // extract the 5th bit
  2404. memcpy(&qh, x0->qh, sizeof(qh));
  2405. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  2406. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  2407. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  2408. tmp[3] = table_b2b_1[(qh >> 24) ];
  2409. const v128_t qhl = wasm_v128_load(tmp + 0);
  2410. const v128_t qhh = wasm_v128_load(tmp + 2);
  2411. const v128_t v0 = wasm_v128_load(x0->qs);
  2412. // 4-bit -> 8-bit
  2413. const v128_t v0l = wasm_v128_and (v0, m4b);
  2414. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2415. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2416. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  2417. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  2418. // load y
  2419. const v128_t v1l = wasm_v128_load(y0->qs);
  2420. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2421. // int8x16 -> int16x8
  2422. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2423. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2424. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2425. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2426. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2427. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2428. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2429. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2430. // dot product
  2431. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  2432. wasm_i32x4_add(
  2433. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2434. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2435. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2436. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2437. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  2438. }
  2439. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2440. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  2441. #elif defined(__AVX2__)
  2442. // Initialize accumulator with zeros
  2443. __m256 acc = _mm256_setzero_ps();
  2444. // Main loop
  2445. for (int i = 0; i < nb; i++) {
  2446. /* Compute combined scale for the block */
  2447. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2448. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2449. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2450. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  2451. bx = _mm256_or_si256(bx, bxhi);
  2452. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2453. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2454. /* Multiply q with scale and accumulate */
  2455. acc = _mm256_fmadd_ps(d, q, acc);
  2456. }
  2457. *s = hsum_float_8(acc);
  2458. #elif defined(__AVX__)
  2459. // Initialize accumulator with zeros
  2460. __m256 acc = _mm256_setzero_ps();
  2461. __m128i mask = _mm_set1_epi8((char)0xF0);
  2462. // Main loop
  2463. for (int i = 0; i < nb; i++) {
  2464. /* Compute combined scale for the block */
  2465. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2466. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2467. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2468. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  2469. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  2470. bxhil = _mm_andnot_si128(bxhil, mask);
  2471. bxhih = _mm_andnot_si128(bxhih, mask);
  2472. __m128i bxl = _mm256_castsi256_si128(bx);
  2473. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  2474. bxl = _mm_or_si128(bxl, bxhil);
  2475. bxh = _mm_or_si128(bxh, bxhih);
  2476. bx = MM256_SET_M128I(bxh, bxl);
  2477. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2478. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2479. /* Multiply q with scale and accumulate */
  2480. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  2481. }
  2482. *s = hsum_float_8(acc);
  2483. #elif defined(__riscv_v_intrinsic)
  2484. float sumf = 0.0;
  2485. uint32_t qh;
  2486. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2487. // These tempory registers are for masking and shift operations
  2488. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  2489. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  2490. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  2491. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  2492. for (int i = 0; i < nb; i++) {
  2493. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  2494. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2495. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  2496. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  2497. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  2498. // ((qh & (1u << (j + 16))) >> (j + 12));
  2499. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  2500. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  2501. // narrowing
  2502. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  2503. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  2504. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  2505. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  2506. // load
  2507. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2508. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2509. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2510. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2511. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2512. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  2513. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  2514. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2515. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2516. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  2517. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  2518. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2519. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2520. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2521. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2522. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2523. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2524. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2525. }
  2526. *s = sumf;
  2527. #else
  2528. // scalar
  2529. float sumf = 0.0;
  2530. for (int i = 0; i < nb; i++) {
  2531. uint32_t qh;
  2532. memcpy(&qh, x[i].qh, sizeof(qh));
  2533. int sumi = 0;
  2534. for (int j = 0; j < qk/2; ++j) {
  2535. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2536. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  2537. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  2538. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  2539. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  2540. }
  2541. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2542. }
  2543. *s = sumf;
  2544. #endif
  2545. }
  2546. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2547. const int qk = QK8_1;
  2548. const int nb = n / qk;
  2549. assert(n % qk == 0);
  2550. assert(qk == QK5_1);
  2551. const block_q5_1 * restrict x = vx;
  2552. const block_q8_1 * restrict y = vy;
  2553. #if defined(__ARM_NEON)
  2554. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2555. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2556. float summs0 = 0.0f;
  2557. float summs1 = 0.0f;
  2558. uint32_t qh0;
  2559. uint32_t qh1;
  2560. uint64_t tmp0[4];
  2561. uint64_t tmp1[4];
  2562. assert(nb % 2 == 0); // TODO: handle odd nb
  2563. for (int i = 0; i < nb; i += 2) {
  2564. const block_q5_1 * restrict x0 = &x[i];
  2565. const block_q5_1 * restrict x1 = &x[i + 1];
  2566. const block_q8_1 * restrict y0 = &y[i];
  2567. const block_q8_1 * restrict y1 = &y[i + 1];
  2568. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2569. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2570. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  2571. // extract the 5th bit via lookup table ((b) << 4)
  2572. memcpy(&qh0, x0->qh, sizeof(qh0));
  2573. memcpy(&qh1, x1->qh, sizeof(qh1));
  2574. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  2575. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  2576. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  2577. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  2578. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  2579. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  2580. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  2581. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  2582. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2583. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2584. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2585. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2586. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2587. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2588. // 4-bit -> 8-bit
  2589. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2590. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2591. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2592. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2593. // add high bit
  2594. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  2595. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  2596. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  2597. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  2598. // load y
  2599. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2600. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2601. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2602. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2603. #if defined(__ARM_FEATURE_DOTPROD)
  2604. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2605. vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2606. vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2607. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2608. vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2609. vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2610. #else
  2611. const int16x8_t pl0l = vmull_s8(vget_low_s8 (v0_0lf), vget_low_s8 (v1_0l));
  2612. const int16x8_t pl0h = vmull_s8(vget_high_s8(v0_0lf), vget_high_s8(v1_0l));
  2613. const int16x8_t ph0l = vmull_s8(vget_low_s8 (v0_0hf), vget_low_s8 (v1_0h));
  2614. const int16x8_t ph0h = vmull_s8(vget_high_s8(v0_0hf), vget_high_s8(v1_0h));
  2615. const int16x8_t pl1l = vmull_s8(vget_low_s8 (v0_1lf), vget_low_s8 (v1_1l));
  2616. const int16x8_t pl1h = vmull_s8(vget_high_s8(v0_1lf), vget_high_s8(v1_1l));
  2617. const int16x8_t ph1l = vmull_s8(vget_low_s8 (v0_1hf), vget_low_s8 (v1_1h));
  2618. const int16x8_t ph1h = vmull_s8(vget_high_s8(v0_1hf), vget_high_s8(v1_1h));
  2619. const int32x4_t pl0 = vaddq_s32(vpaddlq_s16(pl0l), vpaddlq_s16(pl0h));
  2620. const int32x4_t ph0 = vaddq_s32(vpaddlq_s16(ph0l), vpaddlq_s16(ph0h));
  2621. const int32x4_t pl1 = vaddq_s32(vpaddlq_s16(pl1l), vpaddlq_s16(pl1h));
  2622. const int32x4_t ph1 = vaddq_s32(vpaddlq_s16(ph1l), vpaddlq_s16(ph1h));
  2623. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(pl0, ph0)), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2624. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(pl1, ph1)), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2625. #endif
  2626. }
  2627. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  2628. #elif defined(__wasm_simd128__)
  2629. v128_t sumv = wasm_f32x4_splat(0.0f);
  2630. float summs = 0.0f;
  2631. uint32_t qh;
  2632. uint64_t tmp[4];
  2633. // TODO: check if unrolling this is better
  2634. for (int i = 0; i < nb; ++i) {
  2635. const block_q5_1 * restrict x0 = &x[i];
  2636. const block_q8_1 * restrict y0 = &y[i];
  2637. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2638. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2639. // extract the 5th bit
  2640. memcpy(&qh, x0->qh, sizeof(qh));
  2641. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  2642. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  2643. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  2644. tmp[3] = table_b2b_0[(qh >> 24) ];
  2645. const v128_t qhl = wasm_v128_load(tmp + 0);
  2646. const v128_t qhh = wasm_v128_load(tmp + 2);
  2647. const v128_t v0 = wasm_v128_load(x0->qs);
  2648. // 4-bit -> 8-bit
  2649. const v128_t v0l = wasm_v128_and (v0, m4b);
  2650. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2651. // add high bit
  2652. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  2653. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  2654. // load y
  2655. const v128_t v1l = wasm_v128_load(y0->qs);
  2656. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2657. // int8x16 -> int16x8
  2658. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2659. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2660. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2661. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2662. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2663. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2664. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2665. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2666. // dot product
  2667. sumv = wasm_f32x4_add(sumv,
  2668. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  2669. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2670. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2671. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2672. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2673. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  2674. }
  2675. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2676. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  2677. #elif defined(__AVX2__)
  2678. // Initialize accumulator with zeros
  2679. __m256 acc = _mm256_setzero_ps();
  2680. float summs = 0.0f;
  2681. // Main loop
  2682. for (int i = 0; i < nb; i++) {
  2683. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  2684. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2685. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2686. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2687. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  2688. bx = _mm256_or_si256(bx, bxhi);
  2689. const __m256 dy = _mm256_set1_ps(y[i].d);
  2690. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2691. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  2692. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  2693. }
  2694. *s = hsum_float_8(acc) + summs;
  2695. #elif defined(__AVX__)
  2696. // Initialize accumulator with zeros
  2697. __m256 acc = _mm256_setzero_ps();
  2698. __m128i mask = _mm_set1_epi8(0x10);
  2699. float summs = 0.0f;
  2700. // Main loop
  2701. for (int i = 0; i < nb; i++) {
  2702. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  2703. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2704. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2705. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2706. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  2707. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  2708. bxhil = _mm_and_si128(bxhil, mask);
  2709. bxhih = _mm_and_si128(bxhih, mask);
  2710. __m128i bxl = _mm256_castsi256_si128(bx);
  2711. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  2712. bxl = _mm_or_si128(bxl, bxhil);
  2713. bxh = _mm_or_si128(bxh, bxhih);
  2714. bx = MM256_SET_M128I(bxh, bxl);
  2715. const __m256 dy = _mm256_set1_ps(y[i].d);
  2716. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2717. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  2718. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  2719. }
  2720. *s = hsum_float_8(acc) + summs;
  2721. #elif defined(__riscv_v_intrinsic)
  2722. float sumf = 0.0;
  2723. uint32_t qh;
  2724. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2725. // temporary registers for shift operations
  2726. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  2727. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  2728. for (int i = 0; i < nb; i++) {
  2729. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  2730. // load qh
  2731. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  2732. // ((qh >> (j + 0)) << 4) & 0x10;
  2733. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  2734. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  2735. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  2736. // ((qh >> (j + 12)) ) & 0x10;
  2737. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  2738. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  2739. // narrowing
  2740. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  2741. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  2742. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  2743. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  2744. // load
  2745. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2746. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2747. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2748. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2749. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2750. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  2751. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  2752. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2753. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2754. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2755. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2756. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2757. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2758. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2759. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2760. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2761. }
  2762. *s = sumf;
  2763. #else
  2764. // scalar
  2765. float sumf = 0.0;
  2766. for (int i = 0; i < nb; i++) {
  2767. uint32_t qh;
  2768. memcpy(&qh, x[i].qh, sizeof(qh));
  2769. int sumi = 0;
  2770. for (int j = 0; j < qk/2; ++j) {
  2771. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  2772. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  2773. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  2774. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  2775. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  2776. }
  2777. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2778. }
  2779. *s = sumf;
  2780. #endif
  2781. }
  2782. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2783. const int qk = QK8_0;
  2784. const int nb = n / qk;
  2785. assert(n % qk == 0);
  2786. const block_q8_0 * restrict x = vx;
  2787. const block_q8_0 * restrict y = vy;
  2788. #if defined(__ARM_NEON)
  2789. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2790. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2791. assert(nb % 2 == 0); // TODO: handle odd nb
  2792. for (int i = 0; i < nb; i += 2) {
  2793. const block_q8_0 * restrict x0 = &x[i + 0];
  2794. const block_q8_0 * restrict x1 = &x[i + 1];
  2795. const block_q8_0 * restrict y0 = &y[i + 0];
  2796. const block_q8_0 * restrict y1 = &y[i + 1];
  2797. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  2798. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  2799. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  2800. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  2801. // load y
  2802. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  2803. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  2804. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  2805. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  2806. #if defined(__ARM_FEATURE_DOTPROD)
  2807. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2808. vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  2809. vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2810. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2811. vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  2812. vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2813. #else
  2814. const int16x8_t p0_0 = vmull_s8(vget_low_s8 (x0_0), vget_low_s8 (y0_0));
  2815. const int16x8_t p0_1 = vmull_s8(vget_high_s8(x0_0), vget_high_s8(y0_0));
  2816. const int16x8_t p0_2 = vmull_s8(vget_low_s8 (x0_1), vget_low_s8 (y0_1));
  2817. const int16x8_t p0_3 = vmull_s8(vget_high_s8(x0_1), vget_high_s8(y0_1));
  2818. const int16x8_t p1_0 = vmull_s8(vget_low_s8 (x1_0), vget_low_s8 (y1_0));
  2819. const int16x8_t p1_1 = vmull_s8(vget_high_s8(x1_0), vget_high_s8(y1_0));
  2820. const int16x8_t p1_2 = vmull_s8(vget_low_s8 (x1_1), vget_low_s8 (y1_1));
  2821. const int16x8_t p1_3 = vmull_s8(vget_high_s8(x1_1), vget_high_s8(y1_1));
  2822. const int32x4_t p0 = vaddq_s32(vpaddlq_s16(p0_0), vpaddlq_s16(p0_1));
  2823. const int32x4_t p1 = vaddq_s32(vpaddlq_s16(p0_2), vpaddlq_s16(p0_3));
  2824. const int32x4_t p2 = vaddq_s32(vpaddlq_s16(p1_0), vpaddlq_s16(p1_1));
  2825. const int32x4_t p3 = vaddq_s32(vpaddlq_s16(p1_2), vpaddlq_s16(p1_3));
  2826. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(p0, p1)), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2827. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(p2, p3)), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2828. #endif
  2829. }
  2830. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2831. #elif defined(__AVX2__) || defined(__AVX__)
  2832. // Initialize accumulator with zeros
  2833. __m256 acc = _mm256_setzero_ps();
  2834. // Main loop
  2835. for (int i = 0; i < nb; ++i) {
  2836. // Compute combined scale for the block
  2837. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2838. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  2839. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2840. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2841. // Multiply q with scale and accumulate
  2842. #if defined(__AVX2__)
  2843. acc = _mm256_fmadd_ps( d, q, acc );
  2844. #else
  2845. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  2846. #endif
  2847. }
  2848. *s = hsum_float_8(acc);
  2849. #elif defined(__riscv_v_intrinsic)
  2850. float sumf = 0.0;
  2851. size_t vl = __riscv_vsetvl_e8m1(qk);
  2852. for (int i = 0; i < nb; i++) {
  2853. // load elements
  2854. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  2855. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  2856. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  2857. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2858. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  2859. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  2860. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  2861. }
  2862. *s = sumf;
  2863. #else
  2864. // scalar
  2865. float sumf = 0.0;
  2866. for (int i = 0; i < nb; i++) {
  2867. int sumi = 0;
  2868. for (int j = 0; j < qk; j++) {
  2869. sumi += x[i].qs[j]*y[i].qs[j];
  2870. }
  2871. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  2872. }
  2873. *s = sumf;
  2874. #endif
  2875. }
  2876. #if QK_K == 256
  2877. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2878. const block_q2_K * restrict x = vx;
  2879. const block_q8_K * restrict y = vy;
  2880. const int nb = n / QK_K;
  2881. #ifdef __ARM_NEON
  2882. const uint8x16_t m3 = vdupq_n_u8(0x3);
  2883. const uint8x16_t m4 = vdupq_n_u8(0xF);
  2884. #if defined(__ARM_FEATURE_DOTPROD)
  2885. const int32x4_t vzero = vdupq_n_s32(0);
  2886. #endif
  2887. int8x16x2_t q2bytes;
  2888. uint8_t aux[16];
  2889. float sum = 0;
  2890. for (int i = 0; i < nb; ++i) {
  2891. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2892. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2893. const uint8_t * restrict q2 = x[i].qs;
  2894. const int8_t * restrict q8 = y[i].qs;
  2895. const uint8_t * restrict sc = x[i].scales;
  2896. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  2897. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  2898. vst1q_u8(aux, scales);
  2899. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  2900. const int16x8x2_t q8sums = vld1q_s16_x2(y[i].bsums);
  2901. const int16x8x2_t mins16 = {vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))};
  2902. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  2903. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  2904. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  2905. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  2906. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  2907. int isum = 0;
  2908. int is = 0;
  2909. // We use this macro instead of a function call because for some reason
  2910. // the code runs 2-3% slower, even if the function is declared inline
  2911. #if defined(__ARM_FEATURE_DOTPROD)
  2912. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  2913. isum += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  2914. isum += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  2915. #else
  2916. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  2917. {\
  2918. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[0]), vget_low_s8 (q8bytes.val[0])),\
  2919. vmull_s8(vget_high_s8(q2bytes.val[0]), vget_high_s8(q8bytes.val[0])));\
  2920. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[1]), vget_low_s8 (q8bytes.val[1])),\
  2921. vmull_s8(vget_high_s8(q2bytes.val[1]), vget_high_s8(q8bytes.val[1])));\
  2922. isum += vaddvq_s16(p1) * aux[is+(index)] + vaddvq_s16(p2) * aux[is+1+(index)];\
  2923. }
  2924. #endif
  2925. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  2926. q8bytes = vld1q_s8_x2(q8); q8 += 32;\
  2927. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  2928. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  2929. MULTIPLY_ACCUM_WITH_SCALE((index));
  2930. for (int j = 0; j < QK_K/128; ++j) {
  2931. const uint8x16x2_t q2bits = vld1q_u8_x2(q2); q2 += 32;
  2932. int8x16x2_t q8bytes = vld1q_s8_x2(q8); q8 += 32;
  2933. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  2934. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  2935. MULTIPLY_ACCUM_WITH_SCALE(0);
  2936. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  2937. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  2938. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  2939. is += 8;
  2940. }
  2941. sum += d * isum;
  2942. }
  2943. *s = sum;
  2944. #elif defined __AVX2__
  2945. const __m256i m3 = _mm256_set1_epi8(3);
  2946. const __m128i m4 = _mm_set1_epi8(0xF);
  2947. __m256 acc = _mm256_setzero_ps();
  2948. for (int i = 0; i < nb; ++i) {
  2949. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2950. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2951. const uint8_t * restrict q2 = x[i].qs;
  2952. const int8_t * restrict q8 = y[i].qs;
  2953. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  2954. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  2955. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  2956. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  2957. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  2958. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  2959. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  2960. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  2961. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  2962. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  2963. __m256i sumi = _mm256_setzero_si256();
  2964. for (int j = 0; j < QK_K/128; ++j) {
  2965. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  2966. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2967. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2968. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2969. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2970. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  2971. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  2972. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  2973. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  2974. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  2975. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  2976. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  2977. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  2978. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  2979. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  2980. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  2981. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  2982. p0 = _mm256_add_epi32(p0, p1);
  2983. p2 = _mm256_add_epi32(p2, p3);
  2984. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  2985. }
  2986. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  2987. }
  2988. *s = hsum_float_8(acc);
  2989. #elif defined __AVX__
  2990. const __m128i m3 = _mm_set1_epi8(0x3);
  2991. const __m128i m4 = _mm_set1_epi8(0xF);
  2992. const __m128i m2 = _mm_set1_epi8(0x2);
  2993. __m256 acc = _mm256_setzero_ps();
  2994. for (int i = 0; i < nb; ++i) {
  2995. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2996. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2997. const uint8_t * restrict q2 = x[i].qs;
  2998. const int8_t * restrict q8 = y[i].qs;
  2999. // load mins and scales from block_q2_K.scales[QK_K/16]
  3000. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3001. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  3002. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3003. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  3004. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  3005. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  3006. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  3007. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  3008. // sumf += -dmin * summs in 32bits*8
  3009. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  3010. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  3011. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  3012. const __m128i scales[2] = { scales_0, scales_1 };
  3013. __m128i sumi_0 = _mm_setzero_si128();
  3014. __m128i sumi_1 = _mm_setzero_si128();
  3015. for (int j = 0; j < QK_K/128; ++j) {
  3016. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  3017. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3018. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3019. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3020. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3021. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3022. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3023. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3024. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3025. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  3026. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3027. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3028. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3029. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3030. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3031. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3032. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  3033. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3034. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3035. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3036. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  3037. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  3038. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  3039. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  3040. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  3041. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  3042. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  3043. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  3044. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  3045. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  3046. __m128i shuffle = _mm_set1_epi16(0x0100);
  3047. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  3048. shuffle = _mm_add_epi16(shuffle, m2);
  3049. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  3050. shuffle = _mm_add_epi16(shuffle, m2);
  3051. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  3052. shuffle = _mm_add_epi16(shuffle, m2);
  3053. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  3054. shuffle = _mm_add_epi16(shuffle, m2);
  3055. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  3056. shuffle = _mm_add_epi16(shuffle, m2);
  3057. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  3058. shuffle = _mm_add_epi16(shuffle, m2);
  3059. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  3060. shuffle = _mm_add_epi16(shuffle, m2);
  3061. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  3062. p0 = _mm_add_epi32(p0, p1);
  3063. p2 = _mm_add_epi32(p2, p3);
  3064. p4 = _mm_add_epi32(p4, p5);
  3065. p6 = _mm_add_epi32(p6, p7);
  3066. // isum in 32bits*4*2
  3067. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  3068. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  3069. }
  3070. // sumf += dall * isum - dmin * summs in 32bits
  3071. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3072. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  3073. }
  3074. *s = hsum_float_8(acc);
  3075. #elif defined __riscv_v_intrinsic
  3076. float sumf = 0;
  3077. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3078. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  3079. for (int i = 0; i < nb; ++i) {
  3080. const uint8_t * q2 = x[i].qs;
  3081. const int8_t * q8 = y[i].qs;
  3082. const uint8_t * sc = x[i].scales;
  3083. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3084. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3085. size_t vl = 16;
  3086. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  3087. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  3088. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  3089. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  3090. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  3091. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  3092. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  3093. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3094. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  3095. vl = 32;
  3096. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3097. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  3098. uint8_t is=0;
  3099. int isum=0;
  3100. for (int j = 0; j < QK_K/128; ++j) {
  3101. // load Q2
  3102. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  3103. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  3104. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  3105. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  3106. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  3107. // duplicate scale elements for product
  3108. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  3109. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  3110. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  3111. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  3112. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  3113. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  3114. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  3115. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  3116. // load Q8
  3117. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  3118. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  3119. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  3120. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  3121. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  3122. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  3123. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  3124. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  3125. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  3126. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  3127. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  3128. q2+=32; q8+=128; is=8;
  3129. }
  3130. sumf += dall * isum;
  3131. }
  3132. *s = sumf;
  3133. #else
  3134. float sumf = 0;
  3135. for (int i = 0; i < nb; ++i) {
  3136. const uint8_t * q2 = x[i].qs;
  3137. const int8_t * q8 = y[i].qs;
  3138. const uint8_t * sc = x[i].scales;
  3139. int summs = 0;
  3140. for (int j = 0; j < 16; ++j) {
  3141. summs += y[i].bsums[j] * (sc[j] >> 4);
  3142. }
  3143. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3144. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3145. int isum = 0;
  3146. int is = 0;
  3147. int d;
  3148. for (int k = 0; k < QK_K/128; ++k) {
  3149. int shift = 0;
  3150. for (int j = 0; j < 4; ++j) {
  3151. d = sc[is++] & 0xF;
  3152. int isuml = 0;
  3153. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3154. isum += d * isuml;
  3155. d = sc[is++] & 0xF;
  3156. isuml = 0;
  3157. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3158. isum += d * isuml;
  3159. shift += 2;
  3160. q8 += 32;
  3161. }
  3162. q2 += 32;
  3163. }
  3164. sumf += dall * isum - dmin * summs;
  3165. }
  3166. *s = sumf;
  3167. #endif
  3168. }
  3169. #else
  3170. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3171. const block_q2_K * restrict x = vx;
  3172. const block_q8_K * restrict y = vy;
  3173. const int nb = n / QK_K;
  3174. #ifdef __ARM_NEON
  3175. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3176. #if defined(__ARM_FEATURE_DOTPROD)
  3177. const int32x4_t vzero = vdupq_n_s32(0);
  3178. #endif
  3179. int8x16x4_t q2bytes;
  3180. uint32_t aux32[2];
  3181. const uint8_t * scales = (const uint8_t *)aux32;
  3182. float sum = 0;
  3183. for (int i = 0; i < nb; ++i) {
  3184. const float d = y[i].d * (float)x[i].d;
  3185. const float dmin = -y[i].d * (float)x[i].dmin;
  3186. const uint8_t * restrict q2 = x[i].qs;
  3187. const int8_t * restrict q8 = y[i].qs;
  3188. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3189. aux32[0] = sc[0] & 0x0f0f0f0f;
  3190. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3191. 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]);
  3192. int isum1 = 0, isum2 = 0;
  3193. const uint8x16_t q2bits = vld1q_u8(q2);
  3194. const int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  3195. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  3196. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  3197. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  3198. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  3199. #if defined(__ARM_FEATURE_DOTPROD)
  3200. isum1 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  3201. isum2 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  3202. isum1 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  3203. isum2 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  3204. #else
  3205. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3206. vmull_s8(vget_high_s8(q2bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3207. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3208. vmull_s8(vget_high_s8(q2bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3209. isum1 += vaddvq_s16(p1) * scales[0];
  3210. isum2 += vaddvq_s16(p2) * scales[1];
  3211. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3212. vmull_s8(vget_high_s8(q2bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3213. const int16x8_t p4 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3214. vmull_s8(vget_high_s8(q2bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3215. isum1 += vaddvq_s16(p3) * scales[2];
  3216. isum2 += vaddvq_s16(p4) * scales[3];
  3217. #endif
  3218. sum += d * (isum1 + isum2);
  3219. }
  3220. *s = sum;
  3221. #elif defined __AVX2__
  3222. const __m256i m3 = _mm256_set1_epi8(3);
  3223. __m256 acc = _mm256_setzero_ps();
  3224. uint32_t ud, um;
  3225. const uint8_t * restrict db = (const uint8_t *)&ud;
  3226. const uint8_t * restrict mb = (const uint8_t *)&um;
  3227. float summs = 0;
  3228. // TODO: optimize this
  3229. for (int i = 0; i < nb; ++i) {
  3230. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3231. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3232. const uint8_t * restrict q2 = x[i].qs;
  3233. const int8_t * restrict q8 = y[i].qs;
  3234. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3235. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3236. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3237. 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];
  3238. summs += dmin * smin;
  3239. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3240. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  3241. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  3242. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3243. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3244. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3245. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3246. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  3247. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  3248. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  3249. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  3250. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  3251. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  3252. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  3253. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  3254. }
  3255. *s = hsum_float_8(acc) + summs;
  3256. #elif defined __AVX__
  3257. const __m128i m3 = _mm_set1_epi8(3);
  3258. __m256 acc = _mm256_setzero_ps();
  3259. uint32_t ud, um;
  3260. const uint8_t * restrict db = (const uint8_t *)&ud;
  3261. const uint8_t * restrict mb = (const uint8_t *)&um;
  3262. float summs = 0;
  3263. // TODO: optimize this
  3264. for (int i = 0; i < nb; ++i) {
  3265. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3266. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3267. const uint8_t * restrict q2 = x[i].qs;
  3268. const int8_t * restrict q8 = y[i].qs;
  3269. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3270. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3271. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3272. 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];
  3273. summs += dmin * smin;
  3274. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3275. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3276. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3277. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3278. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3279. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3280. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3281. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  3282. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  3283. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  3284. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  3285. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  3286. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  3287. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  3288. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  3289. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  3290. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  3291. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  3292. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  3293. }
  3294. *s = hsum_float_8(acc) + summs;
  3295. #elif defined __riscv_v_intrinsic
  3296. uint32_t aux32[2];
  3297. const uint8_t * scales = (const uint8_t *)aux32;
  3298. float sumf = 0;
  3299. for (int i = 0; i < nb; ++i) {
  3300. const float d = y[i].d * (float)x[i].d;
  3301. const float dmin = -y[i].d * (float)x[i].dmin;
  3302. const uint8_t * restrict q2 = x[i].qs;
  3303. const int8_t * restrict q8 = y[i].qs;
  3304. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3305. aux32[0] = sc[0] & 0x0f0f0f0f;
  3306. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3307. 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]);
  3308. int isum1 = 0;
  3309. int isum2 = 0;
  3310. size_t vl = 16;
  3311. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  3312. // load Q2
  3313. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  3314. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  3315. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  3316. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  3317. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  3318. // load Q8, and take product with Q2
  3319. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3320. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3321. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3322. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3323. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  3324. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  3325. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  3326. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  3327. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  3328. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  3329. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  3330. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  3331. sumf += d * (isum1 + isum2);
  3332. }
  3333. *s = sumf;
  3334. #else
  3335. float sumf = 0;
  3336. int isum[4];
  3337. for (int i = 0; i < nb; ++i) {
  3338. const uint8_t * q2 = x[i].qs;
  3339. const int8_t * q8 = y[i].qs;
  3340. const uint8_t * sc = x[i].scales;
  3341. int summs = 0;
  3342. for (int j = 0; j < QK_K/16; ++j) {
  3343. summs += y[i].bsums[j] * (sc[j] >> 4);
  3344. }
  3345. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3346. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3347. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  3348. for (int l = 0; l < 16; ++l) {
  3349. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  3350. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  3351. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  3352. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  3353. }
  3354. for (int l = 0; l < 4; ++l) {
  3355. isum[l] *= (sc[l] & 0xF);
  3356. }
  3357. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  3358. }
  3359. *s = sumf;
  3360. #endif
  3361. }
  3362. #endif
  3363. #if QK_K == 256
  3364. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3365. assert(n % QK_K == 0);
  3366. const uint32_t kmask1 = 0x03030303;
  3367. const uint32_t kmask2 = 0x0f0f0f0f;
  3368. const block_q3_K * restrict x = vx;
  3369. const block_q8_K * restrict y = vy;
  3370. const int nb = n / QK_K;
  3371. #ifdef __ARM_NEON
  3372. uint32_t aux[3];
  3373. uint32_t utmp[4];
  3374. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3375. #ifdef __ARM_FEATURE_DOTPROD
  3376. const int32x4_t vzero = vdupq_n_s32(0);
  3377. #endif
  3378. const uint8x16_t m0 = vdupq_n_u8(1);
  3379. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  3380. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  3381. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  3382. const int8_t m32 = 32;
  3383. int8x16x4_t q3bytes;
  3384. float sum = 0;
  3385. for (int i = 0; i < nb; ++i) {
  3386. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3387. const uint8_t * restrict q3 = x[i].qs;
  3388. const uint8_t * restrict qh = x[i].hmask;
  3389. const int8_t * restrict q8 = y[i].qs;
  3390. uint8x16x2_t qhbits = vld1q_u8_x2(qh);
  3391. uint8x16x4_t q3h;
  3392. int32_t isum = 0;
  3393. // Set up scales
  3394. memcpy(aux, x[i].scales, 12);
  3395. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3396. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3397. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3398. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3399. int8_t * scale = (int8_t *)utmp;
  3400. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  3401. for (int j = 0; j < QK_K/128; ++j) {
  3402. const uint8x16x2_t q3bits = vld1q_u8_x2(q3); q3 += 32;
  3403. const int8x16x4_t q8bytes_1 = vld1q_s8_x4(q8); q8 += 64;
  3404. const int8x16x4_t q8bytes_2 = vld1q_s8_x4(q8); q8 += 64;
  3405. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  3406. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  3407. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  3408. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  3409. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3410. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3411. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3412. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3413. #if defined(__ARM_FEATURE_DOTPROD)
  3414. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  3415. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  3416. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  3417. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  3418. #else
  3419. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes_1.val[0])),
  3420. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes_1.val[0])));
  3421. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes_1.val[1])),
  3422. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes_1.val[1])));
  3423. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes_1.val[2])),
  3424. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes_1.val[2])));
  3425. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes_1.val[3])),
  3426. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes_1.val[3])));
  3427. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1] + vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  3428. #endif
  3429. scale += 4;
  3430. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  3431. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  3432. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  3433. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  3434. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3435. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3436. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3437. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3438. #if defined(__ARM_FEATURE_DOTPROD)
  3439. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  3440. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  3441. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  3442. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  3443. #else
  3444. p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes_2.val[0])),
  3445. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes_2.val[0])));
  3446. p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes_2.val[1])),
  3447. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes_2.val[1])));
  3448. p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes_2.val[2])),
  3449. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes_2.val[2])));
  3450. p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes_2.val[3])),
  3451. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes_2.val[3])));
  3452. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1] + vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  3453. #endif
  3454. scale += 4;
  3455. if (j == 0) {
  3456. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  3457. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  3458. }
  3459. }
  3460. sum += d * isum;
  3461. }
  3462. *s = sum;
  3463. #elif defined __AVX2__
  3464. const __m256i m3 = _mm256_set1_epi8(3);
  3465. const __m256i mone = _mm256_set1_epi8(1);
  3466. const __m128i m32 = _mm_set1_epi8(32);
  3467. __m256 acc = _mm256_setzero_ps();
  3468. uint32_t aux[3];
  3469. for (int i = 0; i < nb; ++i) {
  3470. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3471. const uint8_t * restrict q3 = x[i].qs;
  3472. const int8_t * restrict q8 = y[i].qs;
  3473. // Set up scales
  3474. memcpy(aux, x[i].scales, 12);
  3475. __m128i scales128 = _mm_set_epi32(
  3476. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3477. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3478. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3479. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3480. scales128 = _mm_sub_epi8(scales128, m32);
  3481. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  3482. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3483. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3484. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3485. // high bit
  3486. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  3487. // integer accumulator
  3488. __m256i sumi = _mm256_setzero_si256();
  3489. int bit = 0;
  3490. int is = 0;
  3491. for (int j = 0; j < QK_K/128; ++j) {
  3492. // load low 2 bits
  3493. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  3494. // prepare low and high bits
  3495. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  3496. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3497. ++bit;
  3498. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  3499. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3500. ++bit;
  3501. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  3502. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3503. ++bit;
  3504. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  3505. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3506. ++bit;
  3507. // load Q8 quants
  3508. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3509. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3510. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3511. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3512. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3513. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3514. // and 2 if the high bit was set)
  3515. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3516. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3517. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  3518. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  3519. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3520. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3521. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  3522. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  3523. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3524. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3525. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  3526. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  3527. // multiply with scales
  3528. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  3529. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  3530. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  3531. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  3532. // accumulate
  3533. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3534. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  3535. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  3536. }
  3537. // multiply with block scale and accumulate
  3538. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3539. }
  3540. *s = hsum_float_8(acc);
  3541. #elif defined __AVX__
  3542. const __m128i m3 = _mm_set1_epi8(3);
  3543. const __m128i mone = _mm_set1_epi8(1);
  3544. const __m128i m32 = _mm_set1_epi8(32);
  3545. const __m128i m2 = _mm_set1_epi8(2);
  3546. __m256 acc = _mm256_setzero_ps();
  3547. const uint32_t *aux;
  3548. for (int i = 0; i < nb; ++i) {
  3549. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3550. const uint8_t * restrict q3 = x[i].qs;
  3551. const int8_t * restrict q8 = y[i].qs;
  3552. // Set up scales
  3553. aux = (const uint32_t *)x[i].scales;
  3554. __m128i scales128 = _mm_set_epi32(
  3555. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3556. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3557. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3558. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3559. scales128 = _mm_sub_epi8(scales128, m32);
  3560. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  3561. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  3562. const __m128i scales[2] = { scales_0, scales_1 };
  3563. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  3564. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  3565. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  3566. // integer accumulator
  3567. __m128i sumi_0 = _mm_setzero_si128();
  3568. __m128i sumi_1 = _mm_setzero_si128();
  3569. for (int j = 0; j < QK_K/128; ++j) {
  3570. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  3571. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3572. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3573. // prepare low and high bits
  3574. const int bit = j << 2;
  3575. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  3576. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  3577. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  3578. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  3579. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  3580. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  3581. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3582. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3583. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  3584. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  3585. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3586. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3587. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  3588. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  3589. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3590. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3591. // load Q8 quants from block_q8_K.qs[QK_K]
  3592. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3593. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3594. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3595. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3596. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3597. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3598. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3599. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3600. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3601. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3602. // and 2 if the high bit was set)
  3603. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  3604. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  3605. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  3606. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  3607. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  3608. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  3609. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  3610. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  3611. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  3612. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  3613. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  3614. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  3615. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  3616. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  3617. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  3618. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  3619. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3620. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3621. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3622. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3623. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  3624. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  3625. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  3626. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  3627. // multiply with scales
  3628. __m128i shuffle = _mm_set1_epi16(0x0100);
  3629. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  3630. shuffle = _mm_add_epi16(shuffle, m2);
  3631. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  3632. shuffle = _mm_add_epi16(shuffle, m2);
  3633. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  3634. shuffle = _mm_add_epi16(shuffle, m2);
  3635. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  3636. shuffle = _mm_add_epi16(shuffle, m2);
  3637. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  3638. shuffle = _mm_add_epi16(shuffle, m2);
  3639. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  3640. shuffle = _mm_add_epi16(shuffle, m2);
  3641. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  3642. shuffle = _mm_add_epi16(shuffle, m2);
  3643. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  3644. // accumulate
  3645. p16_0 = _mm_add_epi32(p16_0, p16_1);
  3646. p16_2 = _mm_add_epi32(p16_2, p16_3);
  3647. p16_4 = _mm_add_epi32(p16_4, p16_5);
  3648. p16_6 = _mm_add_epi32(p16_6, p16_7);
  3649. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3650. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  3651. }
  3652. // multiply with block scale and accumulate
  3653. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3654. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  3655. }
  3656. *s = hsum_float_8(acc);
  3657. #elif defined __riscv_v_intrinsic
  3658. uint32_t aux[3];
  3659. uint32_t utmp[4];
  3660. float sumf = 0;
  3661. for (int i = 0; i < nb; ++i) {
  3662. const uint8_t * restrict q3 = x[i].qs;
  3663. const uint8_t * restrict qh = x[i].hmask;
  3664. const int8_t * restrict q8 = y[i].qs;
  3665. memcpy(aux, x[i].scales, 12);
  3666. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3667. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3668. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3669. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3670. int8_t * scale = (int8_t *)utmp;
  3671. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  3672. size_t vl = 32;
  3673. uint8_t m = 1;
  3674. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3675. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  3676. int sum_t = 0;
  3677. for (int j = 0; j < QK_K; j += 128) {
  3678. vl = 32;
  3679. // load Q3
  3680. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  3681. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  3682. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  3683. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  3684. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  3685. // compute mask for subtraction
  3686. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3687. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  3688. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  3689. m <<= 1;
  3690. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3691. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  3692. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  3693. m <<= 1;
  3694. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3695. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  3696. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  3697. m <<= 1;
  3698. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3699. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  3700. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  3701. m <<= 1;
  3702. // load Q8 and take product with Q3
  3703. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  3704. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  3705. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  3706. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  3707. vl = 16;
  3708. // retreive lane to multiply with scale
  3709. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  3710. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  3711. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  3712. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  3713. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  3714. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  3715. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  3716. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  3717. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  3718. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  3719. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  3720. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  3721. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  3722. q3 += 32; q8 += 128; scale += 8;
  3723. }
  3724. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3725. sumf += d*sum_t;
  3726. }
  3727. *s = sumf;
  3728. #else
  3729. // scalar version
  3730. // This function is written like this so the compiler can manage to vectorize most of it
  3731. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  3732. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  3733. // The ideal situation would be if we could just write the code once, and the compiler would
  3734. // automatically produce the best possible set of machine instructions, instead of us having to manually
  3735. // write vectorized versions for AVX, ARM_NEON, etc.
  3736. int8_t aux8[QK_K];
  3737. int16_t aux16[8];
  3738. float sums [8];
  3739. int32_t aux32[8];
  3740. memset(sums, 0, 8*sizeof(float));
  3741. uint32_t auxs[4];
  3742. const int8_t * scales = (const int8_t*)auxs;
  3743. float sumf = 0;
  3744. for (int i = 0; i < nb; ++i) {
  3745. const uint8_t * restrict q3 = x[i].qs;
  3746. const uint8_t * restrict hm = x[i].hmask;
  3747. const int8_t * restrict q8 = y[i].qs;
  3748. memset(aux32, 0, 8*sizeof(int32_t));
  3749. int8_t * restrict a = aux8;
  3750. uint8_t m = 1;
  3751. for (int j = 0; j < QK_K; j += 128) {
  3752. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  3753. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3754. a += 32; m <<= 1;
  3755. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  3756. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3757. a += 32; m <<= 1;
  3758. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  3759. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3760. a += 32; m <<= 1;
  3761. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  3762. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3763. a += 32; m <<= 1;
  3764. q3 += 32;
  3765. }
  3766. a = aux8;
  3767. memcpy(auxs, x[i].scales, 12);
  3768. uint32_t tmp = auxs[2];
  3769. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  3770. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  3771. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  3772. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  3773. for (int j = 0; j < QK_K/16; ++j) {
  3774. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3775. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  3776. q8 += 8; a += 8;
  3777. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3778. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  3779. q8 += 8; a += 8;
  3780. }
  3781. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3782. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3783. }
  3784. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3785. *s = sumf;
  3786. #endif
  3787. }
  3788. #else
  3789. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3790. assert(n % QK_K == 0);
  3791. const block_q3_K * restrict x = vx;
  3792. const block_q8_K * restrict y = vy;
  3793. const int nb = n / QK_K;
  3794. #ifdef __ARM_NEON
  3795. #ifdef __ARM_FEATURE_DOTPROD
  3796. const int32x4_t vzero = vdupq_n_s32(0);
  3797. #endif
  3798. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3799. const uint8x16_t mh = vdupq_n_u8(4);
  3800. int8x16x4_t q3bytes;
  3801. uint16_t aux16[2];
  3802. int8_t * scales = (int8_t *)aux16;
  3803. float sum = 0;
  3804. for (int i = 0; i < nb; ++i) {
  3805. uint8x16x4_t q3h;
  3806. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  3807. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  3808. const int8x16x4_t q8bytes = vld1q_s8_x4(y[i].qs);
  3809. const uint16_t a = *(const uint16_t *)x[i].scales;
  3810. aux16[0] = a & 0x0f0f;
  3811. aux16[1] = (a >> 4) & 0x0f0f;
  3812. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  3813. 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]);
  3814. const float d = y[i].d * (float)x[i].d;
  3815. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  3816. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  3817. q3h.val[1] = vandq_u8(mh, htmp);
  3818. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  3819. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  3820. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  3821. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  3822. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  3823. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  3824. #if defined(__ARM_FEATURE_DOTPROD)
  3825. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  3826. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  3827. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  3828. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  3829. #else
  3830. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3831. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3832. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3833. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3834. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3835. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3836. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3837. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3838. isum += vaddvq_s16(p0) * scales[0] + vaddvq_s16(p1) * scales[2] + vaddvq_s16(p2) * scales[1] + vaddvq_s16(p3) * scales[3];
  3839. #endif
  3840. sum += d * isum;
  3841. }
  3842. *s = sum;
  3843. #elif defined __AVX2__
  3844. const __m256i m3 = _mm256_set1_epi8(3);
  3845. const __m256i m1 = _mm256_set1_epi8(1);
  3846. __m256 acc = _mm256_setzero_ps();
  3847. uint64_t aux64;
  3848. uint16_t aux16[2];
  3849. const int8_t * aux8 = (const int8_t *)aux16;
  3850. for (int i = 0; i < nb; ++i) {
  3851. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3852. const uint8_t * restrict q3 = x[i].qs;
  3853. const int8_t * restrict q8 = y[i].qs;
  3854. const uint16_t a = *(const uint16_t *)x[i].scales;
  3855. aux16[0] = a & 0x0f0f;
  3856. aux16[1] = (a >> 4) & 0x0f0f;
  3857. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  3858. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  3859. memcpy(&aux64, x[i].hmask, 8);
  3860. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  3861. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  3862. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  3863. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  3864. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  3865. // load low 2 bits
  3866. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  3867. // prepare low and high bits
  3868. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  3869. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  3870. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  3871. // load Q8 quants
  3872. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3873. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3874. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3875. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3876. // and 2 if the high bit was set)
  3877. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3878. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3879. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3880. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3881. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3882. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3883. // multiply with scales
  3884. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  3885. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  3886. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3887. // multiply with block scale and accumulate
  3888. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  3889. }
  3890. *s = hsum_float_8(acc);
  3891. #elif defined __AVX__
  3892. const __m128i m3 = _mm_set1_epi8(3);
  3893. const __m128i m1 = _mm_set1_epi8(1);
  3894. __m256 acc = _mm256_setzero_ps();
  3895. uint64_t aux64;
  3896. uint16_t aux16[2];
  3897. const int8_t * aux8 = (const int8_t *)aux16;
  3898. for (int i = 0; i < nb; ++i) {
  3899. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3900. const uint8_t * restrict q3 = x[i].qs;
  3901. const int8_t * restrict q8 = y[i].qs;
  3902. const uint16_t a = *(const uint16_t *)x[i].scales;
  3903. aux16[0] = a & 0x0f0f;
  3904. aux16[1] = (a >> 4) & 0x0f0f;
  3905. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  3906. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  3907. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  3908. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  3909. memcpy(&aux64, x[i].hmask, 8);
  3910. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  3911. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  3912. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  3913. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  3914. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  3915. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  3916. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  3917. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  3918. // load low 2 bits
  3919. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  3920. // prepare low and high bits
  3921. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  3922. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  3923. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  3924. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  3925. // load Q8 quants
  3926. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3927. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3928. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  3929. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3930. // and 2 if the high bit was set)
  3931. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  3932. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  3933. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  3934. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  3935. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  3936. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  3937. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  3938. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  3939. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3940. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3941. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3942. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3943. // multiply with scales
  3944. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  3945. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  3946. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  3947. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  3948. p16_0 = _mm_add_epi32(p16_0, p16_2);
  3949. p16_1 = _mm_add_epi32(p16_1, p16_3);
  3950. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  3951. // multiply with block scale and accumulate
  3952. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  3953. }
  3954. *s = hsum_float_8(acc);
  3955. #elif defined __riscv_v_intrinsic
  3956. uint16_t aux16[2];
  3957. int8_t * scales = (int8_t *)aux16;
  3958. float sumf = 0;
  3959. for (int i = 0; i < nb; ++i) {
  3960. const uint8_t * restrict q3 = x[i].qs;
  3961. const int8_t * restrict q8 = y[i].qs;
  3962. const uint16_t a = *(const uint16_t *)x[i].scales;
  3963. aux16[0] = a & 0x0f0f;
  3964. aux16[1] = (a >> 4) & 0x0f0f;
  3965. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  3966. 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]);
  3967. const float d = y[i].d * (float)x[i].d;
  3968. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3969. // load qh
  3970. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  3971. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  3972. size_t vl = 16;
  3973. // extend and combine both qh_x1 and qh_x2
  3974. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  3975. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  3976. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  3977. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  3978. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  3979. // load Q3
  3980. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  3981. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  3982. 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);
  3983. 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);
  3984. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  3985. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  3986. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  3987. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  3988. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  3989. // load Q8 and take product with Q3
  3990. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3991. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3992. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3993. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3994. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  3995. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  3996. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  3997. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  3998. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  3999. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4000. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4001. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4002. sumf += d * isum;
  4003. }
  4004. *s = sumf;
  4005. #else
  4006. int8_t aux8[QK_K];
  4007. int16_t aux16[8];
  4008. float sums [8];
  4009. int32_t aux32[8];
  4010. int32_t scales[4];
  4011. memset(sums, 0, 8*sizeof(float));
  4012. float sumf = 0;
  4013. for (int i = 0; i < nb; ++i) {
  4014. const uint8_t * restrict q3 = x[i].qs;
  4015. const uint8_t * restrict hm = x[i].hmask;
  4016. const int8_t * restrict q8 = y[i].qs;
  4017. int8_t * restrict a = aux8;
  4018. for (int l = 0; l < 8; ++l) {
  4019. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  4020. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  4021. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  4022. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  4023. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  4024. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  4025. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  4026. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  4027. }
  4028. scales[0] = (x[i].scales[0] & 0xF) - 8;
  4029. scales[1] = (x[i].scales[0] >> 4) - 8;
  4030. scales[2] = (x[i].scales[1] & 0xF) - 8;
  4031. scales[3] = (x[i].scales[1] >> 4) - 8;
  4032. memset(aux32, 0, 8*sizeof(int32_t));
  4033. for (int j = 0; j < QK_K/16; ++j) {
  4034. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4035. q8 += 8; a += 8;
  4036. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  4037. q8 += 8; a += 8;
  4038. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  4039. }
  4040. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4041. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4042. }
  4043. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4044. *s = sumf;
  4045. #endif
  4046. }
  4047. #endif
  4048. #if QK_K == 256
  4049. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4050. assert(n % QK_K == 0);
  4051. const block_q4_K * restrict x = vx;
  4052. const block_q8_K * restrict y = vy;
  4053. const int nb = n / QK_K;
  4054. static const uint32_t kmask1 = 0x3f3f3f3f;
  4055. static const uint32_t kmask2 = 0x0f0f0f0f;
  4056. static const uint32_t kmask3 = 0x03030303;
  4057. uint32_t utmp[4];
  4058. #ifdef __ARM_NEON
  4059. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4060. #ifdef __ARM_FEATURE_DOTPROD
  4061. const int32x4_t mzero = vdupq_n_s32(0);
  4062. #endif
  4063. int8x16x2_t q4bytes;
  4064. int8x16x2_t q8bytes;
  4065. float sumf = 0;
  4066. for (int i = 0; i < nb; ++i) {
  4067. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4068. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4069. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4070. memcpy(utmp, x[i].scales, 12);
  4071. uint32x2_t mins8 = { 0 };
  4072. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  4073. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  4074. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4075. utmp[0] &= kmask1;
  4076. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  4077. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4078. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4079. sumf -= dmin * vaddvq_s32(prod);
  4080. const uint8_t * scales = (const uint8_t *)utmp;
  4081. const uint8_t * restrict q4 = x[i].qs;
  4082. const int8_t * restrict q8 = y[i].qs;
  4083. int32_t sumi1 = 0;
  4084. int32_t sumi2 = 0;
  4085. for (int j = 0; j < QK_K/64; ++j) {
  4086. const uint8x16x2_t q4bits = vld1q_u8_x2(q4); q4 += 32;
  4087. #ifdef __ARM_FEATURE_DOTPROD
  4088. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  4089. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4090. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4091. const int32x4_t p1 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4092. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  4093. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  4094. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4095. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4096. const int32x4_t p2 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4097. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  4098. #else
  4099. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  4100. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4101. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4102. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  4103. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  4104. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  4105. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  4106. sumi1 += vaddvq_s16(vaddq_s16(p0, p1)) * scales[2*j+0];
  4107. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  4108. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4109. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4110. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  4111. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  4112. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  4113. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  4114. sumi2 += vaddvq_s16(vaddq_s16(p2, p3)) * scales[2*j+1];
  4115. #endif
  4116. }
  4117. sumf += d * (sumi1 + sumi2);
  4118. }
  4119. *s = sumf;
  4120. #elif defined __AVX2__
  4121. const __m256i m4 = _mm256_set1_epi8(0xF);
  4122. __m256 acc = _mm256_setzero_ps();
  4123. __m128 acc_m = _mm_setzero_ps();
  4124. for (int i = 0; i < nb; ++i) {
  4125. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4126. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4127. memcpy(utmp, x[i].scales, 12);
  4128. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4129. const uint32_t uaux = utmp[1] & kmask1;
  4130. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4131. utmp[2] = uaux;
  4132. utmp[0] &= kmask1;
  4133. const uint8_t * restrict q4 = x[i].qs;
  4134. const int8_t * restrict q8 = y[i].qs;
  4135. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4136. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4137. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4138. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4139. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  4140. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4141. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4142. __m256i sumi = _mm256_setzero_si256();
  4143. for (int j = 0; j < QK_K/64; ++j) {
  4144. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4145. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4146. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  4147. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4148. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4149. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4150. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4151. p16l = _mm256_madd_epi16(scale_l, p16l);
  4152. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4153. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4154. p16h = _mm256_madd_epi16(scale_h, p16h);
  4155. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  4156. sumi = _mm256_add_epi32(sumi, sumj);
  4157. }
  4158. __m256 vd = _mm256_set1_ps(d);
  4159. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4160. }
  4161. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4162. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4163. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4164. #elif defined __AVX__
  4165. const __m128i m4 = _mm_set1_epi8(0xF);
  4166. const __m128i m2 = _mm_set1_epi8(0x2);
  4167. __m256 acc = _mm256_setzero_ps();
  4168. __m128 acc_m = _mm_setzero_ps();
  4169. for (int i = 0; i < nb; ++i) {
  4170. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4171. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4172. const uint8_t * restrict q4 = x[i].qs;
  4173. const int8_t * restrict q8 = y[i].qs;
  4174. memcpy(utmp, x[i].scales, 12);
  4175. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4176. const uint32_t uaux = utmp[1] & kmask1;
  4177. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4178. utmp[2] = uaux;
  4179. utmp[0] &= kmask1;
  4180. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4181. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4182. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4183. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4184. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4185. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4186. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4187. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  4188. __m128i sumi_0 = _mm_setzero_si128();
  4189. __m128i sumi_1 = _mm_setzero_si128();
  4190. __m128i shuffle = _mm_set1_epi16(0x0100);
  4191. for (int j = 0; j < QK_K/64; ++j) {
  4192. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  4193. shuffle = _mm_add_epi16(shuffle, m2);
  4194. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  4195. shuffle = _mm_add_epi16(shuffle, m2);
  4196. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4197. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  4198. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4199. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4200. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  4201. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4202. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4203. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  4204. p16l = _mm_madd_epi16(scale_l, p16l);
  4205. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  4206. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4207. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  4208. p16l = _mm_madd_epi16(scale_l, p16l);
  4209. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  4210. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4211. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  4212. p16h = _mm_madd_epi16(scale_h, p16h);
  4213. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  4214. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4215. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  4216. p16h = _mm_madd_epi16(scale_h, p16h);
  4217. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  4218. }
  4219. __m256 vd = _mm256_set1_ps(d);
  4220. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4221. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4222. }
  4223. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4224. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4225. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4226. #elif defined __riscv_v_intrinsic
  4227. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4228. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4229. float sumf = 0;
  4230. for (int i = 0; i < nb; ++i) {
  4231. size_t vl = 8;
  4232. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4233. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4234. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4235. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4236. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4237. memcpy(utmp, x[i].scales, 12);
  4238. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4239. const uint32_t uaux = utmp[1] & kmask1;
  4240. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4241. utmp[2] = uaux;
  4242. utmp[0] &= kmask1;
  4243. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4244. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4245. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4246. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4247. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4248. const uint8_t * restrict q4 = x[i].qs;
  4249. const int8_t * restrict q8 = y[i].qs;
  4250. vl = 32;
  4251. int32_t sum_1 = 0;
  4252. int32_t sum_2 = 0;
  4253. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4254. for (int j = 0; j < QK_K/64; ++j) {
  4255. // load Q4
  4256. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4257. // load Q8 and multiply it with lower Q4 nibble
  4258. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4259. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4260. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  4261. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  4262. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  4263. // load Q8 and multiply it with upper Q4 nibble
  4264. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4265. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4266. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  4267. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  4268. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  4269. q4 += 32; q8 += 64;
  4270. }
  4271. sumf += d*(sum_1 + sum_2);
  4272. }
  4273. *s = sumf;
  4274. #else
  4275. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4276. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4277. int8_t aux8[QK_K];
  4278. int16_t aux16[8];
  4279. float sums [8];
  4280. int32_t aux32[8];
  4281. memset(sums, 0, 8*sizeof(float));
  4282. float sumf = 0;
  4283. for (int i = 0; i < nb; ++i) {
  4284. const uint8_t * restrict q4 = x[i].qs;
  4285. const int8_t * restrict q8 = y[i].qs;
  4286. memset(aux32, 0, 8*sizeof(int32_t));
  4287. int8_t * restrict a = aux8;
  4288. for (int j = 0; j < QK_K/64; ++j) {
  4289. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4290. a += 32;
  4291. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4292. a += 32; q4 += 32;
  4293. }
  4294. memcpy(utmp, x[i].scales, 12);
  4295. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4296. const uint32_t uaux = utmp[1] & kmask1;
  4297. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4298. utmp[2] = uaux;
  4299. utmp[0] &= kmask1;
  4300. int sumi = 0;
  4301. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4302. a = aux8;
  4303. int is = 0;
  4304. for (int j = 0; j < QK_K/32; ++j) {
  4305. int32_t scale = scales[is++];
  4306. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4307. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4308. q8 += 8; a += 8;
  4309. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4310. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4311. q8 += 8; a += 8;
  4312. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4313. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4314. q8 += 8; a += 8;
  4315. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4316. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4317. q8 += 8; a += 8;
  4318. }
  4319. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4320. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4321. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4322. sumf -= dmin * sumi;
  4323. }
  4324. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4325. *s = sumf;
  4326. #endif
  4327. }
  4328. #else
  4329. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4330. assert(n % QK_K == 0);
  4331. const block_q4_K * restrict x = vx;
  4332. const block_q8_K * restrict y = vy;
  4333. const int nb = n / QK_K;
  4334. #ifdef __ARM_NEON
  4335. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4336. #ifdef __ARM_FEATURE_DOTPROD
  4337. const int32x4_t mzero = vdupq_n_s32(0);
  4338. #endif
  4339. float sumf = 0;
  4340. int8x16x2_t q4bytes;
  4341. int8x16x4_t q8bytes;
  4342. float sum_mins = 0.f;
  4343. uint16_t aux16[2];
  4344. const uint8_t * restrict scales = (const uint8_t *)aux16;
  4345. for (int i = 0; i < nb; ++i) {
  4346. const uint8_t * restrict q4 = x[i].qs;
  4347. const int8_t * restrict q8 = y[i].qs;
  4348. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  4349. aux16[0] = a[0] & 0x0f0f;
  4350. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4351. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  4352. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  4353. const float d = y[i].d * (float)x[i].d[0];
  4354. const uint8x16x2_t q4bits = vld1q_u8_x2(q4);
  4355. #ifdef __ARM_FEATURE_DOTPROD
  4356. q8bytes = vld1q_s8_x4(q8);
  4357. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4358. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4359. const int32x4_t p1 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4360. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  4361. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4362. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4363. const int32x4_t p2 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  4364. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  4365. #else
  4366. q8bytes = vld1q_s8_x4(q8);
  4367. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4368. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4369. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  4370. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  4371. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  4372. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  4373. int32_t sumi1 = vaddvq_s16(vaddq_s16(p0, p1)) * scales[0];
  4374. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4375. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4376. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[2])),
  4377. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[2])));
  4378. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[3])),
  4379. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[3])));
  4380. int32_t sumi2 = vaddvq_s16(vaddq_s16(p2, p3)) * scales[1];
  4381. #endif
  4382. sumf += d * (sumi1 + sumi2);
  4383. }
  4384. *s = sumf - sum_mins;
  4385. #elif defined __AVX2__
  4386. const __m256i m4 = _mm256_set1_epi8(0xF);
  4387. __m256 acc = _mm256_setzero_ps();
  4388. float summs = 0;
  4389. uint16_t aux16[2];
  4390. const uint8_t * scales = (const uint8_t *)aux16;
  4391. for (int i = 0; i < nb; ++i) {
  4392. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4393. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4394. const __m256 vd = _mm256_set1_ps(d);
  4395. const uint16_t * a = (const uint16_t *)x[i].scales;
  4396. aux16[0] = a[0] & 0x0f0f;
  4397. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4398. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4399. const uint8_t * restrict q4 = x[i].qs;
  4400. const int8_t * restrict q8 = y[i].qs;
  4401. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4402. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4403. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4404. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4405. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  4406. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4407. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4408. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  4409. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  4410. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  4411. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  4412. }
  4413. *s = hsum_float_8(acc) - summs;
  4414. #elif defined __AVX__
  4415. const __m128i m4 = _mm_set1_epi8(0xF);
  4416. __m256 acc = _mm256_setzero_ps();
  4417. float summs = 0;
  4418. uint16_t aux16[2];
  4419. const uint8_t * scales = (const uint8_t *)aux16;
  4420. for (int i = 0; i < nb; ++i) {
  4421. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4422. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4423. const __m256 vd = _mm256_set1_ps(d);
  4424. const uint16_t * a = (const uint16_t *)x[i].scales;
  4425. aux16[0] = a[0] & 0x0f0f;
  4426. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4427. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4428. const uint8_t * restrict q4 = x[i].qs;
  4429. const int8_t * restrict q8 = y[i].qs;
  4430. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4431. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  4432. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  4433. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  4434. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  4435. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  4436. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  4437. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4438. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4439. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  4440. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  4441. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  4442. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  4443. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  4444. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  4445. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  4446. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  4447. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  4448. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  4449. }
  4450. *s = hsum_float_8(acc) - summs;
  4451. #elif defined __riscv_v_intrinsic
  4452. uint16_t s16[2];
  4453. const uint8_t * restrict scales = (const uint8_t *)s16;
  4454. float sumf = 0;
  4455. for (int i = 0; i < nb; ++i) {
  4456. const uint8_t * restrict q4 = x[i].qs;
  4457. const int8_t * restrict q8 = y[i].qs;
  4458. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4459. s16[0] = b[0] & 0x0f0f;
  4460. s16[1] = (b[0] >> 4) & 0x0f0f;
  4461. 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]));
  4462. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4463. size_t vl = 32;
  4464. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4465. // load Q4
  4466. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4467. // load Q8 and multiply it with lower Q4 nibble
  4468. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4469. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  4470. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  4471. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  4472. // load Q8 and multiply it with upper Q4 nibble
  4473. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4474. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4475. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  4476. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  4477. }
  4478. *s = sumf;
  4479. #else
  4480. uint8_t aux8[QK_K];
  4481. int16_t aux16[16];
  4482. float sums [8];
  4483. memset(sums, 0, 8*sizeof(float));
  4484. uint16_t s16[2];
  4485. const uint8_t * restrict scales = (const uint8_t *)s16;
  4486. float sumf = 0;
  4487. for (int i = 0; i < nb; ++i) {
  4488. const uint8_t * restrict q4 = x[i].qs;
  4489. const int8_t * restrict q8 = y[i].qs;
  4490. uint8_t * restrict a = aux8;
  4491. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  4492. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  4493. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4494. s16[0] = b[0] & 0x0f0f;
  4495. s16[1] = (b[0] >> 4) & 0x0f0f;
  4496. 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]));
  4497. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4498. for (int j = 0; j < QK_K/32; ++j) {
  4499. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  4500. q8 += 16; a += 16;
  4501. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  4502. q8 += 16; a += 16;
  4503. const float dl = d * scales[j];
  4504. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  4505. }
  4506. }
  4507. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4508. *s = sumf;
  4509. #endif
  4510. }
  4511. #endif
  4512. #if QK_K == 256
  4513. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4514. assert(n % QK_K == 0);
  4515. const block_q5_K * restrict x = vx;
  4516. const block_q8_K * restrict y = vy;
  4517. const int nb = n / QK_K;
  4518. static const uint32_t kmask1 = 0x3f3f3f3f;
  4519. static const uint32_t kmask2 = 0x0f0f0f0f;
  4520. static const uint32_t kmask3 = 0x03030303;
  4521. uint32_t utmp[4];
  4522. #ifdef __ARM_NEON
  4523. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4524. const uint8x16_t mone = vdupq_n_u8(1);
  4525. const uint8x16_t mtwo = vdupq_n_u8(2);
  4526. #if defined(__ARM_FEATURE_DOTPROD)
  4527. const int32x4_t mzero = vdupq_n_s32(0);
  4528. #endif
  4529. int8x16x4_t q5bytes;
  4530. float sumf = 0;
  4531. for (int i = 0; i < nb; ++i) {
  4532. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4533. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4534. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4535. memcpy(utmp, x[i].scales, 12);
  4536. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4537. const uint32_t uaux = utmp[1] & kmask1;
  4538. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4539. utmp[2] = uaux;
  4540. utmp[0] &= kmask1;
  4541. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  4542. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  4543. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4544. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4545. int32_t sumi_mins = vaddvq_s32(prod);
  4546. const uint8_t * scales = (const uint8_t *)utmp;
  4547. const uint8_t * restrict q5 = x[i].qs;
  4548. const uint8_t * restrict qh = x[i].qh;
  4549. const int8_t * restrict q8 = y[i].qs;
  4550. uint8x16x2_t qhbits = vld1q_u8_x2(qh);
  4551. uint8x16x4_t q5h;
  4552. int32_t sumi = 0;
  4553. for (int j = 0; j < QK_K/64; ++j) {
  4554. const uint8x16x2_t q5bits = vld1q_u8_x2(q5); q5 += 32;
  4555. const int8x16x4_t q8bytes = vld1q_s8_x4(q8); q8 += 64;
  4556. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  4557. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  4558. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  4559. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  4560. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  4561. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  4562. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  4563. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  4564. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  4565. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  4566. #if defined(__ARM_FEATURE_DOTPROD)
  4567. sumi += vaddvq_s32(vdotq_s32(vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  4568. sumi += vaddvq_s32(vdotq_s32(vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  4569. #else
  4570. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  4571. vmull_s8(vget_high_s8(q5bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  4572. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  4573. vmull_s8(vget_high_s8(q5bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  4574. sumi += vaddvq_s16(vaddq_s16(p0, p1)) * *scales++;
  4575. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  4576. vmull_s8(vget_high_s8(q5bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  4577. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  4578. vmull_s8(vget_high_s8(q5bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  4579. sumi += vaddvq_s16(vaddq_s16(p2, p3)) * *scales++;
  4580. #endif
  4581. }
  4582. sumf += d * sumi - dmin * sumi_mins;
  4583. }
  4584. *s = sumf;
  4585. #elif defined __AVX2__
  4586. const __m256i m4 = _mm256_set1_epi8(0xF);
  4587. const __m128i mzero = _mm_setzero_si128();
  4588. const __m256i mone = _mm256_set1_epi8(1);
  4589. __m256 acc = _mm256_setzero_ps();
  4590. float summs = 0.f;
  4591. for (int i = 0; i < nb; ++i) {
  4592. const uint8_t * restrict q5 = x[i].qs;
  4593. const int8_t * restrict q8 = y[i].qs;
  4594. #if QK_K == 256
  4595. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4596. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4597. memcpy(utmp, x[i].scales, 12);
  4598. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4599. const uint32_t uaux = utmp[1] & kmask1;
  4600. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4601. utmp[2] = uaux;
  4602. utmp[0] &= kmask1;
  4603. #else
  4604. // TODO
  4605. const float d = 0, dmin = 0;
  4606. #endif
  4607. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4608. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4609. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4610. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4611. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4612. summs += dmin * _mm_extract_epi32(hsum, 0);
  4613. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4614. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4615. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  4616. __m256i hmask = mone;
  4617. __m256i sumi = _mm256_setzero_si256();
  4618. int bit = 0;
  4619. for (int j = 0; j < QK_K/64; ++j) {
  4620. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4621. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4622. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  4623. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4624. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4625. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  4626. hmask = _mm256_slli_epi16(hmask, 1);
  4627. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  4628. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4629. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  4630. hmask = _mm256_slli_epi16(hmask, 1);
  4631. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4632. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4633. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  4634. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  4635. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4636. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4637. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  4638. }
  4639. __m256 vd = _mm256_set1_ps(d);
  4640. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4641. }
  4642. *s = hsum_float_8(acc) + summs;
  4643. #elif defined __AVX__
  4644. const __m128i m4 = _mm_set1_epi8(0xF);
  4645. const __m128i mzero = _mm_setzero_si128();
  4646. const __m128i mone = _mm_set1_epi8(1);
  4647. const __m128i m2 = _mm_set1_epi8(2);
  4648. __m256 acc = _mm256_setzero_ps();
  4649. float summs = 0.f;
  4650. for (int i = 0; i < nb; ++i) {
  4651. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4652. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4653. const uint8_t * restrict q5 = x[i].qs;
  4654. const int8_t * restrict q8 = y[i].qs;
  4655. memcpy(utmp, x[i].scales, 12);
  4656. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4657. const uint32_t uaux = utmp[1] & kmask1;
  4658. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4659. utmp[2] = uaux;
  4660. utmp[0] &= kmask1;
  4661. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4662. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4663. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4664. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4665. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4666. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4667. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4668. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4669. summs += dmin * _mm_extract_epi32(hsum, 0);
  4670. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  4671. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  4672. __m128i hmask = mone;
  4673. __m128i sumi_0 = _mm_setzero_si128();
  4674. __m128i sumi_1 = _mm_setzero_si128();
  4675. int bit = 0;
  4676. __m128i shuffle = _mm_set1_epi16(0x0100);
  4677. for (int j = 0; j < QK_K/64; ++j) {
  4678. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  4679. shuffle = _mm_add_epi16(shuffle, m2);
  4680. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  4681. shuffle = _mm_add_epi16(shuffle, m2);
  4682. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4683. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4684. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  4685. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  4686. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4687. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4688. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4689. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4690. hmask = _mm_slli_epi16(hmask, 1);
  4691. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4692. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4693. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  4694. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  4695. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4696. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  4697. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  4698. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  4699. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4700. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4701. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4702. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4703. hmask = _mm_slli_epi16(hmask, 1);
  4704. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4705. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4706. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  4707. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  4708. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  4709. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  4710. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4711. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  4712. }
  4713. __m256 vd = _mm256_set1_ps(d);
  4714. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4715. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4716. }
  4717. *s = hsum_float_8(acc) + summs;
  4718. #elif defined __riscv_v_intrinsic
  4719. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4720. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4721. float sumf = 0;
  4722. float sums = 0.0;
  4723. size_t vl;
  4724. for (int i = 0; i < nb; ++i) {
  4725. vl = 8;
  4726. const uint8_t * restrict q5 = x[i].qs;
  4727. const uint8_t * restrict hm = x[i].qh;
  4728. const int8_t * restrict q8 = y[i].qs;
  4729. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4730. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4731. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4732. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4733. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4734. memcpy(utmp, x[i].scales, 12);
  4735. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4736. const uint32_t uaux = utmp[1] & kmask1;
  4737. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4738. utmp[2] = uaux;
  4739. utmp[0] &= kmask1;
  4740. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4741. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4742. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4743. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4744. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4745. vl = 32;
  4746. int32_t aux32 = 0;
  4747. int is = 0;
  4748. uint8_t m = 1;
  4749. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4750. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  4751. for (int j = 0; j < QK_K/64; ++j) {
  4752. // load Q5 and Q8
  4753. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  4754. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  4755. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  4756. // compute mask for addition
  4757. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  4758. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4759. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  4760. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  4761. m <<= 1;
  4762. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  4763. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4764. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  4765. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  4766. m <<= 1;
  4767. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  4768. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  4769. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  4770. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  4771. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  4772. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  4773. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  4774. q5 += 32; q8 += 64;
  4775. }
  4776. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  4777. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  4778. }
  4779. *s = sumf+sums;
  4780. #else
  4781. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4782. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4783. int8_t aux8[QK_K];
  4784. int16_t aux16[8];
  4785. float sums [8];
  4786. int32_t aux32[8];
  4787. memset(sums, 0, 8*sizeof(float));
  4788. float sumf = 0;
  4789. for (int i = 0; i < nb; ++i) {
  4790. const uint8_t * restrict q4 = x[i].qs;
  4791. const uint8_t * restrict hm = x[i].qh;
  4792. const int8_t * restrict q8 = y[i].qs;
  4793. memset(aux32, 0, 8*sizeof(int32_t));
  4794. int8_t * restrict a = aux8;
  4795. uint8_t m = 1;
  4796. for (int j = 0; j < QK_K/64; ++j) {
  4797. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4798. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4799. a += 32; m <<= 1;
  4800. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4801. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4802. a += 32; m <<= 1;
  4803. q4 += 32;
  4804. }
  4805. memcpy(utmp, x[i].scales, 12);
  4806. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4807. const uint32_t uaux = utmp[1] & kmask1;
  4808. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4809. utmp[2] = uaux;
  4810. utmp[0] &= kmask1;
  4811. int sumi = 0;
  4812. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4813. a = aux8;
  4814. int is = 0;
  4815. for (int j = 0; j < QK_K/32; ++j) {
  4816. int32_t scale = scales[is++];
  4817. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4818. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4819. q8 += 8; a += 8;
  4820. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4821. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4822. q8 += 8; a += 8;
  4823. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4824. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4825. q8 += 8; a += 8;
  4826. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4827. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4828. q8 += 8; a += 8;
  4829. }
  4830. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4831. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4832. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4833. sumf -= dmin * sumi;
  4834. }
  4835. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4836. *s = sumf;
  4837. #endif
  4838. }
  4839. #else
  4840. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4841. assert(n % QK_K == 0);
  4842. const block_q5_K * restrict x = vx;
  4843. const block_q8_K * restrict y = vy;
  4844. const int nb = n / QK_K;
  4845. #ifdef __ARM_NEON
  4846. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4847. const uint8x16_t mh = vdupq_n_u8(16);
  4848. #if defined(__ARM_FEATURE_DOTPROD)
  4849. const int32x4_t mzero = vdupq_n_s32(0);
  4850. #endif
  4851. int8x16x4_t q5bytes;
  4852. uint8x16x4_t q5h;
  4853. float sumf = 0;
  4854. for (int i = 0; i < nb; ++i) {
  4855. const float d = y[i].d * (float)x[i].d;
  4856. const int8_t * sc = x[i].scales;
  4857. const uint8_t * restrict q5 = x[i].qs;
  4858. const uint8_t * restrict qh = x[i].qh;
  4859. const int8_t * restrict q8 = y[i].qs;
  4860. const uint8x8_t qhbits = vld1_u8(qh);
  4861. const uint8x16x2_t q5bits = vld1q_u8_x2(q5);
  4862. const int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  4863. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  4864. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  4865. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  4866. q5h.val[2] = vbicq_u8(mh, htmp);
  4867. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  4868. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  4869. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  4870. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  4871. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  4872. #if defined(__ARM_FEATURE_DOTPROD)
  4873. int32_t sumi1 = sc[0] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  4874. int32_t sumi2 = sc[1] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  4875. int32_t sumi3 = sc[2] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  4876. int32_t sumi4 = sc[3] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  4877. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  4878. #else
  4879. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  4880. vmull_s8(vget_high_s8(q5bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  4881. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  4882. vmull_s8(vget_high_s8(q5bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  4883. int32_t sumi = sc[0] * vaddvq_s16(p0) + sc[1] * vaddvq_s16(p1);
  4884. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  4885. vmull_s8(vget_high_s8(q5bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  4886. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  4887. vmull_s8(vget_high_s8(q5bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  4888. sumi += sc[2] * vaddvq_s16(p2) + sc[3] * vaddvq_s16(p3);
  4889. sumf += d*sumi;
  4890. #endif
  4891. }
  4892. *s = sumf;
  4893. #elif defined __AVX2__
  4894. const __m256i m4 = _mm256_set1_epi8(0xF);
  4895. const __m256i mone = _mm256_set1_epi8(1);
  4896. __m256 acc = _mm256_setzero_ps();
  4897. for (int i = 0; i < nb; ++i) {
  4898. const uint8_t * restrict q5 = x[i].qs;
  4899. const int8_t * restrict q8 = y[i].qs;
  4900. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4901. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  4902. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  4903. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  4904. int64_t aux64;
  4905. memcpy(&aux64, x[i].qh, 8);
  4906. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  4907. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  4908. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  4909. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  4910. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4911. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  4912. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4913. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4914. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  4915. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  4916. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  4917. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  4918. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  4919. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  4920. }
  4921. *s = hsum_float_8(acc);
  4922. #elif defined __AVX__
  4923. const __m128i m4 = _mm_set1_epi8(0xF);
  4924. const __m128i mone = _mm_set1_epi8(1);
  4925. __m256 acc = _mm256_setzero_ps();
  4926. for (int i = 0; i < nb; ++i) {
  4927. const uint8_t * restrict q5 = x[i].qs;
  4928. const int8_t * restrict q8 = y[i].qs;
  4929. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4930. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  4931. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  4932. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  4933. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  4934. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  4935. int64_t aux64;
  4936. memcpy(&aux64, x[i].qh, 8);
  4937. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  4938. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  4939. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  4940. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  4941. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  4942. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  4943. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  4944. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  4945. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  4946. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  4947. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4948. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4949. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  4950. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  4951. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  4952. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  4953. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  4954. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  4955. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  4956. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  4957. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  4958. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  4959. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  4960. }
  4961. *s = hsum_float_8(acc);
  4962. #elif defined __riscv_v_intrinsic
  4963. float sumf = 0;
  4964. for (int i = 0; i < nb; ++i) {
  4965. const float d = y[i].d * (float)x[i].d;
  4966. const int8_t * sc = x[i].scales;
  4967. const uint8_t * restrict q5 = x[i].qs;
  4968. const uint8_t * restrict qh = x[i].qh;
  4969. const int8_t * restrict q8 = y[i].qs;
  4970. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4971. // load qh
  4972. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  4973. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4974. size_t vl = 16;
  4975. // combine both qh_1 and qh_2
  4976. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4977. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  4978. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  4979. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  4980. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  4981. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  4982. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  4983. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  4984. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  4985. // load q5
  4986. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  4987. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  4988. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  4989. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  4990. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  4991. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  4992. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  4993. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  4994. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  4995. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  4996. // load Q8 and multiply it with Q5
  4997. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4998. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4999. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5000. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5001. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5002. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5003. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5004. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5005. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5006. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5007. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5008. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5009. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5010. }
  5011. *s = sumf;
  5012. #else
  5013. int8_t aux8[QK_K];
  5014. int16_t aux16[16];
  5015. float sums [8];
  5016. memset(sums, 0, 8*sizeof(float));
  5017. float sumf = 0;
  5018. for (int i = 0; i < nb; ++i) {
  5019. const uint8_t * restrict q4 = x[i].qs;
  5020. const uint8_t * restrict hm = x[i].qh;
  5021. const int8_t * restrict q8 = y[i].qs;
  5022. int8_t * restrict a = aux8;
  5023. for (int l = 0; l < 32; ++l) {
  5024. a[l+ 0] = q4[l] & 0xF;
  5025. a[l+32] = q4[l] >> 4;
  5026. }
  5027. for (int is = 0; is < 8; ++is) {
  5028. uint8_t m = 1 << is;
  5029. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5030. }
  5031. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5032. const int8_t * restrict sc = x[i].scales;
  5033. for (int j = 0; j < QK_K/16; ++j) {
  5034. const float dl = d * sc[j];
  5035. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5036. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5037. q8 += 16; a += 16;
  5038. }
  5039. }
  5040. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5041. *s = sumf;
  5042. #endif
  5043. }
  5044. #endif
  5045. #if QK_K == 256
  5046. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5047. assert(n % QK_K == 0);
  5048. const block_q6_K * restrict x = vx;
  5049. const block_q8_K * restrict y = vy;
  5050. const int nb = n / QK_K;
  5051. #ifdef __ARM_NEON
  5052. float sum = 0;
  5053. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5054. #if defined(__ARM_FEATURE_DOTPROD)
  5055. const int32x4_t vzero = vdupq_n_s32(0);
  5056. #endif
  5057. //const int8x16_t m32s = vdupq_n_s8(32);
  5058. const uint8x16_t mone = vdupq_n_u8(3);
  5059. int8x16x4_t q6bytes;
  5060. uint8x16x4_t q6h;
  5061. for (int i = 0; i < nb; ++i) {
  5062. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5063. const uint8_t * restrict q6 = x[i].ql;
  5064. const uint8_t * restrict qh = x[i].qh;
  5065. const int8_t * restrict q8 = y[i].qs;
  5066. const int8_t * restrict scale = x[i].scales;
  5067. const int16x8x2_t q8sums = vld1q_s16_x2(y[i].bsums);
  5068. const int8x16_t scales = vld1q_s8(scale);
  5069. const int16x8x2_t q6scales = {vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))};
  5070. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5071. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5072. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5073. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5074. int32_t isum_mins = vaddvq_s32(prod);
  5075. int32_t isum = 0;
  5076. for (int j = 0; j < QK_K/128; ++j) {
  5077. uint8x16x2_t qhbits = vld1q_u8_x2(qh); qh += 32;
  5078. uint8x16x4_t q6bits = vld1q_u8_x4(q6); q6 += 64;
  5079. int8x16x4_t q8bytes = vld1q_s8_x4(q8); q8 += 64;
  5080. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5081. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5082. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5083. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5084. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5085. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5086. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5087. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5088. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5089. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5090. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5091. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  5092. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  5093. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  5094. #if defined(__ARM_FEATURE_DOTPROD)
  5095. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5096. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5097. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5098. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5099. scale += 4;
  5100. #else
  5101. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  5102. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  5103. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  5104. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  5105. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  5106. scale += 2;
  5107. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  5108. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  5109. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  5110. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  5111. isum += vaddvq_s16(p2) * scale[0] + vaddvq_s16(p3) * scale[1];
  5112. scale += 2;
  5113. #endif
  5114. q8bytes = vld1q_s8_x4(q8); q8 += 64;
  5115. shifted = vshrq_n_u8(qhbits.val[0], 4);
  5116. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5117. shifted = vshrq_n_u8(qhbits.val[1], 4);
  5118. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5119. shifted = vshrq_n_u8(qhbits.val[0], 6);
  5120. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5121. shifted = vshrq_n_u8(qhbits.val[1], 6);
  5122. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5123. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  5124. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  5125. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  5126. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  5127. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  5128. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  5129. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  5130. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  5131. #if defined(__ARM_FEATURE_DOTPROD)
  5132. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5133. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5134. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5135. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5136. scale += 4;
  5137. //for (int l = 0; l < 4; ++l) {
  5138. // const int32x4_t p = vdotq_s32(vzero, q6bytes.val[l], q8bytes.val[l]);
  5139. // isum += vaddvq_s32(p) * *scale++;
  5140. //}
  5141. #else
  5142. p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  5143. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  5144. p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  5145. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  5146. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  5147. scale += 2;
  5148. p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  5149. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  5150. p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  5151. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  5152. isum += vaddvq_s16(p2) * scale[0] + vaddvq_s16(p3) * scale[1];
  5153. scale += 2;
  5154. #endif
  5155. }
  5156. //sum += isum * d_all * y[i].d;
  5157. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  5158. }
  5159. *s = sum;
  5160. #elif defined __AVX2__
  5161. const __m256i m4 = _mm256_set1_epi8(0xF);
  5162. const __m256i m2 = _mm256_set1_epi8(3);
  5163. const __m256i m32s = _mm256_set1_epi8(32);
  5164. __m256 acc = _mm256_setzero_ps();
  5165. for (int i = 0; i < nb; ++i) {
  5166. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5167. const uint8_t * restrict q4 = x[i].ql;
  5168. const uint8_t * restrict qh = x[i].qh;
  5169. const int8_t * restrict q8 = y[i].qs;
  5170. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5171. __m256i sumi = _mm256_setzero_si256();
  5172. int is = 0;
  5173. for (int j = 0; j < QK_K/128; ++j) {
  5174. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  5175. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  5176. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  5177. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  5178. is += 4;
  5179. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5180. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5181. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  5182. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  5183. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  5184. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  5185. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  5186. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5187. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  5188. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  5189. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  5190. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5191. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5192. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5193. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5194. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5195. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5196. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  5197. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  5198. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5199. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5200. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  5201. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  5202. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5203. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5204. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5205. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5206. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5207. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5208. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  5209. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  5210. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5211. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  5212. }
  5213. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5214. }
  5215. *s = hsum_float_8(acc);
  5216. #elif defined __AVX__
  5217. const __m128i m4 = _mm_set1_epi8(0xF);
  5218. const __m128i m3 = _mm_set1_epi8(3);
  5219. const __m128i m32s = _mm_set1_epi8(32);
  5220. const __m128i m2 = _mm_set1_epi8(2);
  5221. __m256 acc = _mm256_setzero_ps();
  5222. for (int i = 0; i < nb; ++i) {
  5223. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5224. const uint8_t * restrict q4 = x[i].ql;
  5225. const uint8_t * restrict qh = x[i].qh;
  5226. const int8_t * restrict q8 = y[i].qs;
  5227. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5228. __m128i sumi_0 = _mm_setzero_si128();
  5229. __m128i sumi_1 = _mm_setzero_si128();
  5230. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  5231. for (int j = 0; j < QK_K/128; ++j) {
  5232. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5233. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5234. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  5235. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  5236. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  5237. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  5238. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  5239. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  5240. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  5241. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  5242. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5243. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5244. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5245. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5246. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  5247. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  5248. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  5249. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  5250. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  5251. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  5252. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  5253. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  5254. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5255. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5256. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5257. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5258. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5259. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5260. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5261. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5262. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  5263. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  5264. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  5265. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  5266. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  5267. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  5268. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  5269. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  5270. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  5271. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  5272. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  5273. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  5274. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  5275. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  5276. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  5277. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  5278. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5279. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5280. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5281. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5282. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5283. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5284. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5285. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5286. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5287. shuffle = _mm_add_epi8(shuffle, m2);
  5288. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5289. shuffle = _mm_add_epi8(shuffle, m2);
  5290. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  5291. shuffle = _mm_add_epi8(shuffle, m2);
  5292. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  5293. shuffle = _mm_add_epi8(shuffle, m2);
  5294. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5295. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5296. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5297. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5298. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  5299. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  5300. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  5301. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  5302. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5303. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5304. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  5305. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  5306. }
  5307. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5308. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  5309. }
  5310. *s = hsum_float_8(acc);
  5311. #elif defined __riscv_v_intrinsic
  5312. float sumf = 0;
  5313. for (int i = 0; i < nb; ++i) {
  5314. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5315. const uint8_t * restrict q6 = x[i].ql;
  5316. const uint8_t * restrict qh = x[i].qh;
  5317. const int8_t * restrict q8 = y[i].qs;
  5318. const int8_t * restrict scale = x[i].scales;
  5319. size_t vl;
  5320. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5321. int sum_t = 0;
  5322. int is = 0;
  5323. for (int j = 0; j < QK_K/128; ++j) {
  5324. vl = 32;
  5325. // load qh
  5326. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  5327. // load Q6
  5328. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  5329. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  5330. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  5331. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  5332. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  5333. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  5334. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  5335. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  5336. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  5337. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  5338. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  5339. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  5340. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  5341. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  5342. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  5343. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  5344. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  5345. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  5346. // load Q8 and take product
  5347. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  5348. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5349. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  5350. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  5351. vl = 16;
  5352. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  5353. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  5354. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  5355. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  5356. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  5357. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  5358. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  5359. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  5360. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  5361. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  5362. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  5363. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  5364. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  5365. q6 += 64; qh += 32; q8 += 128; is=8;
  5366. }
  5367. sumf += d * sum_t;
  5368. }
  5369. *s = sumf;
  5370. #else
  5371. int8_t aux8[QK_K];
  5372. int16_t aux16[8];
  5373. float sums [8];
  5374. int32_t aux32[8];
  5375. memset(sums, 0, 8*sizeof(float));
  5376. float sumf = 0;
  5377. for (int i = 0; i < nb; ++i) {
  5378. const uint8_t * restrict q4 = x[i].ql;
  5379. const uint8_t * restrict qh = x[i].qh;
  5380. const int8_t * restrict q8 = y[i].qs;
  5381. memset(aux32, 0, 8*sizeof(int32_t));
  5382. int8_t * restrict a = aux8;
  5383. for (int j = 0; j < QK_K; j += 128) {
  5384. for (int l = 0; l < 32; ++l) {
  5385. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5386. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5387. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5388. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5389. }
  5390. a += 128;
  5391. q4 += 64;
  5392. qh += 32;
  5393. }
  5394. a = aux8;
  5395. int is = 0;
  5396. for (int j = 0; j < QK_K/16; ++j) {
  5397. int scale = x[i].scales[is++];
  5398. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5399. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5400. q8 += 8; a += 8;
  5401. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5402. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5403. q8 += 8; a += 8;
  5404. }
  5405. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5406. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5407. }
  5408. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5409. *s = sumf;
  5410. #endif
  5411. }
  5412. #else
  5413. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5414. assert(n % QK_K == 0);
  5415. const block_q6_K * restrict x = vx;
  5416. const block_q8_K * restrict y = vy;
  5417. const int nb = n / QK_K;
  5418. #ifdef __ARM_NEON
  5419. float sum = 0;
  5420. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5421. const int8x16_t m32s = vdupq_n_s8(32);
  5422. #if defined(__ARM_FEATURE_DOTPROD)
  5423. const int32x4_t vzero = vdupq_n_s32(0);
  5424. #endif
  5425. const uint8x16_t mone = vdupq_n_u8(3);
  5426. int8x16x4_t q6bytes;
  5427. uint8x16x4_t q6h;
  5428. for (int i = 0; i < nb; ++i) {
  5429. const float d_all = (float)x[i].d;
  5430. const uint8_t * restrict q6 = x[i].ql;
  5431. const uint8_t * restrict qh = x[i].qh;
  5432. const int8_t * restrict q8 = y[i].qs;
  5433. const int8_t * restrict scale = x[i].scales;
  5434. int32_t isum = 0;
  5435. uint8x16_t qhbits = vld1q_u8(qh);
  5436. uint8x16x2_t q6bits = vld1q_u8_x2(q6);
  5437. int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  5438. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  5439. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  5440. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5441. shifted = vshrq_n_u8(qhbits, 4);
  5442. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5443. shifted = vshrq_n_u8(qhbits, 6);
  5444. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5445. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5446. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5447. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  5448. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  5449. #if defined(__ARM_FEATURE_DOTPROD)
  5450. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5451. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5452. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5453. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5454. #else
  5455. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  5456. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  5457. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  5458. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  5459. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  5460. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  5461. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  5462. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  5463. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  5464. isum += vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  5465. #endif
  5466. sum += isum * d_all * y[i].d;
  5467. }
  5468. *s = sum;
  5469. #elif defined __AVX2__
  5470. const __m256i m4 = _mm256_set1_epi8(0xF);
  5471. const __m256i m2 = _mm256_set1_epi8(3);
  5472. const __m256i m32s = _mm256_set1_epi8(32);
  5473. __m256 acc = _mm256_setzero_ps();
  5474. for (int i = 0; i < nb; ++i) {
  5475. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5476. const uint8_t * restrict q4 = x[i].ql;
  5477. const uint8_t * restrict qh = x[i].qh;
  5478. const int8_t * restrict q8 = y[i].qs;
  5479. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5480. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5481. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5482. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5483. __m256i sumi = _mm256_setzero_si256();
  5484. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5485. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5486. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5487. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5488. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  5489. 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);
  5490. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5491. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  5492. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5493. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5494. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5495. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5496. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5497. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5498. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5499. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5500. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5501. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5502. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5503. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5504. }
  5505. *s = hsum_float_8(acc);
  5506. #elif defined __AVX__
  5507. const __m128i m4 = _mm_set1_epi8(0xF);
  5508. const __m128i m2 = _mm_set1_epi8(3);
  5509. const __m128i m32s = _mm_set1_epi8(32);
  5510. __m256 acc = _mm256_setzero_ps();
  5511. for (int i = 0; i < nb; ++i) {
  5512. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5513. const uint8_t * restrict q4 = x[i].ql;
  5514. const uint8_t * restrict qh = x[i].qh;
  5515. const int8_t * restrict q8 = y[i].qs;
  5516. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5517. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5518. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5519. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5520. __m128i sumi_0 = _mm_setzero_si128();
  5521. __m128i sumi_1 = _mm_setzero_si128();
  5522. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5523. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5524. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5525. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5526. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  5527. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  5528. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  5529. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  5530. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  5531. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  5532. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  5533. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  5534. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5535. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5536. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  5537. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  5538. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  5539. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  5540. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5541. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5542. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5543. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5544. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5545. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5546. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5547. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5548. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5549. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5550. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5551. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5552. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5553. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5554. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  5555. }
  5556. *s = hsum_float_8(acc);
  5557. #elif defined __riscv_v_intrinsic
  5558. float sumf = 0;
  5559. for (int i = 0; i < nb; ++i) {
  5560. const float d_all = (float)x[i].d;
  5561. const uint8_t * restrict q6 = x[i].ql;
  5562. const uint8_t * restrict qh = x[i].qh;
  5563. const int8_t * restrict q8 = y[i].qs;
  5564. const int8_t * restrict scale = x[i].scales;
  5565. int32_t isum = 0;
  5566. size_t vl = 16;
  5567. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5568. // load Q6
  5569. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  5570. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  5571. // load qh
  5572. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  5573. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5574. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5575. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5576. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5577. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5578. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5579. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5580. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  5581. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  5582. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  5583. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  5584. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  5585. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  5586. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  5587. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  5588. // load Q8 and take product
  5589. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5590. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5591. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5592. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5593. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5594. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5595. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5596. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5597. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  5598. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  5599. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  5600. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  5601. sumf += isum * d_all * y[i].d;
  5602. }
  5603. *s = sumf;
  5604. #else
  5605. int8_t aux8[QK_K];
  5606. int16_t aux16[8];
  5607. float sums [8];
  5608. int32_t aux32[8];
  5609. memset(sums, 0, 8*sizeof(float));
  5610. float sumf = 0;
  5611. for (int i = 0; i < nb; ++i) {
  5612. const uint8_t * restrict q4 = x[i].ql;
  5613. const uint8_t * restrict qh = x[i].qh;
  5614. const int8_t * restrict q8 = y[i].qs;
  5615. memset(aux32, 0, 8*sizeof(int32_t));
  5616. int8_t * restrict a = aux8;
  5617. for (int l = 0; l < 16; ++l) {
  5618. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5619. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5620. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5621. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5622. }
  5623. int is = 0;
  5624. for (int j = 0; j < QK_K/16; ++j) {
  5625. int scale = x[i].scales[is++];
  5626. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5627. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5628. q8 += 8; a += 8;
  5629. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5630. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5631. q8 += 8; a += 8;
  5632. }
  5633. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5634. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5635. }
  5636. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5637. *s = sumf;
  5638. #endif
  5639. }
  5640. #endif