ggml-quants.c 265 KB

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