ggml-quants.c 284 KB

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