ggml-quants.c 284 KB

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