ggml-quants.c 304 KB

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