ggml-quants.c 304 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701
  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. static const uint64_t iq2xxs_grid[256] = {
  1963. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1964. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1965. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1966. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1967. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1968. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1969. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1970. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1971. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1972. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1973. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1974. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1975. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1976. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1977. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1978. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1979. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1980. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1981. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1982. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1983. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1984. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1985. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1986. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1987. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1988. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1989. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1990. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1991. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1992. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1993. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1994. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1995. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1996. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1997. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1998. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1999. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2000. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2001. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2002. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2003. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2004. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2005. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2006. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2007. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2008. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2009. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2010. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2011. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2012. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2013. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2014. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2015. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2016. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2017. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2018. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2019. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2020. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2021. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2022. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2023. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2024. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2025. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2026. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2027. };
  2028. static const uint64_t iq2xs_grid[512] = {
  2029. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2030. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2031. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2032. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2033. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2034. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2035. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2036. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2037. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2038. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2039. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2040. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2041. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2042. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2043. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2044. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2045. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2046. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2047. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2048. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2049. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2050. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2051. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2052. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2053. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2054. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2055. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2056. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2057. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2058. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2059. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2060. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2061. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2062. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2063. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2064. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2065. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2066. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2067. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2068. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2069. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2070. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2071. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2072. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2073. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2074. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2075. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2076. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2077. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2078. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2079. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2080. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2081. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2082. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2083. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2084. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2085. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2086. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2087. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2088. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2089. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2090. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2091. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2092. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2093. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2094. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2095. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2096. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2097. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2098. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2099. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2100. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2101. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2102. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2103. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2104. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2105. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2106. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2107. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2108. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2109. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2110. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2111. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2112. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2113. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2114. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2115. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2116. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2117. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2118. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2119. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2120. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2121. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2122. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2123. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2124. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2125. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2126. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2127. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2128. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2129. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2130. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2131. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2132. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2133. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2134. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2135. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2136. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2137. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2138. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2139. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2140. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2141. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2142. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2143. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2144. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2145. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2146. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2147. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2148. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2149. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2150. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2151. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2152. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2153. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2154. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2155. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2156. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2157. };
  2158. static const uint8_t ksigns_iq2xs[128] = {
  2159. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2160. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2161. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2162. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2163. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2164. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2165. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2166. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2167. };
  2168. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2169. void quantize_row_iq2_xxs_reference(const float * restrict x, block_iq2_xxs * restrict y, int k) {
  2170. (void)x;
  2171. (void)y;
  2172. (void)k;
  2173. assert(k % QK_K == 0);
  2174. //fprintf(stderr, "=========================== %s: not implemented\n", __func__);
  2175. }
  2176. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2177. assert(k % QK_K == 0);
  2178. const int nb = k / QK_K;
  2179. uint32_t aux32[2];
  2180. const uint8_t * aux8 = (const uint8_t *)aux32;
  2181. for (int i = 0; i < nb; i++) {
  2182. const float d = GGML_FP16_TO_FP32(x[i].d);
  2183. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2184. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2185. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2186. for (int l = 0; l < 4; ++l) {
  2187. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2188. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2189. for (int j = 0; j < 8; ++j) {
  2190. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2191. }
  2192. y += 8;
  2193. }
  2194. }
  2195. }
  2196. }
  2197. void quantize_row_iq2_xxs(const float * restrict x, void * restrict vy, int k) {
  2198. assert(k % QK_K == 0);
  2199. block_iq2_xxs * restrict y = vy;
  2200. quantize_row_iq2_xxs_reference(x, y, k);
  2201. }
  2202. size_t ggml_quantize_iq2_xxs(const float * src, void * dst, int n, int k, int64_t * hist) {
  2203. assert(k % QK_K == 0);
  2204. (void)hist; // TODO: collect histograms
  2205. for (int j = 0; j < n; j += k) {
  2206. block_iq2_xxs * restrict y = (block_iq2_xxs *)dst + j/QK_K;
  2207. quantize_row_iq2_xxs_reference(src + j, y, k);
  2208. }
  2209. return (n/QK_K*sizeof(block_iq2_xxs));
  2210. }
  2211. // ====================== 2.3125 bpw (de)-quantization
  2212. void quantize_row_iq2_xs_reference(const float * restrict x, block_iq2_xs * restrict y, int k) {
  2213. (void)x;
  2214. (void)y;
  2215. (void)k;
  2216. assert(k % QK_K == 0);
  2217. //fprintf(stderr, "=========================== %s: not implemented\n", __func__);
  2218. }
  2219. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2220. assert(k % QK_K == 0);
  2221. const int nb = k / QK_K;
  2222. float db[2];
  2223. for (int i = 0; i < nb; i++) {
  2224. const float d = GGML_FP16_TO_FP32(x[i].d);
  2225. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2226. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2227. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2228. for (int l = 0; l < 4; ++l) {
  2229. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2230. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2231. for (int j = 0; j < 8; ++j) {
  2232. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2233. }
  2234. y += 8;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. void quantize_row_iq2_xs(const float * restrict x, void * restrict vy, int k) {
  2240. assert(k % QK_K == 0);
  2241. block_iq2_xs * restrict y = vy;
  2242. quantize_row_iq2_xs_reference(x, y, k);
  2243. }
  2244. size_t ggml_quantize_iq2_xs(const float * src, void * dst, int n, int k, int64_t * hist) {
  2245. assert(k % QK_K == 0);
  2246. (void)hist; // TODO: collect histograms
  2247. for (int j = 0; j < n; j += k) {
  2248. block_iq2_xs * restrict y = (block_iq2_xs *)dst + j/QK_K;
  2249. quantize_row_iq2_xs_reference(src + j, y, k);
  2250. }
  2251. return (n/QK_K*sizeof(block_iq2_xs));
  2252. }
  2253. //===================================== Q8_K ==============================================
  2254. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  2255. assert(k % QK_K == 0);
  2256. const int nb = k / QK_K;
  2257. for (int i = 0; i < nb; i++) {
  2258. float max = 0;
  2259. float amax = 0;
  2260. for (int j = 0; j < QK_K; ++j) {
  2261. float ax = fabsf(x[j]);
  2262. if (ax > amax) {
  2263. amax = ax; max = x[j];
  2264. }
  2265. }
  2266. if (!amax) {
  2267. y[i].d = 0;
  2268. memset(y[i].qs, 0, QK_K);
  2269. x += QK_K;
  2270. continue;
  2271. }
  2272. //const float iscale = -128.f/max;
  2273. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2274. const float iscale = -127.f/max;
  2275. for (int j = 0; j < QK_K; ++j) {
  2276. int v = nearest_int(iscale*x[j]);
  2277. y[i].qs[j] = MIN(127, v);
  2278. }
  2279. for (int j = 0; j < QK_K/16; ++j) {
  2280. int sum = 0;
  2281. for (int ii = 0; ii < 16; ++ii) {
  2282. sum += y[i].qs[j*16 + ii];
  2283. }
  2284. y[i].bsums[j] = sum;
  2285. }
  2286. y[i].d = 1/iscale;
  2287. x += QK_K;
  2288. }
  2289. }
  2290. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  2291. assert(k % QK_K == 0);
  2292. const int nb = k / QK_K;
  2293. for (int i = 0; i < nb; i++) {
  2294. for (int j = 0; j < QK_K; ++j) {
  2295. *y++ = x[i].d * x[i].qs[j];
  2296. }
  2297. }
  2298. }
  2299. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  2300. quantize_row_q8_K_reference(x, y, k);
  2301. }
  2302. //===================================== Dot ptoducts =================================
  2303. //
  2304. // Helper functions
  2305. //
  2306. #if __AVX__ || __AVX2__ || __AVX512F__
  2307. // shuffles to pick the required scales in dot products
  2308. static inline __m256i get_scale_shuffle_q3k(int i) {
  2309. static const uint8_t k_shuffle[128] = {
  2310. 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,
  2311. 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,
  2312. 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,
  2313. 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,
  2314. };
  2315. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2316. }
  2317. static inline __m256i get_scale_shuffle_k4(int i) {
  2318. static const uint8_t k_shuffle[256] = {
  2319. 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,
  2320. 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,
  2321. 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,
  2322. 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,
  2323. 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,
  2324. 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,
  2325. 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,
  2326. 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
  2327. };
  2328. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2329. }
  2330. static inline __m128i get_scale_shuffle(int i) {
  2331. static const uint8_t k_shuffle[128] = {
  2332. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2333. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2334. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2335. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2336. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2337. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2338. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2339. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2340. };
  2341. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2342. }
  2343. #endif
  2344. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2345. const int qk = QK8_0;
  2346. const int nb = n / qk;
  2347. assert(n % qk == 0);
  2348. const block_q4_0 * restrict x = vx;
  2349. const block_q8_0 * restrict y = vy;
  2350. #if defined(__ARM_NEON)
  2351. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2352. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2353. assert(nb % 2 == 0); // TODO: handle odd nb
  2354. for (int i = 0; i < nb; i += 2) {
  2355. const block_q4_0 * restrict x0 = &x[i + 0];
  2356. const block_q4_0 * restrict x1 = &x[i + 1];
  2357. const block_q8_0 * restrict y0 = &y[i + 0];
  2358. const block_q8_0 * restrict y1 = &y[i + 1];
  2359. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2360. const int8x16_t s8b = vdupq_n_s8(0x8);
  2361. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2362. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2363. // 4-bit -> 8-bit
  2364. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2365. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2366. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2367. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2368. // sub 8
  2369. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2370. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2371. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  2372. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  2373. // load y
  2374. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2375. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2376. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2377. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2378. // dot product into int32x4_t
  2379. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  2380. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  2381. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2382. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2383. }
  2384. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2385. #elif defined(__AVX2__)
  2386. // Initialize accumulator with zeros
  2387. __m256 acc = _mm256_setzero_ps();
  2388. // Main loop
  2389. for (int i = 0; i < nb; ++i) {
  2390. /* Compute combined scale for the block */
  2391. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2392. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2393. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  2394. const __m256i off = _mm256_set1_epi8( 8 );
  2395. bx = _mm256_sub_epi8( bx, off );
  2396. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2397. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2398. /* Multiply q with scale and accumulate */
  2399. acc = _mm256_fmadd_ps( d, q, acc );
  2400. }
  2401. *s = hsum_float_8(acc);
  2402. #elif defined(__AVX__)
  2403. // Initialize accumulator with zeros
  2404. __m256 acc = _mm256_setzero_ps();
  2405. // Main loop
  2406. for (int i = 0; i < nb; ++i) {
  2407. // Compute combined scale for the block
  2408. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2409. const __m128i lowMask = _mm_set1_epi8(0xF);
  2410. const __m128i off = _mm_set1_epi8(8);
  2411. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  2412. __m128i bx = _mm_and_si128(lowMask, tmp);
  2413. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  2414. bx = _mm_sub_epi8(bx, off);
  2415. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  2416. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  2417. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2418. bx = _mm_sub_epi8(bx, off);
  2419. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  2420. // Convert int32_t to float
  2421. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  2422. // Apply the scale, and accumulate
  2423. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  2424. }
  2425. *s = hsum_float_8(acc);
  2426. #elif defined(__SSSE3__)
  2427. // set constants
  2428. const __m128i lowMask = _mm_set1_epi8(0xF);
  2429. const __m128i off = _mm_set1_epi8(8);
  2430. // Initialize accumulator with zeros
  2431. __m128 acc_0 = _mm_setzero_ps();
  2432. __m128 acc_1 = _mm_setzero_ps();
  2433. __m128 acc_2 = _mm_setzero_ps();
  2434. __m128 acc_3 = _mm_setzero_ps();
  2435. // First round without accumulation
  2436. {
  2437. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  2438. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  2439. // Compute combined scale for the block 0 and 1
  2440. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  2441. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  2442. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2443. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  2444. bx_0 = _mm_sub_epi8(bx_0, off);
  2445. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2446. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2447. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  2448. bx_1 = _mm_sub_epi8(bx_1, off);
  2449. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2450. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  2451. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  2452. // Compute combined scale for the block 2 and 3
  2453. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  2454. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  2455. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2456. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  2457. bx_2 = _mm_sub_epi8(bx_2, off);
  2458. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2459. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2460. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  2461. bx_3 = _mm_sub_epi8(bx_3, off);
  2462. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2463. // Convert int32_t to float
  2464. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2465. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2466. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2467. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2468. // Apply the scale
  2469. acc_0 = _mm_mul_ps( d_0_1, p0 );
  2470. acc_1 = _mm_mul_ps( d_0_1, p1 );
  2471. acc_2 = _mm_mul_ps( d_2_3, p2 );
  2472. acc_3 = _mm_mul_ps( d_2_3, p3 );
  2473. }
  2474. assert(nb % 2 == 0); // TODO: handle odd nb
  2475. // Main loop
  2476. for (int i = 2; i < nb; i+=2) {
  2477. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  2478. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  2479. // Compute combined scale for the block 0 and 1
  2480. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2481. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  2482. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2483. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  2484. bx_0 = _mm_sub_epi8(bx_0, off);
  2485. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2486. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2487. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2488. bx_1 = _mm_sub_epi8(bx_1, off);
  2489. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2490. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  2491. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  2492. // Compute combined scale for the block 2 and 3
  2493. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  2494. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  2495. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2496. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  2497. bx_2 = _mm_sub_epi8(bx_2, off);
  2498. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2499. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2500. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  2501. bx_3 = _mm_sub_epi8(bx_3, off);
  2502. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2503. // Convert int32_t to float
  2504. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2505. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2506. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2507. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2508. // Apply the scale
  2509. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  2510. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  2511. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  2512. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  2513. // Acummulate
  2514. acc_0 = _mm_add_ps(p0_d, acc_0);
  2515. acc_1 = _mm_add_ps(p1_d, acc_1);
  2516. acc_2 = _mm_add_ps(p2_d, acc_2);
  2517. acc_3 = _mm_add_ps(p3_d, acc_3);
  2518. }
  2519. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  2520. #elif defined(__riscv_v_intrinsic)
  2521. float sumf = 0.0;
  2522. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2523. for (int i = 0; i < nb; i++) {
  2524. // load elements
  2525. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2526. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2527. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2528. // mask and store lower part of x, and then upper part
  2529. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2530. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2531. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2532. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2533. // subtract offset
  2534. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  2535. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  2536. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2537. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2538. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2539. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2540. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2541. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2542. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2543. }
  2544. *s = sumf;
  2545. #else
  2546. // scalar
  2547. float sumf = 0.0;
  2548. for (int i = 0; i < nb; i++) {
  2549. int sumi = 0;
  2550. for (int j = 0; j < qk/2; ++j) {
  2551. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  2552. const int v1 = (x[i].qs[j] >> 4) - 8;
  2553. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2554. }
  2555. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2556. }
  2557. *s = sumf;
  2558. #endif
  2559. }
  2560. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2561. const int qk = QK8_1;
  2562. const int nb = n / qk;
  2563. assert(n % qk == 0);
  2564. const block_q4_1 * restrict x = vx;
  2565. const block_q8_1 * restrict y = vy;
  2566. // TODO: add WASM SIMD
  2567. #if defined(__ARM_NEON)
  2568. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2569. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2570. float summs = 0;
  2571. assert(nb % 2 == 0); // TODO: handle odd nb
  2572. for (int i = 0; i < nb; i += 2) {
  2573. const block_q4_1 * restrict x0 = &x[i + 0];
  2574. const block_q4_1 * restrict x1 = &x[i + 1];
  2575. const block_q8_1 * restrict y0 = &y[i + 0];
  2576. const block_q8_1 * restrict y1 = &y[i + 1];
  2577. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  2578. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2579. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2580. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2581. // 4-bit -> 8-bit
  2582. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2583. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2584. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2585. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2586. // load y
  2587. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2588. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2589. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2590. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2591. // dot product into int32x4_t
  2592. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  2593. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  2594. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2595. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2596. }
  2597. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  2598. #elif defined(__AVX2__) || defined(__AVX__)
  2599. // Initialize accumulator with zeros
  2600. __m256 acc = _mm256_setzero_ps();
  2601. float summs = 0;
  2602. // Main loop
  2603. for (int i = 0; i < nb; ++i) {
  2604. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  2605. const float d1 = y[i].d;
  2606. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2607. const __m256 d0v = _mm256_set1_ps( d0 );
  2608. const __m256 d1v = _mm256_set1_ps( d1 );
  2609. // Compute combined scales
  2610. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  2611. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  2612. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2613. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  2614. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  2615. // Accumulate d0*d1*x*y
  2616. #if defined(__AVX2__)
  2617. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  2618. #else
  2619. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  2620. #endif
  2621. }
  2622. *s = hsum_float_8(acc) + summs;
  2623. #elif defined(__riscv_v_intrinsic)
  2624. float sumf = 0.0;
  2625. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2626. for (int i = 0; i < nb; i++) {
  2627. // load elements
  2628. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2629. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2630. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2631. // mask and store lower part of x, and then upper part
  2632. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2633. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2634. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2635. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2636. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2637. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2638. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2639. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2640. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2641. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2642. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2643. }
  2644. *s = sumf;
  2645. #else
  2646. // scalar
  2647. float sumf = 0.0;
  2648. for (int i = 0; i < nb; i++) {
  2649. int sumi = 0;
  2650. for (int j = 0; j < qk/2; ++j) {
  2651. const int v0 = (x[i].qs[j] & 0x0F);
  2652. const int v1 = (x[i].qs[j] >> 4);
  2653. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2654. }
  2655. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2656. }
  2657. *s = sumf;
  2658. #endif
  2659. }
  2660. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2661. const int qk = QK8_0;
  2662. const int nb = n / qk;
  2663. assert(n % qk == 0);
  2664. assert(qk == QK5_0);
  2665. const block_q5_0 * restrict x = vx;
  2666. const block_q8_0 * restrict y = vy;
  2667. #if defined(__ARM_NEON)
  2668. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2669. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2670. uint32_t qh0;
  2671. uint32_t qh1;
  2672. uint64_t tmp0[4];
  2673. uint64_t tmp1[4];
  2674. assert(nb % 2 == 0); // TODO: handle odd nb
  2675. for (int i = 0; i < nb; i += 2) {
  2676. const block_q5_0 * restrict x0 = &x[i];
  2677. const block_q5_0 * restrict x1 = &x[i + 1];
  2678. const block_q8_0 * restrict y0 = &y[i];
  2679. const block_q8_0 * restrict y1 = &y[i + 1];
  2680. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2681. // extract the 5th bit via lookup table ((!b) << 4)
  2682. memcpy(&qh0, x0->qh, sizeof(qh0));
  2683. memcpy(&qh1, x1->qh, sizeof(qh1));
  2684. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  2685. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  2686. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  2687. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  2688. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  2689. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  2690. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  2691. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  2692. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2693. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2694. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2695. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2696. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2697. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2698. // 4-bit -> 8-bit
  2699. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2700. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2701. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2702. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2703. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2704. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  2705. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  2706. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  2707. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  2708. // load y
  2709. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2710. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2711. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2712. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2713. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2714. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2715. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2716. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2717. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2718. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2719. }
  2720. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2721. #elif defined(__wasm_simd128__)
  2722. v128_t sumv = wasm_f32x4_splat(0.0f);
  2723. uint32_t qh;
  2724. uint64_t tmp[4];
  2725. // TODO: check if unrolling this is better
  2726. for (int i = 0; i < nb; ++i) {
  2727. const block_q5_0 * restrict x0 = &x[i];
  2728. const block_q8_0 * restrict y0 = &y[i];
  2729. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2730. // extract the 5th bit
  2731. memcpy(&qh, x0->qh, sizeof(qh));
  2732. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  2733. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  2734. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  2735. tmp[3] = table_b2b_1[(qh >> 24) ];
  2736. const v128_t qhl = wasm_v128_load(tmp + 0);
  2737. const v128_t qhh = wasm_v128_load(tmp + 2);
  2738. const v128_t v0 = wasm_v128_load(x0->qs);
  2739. // 4-bit -> 8-bit
  2740. const v128_t v0l = wasm_v128_and (v0, m4b);
  2741. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2742. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2743. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  2744. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  2745. // load y
  2746. const v128_t v1l = wasm_v128_load(y0->qs);
  2747. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2748. // int8x16 -> int16x8
  2749. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2750. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2751. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2752. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2753. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2754. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2755. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2756. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2757. // dot product
  2758. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  2759. wasm_i32x4_add(
  2760. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2761. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2762. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2763. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2764. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  2765. }
  2766. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2767. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  2768. #elif defined(__AVX2__)
  2769. // Initialize accumulator with zeros
  2770. __m256 acc = _mm256_setzero_ps();
  2771. // Main loop
  2772. for (int i = 0; i < nb; i++) {
  2773. /* Compute combined scale for the block */
  2774. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2775. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2776. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2777. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  2778. bx = _mm256_or_si256(bx, bxhi);
  2779. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2780. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2781. /* Multiply q with scale and accumulate */
  2782. acc = _mm256_fmadd_ps(d, q, acc);
  2783. }
  2784. *s = hsum_float_8(acc);
  2785. #elif defined(__AVX__)
  2786. // Initialize accumulator with zeros
  2787. __m256 acc = _mm256_setzero_ps();
  2788. __m128i mask = _mm_set1_epi8((char)0xF0);
  2789. // Main loop
  2790. for (int i = 0; i < nb; i++) {
  2791. /* Compute combined scale for the block */
  2792. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2793. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2794. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2795. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  2796. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  2797. bxhil = _mm_andnot_si128(bxhil, mask);
  2798. bxhih = _mm_andnot_si128(bxhih, mask);
  2799. __m128i bxl = _mm256_castsi256_si128(bx);
  2800. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  2801. bxl = _mm_or_si128(bxl, bxhil);
  2802. bxh = _mm_or_si128(bxh, bxhih);
  2803. bx = MM256_SET_M128I(bxh, bxl);
  2804. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2805. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2806. /* Multiply q with scale and accumulate */
  2807. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  2808. }
  2809. *s = hsum_float_8(acc);
  2810. #elif defined(__riscv_v_intrinsic)
  2811. float sumf = 0.0;
  2812. uint32_t qh;
  2813. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2814. // These temporary registers are for masking and shift operations
  2815. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  2816. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  2817. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  2818. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  2819. for (int i = 0; i < nb; i++) {
  2820. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  2821. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2822. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  2823. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  2824. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  2825. // ((qh & (1u << (j + 16))) >> (j + 12));
  2826. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  2827. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  2828. // narrowing
  2829. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  2830. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  2831. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  2832. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  2833. // load
  2834. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2835. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2836. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2837. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2838. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2839. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  2840. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  2841. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2842. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2843. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  2844. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  2845. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2846. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2847. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2848. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2849. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2850. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2851. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2852. }
  2853. *s = sumf;
  2854. #else
  2855. // scalar
  2856. float sumf = 0.0;
  2857. for (int i = 0; i < nb; i++) {
  2858. uint32_t qh;
  2859. memcpy(&qh, x[i].qh, sizeof(qh));
  2860. int sumi = 0;
  2861. for (int j = 0; j < qk/2; ++j) {
  2862. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2863. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  2864. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  2865. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  2866. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  2867. }
  2868. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2869. }
  2870. *s = sumf;
  2871. #endif
  2872. }
  2873. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2874. const int qk = QK8_1;
  2875. const int nb = n / qk;
  2876. assert(n % qk == 0);
  2877. assert(qk == QK5_1);
  2878. const block_q5_1 * restrict x = vx;
  2879. const block_q8_1 * restrict y = vy;
  2880. #if defined(__ARM_NEON)
  2881. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2882. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2883. float summs0 = 0.0f;
  2884. float summs1 = 0.0f;
  2885. uint32_t qh0;
  2886. uint32_t qh1;
  2887. uint64_t tmp0[4];
  2888. uint64_t tmp1[4];
  2889. assert(nb % 2 == 0); // TODO: handle odd nb
  2890. for (int i = 0; i < nb; i += 2) {
  2891. const block_q5_1 * restrict x0 = &x[i];
  2892. const block_q5_1 * restrict x1 = &x[i + 1];
  2893. const block_q8_1 * restrict y0 = &y[i];
  2894. const block_q8_1 * restrict y1 = &y[i + 1];
  2895. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2896. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2897. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  2898. // extract the 5th bit via lookup table ((b) << 4)
  2899. memcpy(&qh0, x0->qh, sizeof(qh0));
  2900. memcpy(&qh1, x1->qh, sizeof(qh1));
  2901. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  2902. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  2903. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  2904. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  2905. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  2906. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  2907. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  2908. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  2909. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2910. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2911. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2912. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2913. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2914. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2915. // 4-bit -> 8-bit
  2916. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2917. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2918. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2919. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2920. // add high bit
  2921. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  2922. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  2923. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  2924. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  2925. // load y
  2926. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2927. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2928. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2929. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2930. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2931. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2932. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2933. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2934. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2935. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2936. }
  2937. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  2938. #elif defined(__wasm_simd128__)
  2939. v128_t sumv = wasm_f32x4_splat(0.0f);
  2940. float summs = 0.0f;
  2941. uint32_t qh;
  2942. uint64_t tmp[4];
  2943. // TODO: check if unrolling this is better
  2944. for (int i = 0; i < nb; ++i) {
  2945. const block_q5_1 * restrict x0 = &x[i];
  2946. const block_q8_1 * restrict y0 = &y[i];
  2947. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2948. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2949. // extract the 5th bit
  2950. memcpy(&qh, x0->qh, sizeof(qh));
  2951. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  2952. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  2953. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  2954. tmp[3] = table_b2b_0[(qh >> 24) ];
  2955. const v128_t qhl = wasm_v128_load(tmp + 0);
  2956. const v128_t qhh = wasm_v128_load(tmp + 2);
  2957. const v128_t v0 = wasm_v128_load(x0->qs);
  2958. // 4-bit -> 8-bit
  2959. const v128_t v0l = wasm_v128_and (v0, m4b);
  2960. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2961. // add high bit
  2962. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  2963. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  2964. // load y
  2965. const v128_t v1l = wasm_v128_load(y0->qs);
  2966. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2967. // int8x16 -> int16x8
  2968. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2969. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2970. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2971. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2972. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2973. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2974. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2975. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2976. // dot product
  2977. sumv = wasm_f32x4_add(sumv,
  2978. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  2979. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2980. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2981. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2982. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2983. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  2984. }
  2985. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2986. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  2987. #elif defined(__AVX2__)
  2988. // Initialize accumulator with zeros
  2989. __m256 acc = _mm256_setzero_ps();
  2990. float summs = 0.0f;
  2991. // Main loop
  2992. for (int i = 0; i < nb; i++) {
  2993. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  2994. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2995. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2996. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2997. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  2998. bx = _mm256_or_si256(bx, bxhi);
  2999. const __m256 dy = _mm256_set1_ps(y[i].d);
  3000. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3001. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3002. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3003. }
  3004. *s = hsum_float_8(acc) + summs;
  3005. #elif defined(__AVX__)
  3006. // Initialize accumulator with zeros
  3007. __m256 acc = _mm256_setzero_ps();
  3008. __m128i mask = _mm_set1_epi8(0x10);
  3009. float summs = 0.0f;
  3010. // Main loop
  3011. for (int i = 0; i < nb; i++) {
  3012. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3013. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3014. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3015. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3016. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3017. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3018. bxhil = _mm_and_si128(bxhil, mask);
  3019. bxhih = _mm_and_si128(bxhih, mask);
  3020. __m128i bxl = _mm256_castsi256_si128(bx);
  3021. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3022. bxl = _mm_or_si128(bxl, bxhil);
  3023. bxh = _mm_or_si128(bxh, bxhih);
  3024. bx = MM256_SET_M128I(bxh, bxl);
  3025. const __m256 dy = _mm256_set1_ps(y[i].d);
  3026. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3027. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3028. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3029. }
  3030. *s = hsum_float_8(acc) + summs;
  3031. #elif defined(__riscv_v_intrinsic)
  3032. float sumf = 0.0;
  3033. uint32_t qh;
  3034. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3035. // temporary registers for shift operations
  3036. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3037. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3038. for (int i = 0; i < nb; i++) {
  3039. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3040. // load qh
  3041. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3042. // ((qh >> (j + 0)) << 4) & 0x10;
  3043. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3044. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3045. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3046. // ((qh >> (j + 12)) ) & 0x10;
  3047. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3048. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3049. // narrowing
  3050. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3051. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3052. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3053. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3054. // load
  3055. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3056. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3057. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3058. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3059. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3060. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3061. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3062. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3063. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3064. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3065. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3066. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3067. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3068. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3069. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3070. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3071. }
  3072. *s = sumf;
  3073. #else
  3074. // scalar
  3075. float sumf = 0.0;
  3076. for (int i = 0; i < nb; i++) {
  3077. uint32_t qh;
  3078. memcpy(&qh, x[i].qh, sizeof(qh));
  3079. int sumi = 0;
  3080. for (int j = 0; j < qk/2; ++j) {
  3081. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3082. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3083. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3084. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3085. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3086. }
  3087. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3088. }
  3089. *s = sumf;
  3090. #endif
  3091. }
  3092. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3093. const int qk = QK8_0;
  3094. const int nb = n / qk;
  3095. assert(n % qk == 0);
  3096. const block_q8_0 * restrict x = vx;
  3097. const block_q8_0 * restrict y = vy;
  3098. #if defined(__ARM_NEON)
  3099. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3100. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3101. assert(nb % 2 == 0); // TODO: handle odd nb
  3102. for (int i = 0; i < nb; i += 2) {
  3103. const block_q8_0 * restrict x0 = &x[i + 0];
  3104. const block_q8_0 * restrict x1 = &x[i + 1];
  3105. const block_q8_0 * restrict y0 = &y[i + 0];
  3106. const block_q8_0 * restrict y1 = &y[i + 1];
  3107. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3108. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3109. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3110. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3111. // load y
  3112. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3113. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3114. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3115. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3116. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3117. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3118. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3119. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3120. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3121. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3122. }
  3123. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3124. #elif defined(__AVX2__) || defined(__AVX__)
  3125. // Initialize accumulator with zeros
  3126. __m256 acc = _mm256_setzero_ps();
  3127. // Main loop
  3128. for (int i = 0; i < nb; ++i) {
  3129. // Compute combined scale for the block
  3130. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3131. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3132. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3133. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3134. // Multiply q with scale and accumulate
  3135. #if defined(__AVX2__)
  3136. acc = _mm256_fmadd_ps( d, q, acc );
  3137. #else
  3138. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3139. #endif
  3140. }
  3141. *s = hsum_float_8(acc);
  3142. #elif defined(__riscv_v_intrinsic)
  3143. float sumf = 0.0;
  3144. size_t vl = __riscv_vsetvl_e8m1(qk);
  3145. for (int i = 0; i < nb; i++) {
  3146. // load elements
  3147. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3148. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3149. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3150. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3151. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3152. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3153. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3154. }
  3155. *s = sumf;
  3156. #else
  3157. // scalar
  3158. float sumf = 0.0;
  3159. for (int i = 0; i < nb; i++) {
  3160. int sumi = 0;
  3161. for (int j = 0; j < qk; j++) {
  3162. sumi += x[i].qs[j]*y[i].qs[j];
  3163. }
  3164. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3165. }
  3166. *s = sumf;
  3167. #endif
  3168. }
  3169. #if QK_K == 256
  3170. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3171. const block_q2_K * restrict x = vx;
  3172. const block_q8_K * restrict y = vy;
  3173. const int nb = n / QK_K;
  3174. #ifdef __ARM_NEON
  3175. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3176. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3177. const int32x4_t vzero = vdupq_n_s32(0);
  3178. ggml_int8x16x2_t q2bytes;
  3179. uint8_t aux[16];
  3180. float sum = 0;
  3181. for (int i = 0; i < nb; ++i) {
  3182. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3183. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3184. const uint8_t * restrict q2 = x[i].qs;
  3185. const int8_t * restrict q8 = y[i].qs;
  3186. const uint8_t * restrict sc = x[i].scales;
  3187. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3188. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3189. vst1q_u8(aux, scales);
  3190. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3191. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3192. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3193. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3194. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3195. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3196. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3197. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3198. int isum = 0;
  3199. int is = 0;
  3200. // We use this macro instead of a function call because for some reason
  3201. // the code runs 2-3% slower, even if the function is declared inline
  3202. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3203. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3204. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3205. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3206. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3207. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3208. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3209. MULTIPLY_ACCUM_WITH_SCALE((index));
  3210. for (int j = 0; j < QK_K/128; ++j) {
  3211. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3212. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3213. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3214. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3215. MULTIPLY_ACCUM_WITH_SCALE(0);
  3216. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3217. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3218. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3219. is += 8;
  3220. }
  3221. sum += d * isum;
  3222. }
  3223. *s = sum;
  3224. #elif defined __AVX2__
  3225. const __m256i m3 = _mm256_set1_epi8(3);
  3226. const __m128i m4 = _mm_set1_epi8(0xF);
  3227. __m256 acc = _mm256_setzero_ps();
  3228. for (int i = 0; i < nb; ++i) {
  3229. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3230. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3231. const uint8_t * restrict q2 = x[i].qs;
  3232. const int8_t * restrict q8 = y[i].qs;
  3233. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3234. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3235. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3236. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3237. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3238. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3239. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3240. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3241. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3242. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3243. __m256i sumi = _mm256_setzero_si256();
  3244. for (int j = 0; j < QK_K/128; ++j) {
  3245. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  3246. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3247. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3248. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3249. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3250. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  3251. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  3252. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  3253. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  3254. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3255. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3256. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  3257. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  3258. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  3259. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  3260. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  3261. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  3262. p0 = _mm256_add_epi32(p0, p1);
  3263. p2 = _mm256_add_epi32(p2, p3);
  3264. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  3265. }
  3266. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3267. }
  3268. *s = hsum_float_8(acc);
  3269. #elif defined __AVX__
  3270. const __m128i m3 = _mm_set1_epi8(0x3);
  3271. const __m128i m4 = _mm_set1_epi8(0xF);
  3272. const __m128i m2 = _mm_set1_epi8(0x2);
  3273. __m256 acc = _mm256_setzero_ps();
  3274. for (int i = 0; i < nb; ++i) {
  3275. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3276. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3277. const uint8_t * restrict q2 = x[i].qs;
  3278. const int8_t * restrict q8 = y[i].qs;
  3279. // load mins and scales from block_q2_K.scales[QK_K/16]
  3280. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3281. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  3282. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3283. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  3284. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  3285. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  3286. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  3287. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  3288. // sumf += -dmin * summs in 32bits*8
  3289. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  3290. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  3291. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  3292. const __m128i scales[2] = { scales_0, scales_1 };
  3293. __m128i sumi_0 = _mm_setzero_si128();
  3294. __m128i sumi_1 = _mm_setzero_si128();
  3295. for (int j = 0; j < QK_K/128; ++j) {
  3296. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  3297. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3298. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3299. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3300. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3301. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3302. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3303. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3304. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3305. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  3306. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3307. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3308. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3309. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3310. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3311. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3312. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  3313. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3314. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3315. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3316. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  3317. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  3318. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  3319. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  3320. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  3321. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  3322. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  3323. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  3324. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  3325. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  3326. __m128i shuffle = _mm_set1_epi16(0x0100);
  3327. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  3328. shuffle = _mm_add_epi16(shuffle, m2);
  3329. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  3330. shuffle = _mm_add_epi16(shuffle, m2);
  3331. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  3332. shuffle = _mm_add_epi16(shuffle, m2);
  3333. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  3334. shuffle = _mm_add_epi16(shuffle, m2);
  3335. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  3336. shuffle = _mm_add_epi16(shuffle, m2);
  3337. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  3338. shuffle = _mm_add_epi16(shuffle, m2);
  3339. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  3340. shuffle = _mm_add_epi16(shuffle, m2);
  3341. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  3342. p0 = _mm_add_epi32(p0, p1);
  3343. p2 = _mm_add_epi32(p2, p3);
  3344. p4 = _mm_add_epi32(p4, p5);
  3345. p6 = _mm_add_epi32(p6, p7);
  3346. // isum in 32bits*4*2
  3347. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  3348. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  3349. }
  3350. // sumf += dall * isum - dmin * summs in 32bits
  3351. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3352. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  3353. }
  3354. *s = hsum_float_8(acc);
  3355. #elif defined __riscv_v_intrinsic
  3356. float sumf = 0;
  3357. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3358. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  3359. for (int i = 0; i < nb; ++i) {
  3360. const uint8_t * q2 = x[i].qs;
  3361. const int8_t * q8 = y[i].qs;
  3362. const uint8_t * sc = x[i].scales;
  3363. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3364. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3365. size_t vl = 16;
  3366. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  3367. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  3368. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  3369. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  3370. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  3371. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  3372. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  3373. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3374. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  3375. vl = 32;
  3376. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3377. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  3378. uint8_t is=0;
  3379. int isum=0;
  3380. for (int j = 0; j < QK_K/128; ++j) {
  3381. // load Q2
  3382. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  3383. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  3384. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  3385. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  3386. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  3387. // duplicate scale elements for product
  3388. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  3389. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  3390. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  3391. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  3392. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  3393. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  3394. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  3395. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  3396. // load Q8
  3397. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  3398. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  3399. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  3400. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  3401. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  3402. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  3403. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  3404. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  3405. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  3406. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  3407. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  3408. q2+=32; q8+=128; is=8;
  3409. }
  3410. sumf += dall * isum;
  3411. }
  3412. *s = sumf;
  3413. #else
  3414. float sumf = 0;
  3415. for (int i = 0; i < nb; ++i) {
  3416. const uint8_t * q2 = x[i].qs;
  3417. const int8_t * q8 = y[i].qs;
  3418. const uint8_t * sc = x[i].scales;
  3419. int summs = 0;
  3420. for (int j = 0; j < 16; ++j) {
  3421. summs += y[i].bsums[j] * (sc[j] >> 4);
  3422. }
  3423. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3424. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3425. int isum = 0;
  3426. int is = 0;
  3427. int d;
  3428. for (int k = 0; k < QK_K/128; ++k) {
  3429. int shift = 0;
  3430. for (int j = 0; j < 4; ++j) {
  3431. d = sc[is++] & 0xF;
  3432. int isuml = 0;
  3433. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3434. isum += d * isuml;
  3435. d = sc[is++] & 0xF;
  3436. isuml = 0;
  3437. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3438. isum += d * isuml;
  3439. shift += 2;
  3440. q8 += 32;
  3441. }
  3442. q2 += 32;
  3443. }
  3444. sumf += dall * isum - dmin * summs;
  3445. }
  3446. *s = sumf;
  3447. #endif
  3448. }
  3449. #else
  3450. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3451. const block_q2_K * restrict x = vx;
  3452. const block_q8_K * restrict y = vy;
  3453. const int nb = n / QK_K;
  3454. #ifdef __ARM_NEON
  3455. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3456. const int32x4_t vzero = vdupq_n_s32(0);
  3457. ggml_int8x16x4_t q2bytes;
  3458. uint32_t aux32[2];
  3459. const uint8_t * scales = (const uint8_t *)aux32;
  3460. float sum = 0;
  3461. for (int i = 0; i < nb; ++i) {
  3462. const float d = y[i].d * (float)x[i].d;
  3463. const float dmin = -y[i].d * (float)x[i].dmin;
  3464. const uint8_t * restrict q2 = x[i].qs;
  3465. const int8_t * restrict q8 = y[i].qs;
  3466. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3467. aux32[0] = sc[0] & 0x0f0f0f0f;
  3468. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3469. 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]);
  3470. int isum1 = 0, isum2 = 0;
  3471. const uint8x16_t q2bits = vld1q_u8(q2);
  3472. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  3473. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  3474. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  3475. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  3476. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  3477. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  3478. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  3479. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  3480. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  3481. sum += d * (isum1 + isum2);
  3482. }
  3483. *s = sum;
  3484. #elif defined __AVX2__
  3485. const __m256i m3 = _mm256_set1_epi8(3);
  3486. __m256 acc = _mm256_setzero_ps();
  3487. uint32_t ud, um;
  3488. const uint8_t * restrict db = (const uint8_t *)&ud;
  3489. const uint8_t * restrict mb = (const uint8_t *)&um;
  3490. float summs = 0;
  3491. // TODO: optimize this
  3492. for (int i = 0; i < nb; ++i) {
  3493. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3494. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3495. const uint8_t * restrict q2 = x[i].qs;
  3496. const int8_t * restrict q8 = y[i].qs;
  3497. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3498. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3499. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3500. 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];
  3501. summs += dmin * smin;
  3502. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3503. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  3504. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  3505. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3506. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3507. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3508. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3509. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  3510. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  3511. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  3512. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  3513. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  3514. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  3515. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  3516. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  3517. }
  3518. *s = hsum_float_8(acc) + summs;
  3519. #elif defined __AVX__
  3520. const __m128i m3 = _mm_set1_epi8(3);
  3521. __m256 acc = _mm256_setzero_ps();
  3522. uint32_t ud, um;
  3523. const uint8_t * restrict db = (const uint8_t *)&ud;
  3524. const uint8_t * restrict mb = (const uint8_t *)&um;
  3525. float summs = 0;
  3526. // TODO: optimize this
  3527. for (int i = 0; i < nb; ++i) {
  3528. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3529. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3530. const uint8_t * restrict q2 = x[i].qs;
  3531. const int8_t * restrict q8 = y[i].qs;
  3532. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3533. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3534. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3535. 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];
  3536. summs += dmin * smin;
  3537. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3538. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3539. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3540. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3541. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3542. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3543. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3544. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  3545. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  3546. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  3547. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  3548. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  3549. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  3550. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  3551. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  3552. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  3553. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  3554. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  3555. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  3556. }
  3557. *s = hsum_float_8(acc) + summs;
  3558. #elif defined __riscv_v_intrinsic
  3559. uint32_t aux32[2];
  3560. const uint8_t * scales = (const uint8_t *)aux32;
  3561. float sumf = 0;
  3562. for (int i = 0; i < nb; ++i) {
  3563. const float d = y[i].d * (float)x[i].d;
  3564. const float dmin = -y[i].d * (float)x[i].dmin;
  3565. const uint8_t * restrict q2 = x[i].qs;
  3566. const int8_t * restrict q8 = y[i].qs;
  3567. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3568. aux32[0] = sc[0] & 0x0f0f0f0f;
  3569. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3570. 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]);
  3571. int isum1 = 0;
  3572. int isum2 = 0;
  3573. size_t vl = 16;
  3574. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  3575. // load Q2
  3576. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  3577. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  3578. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  3579. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  3580. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  3581. // load Q8, and take product with Q2
  3582. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3583. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3584. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3585. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3586. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  3587. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  3588. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  3589. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  3590. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  3591. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  3592. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  3593. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  3594. sumf += d * (isum1 + isum2);
  3595. }
  3596. *s = sumf;
  3597. #else
  3598. float sumf = 0;
  3599. int isum[4];
  3600. for (int i = 0; i < nb; ++i) {
  3601. const uint8_t * q2 = x[i].qs;
  3602. const int8_t * q8 = y[i].qs;
  3603. const uint8_t * sc = x[i].scales;
  3604. int summs = 0;
  3605. for (int j = 0; j < QK_K/16; ++j) {
  3606. summs += y[i].bsums[j] * (sc[j] >> 4);
  3607. }
  3608. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3609. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3610. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  3611. for (int l = 0; l < 16; ++l) {
  3612. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  3613. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  3614. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  3615. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  3616. }
  3617. for (int l = 0; l < 4; ++l) {
  3618. isum[l] *= (sc[l] & 0xF);
  3619. }
  3620. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  3621. }
  3622. *s = sumf;
  3623. #endif
  3624. }
  3625. #endif
  3626. #if QK_K == 256
  3627. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3628. assert(n % QK_K == 0);
  3629. const uint32_t kmask1 = 0x03030303;
  3630. const uint32_t kmask2 = 0x0f0f0f0f;
  3631. const block_q3_K * restrict x = vx;
  3632. const block_q8_K * restrict y = vy;
  3633. const int nb = n / QK_K;
  3634. #ifdef __ARM_NEON
  3635. uint32_t aux[3];
  3636. uint32_t utmp[4];
  3637. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3638. const int32x4_t vzero = vdupq_n_s32(0);
  3639. const uint8x16_t m0 = vdupq_n_u8(1);
  3640. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  3641. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  3642. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  3643. const int8_t m32 = 32;
  3644. ggml_int8x16x4_t q3bytes;
  3645. float sum = 0;
  3646. for (int i = 0; i < nb; ++i) {
  3647. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3648. const uint8_t * restrict q3 = x[i].qs;
  3649. const uint8_t * restrict qh = x[i].hmask;
  3650. const int8_t * restrict q8 = y[i].qs;
  3651. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  3652. ggml_uint8x16x4_t q3h;
  3653. int32_t isum = 0;
  3654. // Set up scales
  3655. memcpy(aux, x[i].scales, 12);
  3656. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3657. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3658. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3659. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3660. int8_t * scale = (int8_t *)utmp;
  3661. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  3662. for (int j = 0; j < QK_K/128; ++j) {
  3663. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  3664. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3665. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3666. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  3667. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  3668. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  3669. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  3670. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3671. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3672. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3673. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3674. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  3675. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  3676. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  3677. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  3678. scale += 4;
  3679. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  3680. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  3681. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  3682. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  3683. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3684. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3685. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3686. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3687. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  3688. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  3689. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  3690. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  3691. scale += 4;
  3692. if (j == 0) {
  3693. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  3694. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  3695. }
  3696. }
  3697. sum += d * isum;
  3698. }
  3699. *s = sum;
  3700. #elif defined __AVX2__
  3701. const __m256i m3 = _mm256_set1_epi8(3);
  3702. const __m256i mone = _mm256_set1_epi8(1);
  3703. const __m128i m32 = _mm_set1_epi8(32);
  3704. __m256 acc = _mm256_setzero_ps();
  3705. uint32_t aux[3];
  3706. for (int i = 0; i < nb; ++i) {
  3707. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3708. const uint8_t * restrict q3 = x[i].qs;
  3709. const int8_t * restrict q8 = y[i].qs;
  3710. // Set up scales
  3711. memcpy(aux, x[i].scales, 12);
  3712. __m128i scales128 = _mm_set_epi32(
  3713. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3714. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3715. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3716. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3717. scales128 = _mm_sub_epi8(scales128, m32);
  3718. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  3719. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3720. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3721. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3722. // high bit
  3723. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  3724. // integer accumulator
  3725. __m256i sumi = _mm256_setzero_si256();
  3726. int bit = 0;
  3727. int is = 0;
  3728. for (int j = 0; j < QK_K/128; ++j) {
  3729. // load low 2 bits
  3730. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  3731. // prepare low and high bits
  3732. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  3733. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3734. ++bit;
  3735. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  3736. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3737. ++bit;
  3738. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  3739. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3740. ++bit;
  3741. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  3742. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3743. ++bit;
  3744. // load Q8 quants
  3745. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3746. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3747. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3748. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3749. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3750. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3751. // and 2 if the high bit was set)
  3752. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3753. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3754. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  3755. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  3756. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3757. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3758. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  3759. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  3760. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3761. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3762. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  3763. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  3764. // multiply with scales
  3765. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  3766. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  3767. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  3768. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  3769. // accumulate
  3770. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3771. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  3772. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  3773. }
  3774. // multiply with block scale and accumulate
  3775. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3776. }
  3777. *s = hsum_float_8(acc);
  3778. #elif defined __AVX__
  3779. const __m128i m3 = _mm_set1_epi8(3);
  3780. const __m128i mone = _mm_set1_epi8(1);
  3781. const __m128i m32 = _mm_set1_epi8(32);
  3782. const __m128i m2 = _mm_set1_epi8(2);
  3783. __m256 acc = _mm256_setzero_ps();
  3784. const uint32_t *aux;
  3785. for (int i = 0; i < nb; ++i) {
  3786. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3787. const uint8_t * restrict q3 = x[i].qs;
  3788. const int8_t * restrict q8 = y[i].qs;
  3789. // Set up scales
  3790. aux = (const uint32_t *)x[i].scales;
  3791. __m128i scales128 = _mm_set_epi32(
  3792. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3793. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3794. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3795. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3796. scales128 = _mm_sub_epi8(scales128, m32);
  3797. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  3798. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  3799. const __m128i scales[2] = { scales_0, scales_1 };
  3800. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  3801. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  3802. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  3803. // integer accumulator
  3804. __m128i sumi_0 = _mm_setzero_si128();
  3805. __m128i sumi_1 = _mm_setzero_si128();
  3806. for (int j = 0; j < QK_K/128; ++j) {
  3807. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  3808. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3809. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3810. // prepare low and high bits
  3811. const int bit = j << 2;
  3812. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  3813. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  3814. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  3815. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  3816. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  3817. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  3818. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3819. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3820. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  3821. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  3822. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3823. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3824. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  3825. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  3826. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3827. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3828. // load Q8 quants from block_q8_K.qs[QK_K]
  3829. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3830. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3831. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3832. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3833. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3834. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3835. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3836. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3837. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3838. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3839. // and 2 if the high bit was set)
  3840. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  3841. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  3842. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  3843. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  3844. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  3845. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  3846. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  3847. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  3848. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  3849. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  3850. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  3851. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  3852. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  3853. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  3854. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  3855. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  3856. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3857. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3858. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3859. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3860. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  3861. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  3862. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  3863. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  3864. // multiply with scales
  3865. __m128i shuffle = _mm_set1_epi16(0x0100);
  3866. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  3867. shuffle = _mm_add_epi16(shuffle, m2);
  3868. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  3869. shuffle = _mm_add_epi16(shuffle, m2);
  3870. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  3871. shuffle = _mm_add_epi16(shuffle, m2);
  3872. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  3873. shuffle = _mm_add_epi16(shuffle, m2);
  3874. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  3875. shuffle = _mm_add_epi16(shuffle, m2);
  3876. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  3877. shuffle = _mm_add_epi16(shuffle, m2);
  3878. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  3879. shuffle = _mm_add_epi16(shuffle, m2);
  3880. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  3881. // accumulate
  3882. p16_0 = _mm_add_epi32(p16_0, p16_1);
  3883. p16_2 = _mm_add_epi32(p16_2, p16_3);
  3884. p16_4 = _mm_add_epi32(p16_4, p16_5);
  3885. p16_6 = _mm_add_epi32(p16_6, p16_7);
  3886. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3887. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  3888. }
  3889. // multiply with block scale and accumulate
  3890. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3891. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  3892. }
  3893. *s = hsum_float_8(acc);
  3894. #elif defined __riscv_v_intrinsic
  3895. uint32_t aux[3];
  3896. uint32_t utmp[4];
  3897. float sumf = 0;
  3898. for (int i = 0; i < nb; ++i) {
  3899. const uint8_t * restrict q3 = x[i].qs;
  3900. const uint8_t * restrict qh = x[i].hmask;
  3901. const int8_t * restrict q8 = y[i].qs;
  3902. memcpy(aux, x[i].scales, 12);
  3903. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3904. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3905. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3906. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3907. int8_t * scale = (int8_t *)utmp;
  3908. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  3909. size_t vl = 32;
  3910. uint8_t m = 1;
  3911. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3912. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  3913. int sum_t = 0;
  3914. for (int j = 0; j < QK_K; j += 128) {
  3915. vl = 32;
  3916. // load Q3
  3917. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  3918. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  3919. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  3920. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  3921. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  3922. // compute mask for subtraction
  3923. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3924. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  3925. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  3926. m <<= 1;
  3927. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3928. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  3929. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  3930. m <<= 1;
  3931. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3932. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  3933. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  3934. m <<= 1;
  3935. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3936. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  3937. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  3938. m <<= 1;
  3939. // load Q8 and take product with Q3
  3940. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  3941. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  3942. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  3943. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  3944. vl = 16;
  3945. // retrieve lane to multiply with scale
  3946. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  3947. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  3948. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  3949. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  3950. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  3951. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  3952. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  3953. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  3954. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  3955. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  3956. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  3957. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  3958. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  3959. q3 += 32; q8 += 128; scale += 8;
  3960. }
  3961. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3962. sumf += d*sum_t;
  3963. }
  3964. *s = sumf;
  3965. #else
  3966. // scalar version
  3967. // This function is written like this so the compiler can manage to vectorize most of it
  3968. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  3969. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  3970. // The ideal situation would be if we could just write the code once, and the compiler would
  3971. // automatically produce the best possible set of machine instructions, instead of us having to manually
  3972. // write vectorized versions for AVX, ARM_NEON, etc.
  3973. int8_t aux8[QK_K];
  3974. int16_t aux16[8];
  3975. float sums [8];
  3976. int32_t aux32[8];
  3977. memset(sums, 0, 8*sizeof(float));
  3978. uint32_t auxs[4];
  3979. const int8_t * scales = (const int8_t*)auxs;
  3980. float sumf = 0;
  3981. for (int i = 0; i < nb; ++i) {
  3982. const uint8_t * restrict q3 = x[i].qs;
  3983. const uint8_t * restrict hm = x[i].hmask;
  3984. const int8_t * restrict q8 = y[i].qs;
  3985. memset(aux32, 0, 8*sizeof(int32_t));
  3986. int8_t * restrict a = aux8;
  3987. uint8_t m = 1;
  3988. for (int j = 0; j < QK_K; j += 128) {
  3989. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  3990. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3991. a += 32; m <<= 1;
  3992. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  3993. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3994. a += 32; m <<= 1;
  3995. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  3996. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3997. a += 32; m <<= 1;
  3998. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  3999. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4000. a += 32; m <<= 1;
  4001. q3 += 32;
  4002. }
  4003. a = aux8;
  4004. memcpy(auxs, x[i].scales, 12);
  4005. uint32_t tmp = auxs[2];
  4006. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4007. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4008. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4009. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4010. for (int j = 0; j < QK_K/16; ++j) {
  4011. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4012. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4013. q8 += 8; a += 8;
  4014. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4015. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4016. q8 += 8; a += 8;
  4017. }
  4018. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4019. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4020. }
  4021. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4022. *s = sumf;
  4023. #endif
  4024. }
  4025. #else
  4026. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4027. assert(n % QK_K == 0);
  4028. const block_q3_K * restrict x = vx;
  4029. const block_q8_K * restrict y = vy;
  4030. const int nb = n / QK_K;
  4031. #ifdef __ARM_NEON
  4032. const int32x4_t vzero = vdupq_n_s32(0);
  4033. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4034. const uint8x16_t mh = vdupq_n_u8(4);
  4035. ggml_int8x16x4_t q3bytes;
  4036. uint16_t aux16[2];
  4037. int8_t * scales = (int8_t *)aux16;
  4038. float sum = 0;
  4039. for (int i = 0; i < nb; ++i) {
  4040. ggml_uint8x16x4_t q3h;
  4041. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4042. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4043. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4044. const uint16_t a = *(const uint16_t *)x[i].scales;
  4045. aux16[0] = a & 0x0f0f;
  4046. aux16[1] = (a >> 4) & 0x0f0f;
  4047. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4048. 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]);
  4049. const float d = y[i].d * (float)x[i].d;
  4050. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4051. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4052. q3h.val[1] = vandq_u8(mh, htmp);
  4053. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4054. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4055. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4056. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4057. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4058. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4059. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4060. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4061. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4062. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4063. sum += d * isum;
  4064. }
  4065. *s = sum;
  4066. #elif defined __AVX2__
  4067. const __m256i m3 = _mm256_set1_epi8(3);
  4068. const __m256i m1 = _mm256_set1_epi8(1);
  4069. __m256 acc = _mm256_setzero_ps();
  4070. uint64_t aux64;
  4071. uint16_t aux16[2];
  4072. const int8_t * aux8 = (const int8_t *)aux16;
  4073. for (int i = 0; i < nb; ++i) {
  4074. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4075. const uint8_t * restrict q3 = x[i].qs;
  4076. const int8_t * restrict q8 = y[i].qs;
  4077. const uint16_t a = *(const uint16_t *)x[i].scales;
  4078. aux16[0] = a & 0x0f0f;
  4079. aux16[1] = (a >> 4) & 0x0f0f;
  4080. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4081. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4082. memcpy(&aux64, x[i].hmask, 8);
  4083. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4084. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4085. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4086. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4087. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4088. // load low 2 bits
  4089. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4090. // prepare low and high bits
  4091. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4092. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4093. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4094. // load Q8 quants
  4095. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4096. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4097. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4098. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4099. // and 2 if the high bit was set)
  4100. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4101. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4102. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4103. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4104. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4105. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4106. // multiply with scales
  4107. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4108. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4109. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4110. // multiply with block scale and accumulate
  4111. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4112. }
  4113. *s = hsum_float_8(acc);
  4114. #elif defined __AVX__
  4115. const __m128i m3 = _mm_set1_epi8(3);
  4116. const __m128i m1 = _mm_set1_epi8(1);
  4117. __m256 acc = _mm256_setzero_ps();
  4118. uint64_t aux64;
  4119. uint16_t aux16[2];
  4120. const int8_t * aux8 = (const int8_t *)aux16;
  4121. for (int i = 0; i < nb; ++i) {
  4122. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4123. const uint8_t * restrict q3 = x[i].qs;
  4124. const int8_t * restrict q8 = y[i].qs;
  4125. const uint16_t a = *(const uint16_t *)x[i].scales;
  4126. aux16[0] = a & 0x0f0f;
  4127. aux16[1] = (a >> 4) & 0x0f0f;
  4128. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4129. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4130. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4131. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4132. memcpy(&aux64, x[i].hmask, 8);
  4133. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4134. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4135. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4136. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4137. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4138. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4139. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4140. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4141. // load low 2 bits
  4142. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4143. // prepare low and high bits
  4144. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4145. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4146. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4147. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4148. // load Q8 quants
  4149. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4150. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4151. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4152. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4153. // and 2 if the high bit was set)
  4154. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4155. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4156. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4157. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4158. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4159. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4160. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4161. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4162. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4163. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4164. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4165. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4166. // multiply with scales
  4167. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4168. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4169. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4170. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4171. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4172. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4173. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4174. // multiply with block scale and accumulate
  4175. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4176. }
  4177. *s = hsum_float_8(acc);
  4178. #elif defined __riscv_v_intrinsic
  4179. uint16_t aux16[2];
  4180. int8_t * scales = (int8_t *)aux16;
  4181. float sumf = 0;
  4182. for (int i = 0; i < nb; ++i) {
  4183. const uint8_t * restrict q3 = x[i].qs;
  4184. const int8_t * restrict q8 = y[i].qs;
  4185. const uint16_t a = *(const uint16_t *)x[i].scales;
  4186. aux16[0] = a & 0x0f0f;
  4187. aux16[1] = (a >> 4) & 0x0f0f;
  4188. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4189. 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]);
  4190. const float d = y[i].d * (float)x[i].d;
  4191. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4192. // load qh
  4193. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4194. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4195. size_t vl = 16;
  4196. // extend and combine both qh_x1 and qh_x2
  4197. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4198. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4199. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4200. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4201. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4202. // load Q3
  4203. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4204. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4205. 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);
  4206. 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);
  4207. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4208. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4209. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4210. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4211. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4212. // load Q8 and take product with Q3
  4213. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4214. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4215. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4216. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4217. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4218. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4219. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4220. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4221. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4222. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4223. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4224. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4225. sumf += d * isum;
  4226. }
  4227. *s = sumf;
  4228. #else
  4229. int8_t aux8[QK_K];
  4230. int16_t aux16[8];
  4231. float sums [8];
  4232. int32_t aux32[8];
  4233. int32_t scales[4];
  4234. memset(sums, 0, 8*sizeof(float));
  4235. float sumf = 0;
  4236. for (int i = 0; i < nb; ++i) {
  4237. const uint8_t * restrict q3 = x[i].qs;
  4238. const uint8_t * restrict hm = x[i].hmask;
  4239. const int8_t * restrict q8 = y[i].qs;
  4240. int8_t * restrict a = aux8;
  4241. for (int l = 0; l < 8; ++l) {
  4242. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  4243. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  4244. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  4245. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  4246. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  4247. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  4248. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  4249. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  4250. }
  4251. scales[0] = (x[i].scales[0] & 0xF) - 8;
  4252. scales[1] = (x[i].scales[0] >> 4) - 8;
  4253. scales[2] = (x[i].scales[1] & 0xF) - 8;
  4254. scales[3] = (x[i].scales[1] >> 4) - 8;
  4255. memset(aux32, 0, 8*sizeof(int32_t));
  4256. for (int j = 0; j < QK_K/16; ++j) {
  4257. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4258. q8 += 8; a += 8;
  4259. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  4260. q8 += 8; a += 8;
  4261. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  4262. }
  4263. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4264. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4265. }
  4266. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4267. *s = sumf;
  4268. #endif
  4269. }
  4270. #endif
  4271. #if QK_K == 256
  4272. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4273. assert(n % QK_K == 0);
  4274. const block_q4_K * restrict x = vx;
  4275. const block_q8_K * restrict y = vy;
  4276. const int nb = n / QK_K;
  4277. static const uint32_t kmask1 = 0x3f3f3f3f;
  4278. static const uint32_t kmask2 = 0x0f0f0f0f;
  4279. static const uint32_t kmask3 = 0x03030303;
  4280. uint32_t utmp[4];
  4281. #ifdef __ARM_NEON
  4282. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4283. const int32x4_t mzero = vdupq_n_s32(0);
  4284. ggml_int8x16x2_t q4bytes;
  4285. ggml_int8x16x2_t q8bytes;
  4286. float sumf = 0;
  4287. for (int i = 0; i < nb; ++i) {
  4288. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4289. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4290. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4291. memcpy(utmp, x[i].scales, 12);
  4292. uint32x2_t mins8 = { 0 };
  4293. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  4294. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  4295. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4296. utmp[0] &= kmask1;
  4297. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  4298. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4299. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4300. sumf -= dmin * vaddvq_s32(prod);
  4301. const uint8_t * scales = (const uint8_t *)utmp;
  4302. const uint8_t * restrict q4 = x[i].qs;
  4303. const int8_t * restrict q8 = y[i].qs;
  4304. int32_t sumi1 = 0;
  4305. int32_t sumi2 = 0;
  4306. for (int j = 0; j < QK_K/64; ++j) {
  4307. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  4308. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4309. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4310. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4311. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4312. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  4313. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4314. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4315. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4316. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4317. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  4318. }
  4319. sumf += d * (sumi1 + sumi2);
  4320. }
  4321. *s = sumf;
  4322. #elif defined __AVX2__
  4323. const __m256i m4 = _mm256_set1_epi8(0xF);
  4324. __m256 acc = _mm256_setzero_ps();
  4325. __m128 acc_m = _mm_setzero_ps();
  4326. for (int i = 0; i < nb; ++i) {
  4327. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4328. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4329. memcpy(utmp, x[i].scales, 12);
  4330. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4331. const uint32_t uaux = utmp[1] & kmask1;
  4332. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4333. utmp[2] = uaux;
  4334. utmp[0] &= kmask1;
  4335. const uint8_t * restrict q4 = x[i].qs;
  4336. const int8_t * restrict q8 = y[i].qs;
  4337. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4338. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4339. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4340. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4341. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  4342. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4343. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4344. __m256i sumi = _mm256_setzero_si256();
  4345. for (int j = 0; j < QK_K/64; ++j) {
  4346. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4347. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4348. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  4349. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4350. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4351. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4352. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4353. p16l = _mm256_madd_epi16(scale_l, p16l);
  4354. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4355. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4356. p16h = _mm256_madd_epi16(scale_h, p16h);
  4357. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  4358. sumi = _mm256_add_epi32(sumi, sumj);
  4359. }
  4360. __m256 vd = _mm256_set1_ps(d);
  4361. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4362. }
  4363. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4364. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4365. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4366. #elif defined __AVX__
  4367. const __m128i m4 = _mm_set1_epi8(0xF);
  4368. const __m128i m2 = _mm_set1_epi8(0x2);
  4369. __m256 acc = _mm256_setzero_ps();
  4370. __m128 acc_m = _mm_setzero_ps();
  4371. for (int i = 0; i < nb; ++i) {
  4372. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4373. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4374. const uint8_t * restrict q4 = x[i].qs;
  4375. const int8_t * restrict q8 = y[i].qs;
  4376. memcpy(utmp, x[i].scales, 12);
  4377. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4378. const uint32_t uaux = utmp[1] & kmask1;
  4379. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4380. utmp[2] = uaux;
  4381. utmp[0] &= kmask1;
  4382. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4383. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4384. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4385. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4386. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4387. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4388. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4389. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  4390. __m128i sumi_0 = _mm_setzero_si128();
  4391. __m128i sumi_1 = _mm_setzero_si128();
  4392. __m128i shuffle = _mm_set1_epi16(0x0100);
  4393. for (int j = 0; j < QK_K/64; ++j) {
  4394. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  4395. shuffle = _mm_add_epi16(shuffle, m2);
  4396. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  4397. shuffle = _mm_add_epi16(shuffle, m2);
  4398. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4399. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  4400. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4401. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4402. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  4403. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4404. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4405. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  4406. p16l = _mm_madd_epi16(scale_l, p16l);
  4407. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  4408. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4409. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  4410. p16l = _mm_madd_epi16(scale_l, p16l);
  4411. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  4412. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4413. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  4414. p16h = _mm_madd_epi16(scale_h, p16h);
  4415. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  4416. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4417. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  4418. p16h = _mm_madd_epi16(scale_h, p16h);
  4419. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  4420. }
  4421. __m256 vd = _mm256_set1_ps(d);
  4422. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4423. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4424. }
  4425. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4426. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4427. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4428. #elif defined __riscv_v_intrinsic
  4429. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4430. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4431. float sumf = 0;
  4432. for (int i = 0; i < nb; ++i) {
  4433. size_t vl = 8;
  4434. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4435. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4436. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4437. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4438. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4439. memcpy(utmp, x[i].scales, 12);
  4440. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4441. const uint32_t uaux = utmp[1] & kmask1;
  4442. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4443. utmp[2] = uaux;
  4444. utmp[0] &= kmask1;
  4445. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4446. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4447. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4448. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4449. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4450. const uint8_t * restrict q4 = x[i].qs;
  4451. const int8_t * restrict q8 = y[i].qs;
  4452. vl = 32;
  4453. int32_t sum_1 = 0;
  4454. int32_t sum_2 = 0;
  4455. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4456. for (int j = 0; j < QK_K/64; ++j) {
  4457. // load Q4
  4458. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4459. // load Q8 and multiply it with lower Q4 nibble
  4460. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4461. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4462. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  4463. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  4464. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  4465. // load Q8 and multiply it with upper Q4 nibble
  4466. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4467. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4468. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  4469. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  4470. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  4471. q4 += 32; q8 += 64;
  4472. }
  4473. sumf += d*(sum_1 + sum_2);
  4474. }
  4475. *s = sumf;
  4476. #else
  4477. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4478. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4479. int8_t aux8[QK_K];
  4480. int16_t aux16[8];
  4481. float sums [8];
  4482. int32_t aux32[8];
  4483. memset(sums, 0, 8*sizeof(float));
  4484. float sumf = 0;
  4485. for (int i = 0; i < nb; ++i) {
  4486. const uint8_t * restrict q4 = x[i].qs;
  4487. const int8_t * restrict q8 = y[i].qs;
  4488. memset(aux32, 0, 8*sizeof(int32_t));
  4489. int8_t * restrict a = aux8;
  4490. for (int j = 0; j < QK_K/64; ++j) {
  4491. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4492. a += 32;
  4493. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4494. a += 32; q4 += 32;
  4495. }
  4496. memcpy(utmp, x[i].scales, 12);
  4497. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4498. const uint32_t uaux = utmp[1] & kmask1;
  4499. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4500. utmp[2] = uaux;
  4501. utmp[0] &= kmask1;
  4502. int sumi = 0;
  4503. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4504. a = aux8;
  4505. int is = 0;
  4506. for (int j = 0; j < QK_K/32; ++j) {
  4507. int32_t scale = scales[is++];
  4508. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4509. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4510. q8 += 8; a += 8;
  4511. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4512. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4513. q8 += 8; a += 8;
  4514. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4515. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4516. q8 += 8; a += 8;
  4517. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4518. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4519. q8 += 8; a += 8;
  4520. }
  4521. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4522. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4523. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4524. sumf -= dmin * sumi;
  4525. }
  4526. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4527. *s = sumf;
  4528. #endif
  4529. }
  4530. #else
  4531. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4532. assert(n % QK_K == 0);
  4533. const block_q4_K * restrict x = vx;
  4534. const block_q8_K * restrict y = vy;
  4535. const int nb = n / QK_K;
  4536. #ifdef __ARM_NEON
  4537. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4538. const int32x4_t mzero = vdupq_n_s32(0);
  4539. float sumf = 0;
  4540. ggml_int8x16x2_t q4bytes;
  4541. ggml_int8x16x4_t q8bytes;
  4542. float sum_mins = 0.f;
  4543. uint16_t aux16[2];
  4544. const uint8_t * restrict scales = (const uint8_t *)aux16;
  4545. for (int i = 0; i < nb; ++i) {
  4546. const uint8_t * restrict q4 = x[i].qs;
  4547. const int8_t * restrict q8 = y[i].qs;
  4548. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  4549. aux16[0] = a[0] & 0x0f0f;
  4550. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4551. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  4552. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  4553. const float d = y[i].d * (float)x[i].d[0];
  4554. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  4555. q8bytes = ggml_vld1q_s8_x4(q8);
  4556. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4557. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4558. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4559. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  4560. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4561. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4562. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  4563. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  4564. sumf += d * (sumi1 + sumi2);
  4565. }
  4566. *s = sumf - sum_mins;
  4567. #elif defined __AVX2__
  4568. const __m256i m4 = _mm256_set1_epi8(0xF);
  4569. __m256 acc = _mm256_setzero_ps();
  4570. float summs = 0;
  4571. uint16_t aux16[2];
  4572. const uint8_t * scales = (const uint8_t *)aux16;
  4573. for (int i = 0; i < nb; ++i) {
  4574. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4575. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4576. const __m256 vd = _mm256_set1_ps(d);
  4577. const uint16_t * a = (const uint16_t *)x[i].scales;
  4578. aux16[0] = a[0] & 0x0f0f;
  4579. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4580. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4581. const uint8_t * restrict q4 = x[i].qs;
  4582. const int8_t * restrict q8 = y[i].qs;
  4583. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4584. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4585. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4586. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4587. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  4588. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4589. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4590. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  4591. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  4592. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  4593. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  4594. }
  4595. *s = hsum_float_8(acc) - summs;
  4596. #elif defined __AVX__
  4597. const __m128i m4 = _mm_set1_epi8(0xF);
  4598. __m256 acc = _mm256_setzero_ps();
  4599. float summs = 0;
  4600. uint16_t aux16[2];
  4601. const uint8_t * scales = (const uint8_t *)aux16;
  4602. for (int i = 0; i < nb; ++i) {
  4603. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4604. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4605. const __m256 vd = _mm256_set1_ps(d);
  4606. const uint16_t * a = (const uint16_t *)x[i].scales;
  4607. aux16[0] = a[0] & 0x0f0f;
  4608. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4609. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4610. const uint8_t * restrict q4 = x[i].qs;
  4611. const int8_t * restrict q8 = y[i].qs;
  4612. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4613. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  4614. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  4615. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  4616. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  4617. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  4618. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  4619. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4620. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4621. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  4622. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  4623. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  4624. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  4625. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  4626. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  4627. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  4628. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  4629. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  4630. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  4631. }
  4632. *s = hsum_float_8(acc) - summs;
  4633. #elif defined __riscv_v_intrinsic
  4634. uint16_t s16[2];
  4635. const uint8_t * restrict scales = (const uint8_t *)s16;
  4636. float sumf = 0;
  4637. for (int i = 0; i < nb; ++i) {
  4638. const uint8_t * restrict q4 = x[i].qs;
  4639. const int8_t * restrict q8 = y[i].qs;
  4640. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4641. s16[0] = b[0] & 0x0f0f;
  4642. s16[1] = (b[0] >> 4) & 0x0f0f;
  4643. 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]));
  4644. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4645. size_t vl = 32;
  4646. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4647. // load Q4
  4648. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4649. // load Q8 and multiply it with lower Q4 nibble
  4650. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4651. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  4652. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  4653. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  4654. // load Q8 and multiply it with upper Q4 nibble
  4655. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4656. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4657. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  4658. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  4659. }
  4660. *s = sumf;
  4661. #else
  4662. uint8_t aux8[QK_K];
  4663. int16_t aux16[16];
  4664. float sums [8];
  4665. memset(sums, 0, 8*sizeof(float));
  4666. uint16_t s16[2];
  4667. const uint8_t * restrict scales = (const uint8_t *)s16;
  4668. float sumf = 0;
  4669. for (int i = 0; i < nb; ++i) {
  4670. const uint8_t * restrict q4 = x[i].qs;
  4671. const int8_t * restrict q8 = y[i].qs;
  4672. uint8_t * restrict a = aux8;
  4673. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  4674. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  4675. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4676. s16[0] = b[0] & 0x0f0f;
  4677. s16[1] = (b[0] >> 4) & 0x0f0f;
  4678. 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]));
  4679. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4680. for (int j = 0; j < QK_K/32; ++j) {
  4681. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  4682. q8 += 16; a += 16;
  4683. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  4684. q8 += 16; a += 16;
  4685. const float dl = d * scales[j];
  4686. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  4687. }
  4688. }
  4689. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4690. *s = sumf;
  4691. #endif
  4692. }
  4693. #endif
  4694. #if QK_K == 256
  4695. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4696. assert(n % QK_K == 0);
  4697. const block_q5_K * restrict x = vx;
  4698. const block_q8_K * restrict y = vy;
  4699. const int nb = n / QK_K;
  4700. static const uint32_t kmask1 = 0x3f3f3f3f;
  4701. static const uint32_t kmask2 = 0x0f0f0f0f;
  4702. static const uint32_t kmask3 = 0x03030303;
  4703. uint32_t utmp[4];
  4704. #ifdef __ARM_NEON
  4705. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4706. const uint8x16_t mone = vdupq_n_u8(1);
  4707. const uint8x16_t mtwo = vdupq_n_u8(2);
  4708. const int32x4_t mzero = vdupq_n_s32(0);
  4709. ggml_int8x16x4_t q5bytes;
  4710. float sumf = 0;
  4711. for (int i = 0; i < nb; ++i) {
  4712. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4713. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4714. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4715. memcpy(utmp, x[i].scales, 12);
  4716. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4717. const uint32_t uaux = utmp[1] & kmask1;
  4718. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4719. utmp[2] = uaux;
  4720. utmp[0] &= kmask1;
  4721. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  4722. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  4723. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4724. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4725. int32_t sumi_mins = vaddvq_s32(prod);
  4726. const uint8_t * scales = (const uint8_t *)utmp;
  4727. const uint8_t * restrict q5 = x[i].qs;
  4728. const uint8_t * restrict qh = x[i].qh;
  4729. const int8_t * restrict q8 = y[i].qs;
  4730. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4731. ggml_uint8x16x4_t q5h;
  4732. int32_t sumi = 0;
  4733. for (int j = 0; j < QK_K/64; ++j) {
  4734. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  4735. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  4736. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  4737. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  4738. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  4739. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  4740. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  4741. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  4742. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  4743. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  4744. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  4745. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  4746. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  4747. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  4748. }
  4749. sumf += d * sumi - dmin * sumi_mins;
  4750. }
  4751. *s = sumf;
  4752. #elif defined __AVX2__
  4753. const __m256i m4 = _mm256_set1_epi8(0xF);
  4754. const __m128i mzero = _mm_setzero_si128();
  4755. const __m256i mone = _mm256_set1_epi8(1);
  4756. __m256 acc = _mm256_setzero_ps();
  4757. float summs = 0.f;
  4758. for (int i = 0; i < nb; ++i) {
  4759. const uint8_t * restrict q5 = x[i].qs;
  4760. const int8_t * restrict q8 = y[i].qs;
  4761. #if QK_K == 256
  4762. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4763. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4764. memcpy(utmp, x[i].scales, 12);
  4765. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4766. const uint32_t uaux = utmp[1] & kmask1;
  4767. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4768. utmp[2] = uaux;
  4769. utmp[0] &= kmask1;
  4770. #else
  4771. // TODO
  4772. const float d = 0, dmin = 0;
  4773. #endif
  4774. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4775. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4776. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4777. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4778. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4779. summs += dmin * _mm_extract_epi32(hsum, 0);
  4780. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4781. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4782. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  4783. __m256i hmask = mone;
  4784. __m256i sumi = _mm256_setzero_si256();
  4785. int bit = 0;
  4786. for (int j = 0; j < QK_K/64; ++j) {
  4787. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4788. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4789. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  4790. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4791. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4792. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  4793. hmask = _mm256_slli_epi16(hmask, 1);
  4794. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  4795. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4796. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  4797. hmask = _mm256_slli_epi16(hmask, 1);
  4798. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4799. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4800. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  4801. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  4802. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4803. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4804. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  4805. }
  4806. __m256 vd = _mm256_set1_ps(d);
  4807. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4808. }
  4809. *s = hsum_float_8(acc) + summs;
  4810. #elif defined __AVX__
  4811. const __m128i m4 = _mm_set1_epi8(0xF);
  4812. const __m128i mzero = _mm_setzero_si128();
  4813. const __m128i mone = _mm_set1_epi8(1);
  4814. const __m128i m2 = _mm_set1_epi8(2);
  4815. __m256 acc = _mm256_setzero_ps();
  4816. float summs = 0.f;
  4817. for (int i = 0; i < nb; ++i) {
  4818. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4819. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4820. const uint8_t * restrict q5 = x[i].qs;
  4821. const int8_t * restrict q8 = y[i].qs;
  4822. memcpy(utmp, x[i].scales, 12);
  4823. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4824. const uint32_t uaux = utmp[1] & kmask1;
  4825. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4826. utmp[2] = uaux;
  4827. utmp[0] &= kmask1;
  4828. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4829. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4830. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4831. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4832. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4833. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4834. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4835. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4836. summs += dmin * _mm_extract_epi32(hsum, 0);
  4837. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  4838. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  4839. __m128i hmask = mone;
  4840. __m128i sumi_0 = _mm_setzero_si128();
  4841. __m128i sumi_1 = _mm_setzero_si128();
  4842. int bit = 0;
  4843. __m128i shuffle = _mm_set1_epi16(0x0100);
  4844. for (int j = 0; j < QK_K/64; ++j) {
  4845. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  4846. shuffle = _mm_add_epi16(shuffle, m2);
  4847. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  4848. shuffle = _mm_add_epi16(shuffle, m2);
  4849. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4850. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4851. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  4852. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  4853. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4854. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4855. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4856. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4857. hmask = _mm_slli_epi16(hmask, 1);
  4858. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4859. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4860. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  4861. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  4862. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4863. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  4864. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  4865. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  4866. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4867. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4868. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4869. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4870. hmask = _mm_slli_epi16(hmask, 1);
  4871. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4872. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4873. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  4874. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  4875. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  4876. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  4877. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4878. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  4879. }
  4880. __m256 vd = _mm256_set1_ps(d);
  4881. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4882. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4883. }
  4884. *s = hsum_float_8(acc) + summs;
  4885. #elif defined __riscv_v_intrinsic
  4886. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4887. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4888. float sumf = 0;
  4889. float sums = 0.0;
  4890. size_t vl;
  4891. for (int i = 0; i < nb; ++i) {
  4892. vl = 8;
  4893. const uint8_t * restrict q5 = x[i].qs;
  4894. const uint8_t * restrict hm = x[i].qh;
  4895. const int8_t * restrict q8 = y[i].qs;
  4896. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4897. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4898. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4899. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4900. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4901. memcpy(utmp, x[i].scales, 12);
  4902. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4903. const uint32_t uaux = utmp[1] & kmask1;
  4904. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4905. utmp[2] = uaux;
  4906. utmp[0] &= kmask1;
  4907. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4908. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4909. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4910. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4911. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4912. vl = 32;
  4913. int32_t aux32 = 0;
  4914. int is = 0;
  4915. uint8_t m = 1;
  4916. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4917. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  4918. for (int j = 0; j < QK_K/64; ++j) {
  4919. // load Q5 and Q8
  4920. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  4921. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  4922. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  4923. // compute mask for addition
  4924. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  4925. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4926. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  4927. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  4928. m <<= 1;
  4929. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  4930. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4931. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  4932. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  4933. m <<= 1;
  4934. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  4935. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  4936. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  4937. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  4938. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  4939. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  4940. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  4941. q5 += 32; q8 += 64;
  4942. }
  4943. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  4944. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  4945. }
  4946. *s = sumf+sums;
  4947. #else
  4948. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4949. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4950. int8_t aux8[QK_K];
  4951. int16_t aux16[8];
  4952. float sums [8];
  4953. int32_t aux32[8];
  4954. memset(sums, 0, 8*sizeof(float));
  4955. float sumf = 0;
  4956. for (int i = 0; i < nb; ++i) {
  4957. const uint8_t * restrict q4 = x[i].qs;
  4958. const uint8_t * restrict hm = x[i].qh;
  4959. const int8_t * restrict q8 = y[i].qs;
  4960. memset(aux32, 0, 8*sizeof(int32_t));
  4961. int8_t * restrict a = aux8;
  4962. uint8_t m = 1;
  4963. for (int j = 0; j < QK_K/64; ++j) {
  4964. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4965. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4966. a += 32; m <<= 1;
  4967. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4968. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4969. a += 32; m <<= 1;
  4970. q4 += 32;
  4971. }
  4972. memcpy(utmp, x[i].scales, 12);
  4973. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4974. const uint32_t uaux = utmp[1] & kmask1;
  4975. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4976. utmp[2] = uaux;
  4977. utmp[0] &= kmask1;
  4978. int sumi = 0;
  4979. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4980. a = aux8;
  4981. int is = 0;
  4982. for (int j = 0; j < QK_K/32; ++j) {
  4983. int32_t scale = scales[is++];
  4984. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4985. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4986. q8 += 8; a += 8;
  4987. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4988. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4989. q8 += 8; a += 8;
  4990. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4991. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4992. q8 += 8; a += 8;
  4993. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4994. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4995. q8 += 8; a += 8;
  4996. }
  4997. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4998. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4999. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5000. sumf -= dmin * sumi;
  5001. }
  5002. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5003. *s = sumf;
  5004. #endif
  5005. }
  5006. #else
  5007. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5008. assert(n % QK_K == 0);
  5009. const block_q5_K * restrict x = vx;
  5010. const block_q8_K * restrict y = vy;
  5011. const int nb = n / QK_K;
  5012. #ifdef __ARM_NEON
  5013. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5014. const uint8x16_t mh = vdupq_n_u8(16);
  5015. const int32x4_t mzero = vdupq_n_s32(0);
  5016. ggml_int8x16x4_t q5bytes;
  5017. ggml_uint8x16x4_t q5h;
  5018. float sumf = 0;
  5019. for (int i = 0; i < nb; ++i) {
  5020. const float d = y[i].d * (float)x[i].d;
  5021. const int8_t * sc = x[i].scales;
  5022. const uint8_t * restrict q5 = x[i].qs;
  5023. const uint8_t * restrict qh = x[i].qh;
  5024. const int8_t * restrict q8 = y[i].qs;
  5025. const uint8x8_t qhbits = vld1_u8(qh);
  5026. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5027. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5028. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5029. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5030. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5031. q5h.val[2] = vbicq_u8(mh, htmp);
  5032. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5033. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5034. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5035. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5036. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5037. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5038. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5039. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5040. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5041. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5042. }
  5043. *s = sumf;
  5044. #elif defined __AVX2__
  5045. const __m256i m4 = _mm256_set1_epi8(0xF);
  5046. const __m256i mone = _mm256_set1_epi8(1);
  5047. __m256 acc = _mm256_setzero_ps();
  5048. for (int i = 0; i < nb; ++i) {
  5049. const uint8_t * restrict q5 = x[i].qs;
  5050. const int8_t * restrict q8 = y[i].qs;
  5051. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5052. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5053. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5054. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5055. int64_t aux64;
  5056. memcpy(&aux64, x[i].qh, 8);
  5057. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5058. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5059. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5060. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5061. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5062. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5063. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5064. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5065. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5066. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5067. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5068. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5069. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5070. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5071. }
  5072. *s = hsum_float_8(acc);
  5073. #elif defined __AVX__
  5074. const __m128i m4 = _mm_set1_epi8(0xF);
  5075. const __m128i mone = _mm_set1_epi8(1);
  5076. __m256 acc = _mm256_setzero_ps();
  5077. for (int i = 0; i < nb; ++i) {
  5078. const uint8_t * restrict q5 = x[i].qs;
  5079. const int8_t * restrict q8 = y[i].qs;
  5080. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5081. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5082. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5083. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5084. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5085. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5086. int64_t aux64;
  5087. memcpy(&aux64, x[i].qh, 8);
  5088. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5089. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5090. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5091. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5092. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5093. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5094. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5095. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5096. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5097. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5098. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5099. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5100. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5101. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5102. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5103. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5104. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5105. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5106. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5107. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5108. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5109. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5110. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5111. }
  5112. *s = hsum_float_8(acc);
  5113. #elif defined __riscv_v_intrinsic
  5114. float sumf = 0;
  5115. for (int i = 0; i < nb; ++i) {
  5116. const float d = y[i].d * (float)x[i].d;
  5117. const int8_t * sc = x[i].scales;
  5118. const uint8_t * restrict q5 = x[i].qs;
  5119. const uint8_t * restrict qh = x[i].qh;
  5120. const int8_t * restrict q8 = y[i].qs;
  5121. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5122. // load qh
  5123. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5124. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5125. size_t vl = 16;
  5126. // combine both qh_1 and qh_2
  5127. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5128. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5129. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5130. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5131. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5132. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5133. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5134. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5135. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5136. // load q5
  5137. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5138. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5139. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5140. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5141. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5142. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5143. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5144. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5145. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5146. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5147. // load Q8 and multiply it with Q5
  5148. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5149. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5150. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5151. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5152. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5153. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5154. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5155. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5156. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5157. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5158. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5159. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5160. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5161. }
  5162. *s = sumf;
  5163. #else
  5164. int8_t aux8[QK_K];
  5165. int16_t aux16[16];
  5166. float sums [8];
  5167. memset(sums, 0, 8*sizeof(float));
  5168. float sumf = 0;
  5169. for (int i = 0; i < nb; ++i) {
  5170. const uint8_t * restrict q4 = x[i].qs;
  5171. const uint8_t * restrict hm = x[i].qh;
  5172. const int8_t * restrict q8 = y[i].qs;
  5173. int8_t * restrict a = aux8;
  5174. for (int l = 0; l < 32; ++l) {
  5175. a[l+ 0] = q4[l] & 0xF;
  5176. a[l+32] = q4[l] >> 4;
  5177. }
  5178. for (int is = 0; is < 8; ++is) {
  5179. uint8_t m = 1 << is;
  5180. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5181. }
  5182. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5183. const int8_t * restrict sc = x[i].scales;
  5184. for (int j = 0; j < QK_K/16; ++j) {
  5185. const float dl = d * sc[j];
  5186. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5187. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5188. q8 += 16; a += 16;
  5189. }
  5190. }
  5191. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5192. *s = sumf;
  5193. #endif
  5194. }
  5195. #endif
  5196. #if QK_K == 256
  5197. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5198. assert(n % QK_K == 0);
  5199. const block_q6_K * restrict x = vx;
  5200. const block_q8_K * restrict y = vy;
  5201. const int nb = n / QK_K;
  5202. #ifdef __ARM_NEON
  5203. float sum = 0;
  5204. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5205. const int32x4_t vzero = vdupq_n_s32(0);
  5206. //const int8x16_t m32s = vdupq_n_s8(32);
  5207. const uint8x16_t mone = vdupq_n_u8(3);
  5208. ggml_int8x16x4_t q6bytes;
  5209. ggml_uint8x16x4_t q6h;
  5210. for (int i = 0; i < nb; ++i) {
  5211. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5212. const uint8_t * restrict q6 = x[i].ql;
  5213. const uint8_t * restrict qh = x[i].qh;
  5214. const int8_t * restrict q8 = y[i].qs;
  5215. const int8_t * restrict scale = x[i].scales;
  5216. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  5217. const int8x16_t scales = vld1q_s8(scale);
  5218. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  5219. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5220. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5221. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5222. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5223. int32_t isum_mins = vaddvq_s32(prod);
  5224. int32_t isum = 0;
  5225. for (int j = 0; j < QK_K/128; ++j) {
  5226. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  5227. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  5228. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5229. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5230. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5231. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5232. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5233. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5234. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5235. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5236. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5237. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5238. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5239. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5240. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  5241. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  5242. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  5243. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5244. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5245. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5246. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5247. scale += 4;
  5248. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5249. shifted = vshrq_n_u8(qhbits.val[0], 4);
  5250. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5251. shifted = vshrq_n_u8(qhbits.val[1], 4);
  5252. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5253. shifted = vshrq_n_u8(qhbits.val[0], 6);
  5254. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5255. shifted = vshrq_n_u8(qhbits.val[1], 6);
  5256. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5257. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  5258. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  5259. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  5260. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  5261. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  5262. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  5263. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  5264. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  5265. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5266. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5267. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5268. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5269. scale += 4;
  5270. }
  5271. //sum += isum * d_all * y[i].d;
  5272. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  5273. }
  5274. *s = sum;
  5275. #elif defined __AVX2__
  5276. const __m256i m4 = _mm256_set1_epi8(0xF);
  5277. const __m256i m2 = _mm256_set1_epi8(3);
  5278. const __m256i m32s = _mm256_set1_epi8(32);
  5279. __m256 acc = _mm256_setzero_ps();
  5280. for (int i = 0; i < nb; ++i) {
  5281. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5282. const uint8_t * restrict q4 = x[i].ql;
  5283. const uint8_t * restrict qh = x[i].qh;
  5284. const int8_t * restrict q8 = y[i].qs;
  5285. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5286. __m256i sumi = _mm256_setzero_si256();
  5287. int is = 0;
  5288. for (int j = 0; j < QK_K/128; ++j) {
  5289. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  5290. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  5291. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  5292. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  5293. is += 4;
  5294. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5295. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5296. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  5297. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  5298. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  5299. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  5300. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  5301. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5302. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  5303. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  5304. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  5305. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5306. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5307. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5308. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5309. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5310. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5311. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  5312. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  5313. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5314. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5315. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  5316. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  5317. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5318. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5319. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5320. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5321. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5322. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5323. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  5324. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  5325. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5326. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  5327. }
  5328. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5329. }
  5330. *s = hsum_float_8(acc);
  5331. #elif defined __AVX__
  5332. const __m128i m4 = _mm_set1_epi8(0xF);
  5333. const __m128i m3 = _mm_set1_epi8(3);
  5334. const __m128i m32s = _mm_set1_epi8(32);
  5335. const __m128i m2 = _mm_set1_epi8(2);
  5336. __m256 acc = _mm256_setzero_ps();
  5337. for (int i = 0; i < nb; ++i) {
  5338. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5339. const uint8_t * restrict q4 = x[i].ql;
  5340. const uint8_t * restrict qh = x[i].qh;
  5341. const int8_t * restrict q8 = y[i].qs;
  5342. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5343. __m128i sumi_0 = _mm_setzero_si128();
  5344. __m128i sumi_1 = _mm_setzero_si128();
  5345. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  5346. for (int j = 0; j < QK_K/128; ++j) {
  5347. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5348. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5349. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  5350. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  5351. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  5352. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  5353. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  5354. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  5355. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  5356. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  5357. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5358. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5359. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5360. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5361. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  5362. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  5363. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  5364. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  5365. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  5366. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  5367. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  5368. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  5369. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5370. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5371. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5372. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5373. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5374. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5375. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5376. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5377. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  5378. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  5379. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  5380. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  5381. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  5382. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  5383. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  5384. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  5385. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  5386. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  5387. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  5388. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  5389. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  5390. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  5391. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  5392. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  5393. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5394. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5395. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5396. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5397. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5398. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5399. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5400. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5401. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5402. shuffle = _mm_add_epi8(shuffle, m2);
  5403. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5404. shuffle = _mm_add_epi8(shuffle, m2);
  5405. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  5406. shuffle = _mm_add_epi8(shuffle, m2);
  5407. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  5408. shuffle = _mm_add_epi8(shuffle, m2);
  5409. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5410. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5411. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5412. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5413. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  5414. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  5415. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  5416. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  5417. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5418. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5419. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  5420. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  5421. }
  5422. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5423. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  5424. }
  5425. *s = hsum_float_8(acc);
  5426. #elif defined __riscv_v_intrinsic
  5427. float sumf = 0;
  5428. for (int i = 0; i < nb; ++i) {
  5429. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5430. const uint8_t * restrict q6 = x[i].ql;
  5431. const uint8_t * restrict qh = x[i].qh;
  5432. const int8_t * restrict q8 = y[i].qs;
  5433. const int8_t * restrict scale = x[i].scales;
  5434. size_t vl;
  5435. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5436. int sum_t = 0;
  5437. int is = 0;
  5438. for (int j = 0; j < QK_K/128; ++j) {
  5439. vl = 32;
  5440. // load qh
  5441. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  5442. // load Q6
  5443. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  5444. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  5445. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  5446. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  5447. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  5448. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  5449. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  5450. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  5451. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  5452. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  5453. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  5454. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  5455. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  5456. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  5457. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  5458. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  5459. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  5460. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  5461. // load Q8 and take product
  5462. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  5463. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5464. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  5465. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  5466. vl = 16;
  5467. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  5468. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  5469. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  5470. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  5471. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  5472. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  5473. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  5474. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  5475. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  5476. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  5477. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  5478. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  5479. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  5480. q6 += 64; qh += 32; q8 += 128; is=8;
  5481. }
  5482. sumf += d * sum_t;
  5483. }
  5484. *s = sumf;
  5485. #else
  5486. int8_t aux8[QK_K];
  5487. int16_t aux16[8];
  5488. float sums [8];
  5489. int32_t aux32[8];
  5490. memset(sums, 0, 8*sizeof(float));
  5491. float sumf = 0;
  5492. for (int i = 0; i < nb; ++i) {
  5493. const uint8_t * restrict q4 = x[i].ql;
  5494. const uint8_t * restrict qh = x[i].qh;
  5495. const int8_t * restrict q8 = y[i].qs;
  5496. memset(aux32, 0, 8*sizeof(int32_t));
  5497. int8_t * restrict a = aux8;
  5498. for (int j = 0; j < QK_K; j += 128) {
  5499. for (int l = 0; l < 32; ++l) {
  5500. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5501. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5502. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5503. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5504. }
  5505. a += 128;
  5506. q4 += 64;
  5507. qh += 32;
  5508. }
  5509. a = aux8;
  5510. int is = 0;
  5511. for (int j = 0; j < QK_K/16; ++j) {
  5512. int scale = x[i].scales[is++];
  5513. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5514. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5515. q8 += 8; a += 8;
  5516. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5517. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5518. q8 += 8; a += 8;
  5519. }
  5520. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5521. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5522. }
  5523. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5524. *s = sumf;
  5525. #endif
  5526. }
  5527. #else
  5528. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5529. assert(n % QK_K == 0);
  5530. const block_q6_K * restrict x = vx;
  5531. const block_q8_K * restrict y = vy;
  5532. const int nb = n / QK_K;
  5533. #ifdef __ARM_NEON
  5534. float sum = 0;
  5535. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5536. const int8x16_t m32s = vdupq_n_s8(32);
  5537. const int32x4_t vzero = vdupq_n_s32(0);
  5538. const uint8x16_t mone = vdupq_n_u8(3);
  5539. ggml_int8x16x4_t q6bytes;
  5540. ggml_uint8x16x4_t q6h;
  5541. for (int i = 0; i < nb; ++i) {
  5542. const float d_all = (float)x[i].d;
  5543. const uint8_t * restrict q6 = x[i].ql;
  5544. const uint8_t * restrict qh = x[i].qh;
  5545. const int8_t * restrict q8 = y[i].qs;
  5546. const int8_t * restrict scale = x[i].scales;
  5547. int32_t isum = 0;
  5548. uint8x16_t qhbits = vld1q_u8(qh);
  5549. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  5550. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5551. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  5552. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  5553. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5554. shifted = vshrq_n_u8(qhbits, 4);
  5555. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5556. shifted = vshrq_n_u8(qhbits, 6);
  5557. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5558. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5559. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5560. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  5561. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  5562. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5563. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5564. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5565. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5566. sum += isum * d_all * y[i].d;
  5567. }
  5568. *s = sum;
  5569. #elif defined __AVX2__
  5570. const __m256i m4 = _mm256_set1_epi8(0xF);
  5571. const __m256i m2 = _mm256_set1_epi8(3);
  5572. const __m256i m32s = _mm256_set1_epi8(32);
  5573. __m256 acc = _mm256_setzero_ps();
  5574. for (int i = 0; i < nb; ++i) {
  5575. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5576. const uint8_t * restrict q4 = x[i].ql;
  5577. const uint8_t * restrict qh = x[i].qh;
  5578. const int8_t * restrict q8 = y[i].qs;
  5579. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5580. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5581. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5582. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5583. __m256i sumi = _mm256_setzero_si256();
  5584. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5585. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5586. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5587. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5588. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  5589. 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);
  5590. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5591. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  5592. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5593. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5594. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5595. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5596. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5597. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5598. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5599. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5600. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5601. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5602. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5603. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5604. }
  5605. *s = hsum_float_8(acc);
  5606. #elif defined __AVX__
  5607. const __m128i m4 = _mm_set1_epi8(0xF);
  5608. const __m128i m2 = _mm_set1_epi8(3);
  5609. const __m128i m32s = _mm_set1_epi8(32);
  5610. __m256 acc = _mm256_setzero_ps();
  5611. for (int i = 0; i < nb; ++i) {
  5612. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5613. const uint8_t * restrict q4 = x[i].ql;
  5614. const uint8_t * restrict qh = x[i].qh;
  5615. const int8_t * restrict q8 = y[i].qs;
  5616. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5617. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5618. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5619. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5620. __m128i sumi_0 = _mm_setzero_si128();
  5621. __m128i sumi_1 = _mm_setzero_si128();
  5622. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5623. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5624. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5625. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5626. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  5627. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  5628. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  5629. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  5630. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  5631. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  5632. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  5633. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  5634. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5635. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5636. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  5637. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  5638. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  5639. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  5640. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5641. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5642. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5643. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5644. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5645. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5646. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5647. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5648. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5649. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5650. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5651. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5652. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5653. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5654. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  5655. }
  5656. *s = hsum_float_8(acc);
  5657. #elif defined __riscv_v_intrinsic
  5658. float sumf = 0;
  5659. for (int i = 0; i < nb; ++i) {
  5660. const float d_all = (float)x[i].d;
  5661. const uint8_t * restrict q6 = x[i].ql;
  5662. const uint8_t * restrict qh = x[i].qh;
  5663. const int8_t * restrict q8 = y[i].qs;
  5664. const int8_t * restrict scale = x[i].scales;
  5665. int32_t isum = 0;
  5666. size_t vl = 16;
  5667. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5668. // load Q6
  5669. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  5670. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  5671. // load qh
  5672. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  5673. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5674. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5675. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5676. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5677. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5678. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5679. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5680. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  5681. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  5682. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  5683. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  5684. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  5685. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  5686. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  5687. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  5688. // load Q8 and take product
  5689. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5690. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5691. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5692. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5693. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5694. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5695. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5696. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5697. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  5698. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  5699. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  5700. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  5701. sumf += isum * d_all * y[i].d;
  5702. }
  5703. *s = sumf;
  5704. #else
  5705. int8_t aux8[QK_K];
  5706. int16_t aux16[8];
  5707. float sums [8];
  5708. int32_t aux32[8];
  5709. memset(sums, 0, 8*sizeof(float));
  5710. float sumf = 0;
  5711. for (int i = 0; i < nb; ++i) {
  5712. const uint8_t * restrict q4 = x[i].ql;
  5713. const uint8_t * restrict qh = x[i].qh;
  5714. const int8_t * restrict q8 = y[i].qs;
  5715. memset(aux32, 0, 8*sizeof(int32_t));
  5716. int8_t * restrict a = aux8;
  5717. for (int l = 0; l < 16; ++l) {
  5718. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5719. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5720. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5721. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5722. }
  5723. int is = 0;
  5724. for (int j = 0; j < QK_K/16; ++j) {
  5725. int scale = x[i].scales[is++];
  5726. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5727. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5728. q8 += 8; a += 8;
  5729. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5730. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5731. q8 += 8; a += 8;
  5732. }
  5733. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5734. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5735. }
  5736. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5737. *s = sumf;
  5738. #endif
  5739. }
  5740. #endif
  5741. static const int8_t keven_signs_q2xs[1024] = {
  5742. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  5743. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  5744. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  5745. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  5746. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  5747. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  5748. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  5749. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  5750. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  5751. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  5752. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  5753. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  5754. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  5755. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  5756. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  5757. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  5758. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  5759. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  5760. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  5761. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  5762. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  5763. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  5764. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  5765. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  5766. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  5767. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  5768. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  5769. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  5770. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  5771. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  5772. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  5773. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  5774. };
  5775. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5776. assert(n % QK_K == 0);
  5777. const block_iq2_xxs * restrict x = vx;
  5778. const block_q8_K * restrict y = vy;
  5779. const int nb = n / QK_K;
  5780. #if defined(__ARM_NEON)
  5781. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  5782. uint32_t aux32[4];
  5783. const uint8_t * aux8 = (const uint8_t *)aux32;
  5784. ggml_int8x16x4_t q2u;
  5785. ggml_int8x16x4_t q2s;
  5786. ggml_int8x16x4_t q8b;
  5787. float sumf = 0;
  5788. for (int i = 0; i < nb; ++i) {
  5789. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5790. const uint16_t * restrict q2 = x[i].qs;
  5791. const int8_t * restrict q8 = y[i].qs;
  5792. float sumf1 = 0, sumf2 = 0;
  5793. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  5794. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  5795. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  5796. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  5797. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  5798. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  5799. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  5800. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  5801. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  5802. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  5803. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  5804. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  5805. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  5806. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  5807. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  5808. 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]);
  5809. 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]);
  5810. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  5811. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  5812. }
  5813. sumf += d*(sumf1 + sumf2);
  5814. }
  5815. *s = 0.25f * sumf;
  5816. #elif defined(__AVX2__)
  5817. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  5818. uint32_t aux32[4];
  5819. const uint8_t * aux8 = (const uint8_t *)aux32;
  5820. __m256 accumf = _mm256_setzero_ps();
  5821. for (int i = 0; i < nb; ++i) {
  5822. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5823. const uint16_t * restrict q2 = x[i].qs;
  5824. const int8_t * restrict q8 = y[i].qs;
  5825. __m256i sumi1 = _mm256_setzero_si256();
  5826. __m256i sumi2 = _mm256_setzero_si256();
  5827. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  5828. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  5829. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  5830. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  5831. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  5832. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  5833. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  5834. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  5835. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  5836. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  5837. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  5838. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  5839. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  5840. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  5841. const uint16_t ls1 = aux32[1] >> 28;
  5842. const uint16_t ls2 = aux32[3] >> 28;
  5843. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  5844. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  5845. sumi1 = _mm256_add_epi32(sumi1, p1);
  5846. sumi2 = _mm256_add_epi32(sumi2, p2);
  5847. }
  5848. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  5849. }
  5850. *s = 0.125f * hsum_float_8(accumf);
  5851. #else
  5852. uint32_t aux32[2];
  5853. const uint8_t * aux8 = (const uint8_t *)aux32;
  5854. float sumf = 0.f;
  5855. for (int i = 0; i < nb; ++i) {
  5856. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5857. const uint16_t * restrict q2 = x[i].qs;
  5858. const int8_t * restrict q8 = y[i].qs;
  5859. int32_t bsum = 0;
  5860. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  5861. memcpy(aux32, q2, 2*sizeof(uint32_t));
  5862. q2 += 4;
  5863. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  5864. int32_t sumi = 0;
  5865. for (int l = 0; l < 4; ++l) {
  5866. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  5867. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  5868. for (int j = 0; j < 8; ++j) {
  5869. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  5870. }
  5871. q8 += 8;
  5872. }
  5873. bsum += sumi * ls;
  5874. }
  5875. sumf += d * bsum;
  5876. }
  5877. *s = 0.125f * sumf;
  5878. #endif
  5879. }
  5880. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5881. assert(n % QK_K == 0);
  5882. const block_iq2_xs * restrict x = vx;
  5883. const block_q8_K * restrict y = vy;
  5884. const int nb = n / QK_K;
  5885. #if defined(__ARM_NEON)
  5886. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  5887. int8x16x4_t q2u;
  5888. int8x16x4_t q2s;
  5889. int8x16x4_t q8b;
  5890. int32x4x4_t scales32;
  5891. float sumf = 0;
  5892. for (int i = 0; i < nb; ++i) {
  5893. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5894. const uint16_t * restrict q2 = x[i].qs;
  5895. const int8_t * restrict q8 = y[i].qs;
  5896. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  5897. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  5898. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  5899. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  5900. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  5901. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  5902. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  5903. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  5904. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  5905. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  5906. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  5907. int32x4_t sumi = vdupq_n_s32(0);
  5908. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  5909. q8b = vld1q_s8_x4(q8); q8 += 64;
  5910. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  5911. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  5912. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  5913. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  5914. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  5915. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  5916. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  5917. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  5918. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  5919. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  5920. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  5921. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  5922. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  5923. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  5924. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  5925. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  5926. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  5927. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  5928. q2 += 8;
  5929. }
  5930. sumf += d*vaddvq_s32(sumi);
  5931. }
  5932. *s = 0.125f * sumf;
  5933. #elif defined(__AVX2__)
  5934. const __m128i m4 = _mm_set1_epi8(0xf);
  5935. const __m128i m1 = _mm_set1_epi8(1);
  5936. const __m128i m511 = _mm_set1_epi16(511);
  5937. const __m128i m127 = _mm_set1_epi16(127);
  5938. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  5939. uint64_t aux64;
  5940. // somewhat hacky, but gives a significant boost in performance
  5941. __m128i aux_gindex, aux_sindex;
  5942. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  5943. const uint16_t * sindex = (const uint16_t *)&aux_sindex;
  5944. __m256 accumf = _mm256_setzero_ps();
  5945. for (int i = 0; i < nb; ++i) {
  5946. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5947. const uint16_t * restrict q2 = x[i].qs;
  5948. const int8_t * restrict q8 = y[i].qs;
  5949. memcpy(&aux64, x[i].scales, 8);
  5950. __m128i stmp = _mm_set1_epi64x(aux64);
  5951. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  5952. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  5953. __m256i sumi1 = _mm256_setzero_si256();
  5954. __m256i sumi2 = _mm256_setzero_si256();
  5955. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  5956. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  5957. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  5958. const __m128i q2_data = _mm_loadu_si128((const __m128i*)q2); q2 += 8;
  5959. aux_gindex = _mm_and_si128(q2_data, m511);
  5960. aux_sindex = _mm_and_si128(_mm_srli_epi16(q2_data, 9), m127);
  5961. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]], iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
  5962. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]], iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
  5963. const __m256i s2_1 = _mm256_set_epi64x(signs64[sindex[3]], signs64[sindex[2]], signs64[sindex[1]], signs64[sindex[0]]);
  5964. const __m256i s2_2 = _mm256_set_epi64x(signs64[sindex[7]], signs64[sindex[6]], signs64[sindex[5]], signs64[sindex[4]]);
  5965. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  5966. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  5967. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  5968. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  5969. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  5970. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  5971. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  5972. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  5973. }
  5974. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  5975. }
  5976. *s = 0.125f * hsum_float_8(accumf);
  5977. #else
  5978. float sumf = 0.f;
  5979. for (int i = 0; i < nb; ++i) {
  5980. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5981. const uint16_t * restrict q2 = x[i].qs;
  5982. const uint8_t * restrict sc = x[i].scales;
  5983. const int8_t * restrict q8 = y[i].qs;
  5984. int32_t bsum = 0;
  5985. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  5986. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  5987. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  5988. int32_t sumi = 0;
  5989. for (int l = 0; l < 2; ++l) {
  5990. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  5991. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  5992. for (int j = 0; j < 8; ++j) {
  5993. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  5994. }
  5995. q8 += 8;
  5996. }
  5997. bsum += sumi * ls1;
  5998. sumi = 0;
  5999. for (int l = 2; l < 4; ++l) {
  6000. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6001. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6002. for (int j = 0; j < 8; ++j) {
  6003. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6004. }
  6005. q8 += 8;
  6006. }
  6007. bsum += sumi * ls2;
  6008. q2 += 4;
  6009. }
  6010. sumf += d * bsum;
  6011. }
  6012. *s = 0.125f * sumf;
  6013. #endif
  6014. }