ggml-quants.c 513 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678
  1. #define GGML_COMMON_IMPL_C
  2. #include "ggml-common.h"
  3. #include "ggml-quants.h"
  4. #include "ggml-impl.h"
  5. #define GGML_COMMON_IMPL_C
  6. #include "ggml-common.h"
  7. #include <math.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <float.h>
  11. #include <stdlib.h> // for qsort
  12. #include <stdio.h> // for GGML_ASSERT
  13. #ifdef __ARM_NEON
  14. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  15. //
  16. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  17. //
  18. #include <arm_neon.h>
  19. #else
  20. #ifdef __wasm_simd128__
  21. #include <wasm_simd128.h>
  22. #else
  23. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  24. #include <altivec.h>
  25. #undef bool
  26. #define bool _Bool
  27. #else
  28. #if defined(_MSC_VER) || defined(__MINGW32__)
  29. #include <intrin.h>
  30. #else
  31. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  32. #if !defined(__riscv)
  33. #include <immintrin.h>
  34. #endif
  35. #endif
  36. #endif
  37. #endif
  38. #endif
  39. #endif
  40. #ifdef __riscv_v_intrinsic
  41. #include <riscv_vector.h>
  42. #endif
  43. #undef MIN
  44. #undef MAX
  45. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  46. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  47. #define UNUSED GGML_UNUSED
  48. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  49. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  50. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  51. // multiply int8_t, add results pairwise twice
  52. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  53. // Get absolute values of x vectors
  54. const __m128i ax = _mm_sign_epi8(x, x);
  55. // Sign the values of the y vectors
  56. const __m128i sy = _mm_sign_epi8(y, x);
  57. // Perform multiplication and create 16-bit values
  58. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  59. const __m128i ones = _mm_set1_epi16(1);
  60. return _mm_madd_epi16(ones, dot);
  61. }
  62. #if __AVX__ || __AVX2__ || __AVX512F__
  63. // horizontally add 8 floats
  64. static inline float hsum_float_8(const __m256 x) {
  65. __m128 res = _mm256_extractf128_ps(x, 1);
  66. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  67. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  68. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  69. return _mm_cvtss_f32(res);
  70. }
  71. // horizontally add 8 int32_t
  72. static inline int hsum_i32_8(const __m256i a) {
  73. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  74. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  75. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  76. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  77. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  78. }
  79. // horizontally add 4 int32_t
  80. static inline int hsum_i32_4(const __m128i a) {
  81. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  82. const __m128i sum64 = _mm_add_epi32(hi64, a);
  83. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  84. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  85. }
  86. #if defined(__AVX2__) || defined(__AVX512F__)
  87. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  88. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  89. uint32_t x32;
  90. memcpy(&x32, x, sizeof(uint32_t));
  91. const __m256i shuf_mask = _mm256_set_epi64x(
  92. 0x0303030303030303, 0x0202020202020202,
  93. 0x0101010101010101, 0x0000000000000000);
  94. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  95. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  96. bytes = _mm256_or_si256(bytes, bit_mask);
  97. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  98. }
  99. // Unpack 32 4-bit fields into 32 bytes
  100. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  101. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  102. {
  103. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  104. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  105. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  106. return _mm256_and_si256(lowMask, bytes);
  107. }
  108. // add int16_t pairwise and return as float vector
  109. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  110. const __m256i ones = _mm256_set1_epi16(1);
  111. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. }
  114. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  115. #if defined(__AVXVNNI__) || defined(__AVX512VNNI__)
  116. const __m256i zero = _mm256_setzero_si256();
  117. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  118. return _mm256_cvtepi32_ps(summed_pairs);
  119. #else
  120. // Perform multiplication and create 16-bit values
  121. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  122. return sum_i16_pairs_float(dot);
  123. #endif
  124. }
  125. // multiply int8_t, add results pairwise twice and return as float vector
  126. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  127. #if __AVXVNNIINT8__
  128. const __m256i zero = _mm256_setzero_si256();
  129. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  130. return _mm256_cvtepi32_ps(summed_pairs);
  131. #else
  132. // Get absolute values of x vectors
  133. const __m256i ax = _mm256_sign_epi8(x, x);
  134. // Sign the values of the y vectors
  135. const __m256i sy = _mm256_sign_epi8(y, x);
  136. return mul_sum_us8_pairs_float(ax, sy);
  137. #endif
  138. }
  139. static inline __m128i packNibbles( __m256i bytes )
  140. {
  141. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  142. #if __AVX512F__
  143. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  144. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  145. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  146. #else
  147. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  148. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  149. __m256i low = _mm256_and_si256( lowByte, bytes );
  150. high = _mm256_srli_epi16( high, 4 );
  151. bytes = _mm256_or_si256( low, high );
  152. // Compress uint16_t lanes into bytes
  153. __m128i r0 = _mm256_castsi256_si128( bytes );
  154. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  155. return _mm_packus_epi16( r0, r1 );
  156. #endif
  157. }
  158. #elif defined(__AVX__)
  159. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  160. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  161. uint32_t x32;
  162. memcpy(&x32, x, sizeof(uint32_t));
  163. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  164. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  165. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  166. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  167. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  168. bytesl = _mm_or_si128(bytesl, bit_mask);
  169. bytesh = _mm_or_si128(bytesh, bit_mask);
  170. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  171. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  172. return MM256_SET_M128I(bytesh, bytesl);
  173. }
  174. // Unpack 32 4-bit fields into 32 bytes
  175. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  176. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  177. {
  178. // Load 16 bytes from memory
  179. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  180. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  181. const __m128i lowMask = _mm_set1_epi8(0xF);
  182. tmpl = _mm_and_si128(lowMask, tmpl);
  183. tmph = _mm_and_si128(lowMask, tmph);
  184. return MM256_SET_M128I(tmph, tmpl);
  185. }
  186. // add int16_t pairwise and return as float vector
  187. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  188. const __m128i ones = _mm_set1_epi16(1);
  189. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  190. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  191. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  192. return _mm256_cvtepi32_ps(summed_pairs);
  193. }
  194. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  195. const __m128i axl = _mm256_castsi256_si128(ax);
  196. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  197. const __m128i syl = _mm256_castsi256_si128(sy);
  198. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  199. // Perform multiplication and create 16-bit values
  200. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  201. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  202. return sum_i16_pairs_float(doth, dotl);
  203. }
  204. // multiply int8_t, add results pairwise twice and return as float vector
  205. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  206. const __m128i xl = _mm256_castsi256_si128(x);
  207. const __m128i xh = _mm256_extractf128_si256(x, 1);
  208. const __m128i yl = _mm256_castsi256_si128(y);
  209. const __m128i yh = _mm256_extractf128_si256(y, 1);
  210. // Get absolute values of x vectors
  211. const __m128i axl = _mm_sign_epi8(xl, xl);
  212. const __m128i axh = _mm_sign_epi8(xh, xh);
  213. // Sign the values of the y vectors
  214. const __m128i syl = _mm_sign_epi8(yl, xl);
  215. const __m128i syh = _mm_sign_epi8(yh, xh);
  216. // Perform multiplication and create 16-bit values
  217. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  218. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  219. return sum_i16_pairs_float(doth, dotl);
  220. }
  221. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  222. {
  223. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  224. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  225. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  226. __m128i low = _mm_and_si128( lowByte, bytes1 );
  227. high = _mm_srli_epi16( high, 4 );
  228. bytes1 = _mm_or_si128( low, high );
  229. high = _mm_andnot_si128( lowByte, bytes2 );
  230. low = _mm_and_si128( lowByte, bytes2 );
  231. high = _mm_srli_epi16( high, 4 );
  232. bytes2 = _mm_or_si128( low, high );
  233. return _mm_packus_epi16( bytes1, bytes2);
  234. }
  235. #endif
  236. #elif defined(__SSSE3__)
  237. // horizontally add 4x4 floats
  238. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  239. __m128 res_0 =_mm_hadd_ps(a, b);
  240. __m128 res_1 =_mm_hadd_ps(c, d);
  241. __m128 res =_mm_hadd_ps(res_0, res_1);
  242. res =_mm_hadd_ps(res, res);
  243. res =_mm_hadd_ps(res, res);
  244. return _mm_cvtss_f32(res);
  245. }
  246. #endif // __AVX__ || __AVX2__ || __AVX512F__
  247. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  248. #if defined(__ARM_NEON)
  249. #ifdef _MSC_VER
  250. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  251. #else
  252. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  253. #endif
  254. #if !defined(__aarch64__)
  255. // 64-bit compatibility
  256. // vaddvq_s16
  257. // vpaddq_s16
  258. // vpaddq_s32
  259. // vaddvq_s32
  260. // vaddvq_f32
  261. // vmaxvq_f32
  262. // vcvtnq_s32_f32
  263. // vzip1_u8
  264. // vzip2_u8
  265. inline static int32_t vaddvq_s16(int16x8_t v) {
  266. return
  267. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  268. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  269. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  270. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  271. }
  272. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  273. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  274. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  275. return vcombine_s16(a0, b0);
  276. }
  277. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  278. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  279. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  280. return vcombine_s32(a0, b0);
  281. }
  282. inline static int32_t vaddvq_s32(int32x4_t v) {
  283. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  284. }
  285. inline static float vaddvq_f32(float32x4_t v) {
  286. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  287. }
  288. inline static float vmaxvq_f32(float32x4_t v) {
  289. return
  290. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  291. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  292. }
  293. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  294. int32x4_t res;
  295. res[0] = roundf(vgetq_lane_f32(v, 0));
  296. res[1] = roundf(vgetq_lane_f32(v, 1));
  297. res[2] = roundf(vgetq_lane_f32(v, 2));
  298. res[3] = roundf(vgetq_lane_f32(v, 3));
  299. return res;
  300. }
  301. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  302. uint8x8_t res;
  303. res[0] = a[0]; res[1] = b[0];
  304. res[2] = a[1]; res[3] = b[1];
  305. res[4] = a[2]; res[5] = b[2];
  306. res[6] = a[3]; res[7] = b[3];
  307. return res;
  308. }
  309. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  310. uint8x8_t res;
  311. res[0] = a[4]; res[1] = b[4];
  312. res[2] = a[5]; res[3] = b[5];
  313. res[4] = a[6]; res[5] = b[6];
  314. res[6] = a[7]; res[7] = b[7];
  315. return res;
  316. }
  317. // vld1q_s16_x2
  318. // vld1q_u8_x2
  319. // vld1q_u8_x4
  320. // vld1q_s8_x2
  321. // vld1q_s8_x4
  322. // TODO: double-check these work correctly
  323. typedef struct ggml_int16x8x2_t {
  324. int16x8_t val[2];
  325. } ggml_int16x8x2_t;
  326. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  327. ggml_int16x8x2_t res;
  328. res.val[0] = vld1q_s16(ptr + 0);
  329. res.val[1] = vld1q_s16(ptr + 8);
  330. return res;
  331. }
  332. typedef struct ggml_uint8x16x2_t {
  333. uint8x16_t val[2];
  334. } ggml_uint8x16x2_t;
  335. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  336. ggml_uint8x16x2_t res;
  337. res.val[0] = vld1q_u8(ptr + 0);
  338. res.val[1] = vld1q_u8(ptr + 16);
  339. return res;
  340. }
  341. typedef struct ggml_uint8x16x4_t {
  342. uint8x16_t val[4];
  343. } ggml_uint8x16x4_t;
  344. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  345. ggml_uint8x16x4_t res;
  346. res.val[0] = vld1q_u8(ptr + 0);
  347. res.val[1] = vld1q_u8(ptr + 16);
  348. res.val[2] = vld1q_u8(ptr + 32);
  349. res.val[3] = vld1q_u8(ptr + 48);
  350. return res;
  351. }
  352. typedef struct ggml_int8x16x2_t {
  353. int8x16_t val[2];
  354. } ggml_int8x16x2_t;
  355. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  356. ggml_int8x16x2_t res;
  357. res.val[0] = vld1q_s8(ptr + 0);
  358. res.val[1] = vld1q_s8(ptr + 16);
  359. return res;
  360. }
  361. typedef struct ggml_int8x16x4_t {
  362. int8x16_t val[4];
  363. } ggml_int8x16x4_t;
  364. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  365. ggml_int8x16x4_t res;
  366. res.val[0] = vld1q_s8(ptr + 0);
  367. res.val[1] = vld1q_s8(ptr + 16);
  368. res.val[2] = vld1q_s8(ptr + 32);
  369. res.val[3] = vld1q_s8(ptr + 48);
  370. return res;
  371. }
  372. // NOTE: not tested
  373. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  374. int8x16_t res;
  375. res[ 0] = a[b[ 0]];
  376. res[ 1] = a[b[ 1]];
  377. res[ 2] = a[b[ 2]];
  378. res[ 3] = a[b[ 3]];
  379. res[ 4] = a[b[ 4]];
  380. res[ 5] = a[b[ 5]];
  381. res[ 6] = a[b[ 6]];
  382. res[ 7] = a[b[ 7]];
  383. res[ 8] = a[b[ 8]];
  384. res[ 9] = a[b[ 9]];
  385. res[10] = a[b[10]];
  386. res[11] = a[b[11]];
  387. res[12] = a[b[12]];
  388. res[13] = a[b[13]];
  389. res[14] = a[b[14]];
  390. res[15] = a[b[15]];
  391. return res;
  392. }
  393. // NOTE: not tested
  394. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  395. uint8x16_t res;
  396. res[ 0] = a[b[ 0]];
  397. res[ 1] = a[b[ 1]];
  398. res[ 2] = a[b[ 2]];
  399. res[ 3] = a[b[ 3]];
  400. res[ 4] = a[b[ 4]];
  401. res[ 5] = a[b[ 5]];
  402. res[ 6] = a[b[ 6]];
  403. res[ 7] = a[b[ 7]];
  404. res[ 8] = a[b[ 8]];
  405. res[ 9] = a[b[ 9]];
  406. res[10] = a[b[10]];
  407. res[11] = a[b[11]];
  408. res[12] = a[b[12]];
  409. res[13] = a[b[13]];
  410. res[14] = a[b[14]];
  411. res[15] = a[b[15]];
  412. return res;
  413. }
  414. #else
  415. #define ggml_int16x8x2_t int16x8x2_t
  416. #define ggml_uint8x16x2_t uint8x16x2_t
  417. #define ggml_uint8x16x4_t uint8x16x4_t
  418. #define ggml_int8x16x2_t int8x16x2_t
  419. #define ggml_int8x16x4_t int8x16x4_t
  420. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  421. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  422. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  423. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  424. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  425. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  426. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  427. #endif
  428. #if !defined(__ARM_FEATURE_DOTPROD)
  429. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  430. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  431. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  432. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  433. }
  434. #else
  435. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  436. #endif
  437. #endif
  438. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  439. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  440. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  441. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  442. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  443. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  444. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  445. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  446. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  447. // precomputed tables for expanding 8bits to 8 bytes:
  448. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  449. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  450. #endif
  451. // reference implementation for deterministic creation of model files
  452. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  453. static const int qk = QK4_0;
  454. assert(k % qk == 0);
  455. const int nb = k / qk;
  456. for (int i = 0; i < nb; i++) {
  457. float amax = 0.0f; // absolute max
  458. float max = 0.0f;
  459. for (int j = 0; j < qk; j++) {
  460. const float v = x[i*qk + j];
  461. if (amax < fabsf(v)) {
  462. amax = fabsf(v);
  463. max = v;
  464. }
  465. }
  466. const float d = max / -8;
  467. const float id = d ? 1.0f/d : 0.0f;
  468. y[i].d = GGML_FP32_TO_FP16(d);
  469. for (int j = 0; j < qk/2; ++j) {
  470. const float x0 = x[i*qk + 0 + j]*id;
  471. const float x1 = x[i*qk + qk/2 + j]*id;
  472. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  473. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  474. y[i].qs[j] = xi0;
  475. y[i].qs[j] |= xi1 << 4;
  476. }
  477. }
  478. }
  479. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  480. quantize_row_q4_0_reference(x, y, k);
  481. }
  482. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  483. const int qk = QK4_1;
  484. assert(k % qk == 0);
  485. const int nb = k / qk;
  486. for (int i = 0; i < nb; i++) {
  487. float min = FLT_MAX;
  488. float max = -FLT_MAX;
  489. for (int j = 0; j < qk; j++) {
  490. const float v = x[i*qk + j];
  491. if (v < min) min = v;
  492. if (v > max) max = v;
  493. }
  494. const float d = (max - min) / ((1 << 4) - 1);
  495. const float id = d ? 1.0f/d : 0.0f;
  496. y[i].d = GGML_FP32_TO_FP16(d);
  497. y[i].m = GGML_FP32_TO_FP16(min);
  498. for (int j = 0; j < qk/2; ++j) {
  499. const float x0 = (x[i*qk + 0 + j] - min)*id;
  500. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  501. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  502. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  503. y[i].qs[j] = xi0;
  504. y[i].qs[j] |= xi1 << 4;
  505. }
  506. }
  507. }
  508. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  509. quantize_row_q4_1_reference(x, y, k);
  510. }
  511. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  512. static const int qk = QK5_0;
  513. assert(k % qk == 0);
  514. const int nb = k / qk;
  515. for (int i = 0; i < nb; i++) {
  516. float amax = 0.0f; // absolute max
  517. float max = 0.0f;
  518. for (int j = 0; j < qk; j++) {
  519. const float v = x[i*qk + j];
  520. if (amax < fabsf(v)) {
  521. amax = fabsf(v);
  522. max = v;
  523. }
  524. }
  525. const float d = max / -16;
  526. const float id = d ? 1.0f/d : 0.0f;
  527. y[i].d = GGML_FP32_TO_FP16(d);
  528. uint32_t qh = 0;
  529. for (int j = 0; j < qk/2; ++j) {
  530. const float x0 = x[i*qk + 0 + j]*id;
  531. const float x1 = x[i*qk + qk/2 + j]*id;
  532. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  533. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  534. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  535. // get the 5-th bit and store it in qh at the right position
  536. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  537. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  538. }
  539. memcpy(&y[i].qh, &qh, sizeof(qh));
  540. }
  541. }
  542. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  543. quantize_row_q5_0_reference(x, y, k);
  544. }
  545. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  546. const int qk = QK5_1;
  547. assert(k % qk == 0);
  548. const int nb = k / qk;
  549. for (int i = 0; i < nb; i++) {
  550. float min = FLT_MAX;
  551. float max = -FLT_MAX;
  552. for (int j = 0; j < qk; j++) {
  553. const float v = x[i*qk + j];
  554. if (v < min) min = v;
  555. if (v > max) max = v;
  556. }
  557. const float d = (max - min) / ((1 << 5) - 1);
  558. const float id = d ? 1.0f/d : 0.0f;
  559. y[i].d = GGML_FP32_TO_FP16(d);
  560. y[i].m = GGML_FP32_TO_FP16(min);
  561. uint32_t qh = 0;
  562. for (int j = 0; j < qk/2; ++j) {
  563. const float x0 = (x[i*qk + 0 + j] - min)*id;
  564. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  565. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  566. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  567. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  568. // get the 5-th bit and store it in qh at the right position
  569. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  570. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  571. }
  572. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  573. }
  574. }
  575. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  576. quantize_row_q5_1_reference(x, y, k);
  577. }
  578. // reference implementation for deterministic creation of model files
  579. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  580. assert(k % QK8_0 == 0);
  581. const int nb = k / QK8_0;
  582. for (int i = 0; i < nb; i++) {
  583. float amax = 0.0f; // absolute max
  584. for (int j = 0; j < QK8_0; j++) {
  585. const float v = x[i*QK8_0 + j];
  586. amax = MAX(amax, fabsf(v));
  587. }
  588. const float d = amax / ((1 << 7) - 1);
  589. const float id = d ? 1.0f/d : 0.0f;
  590. y[i].d = GGML_FP32_TO_FP16(d);
  591. for (int j = 0; j < QK8_0; ++j) {
  592. const float x0 = x[i*QK8_0 + j]*id;
  593. y[i].qs[j] = roundf(x0);
  594. }
  595. }
  596. }
  597. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  598. assert(QK8_0 == 32);
  599. assert(k % QK8_0 == 0);
  600. const int nb = k / QK8_0;
  601. block_q8_0 * restrict y = vy;
  602. #if defined(__ARM_NEON)
  603. for (int i = 0; i < nb; i++) {
  604. float32x4_t srcv [8];
  605. float32x4_t asrcv[8];
  606. float32x4_t amaxv[8];
  607. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  608. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  609. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  610. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  611. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  612. const float amax = vmaxvq_f32(amaxv[0]);
  613. const float d = amax / ((1 << 7) - 1);
  614. const float id = d ? 1.0f/d : 0.0f;
  615. y[i].d = GGML_FP32_TO_FP16(d);
  616. for (int j = 0; j < 8; j++) {
  617. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  618. const int32x4_t vi = vcvtnq_s32_f32(v);
  619. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  620. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  621. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  622. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  623. }
  624. }
  625. #elif defined(__wasm_simd128__)
  626. for (int i = 0; i < nb; i++) {
  627. v128_t srcv [8];
  628. v128_t asrcv[8];
  629. v128_t amaxv[8];
  630. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  631. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  632. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  633. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  634. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  635. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  636. wasm_f32x4_extract_lane(amaxv[0], 1)),
  637. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  638. wasm_f32x4_extract_lane(amaxv[0], 3)));
  639. const float d = amax / ((1 << 7) - 1);
  640. const float id = d ? 1.0f/d : 0.0f;
  641. y[i].d = GGML_FP32_TO_FP16(d);
  642. for (int j = 0; j < 8; j++) {
  643. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  644. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  645. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  646. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  647. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  648. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  649. }
  650. }
  651. #elif defined(__AVX2__) || defined(__AVX__)
  652. for (int i = 0; i < nb; i++) {
  653. // Load elements into 4 AVX vectors
  654. __m256 v0 = _mm256_loadu_ps( x );
  655. __m256 v1 = _mm256_loadu_ps( x + 8 );
  656. __m256 v2 = _mm256_loadu_ps( x + 16 );
  657. __m256 v3 = _mm256_loadu_ps( x + 24 );
  658. x += 32;
  659. // Compute max(abs(e)) for the block
  660. const __m256 signBit = _mm256_set1_ps( -0.0f );
  661. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  662. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  663. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  664. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  665. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  666. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  667. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  668. const float maxScalar = _mm_cvtss_f32( max4 );
  669. // Quantize these floats
  670. const float d = maxScalar / 127.f;
  671. y[i].d = GGML_FP32_TO_FP16(d);
  672. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  673. const __m256 mul = _mm256_set1_ps( id );
  674. // Apply the multiplier
  675. v0 = _mm256_mul_ps( v0, mul );
  676. v1 = _mm256_mul_ps( v1, mul );
  677. v2 = _mm256_mul_ps( v2, mul );
  678. v3 = _mm256_mul_ps( v3, mul );
  679. // Round to nearest integer
  680. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  681. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  682. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  683. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  684. // Convert floats to integers
  685. __m256i i0 = _mm256_cvtps_epi32( v0 );
  686. __m256i i1 = _mm256_cvtps_epi32( v1 );
  687. __m256i i2 = _mm256_cvtps_epi32( v2 );
  688. __m256i i3 = _mm256_cvtps_epi32( v3 );
  689. #if defined(__AVX2__)
  690. // Convert int32 to int16
  691. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  692. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  693. // Convert int16 to int8
  694. 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
  695. // We got our precious signed bytes, but the order is now wrong
  696. // These AVX2 pack instructions process 16-byte pieces independently
  697. // The following instruction is fixing the order
  698. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  699. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  700. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  701. #else
  702. // Since we don't have in AVX some necessary functions,
  703. // we split the registers in half and call AVX2 analogs from SSE
  704. __m128i ni0 = _mm256_castsi256_si128( i0 );
  705. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  706. __m128i ni2 = _mm256_castsi256_si128( i1 );
  707. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  708. __m128i ni4 = _mm256_castsi256_si128( i2 );
  709. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  710. __m128i ni6 = _mm256_castsi256_si128( i3 );
  711. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  712. // Convert int32 to int16
  713. ni0 = _mm_packs_epi32( ni0, ni1 );
  714. ni2 = _mm_packs_epi32( ni2, ni3 );
  715. ni4 = _mm_packs_epi32( ni4, ni5 );
  716. ni6 = _mm_packs_epi32( ni6, ni7 );
  717. // Convert int16 to int8
  718. ni0 = _mm_packs_epi16( ni0, ni2 );
  719. ni4 = _mm_packs_epi16( ni4, ni6 );
  720. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  721. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  722. #endif
  723. }
  724. #elif defined(__riscv_v_intrinsic)
  725. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  726. for (int i = 0; i < nb; i++) {
  727. // load elements
  728. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  729. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  730. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  731. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  732. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  733. const float d = amax / ((1 << 7) - 1);
  734. const float id = d ? 1.0f/d : 0.0f;
  735. y[i].d = GGML_FP32_TO_FP16(d);
  736. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  737. // convert to integer
  738. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  739. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  740. // store result
  741. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  742. }
  743. #else
  744. GGML_UNUSED(nb);
  745. // scalar
  746. quantize_row_q8_0_reference(x, y, k);
  747. #endif
  748. }
  749. // reference implementation for deterministic creation of model files
  750. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  751. assert(QK8_1 == 32);
  752. assert(k % QK8_1 == 0);
  753. const int nb = k / QK8_1;
  754. for (int i = 0; i < nb; i++) {
  755. float amax = 0.0f; // absolute max
  756. for (int j = 0; j < QK8_1; j++) {
  757. const float v = x[i*QK8_1 + j];
  758. amax = MAX(amax, fabsf(v));
  759. }
  760. const float d = amax / ((1 << 7) - 1);
  761. const float id = d ? 1.0f/d : 0.0f;
  762. y[i].d = GGML_FP32_TO_FP16(d);
  763. int sum = 0;
  764. for (int j = 0; j < QK8_1/2; ++j) {
  765. const float v0 = x[i*QK8_1 + j]*id;
  766. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  767. y[i].qs[ j] = roundf(v0);
  768. y[i].qs[QK8_1/2 + j] = roundf(v1);
  769. sum += y[i].qs[ j];
  770. sum += y[i].qs[QK8_1/2 + j];
  771. }
  772. y[i].s = GGML_FP32_TO_FP16(sum*d);
  773. }
  774. }
  775. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  776. assert(k % QK8_1 == 0);
  777. const int nb = k / QK8_1;
  778. block_q8_1 * restrict y = vy;
  779. #if defined(__ARM_NEON)
  780. for (int i = 0; i < nb; i++) {
  781. float32x4_t srcv [8];
  782. float32x4_t asrcv[8];
  783. float32x4_t amaxv[8];
  784. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  785. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  786. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  787. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  788. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  789. const float amax = vmaxvq_f32(amaxv[0]);
  790. const float d = amax / ((1 << 7) - 1);
  791. const float id = d ? 1.0f/d : 0.0f;
  792. y[i].d = GGML_FP32_TO_FP16(d);
  793. int32x4_t accv = vdupq_n_s32(0);
  794. for (int j = 0; j < 8; j++) {
  795. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  796. const int32x4_t vi = vcvtnq_s32_f32(v);
  797. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  798. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  799. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  800. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  801. accv = vaddq_s32(accv, vi);
  802. }
  803. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  804. }
  805. #elif defined(__wasm_simd128__)
  806. for (int i = 0; i < nb; i++) {
  807. v128_t srcv [8];
  808. v128_t asrcv[8];
  809. v128_t amaxv[8];
  810. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  811. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  812. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  813. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  814. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  815. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  816. wasm_f32x4_extract_lane(amaxv[0], 1)),
  817. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  818. wasm_f32x4_extract_lane(amaxv[0], 3)));
  819. const float d = amax / ((1 << 7) - 1);
  820. const float id = d ? 1.0f/d : 0.0f;
  821. y[i].d = GGML_FP32_TO_FP16(d);
  822. v128_t accv = wasm_i32x4_splat(0);
  823. for (int j = 0; j < 8; j++) {
  824. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  825. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  826. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  827. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  828. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  829. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  830. accv = wasm_i32x4_add(accv, vi);
  831. }
  832. y[i].s = GGML_FP32_TO_FP16(
  833. d * (wasm_i32x4_extract_lane(accv, 0) +
  834. wasm_i32x4_extract_lane(accv, 1) +
  835. wasm_i32x4_extract_lane(accv, 2) +
  836. wasm_i32x4_extract_lane(accv, 3)));
  837. }
  838. #elif defined(__AVX2__) || defined(__AVX__)
  839. for (int i = 0; i < nb; i++) {
  840. // Load elements into 4 AVX vectors
  841. __m256 v0 = _mm256_loadu_ps( x );
  842. __m256 v1 = _mm256_loadu_ps( x + 8 );
  843. __m256 v2 = _mm256_loadu_ps( x + 16 );
  844. __m256 v3 = _mm256_loadu_ps( x + 24 );
  845. x += 32;
  846. // Compute max(abs(e)) for the block
  847. const __m256 signBit = _mm256_set1_ps( -0.0f );
  848. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  849. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  850. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  851. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  852. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  853. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  854. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  855. const float maxScalar = _mm_cvtss_f32( max4 );
  856. // Quantize these floats
  857. const float d = maxScalar / 127.f;
  858. y[i].d = GGML_FP32_TO_FP16(d);
  859. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  860. const __m256 mul = _mm256_set1_ps( id );
  861. // Apply the multiplier
  862. v0 = _mm256_mul_ps( v0, mul );
  863. v1 = _mm256_mul_ps( v1, mul );
  864. v2 = _mm256_mul_ps( v2, mul );
  865. v3 = _mm256_mul_ps( v3, mul );
  866. // Round to nearest integer
  867. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  868. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  869. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  870. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  871. // Convert floats to integers
  872. __m256i i0 = _mm256_cvtps_epi32( v0 );
  873. __m256i i1 = _mm256_cvtps_epi32( v1 );
  874. __m256i i2 = _mm256_cvtps_epi32( v2 );
  875. __m256i i3 = _mm256_cvtps_epi32( v3 );
  876. #if defined(__AVX2__)
  877. // Compute the sum of the quants and set y[i].s
  878. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  879. // Convert int32 to int16
  880. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  881. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  882. // Convert int16 to int8
  883. 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
  884. // We got our precious signed bytes, but the order is now wrong
  885. // These AVX2 pack instructions process 16-byte pieces independently
  886. // The following instruction is fixing the order
  887. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  888. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  889. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  890. #else
  891. // Since we don't have in AVX some necessary functions,
  892. // we split the registers in half and call AVX2 analogs from SSE
  893. __m128i ni0 = _mm256_castsi256_si128( i0 );
  894. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  895. __m128i ni2 = _mm256_castsi256_si128( i1 );
  896. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  897. __m128i ni4 = _mm256_castsi256_si128( i2 );
  898. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  899. __m128i ni6 = _mm256_castsi256_si128( i3 );
  900. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  901. // Compute the sum of the quants and set y[i].s
  902. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  903. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  904. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  905. // Convert int32 to int16
  906. ni0 = _mm_packs_epi32( ni0, ni1 );
  907. ni2 = _mm_packs_epi32( ni2, ni3 );
  908. ni4 = _mm_packs_epi32( ni4, ni5 );
  909. ni6 = _mm_packs_epi32( ni6, ni7 );
  910. // Convert int16 to int8
  911. ni0 = _mm_packs_epi16( ni0, ni2 );
  912. ni4 = _mm_packs_epi16( ni4, ni6 );
  913. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  914. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  915. #endif
  916. }
  917. #elif defined(__riscv_v_intrinsic)
  918. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  919. for (int i = 0; i < nb; i++) {
  920. // load elements
  921. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  922. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  923. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  924. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  925. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  926. const float d = amax / ((1 << 7) - 1);
  927. const float id = d ? 1.0f/d : 0.0f;
  928. y[i].d = GGML_FP32_TO_FP16(d);
  929. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  930. // convert to integer
  931. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  932. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  933. // store result
  934. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  935. // compute sum for y[i].s
  936. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  937. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  938. // set y[i].s
  939. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  940. y[i].s = GGML_FP32_TO_FP16(sum*d);
  941. }
  942. #else
  943. GGML_UNUSED(nb);
  944. // scalar
  945. quantize_row_q8_1_reference(x, y, k);
  946. #endif
  947. }
  948. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  949. static const int qk = QK4_0;
  950. assert(k % qk == 0);
  951. const int nb = k / qk;
  952. for (int i = 0; i < nb; i++) {
  953. const float d = GGML_FP16_TO_FP32(x[i].d);
  954. for (int j = 0; j < qk/2; ++j) {
  955. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  956. const int x1 = (x[i].qs[j] >> 4) - 8;
  957. y[i*qk + j + 0 ] = x0*d;
  958. y[i*qk + j + qk/2] = x1*d;
  959. }
  960. }
  961. }
  962. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  963. static const int qk = QK4_1;
  964. assert(k % qk == 0);
  965. const int nb = k / qk;
  966. for (int i = 0; i < nb; i++) {
  967. const float d = GGML_FP16_TO_FP32(x[i].d);
  968. const float m = GGML_FP16_TO_FP32(x[i].m);
  969. for (int j = 0; j < qk/2; ++j) {
  970. const int x0 = (x[i].qs[j] & 0x0F);
  971. const int x1 = (x[i].qs[j] >> 4);
  972. y[i*qk + j + 0 ] = x0*d + m;
  973. y[i*qk + j + qk/2] = x1*d + m;
  974. }
  975. }
  976. }
  977. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  978. static const int qk = QK5_0;
  979. assert(k % qk == 0);
  980. const int nb = k / qk;
  981. for (int i = 0; i < nb; i++) {
  982. const float d = GGML_FP16_TO_FP32(x[i].d);
  983. uint32_t qh;
  984. memcpy(&qh, x[i].qh, sizeof(qh));
  985. for (int j = 0; j < qk/2; ++j) {
  986. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  987. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  988. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  989. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  990. y[i*qk + j + 0 ] = x0*d;
  991. y[i*qk + j + qk/2] = x1*d;
  992. }
  993. }
  994. }
  995. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  996. static const int qk = QK5_1;
  997. assert(k % qk == 0);
  998. const int nb = k / qk;
  999. for (int i = 0; i < nb; i++) {
  1000. const float d = GGML_FP16_TO_FP32(x[i].d);
  1001. const float m = GGML_FP16_TO_FP32(x[i].m);
  1002. uint32_t qh;
  1003. memcpy(&qh, x[i].qh, sizeof(qh));
  1004. for (int j = 0; j < qk/2; ++j) {
  1005. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1006. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1007. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  1008. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  1009. y[i*qk + j + 0 ] = x0*d + m;
  1010. y[i*qk + j + qk/2] = x1*d + m;
  1011. }
  1012. }
  1013. }
  1014. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  1015. static const int qk = QK8_0;
  1016. assert(k % qk == 0);
  1017. const int nb = k / qk;
  1018. for (int i = 0; i < nb; i++) {
  1019. const float d = GGML_FP16_TO_FP32(x[i].d);
  1020. for (int j = 0; j < qk; ++j) {
  1021. y[i*qk + j] = x[i].qs[j]*d;
  1022. }
  1023. }
  1024. }
  1025. //
  1026. // 2-6 bit quantization in super-blocks
  1027. //
  1028. //
  1029. // ===================== Helper functions
  1030. //
  1031. static inline int nearest_int(float fval) {
  1032. assert(fval <= 4194303.f);
  1033. float val = fval + 12582912.f;
  1034. int i; memcpy(&i, &val, sizeof(int));
  1035. return (i & 0x007fffff) - 0x00400000;
  1036. }
  1037. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  1038. const float * restrict qw) {
  1039. float max = 0;
  1040. float amax = 0;
  1041. for (int i = 0; i < n; ++i) {
  1042. float ax = fabsf(x[i]);
  1043. if (ax > amax) { amax = ax; max = x[i]; }
  1044. }
  1045. if (amax < 1e-30f) { // all zero
  1046. for (int i = 0; i < n; ++i) {
  1047. L[i] = 0;
  1048. }
  1049. return 0.f;
  1050. }
  1051. float iscale = -nmax / max;
  1052. if (rmse_type == 0) {
  1053. for (int i = 0; i < n; ++i) {
  1054. int l = nearest_int(iscale * x[i]);
  1055. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1056. }
  1057. return 1/iscale;
  1058. }
  1059. bool return_early = false;
  1060. if (rmse_type < 0) {
  1061. rmse_type = -rmse_type;
  1062. return_early = true;
  1063. }
  1064. float sumlx = 0;
  1065. float suml2 = 0;
  1066. #ifdef HAVE_BUGGY_APPLE_LINKER
  1067. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1068. for (volatile int i = 0; i < n; ++i) {
  1069. #else
  1070. for (int i = 0; i < n; ++i) {
  1071. #endif
  1072. int l = nearest_int(iscale * x[i]);
  1073. l = MAX(-nmax, MIN(nmax-1, l));
  1074. L[i] = l + nmax;
  1075. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1076. sumlx += w*x[i]*l;
  1077. suml2 += w*l*l;
  1078. }
  1079. float scale = sumlx/suml2;
  1080. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1081. float best = scale * sumlx;
  1082. for (int is = -9; is <= 9; ++is) {
  1083. if (is == 0) {
  1084. continue;
  1085. }
  1086. iscale = -(nmax + 0.1f*is) / max;
  1087. sumlx = suml2 = 0;
  1088. for (int i = 0; i < n; ++i) {
  1089. int l = nearest_int(iscale * x[i]);
  1090. l = MAX(-nmax, MIN(nmax-1, l));
  1091. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1092. sumlx += w*x[i]*l;
  1093. suml2 += w*l*l;
  1094. }
  1095. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1096. for (int i = 0; i < n; ++i) {
  1097. int l = nearest_int(iscale * x[i]);
  1098. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1099. }
  1100. scale = sumlx/suml2; best = scale*sumlx;
  1101. }
  1102. }
  1103. return scale;
  1104. }
  1105. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1106. float max = 0;
  1107. float amax = 0;
  1108. for (int i = 0; i < n; ++i) {
  1109. float ax = fabsf(x[i]);
  1110. if (ax > amax) { amax = ax; max = x[i]; }
  1111. }
  1112. if (!amax) { // all zero
  1113. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1114. return 0.f;
  1115. }
  1116. float iscale = -nmax / max;
  1117. if (do_rmse) {
  1118. float sumlx = 0;
  1119. float suml2 = 0;
  1120. for (int i = 0; i < n; ++i) {
  1121. int l = nearest_int(iscale * x[i]);
  1122. l = MAX(-nmax, MIN(nmax-1, l));
  1123. L[i] = l;
  1124. float w = x[i]*x[i];
  1125. sumlx += w*x[i]*l;
  1126. suml2 += w*l*l;
  1127. }
  1128. for (int itry = 0; itry < 5; ++itry) {
  1129. int n_changed = 0;
  1130. for (int i = 0; i < n; ++i) {
  1131. float w = x[i]*x[i];
  1132. float slx = sumlx - w*x[i]*L[i];
  1133. if (slx > 0) {
  1134. float sl2 = suml2 - w*L[i]*L[i];
  1135. int new_l = nearest_int(x[i] * sl2 / slx);
  1136. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1137. if (new_l != L[i]) {
  1138. slx += w*x[i]*new_l;
  1139. sl2 += w*new_l*new_l;
  1140. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1141. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1142. ++n_changed;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. if (!n_changed) {
  1148. break;
  1149. }
  1150. }
  1151. for (int i = 0; i < n; ++i) {
  1152. L[i] += nmax;
  1153. }
  1154. return sumlx / suml2;
  1155. }
  1156. for (int i = 0; i < n; ++i) {
  1157. int l = nearest_int(iscale * x[i]);
  1158. l = MAX(-nmax, MIN(nmax-1, l));
  1159. L[i] = l + nmax;
  1160. }
  1161. return 1/iscale;
  1162. }
  1163. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1164. int ntry, float alpha) {
  1165. float min = x[0];
  1166. float max = x[0];
  1167. for (int i = 1; i < n; ++i) {
  1168. if (x[i] < min) min = x[i];
  1169. if (x[i] > max) max = x[i];
  1170. }
  1171. if (max == min) {
  1172. for (int i = 0; i < n; ++i) L[i] = 0;
  1173. *the_min = 0;
  1174. return 0.f;
  1175. }
  1176. if (min > 0) min = 0;
  1177. float iscale = nmax/(max - min);
  1178. float scale = 1/iscale;
  1179. for (int itry = 0; itry < ntry; ++itry) {
  1180. float sumlx = 0; int suml2 = 0;
  1181. bool did_change = false;
  1182. for (int i = 0; i < n; ++i) {
  1183. int l = nearest_int(iscale*(x[i] - min));
  1184. l = MAX(0, MIN(nmax, l));
  1185. if (l != L[i]) {
  1186. L[i] = l;
  1187. did_change = true;
  1188. }
  1189. sumlx += (x[i] - min)*l;
  1190. suml2 += l*l;
  1191. }
  1192. scale = sumlx/suml2;
  1193. float sum = 0;
  1194. for (int i = 0; i < n; ++i) {
  1195. sum += x[i] - scale*L[i];
  1196. }
  1197. min = alpha*min + (1 - alpha)*sum/n;
  1198. if (min > 0) min = 0;
  1199. iscale = 1/scale;
  1200. if (!did_change) break;
  1201. }
  1202. *the_min = -min;
  1203. return scale;
  1204. }
  1205. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1206. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1207. float rmin, float rdelta, int nstep, bool use_mad) {
  1208. float min = x[0];
  1209. float max = x[0];
  1210. float sum_w = weights[0];
  1211. float sum_x = sum_w * x[0];
  1212. #ifdef HAVE_BUGGY_APPLE_LINKER
  1213. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1214. for (volatile int i = 1; i < n; ++i) {
  1215. #else
  1216. for (int i = 1; i < n; ++i) {
  1217. #endif
  1218. if (x[i] < min) min = x[i];
  1219. if (x[i] > max) max = x[i];
  1220. float w = weights[i];
  1221. sum_w += w;
  1222. sum_x += w * x[i];
  1223. }
  1224. if (min > 0) min = 0;
  1225. if (max == min) {
  1226. for (int i = 0; i < n; ++i) L[i] = 0;
  1227. *the_min = -min;
  1228. return 0.f;
  1229. }
  1230. float iscale = nmax/(max - min);
  1231. float scale = 1/iscale;
  1232. float best_mad = 0;
  1233. for (int i = 0; i < n; ++i) {
  1234. int l = nearest_int(iscale*(x[i] - min));
  1235. L[i] = MAX(0, MIN(nmax, l));
  1236. float diff = scale * L[i] + min - x[i];
  1237. diff = use_mad ? fabsf(diff) : diff * diff;
  1238. float w = weights[i];
  1239. best_mad += w * diff;
  1240. }
  1241. if (nstep < 1) {
  1242. *the_min = -min;
  1243. return scale;
  1244. }
  1245. for (int is = 0; is <= nstep; ++is) {
  1246. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1247. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1248. for (int i = 0; i < n; ++i) {
  1249. int l = nearest_int(iscale*(x[i] - min));
  1250. l = MAX(0, MIN(nmax, l));
  1251. Laux[i] = l;
  1252. float w = weights[i];
  1253. sum_l += w*l;
  1254. sum_l2 += w*l*l;
  1255. sum_xl += w*l*x[i];
  1256. }
  1257. float D = sum_w * sum_l2 - sum_l * sum_l;
  1258. if (D > 0) {
  1259. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1260. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1261. if (this_min > 0) {
  1262. this_min = 0;
  1263. this_scale = sum_xl / sum_l2;
  1264. }
  1265. float mad = 0;
  1266. for (int i = 0; i < n; ++i) {
  1267. float diff = this_scale * Laux[i] + this_min - x[i];
  1268. diff = use_mad ? fabsf(diff) : diff * diff;
  1269. float w = weights[i];
  1270. mad += w * diff;
  1271. }
  1272. if (mad < best_mad) {
  1273. for (int i = 0; i < n; ++i) {
  1274. L[i] = Laux[i];
  1275. }
  1276. best_mad = mad;
  1277. scale = this_scale;
  1278. min = this_min;
  1279. }
  1280. }
  1281. }
  1282. *the_min = -min;
  1283. return scale;
  1284. }
  1285. #if QK_K == 256
  1286. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1287. if (j < 4) {
  1288. *d = q[j] & 63; *m = q[j + 4] & 63;
  1289. } else {
  1290. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1291. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1292. }
  1293. }
  1294. #endif
  1295. //========================- 2-bit (de)-quantization
  1296. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1297. assert(k % QK_K == 0);
  1298. const int nb = k / QK_K;
  1299. uint8_t L[QK_K];
  1300. uint8_t Laux[16];
  1301. float weights[16];
  1302. float mins[QK_K/16];
  1303. float scales[QK_K/16];
  1304. const float q4scale = 15.f;
  1305. for (int i = 0; i < nb; i++) {
  1306. float max_scale = 0; // as we are deducting the min, scales are always positive
  1307. float max_min = 0;
  1308. for (int j = 0; j < QK_K/16; ++j) {
  1309. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1310. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1311. float scale = scales[j];
  1312. if (scale > max_scale) {
  1313. max_scale = scale;
  1314. }
  1315. float min = mins[j];
  1316. if (min > max_min) {
  1317. max_min = min;
  1318. }
  1319. }
  1320. if (max_scale > 0) {
  1321. float iscale = q4scale/max_scale;
  1322. for (int j = 0; j < QK_K/16; ++j) {
  1323. int l = nearest_int(iscale*scales[j]);
  1324. y[i].scales[j] = l;
  1325. }
  1326. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1327. } else {
  1328. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1329. y[i].d = GGML_FP32_TO_FP16(0.f);
  1330. }
  1331. if (max_min > 0) {
  1332. float iscale = q4scale/max_min;
  1333. for (int j = 0; j < QK_K/16; ++j) {
  1334. int l = nearest_int(iscale*mins[j]);
  1335. y[i].scales[j] |= (l << 4);
  1336. }
  1337. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1338. } else {
  1339. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1340. }
  1341. for (int j = 0; j < QK_K/16; ++j) {
  1342. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1343. if (!d) continue;
  1344. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1345. for (int ii = 0; ii < 16; ++ii) {
  1346. int l = nearest_int((x[16*j + ii] + dm)/d);
  1347. l = MAX(0, MIN(3, l));
  1348. L[16*j + ii] = l;
  1349. }
  1350. }
  1351. #if QK_K == 256
  1352. for (int j = 0; j < QK_K; j += 128) {
  1353. for (int l = 0; l < 32; ++l) {
  1354. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1355. }
  1356. }
  1357. #else
  1358. for (int l = 0; l < 16; ++l) {
  1359. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1360. }
  1361. #endif
  1362. x += QK_K;
  1363. }
  1364. }
  1365. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1366. assert(k % QK_K == 0);
  1367. const int nb = k / QK_K;
  1368. for (int i = 0; i < nb; i++) {
  1369. const float d = GGML_FP16_TO_FP32(x[i].d);
  1370. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1371. const uint8_t * q = x[i].qs;
  1372. #if QK_K == 256
  1373. int is = 0;
  1374. float dl, ml;
  1375. for (int n = 0; n < QK_K; n += 128) {
  1376. int shift = 0;
  1377. for (int j = 0; j < 4; ++j) {
  1378. uint8_t sc = x[i].scales[is++];
  1379. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1380. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1381. sc = x[i].scales[is++];
  1382. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1383. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1384. shift += 2;
  1385. }
  1386. q += 32;
  1387. }
  1388. #else
  1389. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1390. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1391. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1392. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1393. for (int l = 0; l < 16; ++l) {
  1394. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1395. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1396. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1397. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1398. }
  1399. y += QK_K;
  1400. #endif
  1401. }
  1402. }
  1403. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1404. quantize_row_q2_K_reference(x, vy, k);
  1405. }
  1406. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1407. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1408. float rmin, float rdelta, int nstep, bool use_mad) {
  1409. float min = x[0];
  1410. float max = x[0];
  1411. float sum_w = weights ? weights[0] : x[0]*x[0];
  1412. float sum_x = sum_w * x[0];
  1413. #ifdef HAVE_BUGGY_APPLE_LINKER
  1414. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1415. for (volatile int i = 1; i < n; ++i) {
  1416. #else
  1417. for (int i = 1; i < n; ++i) {
  1418. #endif
  1419. if (x[i] < min) min = x[i];
  1420. if (x[i] > max) max = x[i];
  1421. float w = weights ? weights[i] : x[i]*x[i];
  1422. sum_w += w;
  1423. sum_x += w * x[i];
  1424. }
  1425. if (min > 0) {
  1426. min = 0;
  1427. }
  1428. if (max <= min) {
  1429. memset(L, 0, n);
  1430. *the_min = -min;
  1431. return 0.f;
  1432. }
  1433. float iscale = nmax/(max - min);
  1434. float scale = 1/iscale;
  1435. float best_mad = 0;
  1436. for (int i = 0; i < n; ++i) {
  1437. int l = nearest_int(iscale*(x[i] - min));
  1438. L[i] = MAX(0, MIN(nmax, l));
  1439. float diff = scale * L[i] + min - x[i];
  1440. diff = use_mad ? fabsf(diff) : diff*diff;
  1441. float w = weights ? weights[i] : x[i]*x[i];
  1442. best_mad += w * diff;
  1443. }
  1444. if (nstep < 1) {
  1445. *the_min = -min;
  1446. return scale;
  1447. }
  1448. for (int is = 0; is <= nstep; ++is) {
  1449. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1450. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1451. for (int i = 0; i < n; ++i) {
  1452. int l = nearest_int(iscale*(x[i] - min));
  1453. l = MAX(0, MIN(nmax, l));
  1454. Laux[i] = l;
  1455. float w = weights ? weights[i] : x[i]*x[i];
  1456. sum_l += w*l;
  1457. sum_l2 += w*l*l;
  1458. sum_xl += w*l*x[i];
  1459. }
  1460. float D = sum_w * sum_l2 - sum_l * sum_l;
  1461. if (D > 0) {
  1462. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1463. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1464. if (this_min > 0) {
  1465. this_min = 0;
  1466. this_scale = sum_xl / sum_l2;
  1467. }
  1468. float mad = 0;
  1469. for (int i = 0; i < n; ++i) {
  1470. float diff = this_scale * Laux[i] + this_min - x[i];
  1471. diff = use_mad ? fabsf(diff) : diff*diff;
  1472. float w = weights ? weights[i] : x[i]*x[i];
  1473. mad += w * diff;
  1474. }
  1475. if (mad < best_mad) {
  1476. for (int i = 0; i < n; ++i) {
  1477. L[i] = Laux[i];
  1478. }
  1479. best_mad = mad;
  1480. scale = this_scale;
  1481. min = this_min;
  1482. }
  1483. }
  1484. }
  1485. *the_min = -min;
  1486. return scale;
  1487. }
  1488. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1489. float max = 0;
  1490. for (int i = 0; i < n; ++i) {
  1491. max = MAX(max, x[i]);
  1492. }
  1493. if (!max) { // all zero
  1494. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1495. return 0.f;
  1496. }
  1497. float iscale = nmax / max;
  1498. for (int i = 0; i < n; ++i) {
  1499. L[i] = nearest_int(iscale * x[i]);
  1500. }
  1501. float scale = 1/iscale;
  1502. float best_mse = 0;
  1503. for (int i = 0; i < n; ++i) {
  1504. float diff = x[i] - scale*L[i];
  1505. float w = quant_weights[i];
  1506. best_mse += w*diff*diff;
  1507. }
  1508. for (int is = -4; is <= 4; ++is) {
  1509. if (is == 0) continue;
  1510. float iscale_is = (0.1f*is + nmax)/max;
  1511. float scale_is = 1/iscale_is;
  1512. float mse = 0;
  1513. for (int i = 0; i < n; ++i) {
  1514. int l = nearest_int(iscale_is*x[i]);
  1515. l = MIN(nmax, l);
  1516. float diff = x[i] - scale_is*l;
  1517. float w = quant_weights[i];
  1518. mse += w*diff*diff;
  1519. }
  1520. if (mse < best_mse) {
  1521. best_mse = mse;
  1522. iscale = iscale_is;
  1523. }
  1524. }
  1525. float sumlx = 0;
  1526. float suml2 = 0;
  1527. for (int i = 0; i < n; ++i) {
  1528. int l = nearest_int(iscale * x[i]);
  1529. l = MIN(nmax, l);
  1530. L[i] = l;
  1531. float w = quant_weights[i];
  1532. sumlx += w*x[i]*l;
  1533. suml2 += w*l*l;
  1534. }
  1535. for (int itry = 0; itry < 5; ++itry) {
  1536. int n_changed = 0;
  1537. for (int i = 0; i < n; ++i) {
  1538. float w = quant_weights[i];
  1539. float slx = sumlx - w*x[i]*L[i];
  1540. float sl2 = suml2 - w*L[i]*L[i];
  1541. if (slx > 0 && sl2 > 0) {
  1542. int new_l = nearest_int(x[i] * sl2 / slx);
  1543. new_l = MIN(nmax, new_l);
  1544. if (new_l != L[i]) {
  1545. slx += w*x[i]*new_l;
  1546. sl2 += w*new_l*new_l;
  1547. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1548. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1549. ++n_changed;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. if (!n_changed) {
  1555. break;
  1556. }
  1557. }
  1558. return sumlx / suml2;
  1559. }
  1560. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1561. GGML_ASSERT(quant_weights);
  1562. assert(k % QK_K == 0);
  1563. const int nb = k / QK_K;
  1564. const bool requantize = true;
  1565. uint8_t L[QK_K];
  1566. uint8_t Laux[16];
  1567. float mins[QK_K/16];
  1568. float scales[QK_K/16];
  1569. float sw[QK_K/16];
  1570. float weight[16];
  1571. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1572. for (int i = 0; i < nb; i++) {
  1573. memset(sw, 0, QK_K/16*sizeof(float));
  1574. float sumx2 = 0;
  1575. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1576. float sigma2 = sumx2/QK_K;
  1577. for (int j = 0; j < QK_K/16; ++j) {
  1578. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1579. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1580. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1581. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1582. }
  1583. float dm, mm;
  1584. #if QK_K == 64
  1585. float max_scale = 0, max_min = 0;
  1586. for (int j = 0; j < QK_K/16; ++j) {
  1587. max_scale = MAX(max_scale, scales[j]);
  1588. max_min = MAX(max_min, mins[j]);
  1589. }
  1590. dm = max_scale/15;
  1591. mm = max_min/15;
  1592. if (max_scale) {
  1593. float id = 1/dm;
  1594. for (int j = 0; j < QK_K/16; ++j) {
  1595. int l = nearest_int(id*scales[j]);
  1596. Ls[j] = MAX(0, MIN(15, l));
  1597. }
  1598. } else {
  1599. memset(Ls, 0, QK_K/16);
  1600. }
  1601. if (max_min) {
  1602. float id = 1/mm;
  1603. for (int j = 0; j < QK_K/16; ++j) {
  1604. int l = nearest_int(id*mins[j]);
  1605. Lm[j] = MAX(0, MIN(15, l));
  1606. }
  1607. } else {
  1608. memset(Lm, 0, QK_K/16);
  1609. }
  1610. #else
  1611. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1612. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1613. #endif
  1614. y[i].d = GGML_FP32_TO_FP16(dm);
  1615. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1616. dm = GGML_FP16_TO_FP32(y[i].d);
  1617. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1618. for (int j = 0; j < QK_K/16; ++j) {
  1619. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1620. }
  1621. if (requantize) {
  1622. for (int j = 0; j < QK_K/16; ++j) {
  1623. const float d = dm * (y[i].scales[j] & 0xF);
  1624. if (!d) continue;
  1625. const float m = mm * (y[i].scales[j] >> 4);
  1626. for (int ii = 0; ii < 16; ++ii) {
  1627. int l = nearest_int((x[16*j + ii] + m)/d);
  1628. l = MAX(0, MIN(3, l));
  1629. L[16*j + ii] = l;
  1630. }
  1631. }
  1632. }
  1633. #if QK_K == 256
  1634. for (int j = 0; j < QK_K; j += 128) {
  1635. for (int l = 0; l < 32; ++l) {
  1636. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1637. }
  1638. }
  1639. #else
  1640. for (int l = 0; l < 16; ++l) {
  1641. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1642. }
  1643. #endif
  1644. x += QK_K;
  1645. }
  1646. }
  1647. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1648. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1649. if (!quant_weights) {
  1650. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1651. }
  1652. else {
  1653. char * qrow = (char *)dst;
  1654. for (int row = 0; row < nrow; ++row) {
  1655. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1656. src += n_per_row;
  1657. qrow += row_size;
  1658. }
  1659. }
  1660. return nrow * row_size;
  1661. }
  1662. //========================= 3-bit (de)-quantization
  1663. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1664. assert(k % QK_K == 0);
  1665. const int nb = k / QK_K;
  1666. int8_t L[QK_K];
  1667. float scales[QK_K / 16];
  1668. for (int i = 0; i < nb; i++) {
  1669. float max_scale = 0;
  1670. float amax = 0;
  1671. for (int j = 0; j < QK_K/16; ++j) {
  1672. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1673. float scale = fabsf(scales[j]);
  1674. if (scale > amax) {
  1675. amax = scale; max_scale = scales[j];
  1676. }
  1677. }
  1678. #if QK_K == 256
  1679. memset(y[i].scales, 0, 12);
  1680. if (max_scale) {
  1681. float iscale = -32.f/max_scale;
  1682. for (int j = 0; j < QK_K/16; ++j) {
  1683. int8_t l = nearest_int(iscale*scales[j]);
  1684. l = MAX(-32, MIN(31, l)) + 32;
  1685. if (j < 8) {
  1686. y[i].scales[j] = l & 0xF;
  1687. } else {
  1688. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1689. }
  1690. l >>= 4;
  1691. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1692. }
  1693. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1694. } else {
  1695. y[i].d = GGML_FP32_TO_FP16(0.f);
  1696. }
  1697. int8_t sc;
  1698. for (int j = 0; j < QK_K/16; ++j) {
  1699. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1700. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1701. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1702. if (!d) {
  1703. continue;
  1704. }
  1705. for (int ii = 0; ii < 16; ++ii) {
  1706. int l = nearest_int(x[16*j + ii]/d);
  1707. l = MAX(-4, MIN(3, l));
  1708. L[16*j + ii] = l + 4;
  1709. }
  1710. }
  1711. #else
  1712. if (max_scale) {
  1713. float iscale = -8.f/max_scale;
  1714. for (int j = 0; j < QK_K/16; j+=2) {
  1715. int l1 = nearest_int(iscale*scales[j]);
  1716. l1 = 8 + MAX(-8, MIN(7, l1));
  1717. int l2 = nearest_int(iscale*scales[j+1]);
  1718. l2 = 8 + MAX(-8, MIN(7, l2));
  1719. y[i].scales[j/2] = l1 | (l2 << 4);
  1720. }
  1721. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1722. } else {
  1723. for (int j = 0; j < QK_K/16; j+=2) {
  1724. y[i].scales[j/2] = 0;
  1725. }
  1726. y[i].d = GGML_FP32_TO_FP16(0.f);
  1727. }
  1728. for (int j = 0; j < QK_K/16; ++j) {
  1729. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1730. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1731. if (!d) {
  1732. continue;
  1733. }
  1734. for (int ii = 0; ii < 16; ++ii) {
  1735. int l = nearest_int(x[16*j + ii]/d);
  1736. l = MAX(-4, MIN(3, l));
  1737. L[16*j + ii] = l + 4;
  1738. }
  1739. }
  1740. #endif
  1741. memset(y[i].hmask, 0, QK_K/8);
  1742. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1743. int m = 0;
  1744. uint8_t hm = 1;
  1745. for (int j = 0; j < QK_K; ++j) {
  1746. if (L[j] > 3) {
  1747. y[i].hmask[m] |= hm;
  1748. L[j] -= 4;
  1749. }
  1750. if (++m == QK_K/8) {
  1751. m = 0; hm <<= 1;
  1752. }
  1753. }
  1754. #if QK_K == 256
  1755. for (int j = 0; j < QK_K; j += 128) {
  1756. for (int l = 0; l < 32; ++l) {
  1757. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1758. }
  1759. }
  1760. #else
  1761. for (int l = 0; l < 16; ++l) {
  1762. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1763. }
  1764. #endif
  1765. x += QK_K;
  1766. }
  1767. }
  1768. #if QK_K == 256
  1769. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1770. assert(k % QK_K == 0);
  1771. const int nb = k / QK_K;
  1772. const uint32_t kmask1 = 0x03030303;
  1773. const uint32_t kmask2 = 0x0f0f0f0f;
  1774. uint32_t aux[4];
  1775. const int8_t * scales = (const int8_t*)aux;
  1776. for (int i = 0; i < nb; i++) {
  1777. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1778. const uint8_t * restrict q = x[i].qs;
  1779. const uint8_t * restrict hm = x[i].hmask;
  1780. uint8_t m = 1;
  1781. memcpy(aux, x[i].scales, 12);
  1782. uint32_t tmp = aux[2];
  1783. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1784. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1785. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1786. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1787. int is = 0;
  1788. float dl;
  1789. for (int n = 0; n < QK_K; n += 128) {
  1790. int shift = 0;
  1791. for (int j = 0; j < 4; ++j) {
  1792. dl = d_all * (scales[is++] - 32);
  1793. for (int l = 0; l < 16; ++l) {
  1794. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1795. }
  1796. dl = d_all * (scales[is++] - 32);
  1797. for (int l = 0; l < 16; ++l) {
  1798. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1799. }
  1800. shift += 2;
  1801. m <<= 1;
  1802. }
  1803. q += 32;
  1804. }
  1805. }
  1806. }
  1807. #else
  1808. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1809. assert(k % QK_K == 0);
  1810. assert(QK_K == 64);
  1811. const int nb = k / QK_K;
  1812. for (int i = 0; i < nb; i++) {
  1813. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1814. const uint8_t * restrict q = x[i].qs;
  1815. const uint8_t * restrict hm = x[i].hmask;
  1816. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1817. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1818. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1819. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1820. for (int l=0; l<8; ++l) {
  1821. uint8_t h = hm[l];
  1822. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1823. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1824. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1825. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1826. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1827. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1828. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1829. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1830. }
  1831. y += QK_K;
  1832. }
  1833. }
  1834. #endif
  1835. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1836. quantize_row_q3_K_reference(x, vy, k);
  1837. }
  1838. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1839. #if QK_K != 256
  1840. (void)quant_weights;
  1841. quantize_row_q3_K_reference(x, y, n_per_row);
  1842. #else
  1843. assert(n_per_row % QK_K == 0);
  1844. const int nb = n_per_row / QK_K;
  1845. int8_t L[QK_K];
  1846. float scales[QK_K / 16];
  1847. float weight[16];
  1848. float sw[QK_K / 16];
  1849. int8_t Ls[QK_K / 16];
  1850. for (int i = 0; i < nb; i++) {
  1851. float sumx2 = 0;
  1852. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1853. float sigma2 = 2*sumx2/QK_K;
  1854. for (int j = 0; j < QK_K/16; ++j) {
  1855. if (quant_weights) {
  1856. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1857. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1858. } else {
  1859. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1860. }
  1861. float sumw = 0;
  1862. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1863. sw[j] = sumw;
  1864. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1865. }
  1866. memset(y[i].scales, 0, 12);
  1867. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1868. for (int j = 0; j < QK_K/16; ++j) {
  1869. int l = Ls[j];
  1870. if (j < 8) {
  1871. y[i].scales[j] = l & 0xF;
  1872. } else {
  1873. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1874. }
  1875. l >>= 4;
  1876. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1877. }
  1878. y[i].d = GGML_FP32_TO_FP16(d_block);
  1879. int8_t sc;
  1880. for (int j = 0; j < QK_K/16; ++j) {
  1881. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1882. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1883. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1884. if (!d) {
  1885. continue;
  1886. }
  1887. for (int ii = 0; ii < 16; ++ii) {
  1888. int l = nearest_int(x[16*j + ii]/d);
  1889. l = MAX(-4, MIN(3, l));
  1890. L[16*j + ii] = l + 4;
  1891. }
  1892. }
  1893. memset(y[i].hmask, 0, QK_K/8);
  1894. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1895. int m = 0;
  1896. uint8_t hm = 1;
  1897. for (int j = 0; j < QK_K; ++j) {
  1898. if (L[j] > 3) {
  1899. y[i].hmask[m] |= hm;
  1900. L[j] -= 4;
  1901. }
  1902. if (++m == QK_K/8) {
  1903. m = 0; hm <<= 1;
  1904. }
  1905. }
  1906. for (int j = 0; j < QK_K; j += 128) {
  1907. for (int l = 0; l < 32; ++l) {
  1908. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1909. }
  1910. }
  1911. x += QK_K;
  1912. }
  1913. #endif
  1914. }
  1915. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1916. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1917. if (!quant_weights) {
  1918. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1919. }
  1920. else {
  1921. char * qrow = (char *)dst;
  1922. for (int row = 0; row < nrow; ++row) {
  1923. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1924. src += n_per_row;
  1925. qrow += row_size;
  1926. }
  1927. }
  1928. return nrow * row_size;
  1929. }
  1930. // ====================== 4-bit (de)-quantization
  1931. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1932. assert(k % QK_K == 0);
  1933. const int nb = k / QK_K;
  1934. uint8_t L[QK_K];
  1935. uint8_t Laux[32];
  1936. float weights[32];
  1937. float mins[QK_K/32];
  1938. float scales[QK_K/32];
  1939. for (int i = 0; i < nb; i++) {
  1940. float max_scale = 0; // as we are deducting the min, scales are always positive
  1941. float max_min = 0;
  1942. for (int j = 0; j < QK_K/32; ++j) {
  1943. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1944. float sum_x2 = 0;
  1945. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1946. float av_x = sqrtf(sum_x2/32);
  1947. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1948. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1949. float scale = scales[j];
  1950. if (scale > max_scale) {
  1951. max_scale = scale;
  1952. }
  1953. float min = mins[j];
  1954. if (min > max_min) {
  1955. max_min = min;
  1956. }
  1957. }
  1958. #if QK_K == 256
  1959. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1960. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1961. for (int j = 0; j < QK_K/32; ++j) {
  1962. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1963. uint8_t lm = nearest_int(inv_min*mins[j]);
  1964. ls = MIN(63, ls);
  1965. lm = MIN(63, lm);
  1966. if (j < 4) {
  1967. y[i].scales[j] = ls;
  1968. y[i].scales[j+4] = lm;
  1969. } else {
  1970. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1971. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1972. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1973. }
  1974. }
  1975. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1976. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1977. uint8_t sc, m;
  1978. for (int j = 0; j < QK_K/32; ++j) {
  1979. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1980. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1981. if (!d) continue;
  1982. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1983. for (int ii = 0; ii < 32; ++ii) {
  1984. int l = nearest_int((x[32*j + ii] + dm)/d);
  1985. l = MAX(0, MIN(15, l));
  1986. L[32*j + ii] = l;
  1987. }
  1988. }
  1989. #else
  1990. const float s_factor = 15.f;
  1991. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1992. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1993. int d1 = nearest_int(inv_scale*scales[0]);
  1994. int m1 = nearest_int(inv_min*mins[0]);
  1995. int d2 = nearest_int(inv_scale*scales[1]);
  1996. int m2 = nearest_int(inv_min*mins[1]);
  1997. y[i].scales[0] = d1 | (m1 << 4);
  1998. y[i].scales[1] = d2 | (m2 << 4);
  1999. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  2000. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  2001. float sumlx = 0;
  2002. int suml2 = 0;
  2003. for (int j = 0; j < QK_K/32; ++j) {
  2004. const uint8_t sd = y[i].scales[j] & 0xF;
  2005. const uint8_t sm = y[i].scales[j] >> 4;
  2006. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  2007. if (!d) continue;
  2008. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  2009. for (int ii = 0; ii < 32; ++ii) {
  2010. int l = nearest_int((x[32*j + ii] + m)/d);
  2011. l = MAX(0, MIN(15, l));
  2012. L[32*j + ii] = l;
  2013. sumlx += (x[32*j + ii] + m)*l*sd;
  2014. suml2 += l*l*sd*sd;
  2015. }
  2016. }
  2017. if (suml2) {
  2018. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  2019. }
  2020. #endif
  2021. uint8_t * q = y[i].qs;
  2022. for (int j = 0; j < QK_K; j += 64) {
  2023. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2024. q += 32;
  2025. }
  2026. x += QK_K;
  2027. }
  2028. }
  2029. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  2030. assert(k % QK_K == 0);
  2031. const int nb = k / QK_K;
  2032. for (int i = 0; i < nb; i++) {
  2033. const uint8_t * q = x[i].qs;
  2034. #if QK_K == 256
  2035. const float d = GGML_FP16_TO_FP32(x[i].d);
  2036. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2037. int is = 0;
  2038. uint8_t sc, m;
  2039. for (int j = 0; j < QK_K; j += 64) {
  2040. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2041. const float d1 = d * sc; const float m1 = min * m;
  2042. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2043. const float d2 = d * sc; const float m2 = min * m;
  2044. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  2045. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  2046. q += 32; is += 2;
  2047. }
  2048. #else
  2049. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  2050. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  2051. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  2052. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  2053. for (int l = 0; l < 32; ++l) {
  2054. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  2055. y[l+32] = d2 * (q[l] >> 4) - m2;
  2056. }
  2057. y += QK_K;
  2058. #endif
  2059. }
  2060. }
  2061. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  2062. assert(k % QK_K == 0);
  2063. block_q4_K * restrict y = vy;
  2064. quantize_row_q4_K_reference(x, y, k);
  2065. }
  2066. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2067. #if QK_K != 256
  2068. (void)quant_weights;
  2069. quantize_row_q4_K_reference(x, y, n_per_row);
  2070. #else
  2071. assert(n_per_row % QK_K == 0);
  2072. const int nb = n_per_row / QK_K;
  2073. uint8_t L[QK_K];
  2074. uint8_t Laux[32];
  2075. uint8_t Ls[QK_K/32];
  2076. uint8_t Lm[QK_K/32];
  2077. float weights[32];
  2078. float sw[QK_K/32];
  2079. float mins[QK_K/32];
  2080. float scales[QK_K/32];
  2081. for (int i = 0; i < nb; i++) {
  2082. float sum_x2 = 0;
  2083. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2084. float sigma2 = 2*sum_x2/QK_K;
  2085. float av_x = sqrtf(sigma2);
  2086. for (int j = 0; j < QK_K/32; ++j) {
  2087. if (quant_weights) {
  2088. const float * qw = quant_weights + QK_K*i + 32*j;
  2089. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2090. } else {
  2091. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2092. }
  2093. float sumw = 0;
  2094. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2095. sw[j] = sumw;
  2096. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2097. }
  2098. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2099. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2100. for (int j = 0; j < QK_K/32; ++j) {
  2101. uint8_t ls = Ls[j];
  2102. uint8_t lm = Lm[j];
  2103. if (j < 4) {
  2104. y[i].scales[j] = ls;
  2105. y[i].scales[j+4] = lm;
  2106. } else {
  2107. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2108. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2109. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2110. }
  2111. }
  2112. y[i].d = GGML_FP32_TO_FP16(d_block);
  2113. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2114. uint8_t sc, m;
  2115. for (int j = 0; j < QK_K/32; ++j) {
  2116. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2117. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2118. if (!d) continue;
  2119. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2120. for (int ii = 0; ii < 32; ++ii) {
  2121. int l = nearest_int((x[32*j + ii] + dm)/d);
  2122. l = MAX(0, MIN(15, l));
  2123. L[32*j + ii] = l;
  2124. }
  2125. }
  2126. uint8_t * q = y[i].qs;
  2127. for (int j = 0; j < QK_K; j += 64) {
  2128. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2129. q += 32;
  2130. }
  2131. x += QK_K;
  2132. }
  2133. #endif
  2134. }
  2135. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2136. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2137. if (!quant_weights) {
  2138. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2139. }
  2140. else {
  2141. char * qrow = (char *)dst;
  2142. for (int row = 0; row < nrow; ++row) {
  2143. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2144. src += n_per_row;
  2145. qrow += row_size;
  2146. }
  2147. }
  2148. return nrow * row_size;
  2149. }
  2150. // ====================== 5-bit (de)-quantization
  2151. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2152. assert(k % QK_K == 0);
  2153. const int nb = k / QK_K;
  2154. #if QK_K == 256
  2155. uint8_t L[QK_K];
  2156. float mins[QK_K/32];
  2157. float scales[QK_K/32];
  2158. float weights[32];
  2159. uint8_t Laux[32];
  2160. #else
  2161. int8_t L[QK_K];
  2162. float scales[QK_K/16];
  2163. #endif
  2164. for (int i = 0; i < nb; i++) {
  2165. #if QK_K == 256
  2166. float max_scale = 0; // as we are deducting the min, scales are always positive
  2167. float max_min = 0;
  2168. for (int j = 0; j < QK_K/32; ++j) {
  2169. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2170. float sum_x2 = 0;
  2171. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2172. float av_x = sqrtf(sum_x2/32);
  2173. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2174. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2175. float scale = scales[j];
  2176. if (scale > max_scale) {
  2177. max_scale = scale;
  2178. }
  2179. float min = mins[j];
  2180. if (min > max_min) {
  2181. max_min = min;
  2182. }
  2183. }
  2184. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2185. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2186. for (int j = 0; j < QK_K/32; ++j) {
  2187. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2188. uint8_t lm = nearest_int(inv_min*mins[j]);
  2189. ls = MIN(63, ls);
  2190. lm = MIN(63, lm);
  2191. if (j < 4) {
  2192. y[i].scales[j] = ls;
  2193. y[i].scales[j+4] = lm;
  2194. } else {
  2195. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2196. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2197. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2198. }
  2199. }
  2200. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2201. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2202. uint8_t sc, m;
  2203. for (int j = 0; j < QK_K/32; ++j) {
  2204. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2205. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2206. if (!d) continue;
  2207. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2208. for (int ii = 0; ii < 32; ++ii) {
  2209. int l = nearest_int((x[32*j + ii] + dm)/d);
  2210. l = MAX(0, MIN(31, l));
  2211. L[32*j + ii] = l;
  2212. }
  2213. }
  2214. uint8_t * restrict qh = y[i].qh;
  2215. uint8_t * restrict ql = y[i].qs;
  2216. memset(qh, 0, QK_K/8);
  2217. uint8_t m1 = 1, m2 = 2;
  2218. for (int n = 0; n < QK_K; n += 64) {
  2219. for (int j = 0; j < 32; ++j) {
  2220. int l1 = L[n + j];
  2221. if (l1 > 15) {
  2222. l1 -= 16; qh[j] |= m1;
  2223. }
  2224. int l2 = L[n + j + 32];
  2225. if (l2 > 15) {
  2226. l2 -= 16; qh[j] |= m2;
  2227. }
  2228. ql[j] = l1 | (l2 << 4);
  2229. }
  2230. m1 <<= 2; m2 <<= 2;
  2231. ql += 32;
  2232. }
  2233. #else
  2234. float max_scale = 0, amax = 0;
  2235. for (int j = 0; j < QK_K/16; ++j) {
  2236. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2237. float abs_scale = fabsf(scales[j]);
  2238. if (abs_scale > amax) {
  2239. amax = abs_scale;
  2240. max_scale = scales[j];
  2241. }
  2242. }
  2243. float iscale = -128.f/max_scale;
  2244. for (int j = 0; j < QK_K/16; ++j) {
  2245. int l = nearest_int(iscale*scales[j]);
  2246. y[i].scales[j] = MAX(-128, MIN(127, l));
  2247. }
  2248. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2249. for (int j = 0; j < QK_K/16; ++j) {
  2250. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2251. if (!d) continue;
  2252. for (int ii = 0; ii < 16; ++ii) {
  2253. int l = nearest_int(x[16*j + ii]/d);
  2254. l = MAX(-16, MIN(15, l));
  2255. L[16*j + ii] = l + 16;
  2256. }
  2257. }
  2258. uint8_t * restrict qh = y[i].qh;
  2259. uint8_t * restrict ql = y[i].qs;
  2260. memset(qh, 0, QK_K/8);
  2261. for (int j = 0; j < 32; ++j) {
  2262. int jm = j%8;
  2263. int is = j/8;
  2264. int l1 = L[j];
  2265. if (l1 > 15) {
  2266. l1 -= 16; qh[jm] |= (1 << is);
  2267. }
  2268. int l2 = L[j + 32];
  2269. if (l2 > 15) {
  2270. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2271. }
  2272. ql[j] = l1 | (l2 << 4);
  2273. }
  2274. #endif
  2275. x += QK_K;
  2276. }
  2277. }
  2278. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2279. assert(k % QK_K == 0);
  2280. const int nb = k / QK_K;
  2281. for (int i = 0; i < nb; i++) {
  2282. const uint8_t * ql = x[i].qs;
  2283. const uint8_t * qh = x[i].qh;
  2284. #if QK_K == 256
  2285. const float d = GGML_FP16_TO_FP32(x[i].d);
  2286. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2287. int is = 0;
  2288. uint8_t sc, m;
  2289. uint8_t u1 = 1, u2 = 2;
  2290. for (int j = 0; j < QK_K; j += 64) {
  2291. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2292. const float d1 = d * sc; const float m1 = min * m;
  2293. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2294. const float d2 = d * sc; const float m2 = min * m;
  2295. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2296. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2297. ql += 32; is += 2;
  2298. u1 <<= 2; u2 <<= 2;
  2299. }
  2300. #else
  2301. float d = GGML_FP16_TO_FP32(x[i].d);
  2302. const int8_t * restrict s = x[i].scales;
  2303. for (int l = 0; l < 8; ++l) {
  2304. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2305. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2306. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2307. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2308. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2309. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2310. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2311. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2312. }
  2313. y += QK_K;
  2314. #endif
  2315. }
  2316. }
  2317. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2318. assert(k % QK_K == 0);
  2319. block_q5_K * restrict y = vy;
  2320. quantize_row_q5_K_reference(x, y, k);
  2321. }
  2322. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2323. #if QK_K != 256
  2324. (void)quant_weights;
  2325. quantize_row_q5_K_reference(x, y, n_per_row);
  2326. #else
  2327. assert(n_per_row % QK_K == 0);
  2328. const int nb = n_per_row / QK_K;
  2329. uint8_t L[QK_K];
  2330. uint8_t Laux[32];
  2331. uint8_t Ls[QK_K/32];
  2332. uint8_t Lm[QK_K/32];
  2333. float mins[QK_K/32];
  2334. float scales[QK_K/32];
  2335. float sw[QK_K/32];
  2336. float weights[32];
  2337. for (int i = 0; i < nb; i++) {
  2338. float sum_x2 = 0;
  2339. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2340. float sigma2 = 2*sum_x2/QK_K;
  2341. float av_x = sqrtf(sigma2);
  2342. for (int j = 0; j < QK_K/32; ++j) {
  2343. if (quant_weights) {
  2344. const float * qw = quant_weights + QK_K*i + 32*j;
  2345. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2346. } else {
  2347. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2348. }
  2349. float sumw = 0;
  2350. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2351. sw[j] = sumw;
  2352. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2353. }
  2354. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2355. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2356. for (int j = 0; j < QK_K/32; ++j) {
  2357. uint8_t ls = Ls[j];
  2358. uint8_t lm = Lm[j];
  2359. ls = MIN(63, ls);
  2360. lm = MIN(63, lm);
  2361. if (j < 4) {
  2362. y[i].scales[j] = ls;
  2363. y[i].scales[j+4] = lm;
  2364. } else {
  2365. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2366. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2367. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2368. }
  2369. }
  2370. y[i].d = GGML_FP32_TO_FP16(d_block);
  2371. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2372. uint8_t sc, m;
  2373. for (int j = 0; j < QK_K/32; ++j) {
  2374. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2375. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2376. if (!d) continue;
  2377. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2378. for (int ii = 0; ii < 32; ++ii) {
  2379. int l = nearest_int((x[32*j + ii] + dm)/d);
  2380. l = MAX(0, MIN(31, l));
  2381. L[32*j + ii] = l;
  2382. }
  2383. }
  2384. uint8_t * restrict qh = y[i].qh;
  2385. uint8_t * restrict ql = y[i].qs;
  2386. memset(qh, 0, QK_K/8);
  2387. uint8_t m1 = 1, m2 = 2;
  2388. for (int n = 0; n < QK_K; n += 64) {
  2389. for (int j = 0; j < 32; ++j) {
  2390. int l1 = L[n + j];
  2391. if (l1 > 15) {
  2392. l1 -= 16; qh[j] |= m1;
  2393. }
  2394. int l2 = L[n + j + 32];
  2395. if (l2 > 15) {
  2396. l2 -= 16; qh[j] |= m2;
  2397. }
  2398. ql[j] = l1 | (l2 << 4);
  2399. }
  2400. m1 <<= 2; m2 <<= 2;
  2401. ql += 32;
  2402. }
  2403. x += QK_K;
  2404. }
  2405. #endif
  2406. }
  2407. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2408. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2409. if (!quant_weights) {
  2410. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2411. }
  2412. else {
  2413. char * qrow = (char *)dst;
  2414. for (int row = 0; row < nrow; ++row) {
  2415. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2416. src += n_per_row;
  2417. qrow += row_size;
  2418. }
  2419. }
  2420. return nrow * row_size;
  2421. }
  2422. // ====================== 6-bit (de)-quantization
  2423. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2424. assert(k % QK_K == 0);
  2425. const int nb = k / QK_K;
  2426. int8_t L[QK_K];
  2427. float scales[QK_K/16];
  2428. for (int i = 0; i < nb; i++) {
  2429. float max_scale = 0;
  2430. float max_abs_scale = 0;
  2431. for (int ib = 0; ib < QK_K/16; ++ib) {
  2432. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2433. scales[ib] = scale;
  2434. const float abs_scale = fabsf(scale);
  2435. if (abs_scale > max_abs_scale) {
  2436. max_abs_scale = abs_scale;
  2437. max_scale = scale;
  2438. }
  2439. }
  2440. if (!max_abs_scale) {
  2441. memset(&y[i], 0, sizeof(block_q6_K));
  2442. y[i].d = GGML_FP32_TO_FP16(0.f);
  2443. x += QK_K;
  2444. continue;
  2445. }
  2446. float iscale = -128.f/max_scale;
  2447. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2448. for (int ib = 0; ib < QK_K/16; ++ib) {
  2449. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2450. }
  2451. for (int j = 0; j < QK_K/16; ++j) {
  2452. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2453. if (!d) {
  2454. continue;
  2455. }
  2456. for (int ii = 0; ii < 16; ++ii) {
  2457. int l = nearest_int(x[16*j + ii]/d);
  2458. l = MAX(-32, MIN(31, l));
  2459. L[16*j + ii] = l + 32;
  2460. }
  2461. }
  2462. uint8_t * restrict ql = y[i].ql;
  2463. uint8_t * restrict qh = y[i].qh;
  2464. #if QK_K == 256
  2465. for (int j = 0; j < QK_K; j += 128) {
  2466. for (int l = 0; l < 32; ++l) {
  2467. const uint8_t q1 = L[j + l + 0] & 0xF;
  2468. const uint8_t q2 = L[j + l + 32] & 0xF;
  2469. const uint8_t q3 = L[j + l + 64] & 0xF;
  2470. const uint8_t q4 = L[j + l + 96] & 0xF;
  2471. ql[l+ 0] = q1 | (q3 << 4);
  2472. ql[l+32] = q2 | (q4 << 4);
  2473. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2474. }
  2475. ql += 64;
  2476. qh += 32;
  2477. }
  2478. #else
  2479. for (int l = 0; l < 32; ++l) {
  2480. const uint8_t q1 = L[l + 0] & 0xF;
  2481. const uint8_t q2 = L[l + 32] & 0xF;
  2482. ql[l] = q1 | (q2 << 4);
  2483. }
  2484. for (int l = 0; l < 16; ++l) {
  2485. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2486. }
  2487. #endif
  2488. x += QK_K;
  2489. }
  2490. }
  2491. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2492. assert(k % QK_K == 0);
  2493. const int nb = k / QK_K;
  2494. for (int i = 0; i < nb; i++) {
  2495. const float d = GGML_FP16_TO_FP32(x[i].d);
  2496. const uint8_t * restrict ql = x[i].ql;
  2497. const uint8_t * restrict qh = x[i].qh;
  2498. const int8_t * restrict sc = x[i].scales;
  2499. #if QK_K == 256
  2500. for (int n = 0; n < QK_K; n += 128) {
  2501. for (int l = 0; l < 32; ++l) {
  2502. int is = l/16;
  2503. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2504. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2505. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2506. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2507. y[l + 0] = d * sc[is + 0] * q1;
  2508. y[l + 32] = d * sc[is + 2] * q2;
  2509. y[l + 64] = d * sc[is + 4] * q3;
  2510. y[l + 96] = d * sc[is + 6] * q4;
  2511. }
  2512. y += 128;
  2513. ql += 64;
  2514. qh += 32;
  2515. sc += 8;
  2516. }
  2517. #else
  2518. for (int l = 0; l < 16; ++l) {
  2519. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2520. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2521. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2522. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2523. y[l+ 0] = d * sc[0] * q1;
  2524. y[l+16] = d * sc[1] * q2;
  2525. y[l+32] = d * sc[2] * q3;
  2526. y[l+48] = d * sc[3] * q4;
  2527. }
  2528. y += 64;
  2529. #endif
  2530. }
  2531. }
  2532. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2533. assert(k % QK_K == 0);
  2534. block_q6_K * restrict y = vy;
  2535. quantize_row_q6_K_reference(x, y, k);
  2536. }
  2537. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2538. #if QK_K != 256
  2539. (void)quant_weights;
  2540. quantize_row_q6_K_reference(x, y, n_per_row);
  2541. #else
  2542. assert(n_per_row % QK_K == 0);
  2543. const int nb = n_per_row / QK_K;
  2544. int8_t L[QK_K];
  2545. float scales[QK_K/16];
  2546. //float weights[16];
  2547. for (int i = 0; i < nb; i++) {
  2548. //float sum_x2 = 0;
  2549. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2550. //float sigma2 = sum_x2/QK_K;
  2551. float max_scale = 0;
  2552. float max_abs_scale = 0;
  2553. for (int ib = 0; ib < QK_K/16; ++ib) {
  2554. float scale;
  2555. if (quant_weights) {
  2556. const float * qw = quant_weights + QK_K*i + 16*ib;
  2557. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2558. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2559. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2560. } else {
  2561. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2562. }
  2563. scales[ib] = scale;
  2564. const float abs_scale = fabsf(scale);
  2565. if (abs_scale > max_abs_scale) {
  2566. max_abs_scale = abs_scale;
  2567. max_scale = scale;
  2568. }
  2569. }
  2570. if (!max_abs_scale) {
  2571. memset(&y[i], 0, sizeof(block_q6_K));
  2572. y[i].d = GGML_FP32_TO_FP16(0.f);
  2573. x += QK_K;
  2574. continue;
  2575. }
  2576. float iscale = -128.f/max_scale;
  2577. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2578. for (int ib = 0; ib < QK_K/16; ++ib) {
  2579. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2580. }
  2581. for (int j = 0; j < QK_K/16; ++j) {
  2582. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2583. if (!d) {
  2584. continue;
  2585. }
  2586. for (int ii = 0; ii < 16; ++ii) {
  2587. int l = nearest_int(x[16*j + ii]/d);
  2588. l = MAX(-32, MIN(31, l));
  2589. L[16*j + ii] = l + 32;
  2590. }
  2591. }
  2592. uint8_t * restrict ql = y[i].ql;
  2593. uint8_t * restrict qh = y[i].qh;
  2594. for (int j = 0; j < QK_K; j += 128) {
  2595. for (int l = 0; l < 32; ++l) {
  2596. const uint8_t q1 = L[j + l + 0] & 0xF;
  2597. const uint8_t q2 = L[j + l + 32] & 0xF;
  2598. const uint8_t q3 = L[j + l + 64] & 0xF;
  2599. const uint8_t q4 = L[j + l + 96] & 0xF;
  2600. ql[l+ 0] = q1 | (q3 << 4);
  2601. ql[l+32] = q2 | (q4 << 4);
  2602. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2603. }
  2604. ql += 64;
  2605. qh += 32;
  2606. }
  2607. x += QK_K;
  2608. }
  2609. #endif
  2610. }
  2611. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2612. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2613. if (!quant_weights) {
  2614. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2615. }
  2616. else {
  2617. char * qrow = (char *)dst;
  2618. for (int row = 0; row < nrow; ++row) {
  2619. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2620. src += n_per_row;
  2621. qrow += row_size;
  2622. }
  2623. }
  2624. return nrow * row_size;
  2625. }
  2626. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2627. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2628. if (!quant_weights) {
  2629. quantize_row_q4_0_reference(x, y, n_per_row);
  2630. return;
  2631. }
  2632. float weight[QK4_0];
  2633. int8_t L[QK4_0];
  2634. float sum_x2 = 0;
  2635. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2636. float sigma2 = sum_x2/n_per_row;
  2637. const int nb = n_per_row/QK4_0;
  2638. for (int ib = 0; ib < nb; ++ib) {
  2639. const float * xb = x + QK4_0 * ib;
  2640. const float * qw = quant_weights + QK4_0 * ib;
  2641. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2642. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2643. y[ib].d = GGML_FP32_TO_FP16(d);
  2644. for (int j = 0; j < 16; ++j) {
  2645. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2646. }
  2647. }
  2648. }
  2649. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2650. if (!quant_weights) {
  2651. quantize_row_q4_0_reference(src, dst, nrow*n_per_row);
  2652. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2653. }
  2654. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2655. char * qrow = (char *)dst;
  2656. for (int row = 0; row < nrow; ++row) {
  2657. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2658. src += n_per_row;
  2659. qrow += row_size;
  2660. }
  2661. return nrow * row_size;
  2662. }
  2663. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2664. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2665. if (!quant_weights) {
  2666. quantize_row_q4_1_reference(x, y, n_per_row);
  2667. return;
  2668. }
  2669. float weight[QK4_1];
  2670. uint8_t L[QK4_1], Laux[QK4_1];
  2671. float sum_x2 = 0;
  2672. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2673. float sigma2 = sum_x2/n_per_row;
  2674. const int nb = n_per_row/QK4_1;
  2675. for (int ib = 0; ib < nb; ++ib) {
  2676. const float * xb = x + QK4_1 * ib;
  2677. const float * qw = quant_weights + QK4_1 * ib;
  2678. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2679. float min;
  2680. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2681. y[ib].d = GGML_FP32_TO_FP16(d);
  2682. y[ib].m = GGML_FP32_TO_FP16(-min);
  2683. for (int j = 0; j < 16; ++j) {
  2684. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2685. }
  2686. }
  2687. }
  2688. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2689. if (!quant_weights) {
  2690. quantize_row_q4_1_reference(src, dst, nrow*n_per_row);
  2691. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2692. }
  2693. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2694. char * qrow = (char *)dst;
  2695. for (int row = 0; row < nrow; ++row) {
  2696. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2697. src += n_per_row;
  2698. qrow += row_size;
  2699. }
  2700. return nrow * row_size;
  2701. }
  2702. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2703. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2704. if (!quant_weights) {
  2705. quantize_row_q5_0_reference(x, y, n_per_row);
  2706. return;
  2707. }
  2708. float weight[QK5_0];
  2709. int8_t L[QK5_0];
  2710. float sum_x2 = 0;
  2711. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2712. float sigma2 = sum_x2/n_per_row;
  2713. const int nb = n_per_row/QK5_0;
  2714. for (int ib = 0; ib < nb; ++ib) {
  2715. const float * xb = x + QK5_0 * ib;
  2716. const float * qw = quant_weights + QK5_0 * ib;
  2717. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2718. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2719. y[ib].d = GGML_FP32_TO_FP16(d);
  2720. uint32_t qh = 0;
  2721. for (int j = 0; j < 16; ++j) {
  2722. const uint8_t xi0 = L[j];
  2723. const uint8_t xi1 = L[j+16];
  2724. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2725. // get the 5-th bit and store it in qh at the right position
  2726. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2727. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2728. }
  2729. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2730. }
  2731. }
  2732. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2733. if (!quant_weights) {
  2734. quantize_row_q5_0_reference(src, dst, nrow*n_per_row);
  2735. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2736. }
  2737. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2738. char * qrow = (char *)dst;
  2739. for (int row = 0; row < nrow; ++row) {
  2740. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2741. src += n_per_row;
  2742. qrow += row_size;
  2743. }
  2744. return nrow * row_size;
  2745. }
  2746. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2747. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2748. if (!quant_weights) {
  2749. quantize_row_q5_1_reference(x, y, n_per_row);
  2750. return;
  2751. }
  2752. float weight[QK5_1];
  2753. uint8_t L[QK5_1], Laux[QK5_1];
  2754. float sum_x2 = 0;
  2755. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2756. float sigma2 = sum_x2/n_per_row;
  2757. const int nb = n_per_row/QK5_1;
  2758. for (int ib = 0; ib < nb; ++ib) {
  2759. const float * xb = x + QK5_1 * ib;
  2760. const float * qw = quant_weights + QK5_1 * ib;
  2761. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2762. float min;
  2763. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2764. y[ib].d = GGML_FP32_TO_FP16(d);
  2765. y[ib].m = GGML_FP32_TO_FP16(-min);
  2766. uint32_t qh = 0;
  2767. for (int j = 0; j < 16; ++j) {
  2768. const uint8_t xi0 = L[j];
  2769. const uint8_t xi1 = L[j+16];
  2770. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2771. // get the 5-th bit and store it in qh at the right position
  2772. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2773. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2774. }
  2775. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2776. }
  2777. }
  2778. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2779. if (!quant_weights) {
  2780. quantize_row_q5_1_reference(src, dst, nrow*n_per_row);
  2781. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2782. }
  2783. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2784. char * qrow = (char *)dst;
  2785. for (int row = 0; row < nrow; ++row) {
  2786. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2787. src += n_per_row;
  2788. qrow += row_size;
  2789. }
  2790. return nrow * row_size;
  2791. }
  2792. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2793. (void)quant_weights; // not used
  2794. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2795. quantize_row_q8_0_reference(src, dst, nrow*n_per_row);
  2796. return nrow * row_size;
  2797. }
  2798. // ====================== "True" 2-bit (de)-quantization
  2799. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2800. assert(k % QK_K == 0);
  2801. const int nb = k / QK_K;
  2802. uint32_t aux32[2];
  2803. const uint8_t * aux8 = (const uint8_t *)aux32;
  2804. for (int i = 0; i < nb; i++) {
  2805. const float d = GGML_FP16_TO_FP32(x[i].d);
  2806. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2807. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2808. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2809. for (int l = 0; l < 4; ++l) {
  2810. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2811. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2812. for (int j = 0; j < 8; ++j) {
  2813. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2814. }
  2815. y += 8;
  2816. }
  2817. }
  2818. }
  2819. }
  2820. // ====================== 2.3125 bpw (de)-quantization
  2821. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2822. assert(k % QK_K == 0);
  2823. const int nb = k / QK_K;
  2824. float db[2];
  2825. for (int i = 0; i < nb; i++) {
  2826. const float d = GGML_FP16_TO_FP32(x[i].d);
  2827. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2828. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2829. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2830. for (int l = 0; l < 4; ++l) {
  2831. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2832. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2833. for (int j = 0; j < 8; ++j) {
  2834. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2835. }
  2836. y += 8;
  2837. }
  2838. }
  2839. }
  2840. }
  2841. // ====================== 2.5625 bpw (de)-quantization
  2842. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int k) {
  2843. assert(k % QK_K == 0);
  2844. const int nb = k / QK_K;
  2845. float db[2];
  2846. for (int i = 0; i < nb; i++) {
  2847. const float d = GGML_FP16_TO_FP32(x[i].d);
  2848. const uint8_t * qs = x[i].qs;
  2849. const uint8_t * qh = x[i].qh;
  2850. const uint8_t * signs = qs + QK_K/8;
  2851. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2852. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2853. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2854. for (int l = 0; l < 4; ++l) {
  2855. const float dl = db[l/2];
  2856. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2857. for (int j = 0; j < 8; ++j) {
  2858. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2859. }
  2860. y += 8;
  2861. }
  2862. qs += 4;
  2863. signs += 4;
  2864. }
  2865. }
  2866. }
  2867. // ====================== 3.0625 bpw (de)-quantization
  2868. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  2869. assert(k % QK_K == 0);
  2870. const int nb = k / QK_K;
  2871. uint32_t aux32;
  2872. for (int i = 0; i < nb; i++) {
  2873. const float d = GGML_FP16_TO_FP32(x[i].d);
  2874. const uint8_t * qs = x[i].qs;
  2875. const uint8_t * scales_and_signs = qs + QK_K/4;
  2876. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2877. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2878. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2879. for (int l = 0; l < 4; ++l) {
  2880. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2881. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2882. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2883. for (int j = 0; j < 4; ++j) {
  2884. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2885. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2886. }
  2887. y += 8;
  2888. }
  2889. qs += 8;
  2890. }
  2891. }
  2892. }
  2893. // ====================== 3.3125 bpw (de)-quantization
  2894. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int k) {
  2895. assert(k % QK_K == 0);
  2896. const int nb = k / QK_K;
  2897. for (int i = 0; i < nb; i++) {
  2898. const float d = GGML_FP16_TO_FP32(x[i].d);
  2899. const uint8_t * qs = x[i].qs;
  2900. const uint8_t * qh = x[i].qh;
  2901. const uint8_t * signs = x[i].signs;
  2902. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2903. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2904. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2905. for (int l = 0; l < 4; ++l) {
  2906. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2907. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2908. for (int j = 0; j < 4; ++j) {
  2909. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2910. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2911. }
  2912. y += 8;
  2913. }
  2914. qs += 8;
  2915. signs += 4;
  2916. for (int l = 0; l < 4; ++l) {
  2917. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2918. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2919. for (int j = 0; j < 4; ++j) {
  2920. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2921. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2922. }
  2923. y += 8;
  2924. }
  2925. qh += 2;
  2926. qs += 8;
  2927. signs += 4;
  2928. }
  2929. }
  2930. }
  2931. // ====================== 1.5625 bpw (de)-quantization
  2932. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int k) {
  2933. assert(k % QK_K == 0);
  2934. const int nb = k / QK_K;
  2935. for (int i = 0; i < nb; i++) {
  2936. const float d = GGML_FP16_TO_FP32(x[i].d);
  2937. const uint8_t * qs = x[i].qs;
  2938. const uint16_t * qh = x[i].qh;
  2939. for (int ib = 0; ib < QK_K/32; ++ib) {
  2940. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2941. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2942. for (int l = 0; l < 4; ++l) {
  2943. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2944. for (int j = 0; j < 8; ++j) {
  2945. y[j] = dl * (grid[j] + delta);
  2946. }
  2947. y += 8;
  2948. }
  2949. qs += 4;
  2950. }
  2951. }
  2952. }
  2953. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int k) {
  2954. assert(k % QK_K == 0);
  2955. const int nb = k / QK_K;
  2956. float delta[4];
  2957. uint16_t idx[4];
  2958. #if QK_K != 64
  2959. iq1m_scale_t scale;
  2960. #endif
  2961. for (int i = 0; i < nb; i++) {
  2962. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2963. #if QK_K == 64
  2964. const float d = GGML_FP16_TO_FP32(x[i].d);
  2965. #else
  2966. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2967. const float d = GGML_FP16_TO_FP32(scale.f16);
  2968. #endif
  2969. const uint8_t * qs = x[i].qs;
  2970. const uint8_t * qh = x[i].qh;
  2971. for (int ib = 0; ib < QK_K/32; ++ib) {
  2972. #if QK_K == 64
  2973. const float dl1 = d * (2*((sc[ib/2] >> (8*(ib%2)+0)) & 0xf) + 1);
  2974. const float dl2 = d * (2*((sc[ib/2] >> (8*(ib%2)+4)) & 0xf) + 1);
  2975. #else
  2976. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2977. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2978. #endif
  2979. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2980. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2981. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2982. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2983. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2984. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2985. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2986. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2987. for (int l = 0; l < 2; ++l) {
  2988. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2989. for (int j = 0; j < 8; ++j) {
  2990. y[j] = dl1 * (grid[j] + delta[l]);
  2991. }
  2992. y += 8;
  2993. }
  2994. for (int l = 2; l < 4; ++l) {
  2995. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2996. for (int j = 0; j < 8; ++j) {
  2997. y[j] = dl2 * (grid[j] + delta[l]);
  2998. }
  2999. y += 8;
  3000. }
  3001. qs += 4;
  3002. qh += 2;
  3003. }
  3004. }
  3005. }
  3006. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  3007. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int k) {
  3008. assert(k % QK4_NL == 0);
  3009. const int nb = k / QK4_NL;
  3010. for (int i = 0; i < nb; i++) {
  3011. const uint8_t * qs = x[i].qs;
  3012. const float d = GGML_FP16_TO_FP32(x[i].d);
  3013. for (int j = 0; j < QK4_NL/2; ++j) {
  3014. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  3015. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  3016. }
  3017. y += QK4_NL;
  3018. qs += QK4_NL/2;
  3019. }
  3020. }
  3021. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int k) {
  3022. assert(k % QK_K == 0);
  3023. #if QK_K == 64
  3024. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  3025. #else
  3026. const int nb = k / QK_K;
  3027. for (int i = 0; i < nb; i++) {
  3028. const uint8_t * qs = x[i].qs;
  3029. const float d = GGML_FP16_TO_FP32(x[i].d);
  3030. for (int ib = 0; ib < QK_K/32; ++ib) {
  3031. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  3032. const float dl = d * (ls - 32);
  3033. for (int j = 0; j < 16; ++j) {
  3034. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  3035. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  3036. }
  3037. y += 32;
  3038. qs += 16;
  3039. }
  3040. }
  3041. #endif
  3042. }
  3043. //===================================== Q8_K ==============================================
  3044. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3045. assert(k % QK_K == 0);
  3046. const int nb = k / QK_K;
  3047. for (int i = 0; i < nb; i++) {
  3048. float max = 0;
  3049. float amax = 0;
  3050. for (int j = 0; j < QK_K; ++j) {
  3051. float ax = fabsf(x[j]);
  3052. if (ax > amax) {
  3053. amax = ax; max = x[j];
  3054. }
  3055. }
  3056. if (!amax) {
  3057. y[i].d = 0;
  3058. memset(y[i].qs, 0, QK_K);
  3059. x += QK_K;
  3060. continue;
  3061. }
  3062. //const float iscale = -128.f/max;
  3063. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3064. const float iscale = -127.f/max;
  3065. for (int j = 0; j < QK_K; ++j) {
  3066. int v = nearest_int(iscale*x[j]);
  3067. y[i].qs[j] = MIN(127, v);
  3068. }
  3069. for (int j = 0; j < QK_K/16; ++j) {
  3070. int sum = 0;
  3071. for (int ii = 0; ii < 16; ++ii) {
  3072. sum += y[i].qs[j*16 + ii];
  3073. }
  3074. y[i].bsums[j] = sum;
  3075. }
  3076. y[i].d = 1/iscale;
  3077. x += QK_K;
  3078. }
  3079. }
  3080. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3081. assert(k % QK_K == 0);
  3082. const int nb = k / QK_K;
  3083. for (int i = 0; i < nb; i++) {
  3084. for (int j = 0; j < QK_K; ++j) {
  3085. *y++ = x[i].d * x[i].qs[j];
  3086. }
  3087. }
  3088. }
  3089. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3090. quantize_row_q8_K_reference(x, y, k);
  3091. }
  3092. //===================================== Dot ptoducts =================================
  3093. //
  3094. // Helper functions
  3095. //
  3096. #if __AVX__ || __AVX2__ || __AVX512F__
  3097. // shuffles to pick the required scales in dot products
  3098. static inline __m256i get_scale_shuffle_q3k(int i) {
  3099. static const uint8_t k_shuffle[128] = {
  3100. 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,
  3101. 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,
  3102. 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,
  3103. 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,
  3104. };
  3105. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3106. }
  3107. static inline __m256i get_scale_shuffle_k4(int i) {
  3108. static const uint8_t k_shuffle[256] = {
  3109. 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,
  3110. 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,
  3111. 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,
  3112. 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,
  3113. 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,
  3114. 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,
  3115. 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,
  3116. 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
  3117. };
  3118. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3119. }
  3120. static inline __m128i get_scale_shuffle(int i) {
  3121. static const uint8_t k_shuffle[128] = {
  3122. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3123. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3124. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3125. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3126. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3127. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3128. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3129. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3130. };
  3131. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3132. }
  3133. #endif
  3134. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3135. const int qk = QK8_0;
  3136. const int nb = n / qk;
  3137. assert(n % qk == 0);
  3138. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3139. assert((nrc == 2) || (nrc == 1));
  3140. #else
  3141. assert(nrc == 1);
  3142. #endif
  3143. UNUSED(nrc);
  3144. UNUSED(bx);
  3145. UNUSED(by);
  3146. UNUSED(bs);
  3147. const block_q4_0 * restrict x = vx;
  3148. const block_q8_0 * restrict y = vy;
  3149. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3150. if (nrc == 2) {
  3151. const block_q4_0 * restrict vx0 = vx;
  3152. const block_q4_0 * restrict vx1 = vx + bx;
  3153. const block_q8_0 * restrict vy0 = vy;
  3154. const block_q8_0 * restrict vy1 = vy + by;
  3155. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3156. for (int i = 0; i < nb; i++) {
  3157. const block_q4_0 * restrict b_x0 = &vx0[i];
  3158. const block_q4_0 * restrict b_x1 = &vx1[i];
  3159. const block_q8_0 * restrict b_y0 = &vy0[i];
  3160. const block_q8_0 * restrict b_y1 = &vy1[i];
  3161. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3162. const int8x16_t s8b = vdupq_n_s8(0x8);
  3163. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3164. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3165. // 4-bit -> 8-bit
  3166. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3167. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3168. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3169. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3170. // sub 8
  3171. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  3172. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  3173. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  3174. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  3175. // load y
  3176. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3177. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3178. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3179. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3180. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3181. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3182. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3183. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3184. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3185. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3186. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3187. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3188. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3189. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3190. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3191. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3192. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3193. l1, r1)), l2, r2)), l3, r3))), scale);
  3194. }
  3195. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3196. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3197. vst1_f32(s, vget_low_f32(sumv2));
  3198. vst1_f32(s + bs, vget_high_f32(sumv2));
  3199. return;
  3200. }
  3201. #endif
  3202. #if defined(__ARM_NEON)
  3203. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3204. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3205. assert(nb % 2 == 0); // TODO: handle odd nb
  3206. for (int i = 0; i < nb; i += 2) {
  3207. const block_q4_0 * restrict x0 = &x[i + 0];
  3208. const block_q4_0 * restrict x1 = &x[i + 1];
  3209. const block_q8_0 * restrict y0 = &y[i + 0];
  3210. const block_q8_0 * restrict y1 = &y[i + 1];
  3211. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3212. const int8x16_t s8b = vdupq_n_s8(0x8);
  3213. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3214. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3215. // 4-bit -> 8-bit
  3216. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3217. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3218. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3219. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3220. // sub 8
  3221. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3222. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3223. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3224. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3225. // load y
  3226. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3227. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3228. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3229. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3230. // dot product into int32x4_t
  3231. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3232. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3233. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3234. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3235. }
  3236. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3237. #elif defined(__AVX2__)
  3238. // Initialize accumulator with zeros
  3239. __m256 acc = _mm256_setzero_ps();
  3240. // Main loop
  3241. for (int i = 0; i < nb; ++i) {
  3242. /* Compute combined scale for the block */
  3243. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3244. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3245. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3246. const __m256i off = _mm256_set1_epi8( 8 );
  3247. qx = _mm256_sub_epi8( qx, off );
  3248. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3249. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3250. /* Multiply q with scale and accumulate */
  3251. acc = _mm256_fmadd_ps( d, q, acc );
  3252. }
  3253. *s = hsum_float_8(acc);
  3254. #elif defined(__AVX__)
  3255. // Initialize accumulator with zeros
  3256. __m256 acc = _mm256_setzero_ps();
  3257. // Main loop
  3258. for (int i = 0; i < nb; ++i) {
  3259. // Compute combined scale for the block
  3260. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3261. const __m128i lowMask = _mm_set1_epi8(0xF);
  3262. const __m128i off = _mm_set1_epi8(8);
  3263. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3264. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3265. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3266. bx_0 = _mm_sub_epi8(bx_0, off);
  3267. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3268. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3269. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3270. bx_0 = _mm_sub_epi8(bx_0, off);
  3271. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3272. // Convert int32_t to float
  3273. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3274. // Apply the scale, and accumulate
  3275. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3276. }
  3277. *s = hsum_float_8(acc);
  3278. #elif defined(__SSSE3__)
  3279. // set constants
  3280. const __m128i lowMask = _mm_set1_epi8(0xF);
  3281. const __m128i off = _mm_set1_epi8(8);
  3282. // Initialize accumulator with zeros
  3283. __m128 acc_0 = _mm_setzero_ps();
  3284. __m128 acc_1 = _mm_setzero_ps();
  3285. __m128 acc_2 = _mm_setzero_ps();
  3286. __m128 acc_3 = _mm_setzero_ps();
  3287. // First round without accumulation
  3288. {
  3289. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3290. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3291. // Compute combined scale for the block 0 and 1
  3292. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3293. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3294. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3295. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3296. bx_0 = _mm_sub_epi8(bx_0, off);
  3297. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3298. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3299. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3300. bx_1 = _mm_sub_epi8(bx_1, off);
  3301. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3302. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3303. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3304. // Compute combined scale for the block 2 and 3
  3305. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3306. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3307. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3308. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3309. bx_2 = _mm_sub_epi8(bx_2, off);
  3310. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3311. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3312. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3313. bx_3 = _mm_sub_epi8(bx_3, off);
  3314. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3315. // Convert int32_t to float
  3316. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3317. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3318. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3319. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3320. // Apply the scale
  3321. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3322. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3323. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3324. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3325. }
  3326. assert(nb % 2 == 0); // TODO: handle odd nb
  3327. // Main loop
  3328. for (int i = 2; i < nb; i+=2) {
  3329. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3330. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3331. // Compute combined scale for the block 0 and 1
  3332. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3333. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3334. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3335. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3336. bx_0 = _mm_sub_epi8(bx_0, off);
  3337. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3338. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3339. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3340. bx_1 = _mm_sub_epi8(bx_1, off);
  3341. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3342. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3343. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3344. // Compute combined scale for the block 2 and 3
  3345. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3346. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3347. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3348. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3349. bx_2 = _mm_sub_epi8(bx_2, off);
  3350. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3351. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3352. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3353. bx_3 = _mm_sub_epi8(bx_3, off);
  3354. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3355. // Convert int32_t to float
  3356. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3357. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3358. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3359. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3360. // Apply the scale
  3361. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3362. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3363. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3364. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3365. // Acummulate
  3366. acc_0 = _mm_add_ps(p0_d, acc_0);
  3367. acc_1 = _mm_add_ps(p1_d, acc_1);
  3368. acc_2 = _mm_add_ps(p2_d, acc_2);
  3369. acc_3 = _mm_add_ps(p3_d, acc_3);
  3370. }
  3371. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3372. #elif defined(__riscv_v_intrinsic)
  3373. float sumf = 0.0;
  3374. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3375. for (int i = 0; i < nb; i++) {
  3376. // load elements
  3377. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3378. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3379. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3380. // mask and store lower part of x, and then upper part
  3381. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3382. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3383. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3384. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3385. // subtract offset
  3386. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3387. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3388. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3389. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3390. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3391. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3392. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3393. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3394. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3395. }
  3396. *s = sumf;
  3397. #else
  3398. // scalar
  3399. float sumf = 0.0;
  3400. for (int i = 0; i < nb; i++) {
  3401. int sumi = 0;
  3402. for (int j = 0; j < qk/2; ++j) {
  3403. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3404. const int v1 = (x[i].qs[j] >> 4) - 8;
  3405. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3406. }
  3407. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3408. }
  3409. *s = sumf;
  3410. #endif
  3411. }
  3412. void ggml_vec_dot_q4_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3413. const int qk = QK8_1;
  3414. const int nb = n / qk;
  3415. assert(n % qk == 0);
  3416. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3417. assert((nrc == 2) || (nrc == 1));
  3418. #else
  3419. assert(nrc == 1);
  3420. #endif
  3421. UNUSED(nrc);
  3422. UNUSED(bx);
  3423. UNUSED(by);
  3424. UNUSED(bs);
  3425. const block_q4_1 * restrict x = vx;
  3426. const block_q8_1 * restrict y = vy;
  3427. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3428. if (nrc == 2) {
  3429. const block_q4_1 * restrict vx0 = vx;
  3430. const block_q4_1 * restrict vx1 = vx + bx;
  3431. const block_q8_1 * restrict vy0 = vy;
  3432. const block_q8_1 * restrict vy1 = vy + by;
  3433. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3434. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3435. for (int i = 0; i < nb; i++) {
  3436. const block_q4_1 * restrict b_x0 = &vx0[i];
  3437. const block_q4_1 * restrict b_x1 = &vx1[i];
  3438. const block_q8_1 * restrict b_y0 = &vy0[i];
  3439. const block_q8_1 * restrict b_y1 = &vy1[i];
  3440. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3441. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3442. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3443. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3444. summs0 += summs_t;
  3445. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3446. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3447. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3448. // 4-bit -> 8-bit
  3449. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3450. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3451. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3452. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3453. // load y
  3454. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3455. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3456. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3457. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3458. // mmla into int32x4_t
  3459. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3460. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3461. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3462. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3463. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3464. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3465. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3466. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3467. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3468. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3469. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3470. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3471. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3472. l1, r1)), l2, r2)), l3, r3))), scale);
  3473. }
  3474. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3475. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3476. sumv2 = sumv2 + summs0;
  3477. vst1_f32(s, vget_low_f32(sumv2));
  3478. vst1_f32(s + bs, vget_high_f32(sumv2));
  3479. return;
  3480. }
  3481. #endif
  3482. // TODO: add WASM SIMD
  3483. #if defined(__ARM_NEON)
  3484. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3485. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3486. float summs = 0;
  3487. assert(nb % 2 == 0); // TODO: handle odd nb
  3488. for (int i = 0; i < nb; i += 2) {
  3489. const block_q4_1 * restrict x0 = &x[i + 0];
  3490. const block_q4_1 * restrict x1 = &x[i + 1];
  3491. const block_q8_1 * restrict y0 = &y[i + 0];
  3492. const block_q8_1 * restrict y1 = &y[i + 1];
  3493. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s) + GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3494. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3495. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3496. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3497. // 4-bit -> 8-bit
  3498. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3499. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3500. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3501. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3502. // load y
  3503. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3504. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3505. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3506. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3507. // dot product into int32x4_t
  3508. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3509. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3510. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3511. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3512. }
  3513. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3514. #elif defined(__AVX2__) || defined(__AVX__)
  3515. // Initialize accumulator with zeros
  3516. __m256 acc = _mm256_setzero_ps();
  3517. float summs = 0;
  3518. // Main loop
  3519. for (int i = 0; i < nb; ++i) {
  3520. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3521. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3522. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3523. const __m256 d0v = _mm256_set1_ps( d0 );
  3524. const __m256 d1v = _mm256_set1_ps( d1 );
  3525. // Compute combined scales
  3526. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3527. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3528. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3529. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3530. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3531. // Accumulate d0*d1*x*y
  3532. #if defined(__AVX2__)
  3533. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3534. #else
  3535. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3536. #endif
  3537. }
  3538. *s = hsum_float_8(acc) + summs;
  3539. #elif defined(__riscv_v_intrinsic)
  3540. float sumf = 0.0;
  3541. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3542. for (int i = 0; i < nb; i++) {
  3543. // load elements
  3544. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3545. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3546. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3547. // mask and store lower part of x, and then upper part
  3548. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3549. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3550. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3551. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3552. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3553. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3554. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3555. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3556. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3557. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3558. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3559. }
  3560. *s = sumf;
  3561. #else
  3562. // scalar
  3563. float sumf = 0.0;
  3564. for (int i = 0; i < nb; i++) {
  3565. int sumi = 0;
  3566. for (int j = 0; j < qk/2; ++j) {
  3567. const int v0 = (x[i].qs[j] & 0x0F);
  3568. const int v1 = (x[i].qs[j] >> 4);
  3569. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3570. }
  3571. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3572. }
  3573. *s = sumf;
  3574. #endif
  3575. }
  3576. void ggml_vec_dot_q5_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3577. const int qk = QK8_0;
  3578. const int nb = n / qk;
  3579. assert(n % qk == 0);
  3580. assert(qk == QK5_0);
  3581. assert(nrc == 1);
  3582. UNUSED(nrc);
  3583. UNUSED(bx);
  3584. UNUSED(by);
  3585. UNUSED(bs);
  3586. const block_q5_0 * restrict x = vx;
  3587. const block_q8_0 * restrict y = vy;
  3588. #if defined(__ARM_NEON)
  3589. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3590. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3591. uint32_t qh0;
  3592. uint32_t qh1;
  3593. uint64_t tmp0[4];
  3594. uint64_t tmp1[4];
  3595. assert(nb % 2 == 0); // TODO: handle odd nb
  3596. for (int i = 0; i < nb; i += 2) {
  3597. const block_q5_0 * restrict x0 = &x[i];
  3598. const block_q5_0 * restrict x1 = &x[i + 1];
  3599. const block_q8_0 * restrict y0 = &y[i];
  3600. const block_q8_0 * restrict y1 = &y[i + 1];
  3601. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3602. // extract the 5th bit via lookup table ((!b) << 4)
  3603. memcpy(&qh0, x0->qh, sizeof(qh0));
  3604. memcpy(&qh1, x1->qh, sizeof(qh1));
  3605. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3606. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3607. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3608. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3609. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3610. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3611. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3612. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3613. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3614. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3615. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3616. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3617. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3618. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3619. // 4-bit -> 8-bit
  3620. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3621. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3622. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3623. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3624. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3625. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3626. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3627. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3628. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3629. // load y
  3630. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3631. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3632. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3633. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3634. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3635. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3636. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3637. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3638. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3639. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3640. }
  3641. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3642. #elif defined(__wasm_simd128__)
  3643. v128_t sumv = wasm_f32x4_splat(0.0f);
  3644. uint32_t qh;
  3645. uint64_t tmp[4];
  3646. // TODO: check if unrolling this is better
  3647. for (int i = 0; i < nb; ++i) {
  3648. const block_q5_0 * restrict x0 = &x[i];
  3649. const block_q8_0 * restrict y0 = &y[i];
  3650. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3651. // extract the 5th bit
  3652. memcpy(&qh, x0->qh, sizeof(qh));
  3653. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3654. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3655. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3656. tmp[3] = table_b2b_1[(qh >> 24) ];
  3657. const v128_t qhl = wasm_v128_load(tmp + 0);
  3658. const v128_t qhh = wasm_v128_load(tmp + 2);
  3659. const v128_t v0 = wasm_v128_load(x0->qs);
  3660. // 4-bit -> 8-bit
  3661. const v128_t v0l = wasm_v128_and (v0, m4b);
  3662. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3663. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3664. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3665. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3666. // load y
  3667. const v128_t v1l = wasm_v128_load(y0->qs);
  3668. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3669. // int8x16 -> int16x8
  3670. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3671. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3672. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3673. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3674. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3675. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3676. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3677. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3678. // dot product
  3679. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3680. wasm_i32x4_add(
  3681. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3682. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3683. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3684. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3685. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3686. }
  3687. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3688. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3689. #elif defined(__AVX2__)
  3690. // Initialize accumulator with zeros
  3691. __m256 acc = _mm256_setzero_ps();
  3692. // Main loop
  3693. for (int i = 0; i < nb; i++) {
  3694. /* Compute combined scale for the block */
  3695. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3696. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3697. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3698. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3699. qx = _mm256_or_si256(qx, bxhi);
  3700. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3701. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3702. /* Multiply q with scale and accumulate */
  3703. acc = _mm256_fmadd_ps(d, q, acc);
  3704. }
  3705. *s = hsum_float_8(acc);
  3706. #elif defined(__AVX__)
  3707. // Initialize accumulator with zeros
  3708. __m256 acc = _mm256_setzero_ps();
  3709. __m128i mask = _mm_set1_epi8((char)0xF0);
  3710. // Main loop
  3711. for (int i = 0; i < nb; i++) {
  3712. /* Compute combined scale for the block */
  3713. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3714. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3715. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3716. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3717. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3718. bxhil = _mm_andnot_si128(bxhil, mask);
  3719. bxhih = _mm_andnot_si128(bxhih, mask);
  3720. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3721. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3722. bxl = _mm_or_si128(bxl, bxhil);
  3723. bxh = _mm_or_si128(bxh, bxhih);
  3724. bx_0 = MM256_SET_M128I(bxh, bxl);
  3725. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3726. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3727. /* Multiply q with scale and accumulate */
  3728. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3729. }
  3730. *s = hsum_float_8(acc);
  3731. #elif defined(__riscv_v_intrinsic)
  3732. float sumf = 0.0;
  3733. uint32_t qh;
  3734. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3735. // These temporary registers are for masking and shift operations
  3736. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3737. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3738. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3739. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3740. for (int i = 0; i < nb; i++) {
  3741. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3742. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3743. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3744. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3745. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3746. // ((qh & (1u << (j + 16))) >> (j + 12));
  3747. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3748. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3749. // narrowing
  3750. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3751. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3752. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3753. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3754. // load
  3755. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3756. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3757. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3758. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3759. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3760. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3761. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3762. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3763. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3764. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3765. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3766. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3767. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3768. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3769. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3770. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3771. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3772. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3773. }
  3774. *s = sumf;
  3775. #else
  3776. // scalar
  3777. float sumf = 0.0;
  3778. for (int i = 0; i < nb; i++) {
  3779. uint32_t qh;
  3780. memcpy(&qh, x[i].qh, sizeof(qh));
  3781. int sumi = 0;
  3782. for (int j = 0; j < qk/2; ++j) {
  3783. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3784. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3785. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3786. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3787. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3788. }
  3789. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3790. }
  3791. *s = sumf;
  3792. #endif
  3793. }
  3794. void ggml_vec_dot_q5_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3795. const int qk = QK8_1;
  3796. const int nb = n / qk;
  3797. assert(n % qk == 0);
  3798. assert(qk == QK5_1);
  3799. assert(nrc == 1);
  3800. UNUSED(nrc);
  3801. UNUSED(bx);
  3802. UNUSED(by);
  3803. UNUSED(bs);
  3804. const block_q5_1 * restrict x = vx;
  3805. const block_q8_1 * restrict y = vy;
  3806. #if defined(__ARM_NEON)
  3807. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3808. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3809. float summs0 = 0.0f;
  3810. float summs1 = 0.0f;
  3811. uint32_t qh0;
  3812. uint32_t qh1;
  3813. uint64_t tmp0[4];
  3814. uint64_t tmp1[4];
  3815. assert(nb % 2 == 0); // TODO: handle odd nb
  3816. for (int i = 0; i < nb; i += 2) {
  3817. const block_q5_1 * restrict x0 = &x[i];
  3818. const block_q5_1 * restrict x1 = &x[i + 1];
  3819. const block_q8_1 * restrict y0 = &y[i];
  3820. const block_q8_1 * restrict y1 = &y[i + 1];
  3821. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3822. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3823. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3824. // extract the 5th bit via lookup table ((b) << 4)
  3825. memcpy(&qh0, x0->qh, sizeof(qh0));
  3826. memcpy(&qh1, x1->qh, sizeof(qh1));
  3827. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3828. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3829. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3830. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3831. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3832. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3833. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3834. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3835. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3836. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3837. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3838. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3839. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3840. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3841. // 4-bit -> 8-bit
  3842. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3843. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3844. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3845. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3846. // add high bit
  3847. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3848. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3849. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3850. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3851. // load y
  3852. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3853. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3854. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3855. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3856. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3857. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3858. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3859. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3860. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3861. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3862. }
  3863. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3864. #elif defined(__wasm_simd128__)
  3865. v128_t sumv = wasm_f32x4_splat(0.0f);
  3866. float summs = 0.0f;
  3867. uint32_t qh;
  3868. uint64_t tmp[4];
  3869. // TODO: check if unrolling this is better
  3870. for (int i = 0; i < nb; ++i) {
  3871. const block_q5_1 * restrict x0 = &x[i];
  3872. const block_q8_1 * restrict y0 = &y[i];
  3873. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3874. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3875. // extract the 5th bit
  3876. memcpy(&qh, x0->qh, sizeof(qh));
  3877. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3878. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3879. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3880. tmp[3] = table_b2b_0[(qh >> 24) ];
  3881. const v128_t qhl = wasm_v128_load(tmp + 0);
  3882. const v128_t qhh = wasm_v128_load(tmp + 2);
  3883. const v128_t v0 = wasm_v128_load(x0->qs);
  3884. // 4-bit -> 8-bit
  3885. const v128_t v0l = wasm_v128_and (v0, m4b);
  3886. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3887. // add high bit
  3888. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3889. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3890. // load y
  3891. const v128_t v1l = wasm_v128_load(y0->qs);
  3892. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3893. // int8x16 -> int16x8
  3894. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3895. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3896. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3897. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3898. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3899. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3900. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3901. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3902. // dot product
  3903. sumv = wasm_f32x4_add(sumv,
  3904. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3905. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3906. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3907. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3908. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3909. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3910. }
  3911. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3912. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3913. #elif defined(__AVX2__)
  3914. // Initialize accumulator with zeros
  3915. __m256 acc = _mm256_setzero_ps();
  3916. float summs = 0.0f;
  3917. // Main loop
  3918. for (int i = 0; i < nb; i++) {
  3919. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3920. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3921. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3922. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3923. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3924. qx = _mm256_or_si256(qx, bxhi);
  3925. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3926. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3927. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3928. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3929. }
  3930. *s = hsum_float_8(acc) + summs;
  3931. #elif defined(__AVX__)
  3932. // Initialize accumulator with zeros
  3933. __m256 acc = _mm256_setzero_ps();
  3934. __m128i mask = _mm_set1_epi8(0x10);
  3935. float summs = 0.0f;
  3936. // Main loop
  3937. for (int i = 0; i < nb; i++) {
  3938. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3939. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3940. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3941. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3942. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3943. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3944. bxhil = _mm_and_si128(bxhil, mask);
  3945. bxhih = _mm_and_si128(bxhih, mask);
  3946. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3947. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3948. bxl = _mm_or_si128(bxl, bxhil);
  3949. bxh = _mm_or_si128(bxh, bxhih);
  3950. bx_0 = MM256_SET_M128I(bxh, bxl);
  3951. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3952. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3953. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3954. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3955. }
  3956. *s = hsum_float_8(acc) + summs;
  3957. #elif defined(__riscv_v_intrinsic)
  3958. float sumf = 0.0;
  3959. uint32_t qh;
  3960. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3961. // temporary registers for shift operations
  3962. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3963. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3964. for (int i = 0; i < nb; i++) {
  3965. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3966. // load qh
  3967. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3968. // ((qh >> (j + 0)) << 4) & 0x10;
  3969. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3970. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3971. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3972. // ((qh >> (j + 12)) ) & 0x10;
  3973. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3974. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3975. // narrowing
  3976. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3977. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3978. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3979. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3980. // load
  3981. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3982. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3983. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3984. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3985. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3986. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3987. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3988. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3989. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3990. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3991. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3992. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3993. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3994. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3995. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3996. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3997. }
  3998. *s = sumf;
  3999. #else
  4000. // scalar
  4001. float sumf = 0.0;
  4002. for (int i = 0; i < nb; i++) {
  4003. uint32_t qh;
  4004. memcpy(&qh, x[i].qh, sizeof(qh));
  4005. int sumi = 0;
  4006. for (int j = 0; j < qk/2; ++j) {
  4007. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  4008. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  4009. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  4010. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  4011. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  4012. }
  4013. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  4014. }
  4015. *s = sumf;
  4016. #endif
  4017. }
  4018. void ggml_vec_dot_q8_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4019. const int qk = QK8_0;
  4020. const int nb = n / qk;
  4021. assert(n % qk == 0);
  4022. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4023. assert((nrc == 2) || (nrc == 1));
  4024. #else
  4025. assert(nrc == 1);
  4026. #endif
  4027. UNUSED(nrc);
  4028. UNUSED(bx);
  4029. UNUSED(by);
  4030. UNUSED(bs);
  4031. const block_q8_0 * restrict x = vx;
  4032. const block_q8_0 * restrict y = vy;
  4033. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4034. if (nrc == 2) {
  4035. const block_q8_0 * restrict vx0 = vx;
  4036. const block_q8_0 * restrict vx1 = vx + bx;
  4037. const block_q8_0 * restrict vy0 = vy;
  4038. const block_q8_0 * restrict vy1 = vy + by;
  4039. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4040. for (int i = 0; i < nb; i++) {
  4041. const block_q8_0 * restrict b_x0 = &vx0[i];
  4042. const block_q8_0 * restrict b_y0 = &vy0[i];
  4043. const block_q8_0 * restrict b_x1 = &vx1[i];
  4044. const block_q8_0 * restrict b_y1 = &vy1[i];
  4045. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  4046. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  4047. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  4048. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  4049. // load y
  4050. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  4051. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  4052. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  4053. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  4054. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  4055. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  4056. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  4057. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  4058. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4059. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4060. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4061. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4062. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4063. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4064. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4065. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4066. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  4067. l1, r1)), l2, r2)), l3, r3))), scale);
  4068. }
  4069. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  4070. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  4071. vst1_f32(s, vget_low_f32(sumv2));
  4072. vst1_f32(s + bs, vget_high_f32(sumv2));
  4073. return;
  4074. }
  4075. #endif
  4076. #if defined(__ARM_NEON)
  4077. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4078. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4079. assert(nb % 2 == 0); // TODO: handle odd nb
  4080. for (int i = 0; i < nb; i += 2) {
  4081. const block_q8_0 * restrict x0 = &x[i + 0];
  4082. const block_q8_0 * restrict x1 = &x[i + 1];
  4083. const block_q8_0 * restrict y0 = &y[i + 0];
  4084. const block_q8_0 * restrict y1 = &y[i + 1];
  4085. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  4086. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  4087. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  4088. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  4089. // load y
  4090. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  4091. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  4092. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  4093. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  4094. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4095. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  4096. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4097. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4098. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  4099. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4100. }
  4101. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  4102. #elif defined(__AVX2__) || defined(__AVX__)
  4103. // Initialize accumulator with zeros
  4104. __m256 acc = _mm256_setzero_ps();
  4105. // Main loop
  4106. for (int i = 0; i < nb; ++i) {
  4107. // Compute combined scale for the block
  4108. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4109. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  4110. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4111. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4112. // Multiply q with scale and accumulate
  4113. #if defined(__AVX2__)
  4114. acc = _mm256_fmadd_ps( d, q, acc );
  4115. #else
  4116. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  4117. #endif
  4118. }
  4119. *s = hsum_float_8(acc);
  4120. #elif defined(__riscv_v_intrinsic)
  4121. float sumf = 0.0;
  4122. size_t vl = __riscv_vsetvl_e8m1(qk);
  4123. for (int i = 0; i < nb; i++) {
  4124. // load elements
  4125. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  4126. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  4127. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  4128. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4129. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  4130. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  4131. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4132. }
  4133. *s = sumf;
  4134. #else
  4135. // scalar
  4136. float sumf = 0.0;
  4137. for (int i = 0; i < nb; i++) {
  4138. int sumi = 0;
  4139. for (int j = 0; j < qk; j++) {
  4140. sumi += x[i].qs[j]*y[i].qs[j];
  4141. }
  4142. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4143. }
  4144. *s = sumf;
  4145. #endif
  4146. }
  4147. #if QK_K == 256
  4148. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4149. assert(nrc == 1);
  4150. UNUSED(nrc);
  4151. UNUSED(bx);
  4152. UNUSED(by);
  4153. UNUSED(bs);
  4154. const block_q2_K * restrict x = vx;
  4155. const block_q8_K * restrict y = vy;
  4156. const int nb = n / QK_K;
  4157. #ifdef __ARM_NEON
  4158. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4159. const uint8x16_t m4 = vdupq_n_u8(0xF);
  4160. const int32x4_t vzero = vdupq_n_s32(0);
  4161. ggml_int8x16x2_t q2bytes;
  4162. uint8_t aux[16];
  4163. float sum = 0;
  4164. for (int i = 0; i < nb; ++i) {
  4165. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4166. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4167. const uint8_t * restrict q2 = x[i].qs;
  4168. const int8_t * restrict q8 = y[i].qs;
  4169. const uint8_t * restrict sc = x[i].scales;
  4170. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4171. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4172. vst1q_u8(aux, scales);
  4173. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4174. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4175. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4176. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4177. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4178. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4179. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4180. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4181. int isum = 0;
  4182. int is = 0;
  4183. // We use this macro instead of a function call because for some reason
  4184. // the code runs 2-3% slower, even if the function is declared inline
  4185. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4186. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4187. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4188. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4189. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4190. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4191. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4192. MULTIPLY_ACCUM_WITH_SCALE((index));
  4193. for (int j = 0; j < QK_K/128; ++j) {
  4194. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4195. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4196. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4197. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4198. MULTIPLY_ACCUM_WITH_SCALE(0);
  4199. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4200. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4201. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4202. is += 8;
  4203. }
  4204. sum += d * isum;
  4205. }
  4206. *s = sum;
  4207. #elif defined __AVX2__
  4208. const __m256i m3 = _mm256_set1_epi8(3);
  4209. const __m128i m4 = _mm_set1_epi8(0xF);
  4210. __m256 acc = _mm256_setzero_ps();
  4211. for (int i = 0; i < nb; ++i) {
  4212. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4213. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4214. const uint8_t * restrict q2 = x[i].qs;
  4215. const int8_t * restrict q8 = y[i].qs;
  4216. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4217. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4218. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4219. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4220. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4221. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4222. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4223. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4224. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4225. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4226. __m256i sumi = _mm256_setzero_si256();
  4227. for (int j = 0; j < QK_K/128; ++j) {
  4228. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4229. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4230. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4231. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4232. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4233. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4234. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4235. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4236. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4237. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4238. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4239. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4240. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4241. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4242. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4243. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4244. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4245. p0 = _mm256_add_epi32(p0, p1);
  4246. p2 = _mm256_add_epi32(p2, p3);
  4247. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4248. }
  4249. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4250. }
  4251. *s = hsum_float_8(acc);
  4252. #elif defined __AVX__
  4253. const __m128i m3 = _mm_set1_epi8(0x3);
  4254. const __m128i m4 = _mm_set1_epi8(0xF);
  4255. const __m128i m2 = _mm_set1_epi8(0x2);
  4256. __m256 acc = _mm256_setzero_ps();
  4257. for (int i = 0; i < nb; ++i) {
  4258. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4259. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4260. const uint8_t * restrict q2 = x[i].qs;
  4261. const int8_t * restrict q8 = y[i].qs;
  4262. // load mins and scales from block_q2_K.scales[QK_K/16]
  4263. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4264. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4265. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4266. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4267. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4268. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4269. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4270. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4271. // sumf += -dmin * summs in 32bits*8
  4272. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4273. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4274. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4275. const __m128i scales[2] = { scales_0, scales_1 };
  4276. __m128i sumi_0 = _mm_setzero_si128();
  4277. __m128i sumi_1 = _mm_setzero_si128();
  4278. for (int j = 0; j < QK_K/128; ++j) {
  4279. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4280. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4281. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4282. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4283. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4284. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4285. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4286. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4287. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4288. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4289. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4290. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4291. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4292. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4293. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4294. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4295. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4296. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4297. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4298. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4299. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4300. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4301. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4302. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4303. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4304. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4305. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4306. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4307. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4308. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4309. __m128i shuffle = _mm_set1_epi16(0x0100);
  4310. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4311. shuffle = _mm_add_epi16(shuffle, m2);
  4312. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4313. shuffle = _mm_add_epi16(shuffle, m2);
  4314. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4315. shuffle = _mm_add_epi16(shuffle, m2);
  4316. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4317. shuffle = _mm_add_epi16(shuffle, m2);
  4318. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4319. shuffle = _mm_add_epi16(shuffle, m2);
  4320. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4321. shuffle = _mm_add_epi16(shuffle, m2);
  4322. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4323. shuffle = _mm_add_epi16(shuffle, m2);
  4324. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4325. p0 = _mm_add_epi32(p0, p1);
  4326. p2 = _mm_add_epi32(p2, p3);
  4327. p4 = _mm_add_epi32(p4, p5);
  4328. p6 = _mm_add_epi32(p6, p7);
  4329. // isum in 32bits*4*2
  4330. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4331. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4332. }
  4333. // sumf += dall * isum - dmin * summs in 32bits
  4334. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4335. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4336. }
  4337. *s = hsum_float_8(acc);
  4338. #elif defined __riscv_v_intrinsic
  4339. float sumf = 0;
  4340. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4341. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4342. for (int i = 0; i < nb; ++i) {
  4343. const uint8_t * q2 = x[i].qs;
  4344. const int8_t * q8 = y[i].qs;
  4345. const uint8_t * sc = x[i].scales;
  4346. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4347. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4348. size_t vl = 16;
  4349. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4350. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4351. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4352. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4353. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4354. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4355. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4356. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4357. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4358. vl = 32;
  4359. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4360. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4361. uint8_t is=0;
  4362. int isum=0;
  4363. for (int j = 0; j < QK_K/128; ++j) {
  4364. // load Q2
  4365. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4366. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4367. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4368. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4369. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4370. // duplicate scale elements for product
  4371. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4372. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4373. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4374. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4375. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4376. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4377. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4378. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4379. // load Q8
  4380. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4381. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4382. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4383. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4384. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4385. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4386. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4387. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4388. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4389. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4390. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4391. q2+=32; q8+=128; is=8;
  4392. }
  4393. sumf += dall * isum;
  4394. }
  4395. *s = sumf;
  4396. #else
  4397. float sumf = 0;
  4398. for (int i = 0; i < nb; ++i) {
  4399. const uint8_t * q2 = x[i].qs;
  4400. const int8_t * q8 = y[i].qs;
  4401. const uint8_t * sc = x[i].scales;
  4402. int summs = 0;
  4403. for (int j = 0; j < 16; ++j) {
  4404. summs += y[i].bsums[j] * (sc[j] >> 4);
  4405. }
  4406. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4407. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4408. int isum = 0;
  4409. int is = 0;
  4410. int d;
  4411. for (int k = 0; k < QK_K/128; ++k) {
  4412. int shift = 0;
  4413. for (int j = 0; j < 4; ++j) {
  4414. d = sc[is++] & 0xF;
  4415. int isuml = 0;
  4416. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4417. isum += d * isuml;
  4418. d = sc[is++] & 0xF;
  4419. isuml = 0;
  4420. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4421. isum += d * isuml;
  4422. shift += 2;
  4423. q8 += 32;
  4424. }
  4425. q2 += 32;
  4426. }
  4427. sumf += dall * isum - dmin * summs;
  4428. }
  4429. *s = sumf;
  4430. #endif
  4431. }
  4432. #else
  4433. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4434. assert(nrc == 1);
  4435. UNUSED(nrc);
  4436. UNUSED(bx);
  4437. UNUSED(by);
  4438. UNUSED(bs);
  4439. const block_q2_K * restrict x = vx;
  4440. const block_q8_K * restrict y = vy;
  4441. const int nb = n / QK_K;
  4442. #ifdef __ARM_NEON
  4443. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4444. const int32x4_t vzero = vdupq_n_s32(0);
  4445. ggml_int8x16x4_t q2bytes;
  4446. uint32_t aux32[2];
  4447. const uint8_t * scales = (const uint8_t *)aux32;
  4448. float sum = 0;
  4449. for (int i = 0; i < nb; ++i) {
  4450. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4451. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4452. const uint8_t * restrict q2 = x[i].qs;
  4453. const int8_t * restrict q8 = y[i].qs;
  4454. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4455. aux32[0] = sc[0] & 0x0f0f0f0f;
  4456. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4457. 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]);
  4458. int isum1 = 0, isum2 = 0;
  4459. const uint8x16_t q2bits = vld1q_u8(q2);
  4460. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4461. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4462. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4463. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4464. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4465. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4466. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4467. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4468. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4469. sum += d * (isum1 + isum2);
  4470. }
  4471. *s = sum;
  4472. #elif defined __AVX2__
  4473. const __m256i m3 = _mm256_set1_epi8(3);
  4474. __m256 acc = _mm256_setzero_ps();
  4475. uint32_t ud, um;
  4476. const uint8_t * restrict db = (const uint8_t *)&ud;
  4477. const uint8_t * restrict mb = (const uint8_t *)&um;
  4478. float summs = 0;
  4479. // TODO: optimize this
  4480. for (int i = 0; i < nb; ++i) {
  4481. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4482. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4483. const uint8_t * restrict q2 = x[i].qs;
  4484. const int8_t * restrict q8 = y[i].qs;
  4485. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4486. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4487. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4488. 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];
  4489. summs += dmin * smin;
  4490. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4491. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4492. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4493. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4494. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4495. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4496. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4497. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4498. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4499. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4500. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4501. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4502. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4503. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4504. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4505. }
  4506. *s = hsum_float_8(acc) + summs;
  4507. #elif defined __AVX__
  4508. const __m128i m3 = _mm_set1_epi8(3);
  4509. __m256 acc = _mm256_setzero_ps();
  4510. uint32_t ud, um;
  4511. const uint8_t * restrict db = (const uint8_t *)&ud;
  4512. const uint8_t * restrict mb = (const uint8_t *)&um;
  4513. float summs = 0;
  4514. // TODO: optimize this
  4515. for (int i = 0; i < nb; ++i) {
  4516. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4517. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4518. const uint8_t * restrict q2 = x[i].qs;
  4519. const int8_t * restrict q8 = y[i].qs;
  4520. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4521. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4522. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4523. 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];
  4524. summs += dmin * smin;
  4525. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4526. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4527. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4528. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4529. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4530. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4531. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4532. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4533. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4534. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4535. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4536. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4537. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4538. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4539. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4540. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4541. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4542. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4543. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4544. }
  4545. *s = hsum_float_8(acc) + summs;
  4546. #elif defined __riscv_v_intrinsic
  4547. uint32_t aux32[2];
  4548. const uint8_t * scales = (const uint8_t *)aux32;
  4549. float sumf = 0;
  4550. for (int i = 0; i < nb; ++i) {
  4551. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4552. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4553. const uint8_t * restrict q2 = x[i].qs;
  4554. const int8_t * restrict q8 = y[i].qs;
  4555. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4556. aux32[0] = sc[0] & 0x0f0f0f0f;
  4557. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4558. 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]);
  4559. int isum1 = 0;
  4560. int isum2 = 0;
  4561. size_t vl = 16;
  4562. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4563. // load Q2
  4564. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4565. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4566. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4567. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4568. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4569. // load Q8, and take product with Q2
  4570. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4571. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4572. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4573. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4574. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4575. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4576. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4577. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4578. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4579. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4580. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4581. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4582. sumf += d * (isum1 + isum2);
  4583. }
  4584. *s = sumf;
  4585. #else
  4586. float sumf = 0;
  4587. int isum[QK_K/16];
  4588. for (int i = 0; i < nb; ++i) {
  4589. const uint8_t * q2 = x[i].qs;
  4590. const int8_t * q8 = y[i].qs;
  4591. const uint8_t * sc = x[i].scales;
  4592. int summs = 0;
  4593. for (int j = 0; j < QK_K/16; ++j) {
  4594. summs += y[i].bsums[j] * (sc[j] >> 4);
  4595. }
  4596. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4597. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4598. memset(isum, 0, (QK_K/16)*sizeof(int));
  4599. for (int l = 0; l < 16; ++l) {
  4600. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4601. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4602. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4603. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4604. }
  4605. for (int l = 0; l < QK_K/16; ++l) {
  4606. isum[l] *= (sc[l] & 0xF);
  4607. }
  4608. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4609. }
  4610. *s = sumf;
  4611. #endif
  4612. }
  4613. #endif
  4614. #if QK_K == 256
  4615. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4616. assert(n % QK_K == 0);
  4617. assert(nrc == 1);
  4618. UNUSED(nrc);
  4619. UNUSED(bx);
  4620. UNUSED(by);
  4621. UNUSED(bs);
  4622. const uint32_t kmask1 = 0x03030303;
  4623. const uint32_t kmask2 = 0x0f0f0f0f;
  4624. const block_q3_K * restrict x = vx;
  4625. const block_q8_K * restrict y = vy;
  4626. const int nb = n / QK_K;
  4627. #ifdef __ARM_NEON
  4628. uint32_t aux[3];
  4629. uint32_t utmp[4];
  4630. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4631. const int32x4_t vzero = vdupq_n_s32(0);
  4632. const uint8x16_t m0 = vdupq_n_u8(1);
  4633. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4634. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4635. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4636. const int8_t m32 = 32;
  4637. ggml_int8x16x4_t q3bytes;
  4638. float sum = 0;
  4639. for (int i = 0; i < nb; ++i) {
  4640. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4641. const uint8_t * restrict q3 = x[i].qs;
  4642. const uint8_t * restrict qh = x[i].hmask;
  4643. const int8_t * restrict q8 = y[i].qs;
  4644. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4645. ggml_uint8x16x4_t q3h;
  4646. int32_t isum = 0;
  4647. // Set up scales
  4648. memcpy(aux, x[i].scales, 12);
  4649. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4650. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4651. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4652. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4653. int8_t * scale = (int8_t *)utmp;
  4654. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4655. for (int j = 0; j < QK_K/128; ++j) {
  4656. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4657. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4658. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4659. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4660. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4661. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4662. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4663. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4664. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4665. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4666. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4667. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4668. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4669. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4670. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4671. scale += 4;
  4672. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4673. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4674. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4675. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4676. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4677. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4678. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4679. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4680. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4681. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4682. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4683. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4684. scale += 4;
  4685. if (j == 0) {
  4686. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4687. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4688. }
  4689. }
  4690. sum += d * isum;
  4691. }
  4692. *s = sum;
  4693. #elif defined __AVX2__
  4694. const __m256i m3 = _mm256_set1_epi8(3);
  4695. const __m256i mone = _mm256_set1_epi8(1);
  4696. const __m128i m32 = _mm_set1_epi8(32);
  4697. __m256 acc = _mm256_setzero_ps();
  4698. uint32_t aux[3];
  4699. for (int i = 0; i < nb; ++i) {
  4700. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4701. const uint8_t * restrict q3 = x[i].qs;
  4702. const int8_t * restrict q8 = y[i].qs;
  4703. // Set up scales
  4704. memcpy(aux, x[i].scales, 12);
  4705. __m128i scales128 = _mm_set_epi32(
  4706. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4707. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4708. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4709. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4710. scales128 = _mm_sub_epi8(scales128, m32);
  4711. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4712. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4713. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4714. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4715. // high bit
  4716. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4717. // integer accumulator
  4718. __m256i sumi = _mm256_setzero_si256();
  4719. int bit = 0;
  4720. int is = 0;
  4721. for (int j = 0; j < QK_K/128; ++j) {
  4722. // load low 2 bits
  4723. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4724. // prepare low and high bits
  4725. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4726. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4727. ++bit;
  4728. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4729. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4730. ++bit;
  4731. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4732. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4733. ++bit;
  4734. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4735. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4736. ++bit;
  4737. // load Q8 quants
  4738. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4739. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4740. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4741. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4742. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4743. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4744. // and 2 if the high bit was set)
  4745. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4746. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4747. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4748. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4749. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4750. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4751. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4752. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4753. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4754. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4755. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4756. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4757. // multiply with scales
  4758. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4759. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4760. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4761. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4762. // accumulate
  4763. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4764. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4765. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4766. }
  4767. // multiply with block scale and accumulate
  4768. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4769. }
  4770. *s = hsum_float_8(acc);
  4771. #elif defined __AVX__
  4772. const __m128i m3 = _mm_set1_epi8(3);
  4773. const __m128i mone = _mm_set1_epi8(1);
  4774. const __m128i m32 = _mm_set1_epi8(32);
  4775. const __m128i m2 = _mm_set1_epi8(2);
  4776. __m256 acc = _mm256_setzero_ps();
  4777. const uint32_t *aux;
  4778. for (int i = 0; i < nb; ++i) {
  4779. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4780. const uint8_t * restrict q3 = x[i].qs;
  4781. const int8_t * restrict q8 = y[i].qs;
  4782. // Set up scales
  4783. aux = (const uint32_t *)x[i].scales;
  4784. __m128i scales128 = _mm_set_epi32(
  4785. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4786. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4787. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4788. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4789. scales128 = _mm_sub_epi8(scales128, m32);
  4790. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4791. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4792. const __m128i scales[2] = { scales_0, scales_1 };
  4793. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4794. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4795. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4796. // integer accumulator
  4797. __m128i sumi_0 = _mm_setzero_si128();
  4798. __m128i sumi_1 = _mm_setzero_si128();
  4799. for (int j = 0; j < QK_K/128; ++j) {
  4800. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4801. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4802. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4803. // prepare low and high bits
  4804. const int bit = j << 2;
  4805. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4806. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4807. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4808. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4809. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4810. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4811. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4812. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4813. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4814. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4815. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4816. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4817. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4818. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4819. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4820. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4821. // load Q8 quants from block_q8_K.qs[QK_K]
  4822. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4823. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4824. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4825. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4826. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4827. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4828. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4829. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4830. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4831. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4832. // and 2 if the high bit was set)
  4833. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4834. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4835. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4836. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4837. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4838. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4839. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4840. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4841. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4842. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4843. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4844. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4845. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4846. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4847. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4848. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4849. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4850. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4851. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4852. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4853. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4854. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4855. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4856. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4857. // multiply with scales
  4858. __m128i shuffle = _mm_set1_epi16(0x0100);
  4859. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4860. shuffle = _mm_add_epi16(shuffle, m2);
  4861. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4862. shuffle = _mm_add_epi16(shuffle, m2);
  4863. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4864. shuffle = _mm_add_epi16(shuffle, m2);
  4865. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4866. shuffle = _mm_add_epi16(shuffle, m2);
  4867. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4868. shuffle = _mm_add_epi16(shuffle, m2);
  4869. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4870. shuffle = _mm_add_epi16(shuffle, m2);
  4871. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4872. shuffle = _mm_add_epi16(shuffle, m2);
  4873. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4874. // accumulate
  4875. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4876. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4877. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4878. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4879. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4880. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4881. }
  4882. // multiply with block scale and accumulate
  4883. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4884. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4885. }
  4886. *s = hsum_float_8(acc);
  4887. #elif defined __riscv_v_intrinsic
  4888. uint32_t aux[3];
  4889. uint32_t utmp[4];
  4890. float sumf = 0;
  4891. for (int i = 0; i < nb; ++i) {
  4892. const uint8_t * restrict q3 = x[i].qs;
  4893. const uint8_t * restrict qh = x[i].hmask;
  4894. const int8_t * restrict q8 = y[i].qs;
  4895. memcpy(aux, x[i].scales, 12);
  4896. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4897. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4898. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4899. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4900. int8_t * scale = (int8_t *)utmp;
  4901. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4902. size_t vl = 32;
  4903. uint8_t m = 1;
  4904. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4905. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4906. int sum_t = 0;
  4907. for (int j = 0; j < QK_K; j += 128) {
  4908. vl = 32;
  4909. // load Q3
  4910. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4911. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4912. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4913. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4914. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4915. // compute mask for subtraction
  4916. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4917. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4918. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4919. m <<= 1;
  4920. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4921. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4922. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4923. m <<= 1;
  4924. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4925. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4926. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4927. m <<= 1;
  4928. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4929. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4930. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4931. m <<= 1;
  4932. // load Q8 and take product with Q3
  4933. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4934. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4935. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4936. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4937. vl = 16;
  4938. // retrieve lane to multiply with scale
  4939. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4940. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4941. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4942. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4943. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4944. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4945. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4946. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4947. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4948. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4949. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4950. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4951. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4952. q3 += 32; q8 += 128; scale += 8;
  4953. }
  4954. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4955. sumf += d*sum_t;
  4956. }
  4957. *s = sumf;
  4958. #else
  4959. // scalar version
  4960. // This function is written like this so the compiler can manage to vectorize most of it
  4961. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4962. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4963. // The ideal situation would be if we could just write the code once, and the compiler would
  4964. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4965. // write vectorized versions for AVX, ARM_NEON, etc.
  4966. int8_t aux8[QK_K];
  4967. int16_t aux16[8];
  4968. float sums [8];
  4969. int32_t aux32[8];
  4970. memset(sums, 0, 8*sizeof(float));
  4971. uint32_t auxs[4];
  4972. const int8_t * scales = (const int8_t*)auxs;
  4973. float sumf = 0;
  4974. for (int i = 0; i < nb; ++i) {
  4975. const uint8_t * restrict q3 = x[i].qs;
  4976. const uint8_t * restrict hm = x[i].hmask;
  4977. const int8_t * restrict q8 = y[i].qs;
  4978. memset(aux32, 0, 8*sizeof(int32_t));
  4979. int8_t * restrict a = aux8;
  4980. uint8_t m = 1;
  4981. for (int j = 0; j < QK_K; j += 128) {
  4982. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4983. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4984. a += 32; m <<= 1;
  4985. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4986. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4987. a += 32; m <<= 1;
  4988. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4989. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4990. a += 32; m <<= 1;
  4991. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4992. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4993. a += 32; m <<= 1;
  4994. q3 += 32;
  4995. }
  4996. a = aux8;
  4997. memcpy(auxs, x[i].scales, 12);
  4998. uint32_t tmp = auxs[2];
  4999. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  5000. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  5001. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  5002. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  5003. for (int j = 0; j < QK_K/16; ++j) {
  5004. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5005. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  5006. q8 += 8; a += 8;
  5007. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5008. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  5009. q8 += 8; a += 8;
  5010. }
  5011. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5012. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5013. }
  5014. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5015. *s = sumf;
  5016. #endif
  5017. }
  5018. #else
  5019. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5020. assert(n % QK_K == 0);
  5021. assert(nrc == 1);
  5022. UNUSED(nrc);
  5023. UNUSED(bx);
  5024. UNUSED(by);
  5025. UNUSED(bs);
  5026. const block_q3_K * restrict x = vx;
  5027. const block_q8_K * restrict y = vy;
  5028. const int nb = n / QK_K;
  5029. #ifdef __ARM_NEON
  5030. const int32x4_t vzero = vdupq_n_s32(0);
  5031. const uint8x16_t m3b = vdupq_n_u8(0x3);
  5032. const uint8x16_t mh = vdupq_n_u8(4);
  5033. ggml_int8x16x4_t q3bytes;
  5034. uint16_t aux16[2];
  5035. int8_t * scales = (int8_t *)aux16;
  5036. float sum = 0;
  5037. for (int i = 0; i < nb; ++i) {
  5038. ggml_uint8x16x4_t q3h;
  5039. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  5040. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  5041. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  5042. const uint16_t a = *(const uint16_t *)x[i].scales;
  5043. aux16[0] = a & 0x0f0f;
  5044. aux16[1] = (a >> 4) & 0x0f0f;
  5045. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5046. 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]);
  5047. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5048. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  5049. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  5050. q3h.val[1] = vandq_u8(mh, htmp);
  5051. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  5052. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  5053. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  5054. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  5055. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  5056. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  5057. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  5058. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  5059. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  5060. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  5061. sum += d * isum;
  5062. }
  5063. *s = sum;
  5064. #elif defined __AVX2__
  5065. const __m256i m3 = _mm256_set1_epi8(3);
  5066. const __m256i m1 = _mm256_set1_epi8(1);
  5067. __m256 acc = _mm256_setzero_ps();
  5068. uint64_t aux64;
  5069. uint16_t aux16[2];
  5070. const int8_t * aux8 = (const int8_t *)aux16;
  5071. for (int i = 0; i < nb; ++i) {
  5072. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5073. const uint8_t * restrict q3 = x[i].qs;
  5074. const int8_t * restrict q8 = y[i].qs;
  5075. const uint16_t a = *(const uint16_t *)x[i].scales;
  5076. aux16[0] = a & 0x0f0f;
  5077. aux16[1] = (a >> 4) & 0x0f0f;
  5078. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  5079. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  5080. memcpy(&aux64, x[i].hmask, 8);
  5081. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5082. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  5083. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  5084. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  5085. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  5086. // load low 2 bits
  5087. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5088. // prepare low and high bits
  5089. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  5090. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  5091. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  5092. // load Q8 quants
  5093. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5094. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5095. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5096. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5097. // and 2 if the high bit was set)
  5098. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  5099. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  5100. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  5101. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  5102. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5103. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5104. // multiply with scales
  5105. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5106. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5107. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  5108. // multiply with block scale and accumulate
  5109. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  5110. }
  5111. *s = hsum_float_8(acc);
  5112. #elif defined __AVX__
  5113. const __m128i m3 = _mm_set1_epi8(3);
  5114. const __m128i m1 = _mm_set1_epi8(1);
  5115. __m256 acc = _mm256_setzero_ps();
  5116. uint64_t aux64;
  5117. uint16_t aux16[2];
  5118. const int8_t * aux8 = (const int8_t *)aux16;
  5119. for (int i = 0; i < nb; ++i) {
  5120. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5121. const uint8_t * restrict q3 = x[i].qs;
  5122. const int8_t * restrict q8 = y[i].qs;
  5123. const uint16_t a = *(const uint16_t *)x[i].scales;
  5124. aux16[0] = a & 0x0f0f;
  5125. aux16[1] = (a >> 4) & 0x0f0f;
  5126. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  5127. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  5128. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  5129. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  5130. memcpy(&aux64, x[i].hmask, 8);
  5131. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5132. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  5133. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  5134. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  5135. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  5136. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  5137. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  5138. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  5139. // load low 2 bits
  5140. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5141. // prepare low and high bits
  5142. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  5143. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  5144. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  5145. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  5146. // load Q8 quants
  5147. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5148. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5149. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  5150. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5151. // and 2 if the high bit was set)
  5152. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  5153. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  5154. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  5155. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  5156. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  5157. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  5158. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  5159. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  5160. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5161. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5162. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5163. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5164. // multiply with scales
  5165. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5166. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  5167. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  5168. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  5169. p16_0 = _mm_add_epi32(p16_0, p16_2);
  5170. p16_1 = _mm_add_epi32(p16_1, p16_3);
  5171. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  5172. // multiply with block scale and accumulate
  5173. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  5174. }
  5175. *s = hsum_float_8(acc);
  5176. #elif defined __riscv_v_intrinsic
  5177. uint16_t aux16[2];
  5178. int8_t * scales = (int8_t *)aux16;
  5179. float sumf = 0;
  5180. for (int i = 0; i < nb; ++i) {
  5181. const uint8_t * restrict q3 = x[i].qs;
  5182. const int8_t * restrict q8 = y[i].qs;
  5183. const uint16_t a = *(const uint16_t *)x[i].scales;
  5184. aux16[0] = a & 0x0f0f;
  5185. aux16[1] = (a >> 4) & 0x0f0f;
  5186. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5187. 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]);
  5188. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5189. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5190. // load qh
  5191. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5192. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5193. size_t vl = 16;
  5194. // extend and combine both qh_x1 and qh_x2
  5195. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5196. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5197. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5198. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5199. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5200. // load Q3
  5201. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5202. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5203. 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);
  5204. 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);
  5205. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5206. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5207. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5208. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5209. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5210. // load Q8 and take product with Q3
  5211. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5212. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5213. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5214. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5215. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5216. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5217. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5218. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5219. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5220. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5221. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5222. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5223. sumf += d * isum;
  5224. }
  5225. *s = sumf;
  5226. #else
  5227. int8_t aux8[QK_K];
  5228. int16_t aux16[8];
  5229. float sums [8];
  5230. int32_t aux32[8];
  5231. int32_t scales[4];
  5232. memset(sums, 0, 8*sizeof(float));
  5233. float sumf = 0;
  5234. for (int i = 0; i < nb; ++i) {
  5235. const uint8_t * restrict q3 = x[i].qs;
  5236. const uint8_t * restrict hm = x[i].hmask;
  5237. const int8_t * restrict q8 = y[i].qs;
  5238. int8_t * restrict a = aux8;
  5239. for (int l = 0; l < 8; ++l) {
  5240. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5241. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5242. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5243. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5244. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5245. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5246. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5247. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5248. }
  5249. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5250. scales[1] = (x[i].scales[0] >> 4) - 8;
  5251. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5252. scales[3] = (x[i].scales[1] >> 4) - 8;
  5253. memset(aux32, 0, 8*sizeof(int32_t));
  5254. for (int j = 0; j < QK_K/16; ++j) {
  5255. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5256. q8 += 8; a += 8;
  5257. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5258. q8 += 8; a += 8;
  5259. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5260. }
  5261. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5262. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5263. }
  5264. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5265. *s = sumf;
  5266. #endif
  5267. }
  5268. #endif
  5269. #if QK_K == 256
  5270. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5271. assert(n % QK_K == 0);
  5272. assert(nrc == 1);
  5273. UNUSED(nrc);
  5274. UNUSED(bx);
  5275. UNUSED(by);
  5276. UNUSED(bs);
  5277. const block_q4_K * restrict x = vx;
  5278. const block_q8_K * restrict y = vy;
  5279. const int nb = n / QK_K;
  5280. static const uint32_t kmask1 = 0x3f3f3f3f;
  5281. static const uint32_t kmask2 = 0x0f0f0f0f;
  5282. static const uint32_t kmask3 = 0x03030303;
  5283. uint32_t utmp[4];
  5284. #ifdef __ARM_NEON
  5285. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5286. const int32x4_t mzero = vdupq_n_s32(0);
  5287. ggml_int8x16x2_t q4bytes;
  5288. ggml_int8x16x2_t q8bytes;
  5289. float sumf = 0;
  5290. for (int i = 0; i < nb; ++i) {
  5291. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5292. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5293. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5294. memcpy(utmp, x[i].scales, 12);
  5295. uint32x2_t mins8 = { 0 };
  5296. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5297. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5298. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5299. utmp[0] &= kmask1;
  5300. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5301. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5302. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5303. sumf -= dmin * vaddvq_s32(prod);
  5304. const uint8_t * scales = (const uint8_t *)utmp;
  5305. const uint8_t * restrict q4 = x[i].qs;
  5306. const int8_t * restrict q8 = y[i].qs;
  5307. int32_t sumi1 = 0;
  5308. int32_t sumi2 = 0;
  5309. for (int j = 0; j < QK_K/64; ++j) {
  5310. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5311. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5312. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5313. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5314. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5315. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5316. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5317. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5318. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5319. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5320. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5321. }
  5322. sumf += d * (sumi1 + sumi2);
  5323. }
  5324. *s = sumf;
  5325. #elif defined __AVX2__
  5326. const __m256i m4 = _mm256_set1_epi8(0xF);
  5327. __m256 acc = _mm256_setzero_ps();
  5328. __m128 acc_m = _mm_setzero_ps();
  5329. for (int i = 0; i < nb; ++i) {
  5330. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5331. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5332. memcpy(utmp, x[i].scales, 12);
  5333. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5334. const uint32_t uaux = utmp[1] & kmask1;
  5335. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5336. utmp[2] = uaux;
  5337. utmp[0] &= kmask1;
  5338. const uint8_t * restrict q4 = x[i].qs;
  5339. const int8_t * restrict q8 = y[i].qs;
  5340. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5341. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5342. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5343. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5344. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5345. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5346. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5347. __m256i sumi = _mm256_setzero_si256();
  5348. for (int j = 0; j < QK_K/64; ++j) {
  5349. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5350. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5351. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5352. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5353. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5354. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5355. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5356. p16l = _mm256_madd_epi16(scale_l, p16l);
  5357. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5358. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5359. p16h = _mm256_madd_epi16(scale_h, p16h);
  5360. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5361. sumi = _mm256_add_epi32(sumi, sumj);
  5362. }
  5363. __m256 vd = _mm256_set1_ps(d);
  5364. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5365. }
  5366. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5367. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5368. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5369. #elif defined __AVX__
  5370. const __m128i m4 = _mm_set1_epi8(0xF);
  5371. const __m128i m2 = _mm_set1_epi8(0x2);
  5372. __m256 acc = _mm256_setzero_ps();
  5373. __m128 acc_m = _mm_setzero_ps();
  5374. for (int i = 0; i < nb; ++i) {
  5375. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5376. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5377. const uint8_t * restrict q4 = x[i].qs;
  5378. const int8_t * restrict q8 = y[i].qs;
  5379. memcpy(utmp, x[i].scales, 12);
  5380. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5381. const uint32_t uaux = utmp[1] & kmask1;
  5382. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5383. utmp[2] = uaux;
  5384. utmp[0] &= kmask1;
  5385. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5386. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5387. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5388. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5389. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5390. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5391. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5392. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5393. __m128i sumi_0 = _mm_setzero_si128();
  5394. __m128i sumi_1 = _mm_setzero_si128();
  5395. __m128i shuffle = _mm_set1_epi16(0x0100);
  5396. for (int j = 0; j < QK_K/64; ++j) {
  5397. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5398. shuffle = _mm_add_epi16(shuffle, m2);
  5399. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5400. shuffle = _mm_add_epi16(shuffle, m2);
  5401. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5402. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5403. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5404. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5405. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5406. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5407. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5408. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5409. p16l = _mm_madd_epi16(scale_l, p16l);
  5410. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5411. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5412. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5413. p16l = _mm_madd_epi16(scale_l, p16l);
  5414. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5415. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5416. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5417. p16h = _mm_madd_epi16(scale_h, p16h);
  5418. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5419. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5420. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5421. p16h = _mm_madd_epi16(scale_h, p16h);
  5422. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5423. }
  5424. __m256 vd = _mm256_set1_ps(d);
  5425. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5426. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5427. }
  5428. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5429. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5430. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5431. #elif defined __riscv_v_intrinsic
  5432. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5433. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5434. float sumf = 0;
  5435. for (int i = 0; i < nb; ++i) {
  5436. size_t vl = 8;
  5437. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5438. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5439. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5440. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5441. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5442. memcpy(utmp, x[i].scales, 12);
  5443. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5444. const uint32_t uaux = utmp[1] & kmask1;
  5445. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5446. utmp[2] = uaux;
  5447. utmp[0] &= kmask1;
  5448. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5449. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5450. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5451. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5452. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5453. const uint8_t * restrict q4 = x[i].qs;
  5454. const int8_t * restrict q8 = y[i].qs;
  5455. vl = 32;
  5456. int32_t sum_1 = 0;
  5457. int32_t sum_2 = 0;
  5458. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5459. for (int j = 0; j < QK_K/64; ++j) {
  5460. // load Q4
  5461. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5462. // load Q8 and multiply it with lower Q4 nibble
  5463. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5464. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5465. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5466. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5467. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5468. // load Q8 and multiply it with upper Q4 nibble
  5469. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5470. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5471. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5472. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5473. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5474. q4 += 32; q8 += 64;
  5475. }
  5476. sumf += d*(sum_1 + sum_2);
  5477. }
  5478. *s = sumf;
  5479. #else
  5480. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5481. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5482. int8_t aux8[QK_K];
  5483. int16_t aux16[8];
  5484. float sums [8];
  5485. int32_t aux32[8];
  5486. memset(sums, 0, 8*sizeof(float));
  5487. float sumf = 0;
  5488. for (int i = 0; i < nb; ++i) {
  5489. const uint8_t * restrict q4 = x[i].qs;
  5490. const int8_t * restrict q8 = y[i].qs;
  5491. memset(aux32, 0, 8*sizeof(int32_t));
  5492. int8_t * restrict a = aux8;
  5493. for (int j = 0; j < QK_K/64; ++j) {
  5494. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5495. a += 32;
  5496. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5497. a += 32; q4 += 32;
  5498. }
  5499. memcpy(utmp, x[i].scales, 12);
  5500. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5501. const uint32_t uaux = utmp[1] & kmask1;
  5502. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5503. utmp[2] = uaux;
  5504. utmp[0] &= kmask1;
  5505. int sumi = 0;
  5506. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5507. a = aux8;
  5508. int is = 0;
  5509. for (int j = 0; j < QK_K/32; ++j) {
  5510. int32_t scale = scales[is++];
  5511. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5512. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5513. q8 += 8; a += 8;
  5514. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5515. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5516. q8 += 8; a += 8;
  5517. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5518. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5519. q8 += 8; a += 8;
  5520. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5521. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5522. q8 += 8; a += 8;
  5523. }
  5524. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5525. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5526. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5527. sumf -= dmin * sumi;
  5528. }
  5529. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5530. *s = sumf;
  5531. #endif
  5532. }
  5533. #else
  5534. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5535. assert(n % QK_K == 0);
  5536. assert(nrc == 1);
  5537. UNUSED(nrc);
  5538. UNUSED(bx);
  5539. UNUSED(by);
  5540. UNUSED(bs);
  5541. const block_q4_K * restrict x = vx;
  5542. const block_q8_K * restrict y = vy;
  5543. const int nb = n / QK_K;
  5544. #ifdef __ARM_NEON
  5545. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5546. const int32x4_t mzero = vdupq_n_s32(0);
  5547. float sumf = 0;
  5548. ggml_int8x16x2_t q4bytes;
  5549. ggml_int8x16x4_t q8bytes;
  5550. float sum_mins = 0.f;
  5551. uint16_t aux16[2];
  5552. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5553. for (int i = 0; i < nb; ++i) {
  5554. const uint8_t * restrict q4 = x[i].qs;
  5555. const int8_t * restrict q8 = y[i].qs;
  5556. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5557. aux16[0] = a[0] & 0x0f0f;
  5558. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5559. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5560. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5561. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5562. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5563. q8bytes = ggml_vld1q_s8_x4(q8);
  5564. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5565. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5566. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5567. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5568. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5569. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5570. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5571. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5572. sumf += d * (sumi1 + sumi2);
  5573. }
  5574. *s = sumf - sum_mins;
  5575. #elif defined __AVX2__
  5576. const __m256i m4 = _mm256_set1_epi8(0xF);
  5577. __m256 acc = _mm256_setzero_ps();
  5578. float summs = 0;
  5579. uint16_t aux16[2];
  5580. const uint8_t * scales = (const uint8_t *)aux16;
  5581. for (int i = 0; i < nb; ++i) {
  5582. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5583. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5584. const __m256 vd = _mm256_set1_ps(d);
  5585. const uint16_t * a = (const uint16_t *)x[i].scales;
  5586. aux16[0] = a[0] & 0x0f0f;
  5587. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5588. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5589. const uint8_t * restrict q4 = x[i].qs;
  5590. const int8_t * restrict q8 = y[i].qs;
  5591. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5592. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5593. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5594. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5595. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5596. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5597. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5598. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5599. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5600. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5601. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5602. }
  5603. *s = hsum_float_8(acc) - summs;
  5604. #elif defined __AVX__
  5605. const __m128i m4 = _mm_set1_epi8(0xF);
  5606. __m256 acc = _mm256_setzero_ps();
  5607. float summs = 0;
  5608. uint16_t aux16[2];
  5609. const uint8_t * scales = (const uint8_t *)aux16;
  5610. for (int i = 0; i < nb; ++i) {
  5611. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5612. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5613. const __m256 vd = _mm256_set1_ps(d);
  5614. const uint16_t * a = (const uint16_t *)x[i].scales;
  5615. aux16[0] = a[0] & 0x0f0f;
  5616. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5617. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5618. const uint8_t * restrict q4 = x[i].qs;
  5619. const int8_t * restrict q8 = y[i].qs;
  5620. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5621. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5622. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5623. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5624. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5625. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5626. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5627. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5628. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5629. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5630. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5631. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5632. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5633. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5634. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5635. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5636. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5637. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5638. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5639. }
  5640. *s = hsum_float_8(acc) - summs;
  5641. #elif defined __riscv_v_intrinsic
  5642. uint16_t s16[2];
  5643. const uint8_t * restrict scales = (const uint8_t *)s16;
  5644. float sumf = 0;
  5645. for (int i = 0; i < nb; ++i) {
  5646. const uint8_t * restrict q4 = x[i].qs;
  5647. const int8_t * restrict q8 = y[i].qs;
  5648. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5649. s16[0] = b[0] & 0x0f0f;
  5650. s16[1] = (b[0] >> 4) & 0x0f0f;
  5651. 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]));
  5652. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5653. size_t vl = 32;
  5654. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5655. // load Q4
  5656. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5657. // load Q8 and multiply it with lower Q4 nibble
  5658. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5659. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5660. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5661. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5662. // load Q8 and multiply it with upper Q4 nibble
  5663. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5664. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5665. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5666. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5667. }
  5668. *s = sumf;
  5669. #else
  5670. uint8_t aux8[QK_K];
  5671. int16_t aux16[16];
  5672. float sums [8];
  5673. memset(sums, 0, 8*sizeof(float));
  5674. uint16_t s16[2];
  5675. const uint8_t * restrict scales = (const uint8_t *)s16;
  5676. float sumf = 0;
  5677. for (int i = 0; i < nb; ++i) {
  5678. const uint8_t * restrict q4 = x[i].qs;
  5679. const int8_t * restrict q8 = y[i].qs;
  5680. uint8_t * restrict a = aux8;
  5681. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5682. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5683. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5684. s16[0] = b[0] & 0x0f0f;
  5685. s16[1] = (b[0] >> 4) & 0x0f0f;
  5686. 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]));
  5687. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5688. for (int j = 0; j < QK_K/32; ++j) {
  5689. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5690. q8 += 16; a += 16;
  5691. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5692. q8 += 16; a += 16;
  5693. const float dl = d * scales[j];
  5694. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5695. }
  5696. }
  5697. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5698. *s = sumf;
  5699. #endif
  5700. }
  5701. #endif
  5702. #if QK_K == 256
  5703. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5704. assert(n % QK_K == 0);
  5705. assert(nrc == 1);
  5706. UNUSED(nrc);
  5707. UNUSED(bx);
  5708. UNUSED(by);
  5709. UNUSED(bs);
  5710. const block_q5_K * restrict x = vx;
  5711. const block_q8_K * restrict y = vy;
  5712. const int nb = n / QK_K;
  5713. static const uint32_t kmask1 = 0x3f3f3f3f;
  5714. static const uint32_t kmask2 = 0x0f0f0f0f;
  5715. static const uint32_t kmask3 = 0x03030303;
  5716. uint32_t utmp[4];
  5717. #ifdef __ARM_NEON
  5718. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5719. const uint8x16_t mone = vdupq_n_u8(1);
  5720. const uint8x16_t mtwo = vdupq_n_u8(2);
  5721. const int32x4_t mzero = vdupq_n_s32(0);
  5722. ggml_int8x16x4_t q5bytes;
  5723. float sumf = 0;
  5724. for (int i = 0; i < nb; ++i) {
  5725. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5726. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5727. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5728. memcpy(utmp, x[i].scales, 12);
  5729. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5730. const uint32_t uaux = utmp[1] & kmask1;
  5731. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5732. utmp[2] = uaux;
  5733. utmp[0] &= kmask1;
  5734. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5735. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5736. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5737. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5738. int32_t sumi_mins = vaddvq_s32(prod);
  5739. const uint8_t * scales = (const uint8_t *)utmp;
  5740. const uint8_t * restrict q5 = x[i].qs;
  5741. const uint8_t * restrict qh = x[i].qh;
  5742. const int8_t * restrict q8 = y[i].qs;
  5743. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5744. ggml_uint8x16x4_t q5h;
  5745. int32_t sumi = 0;
  5746. for (int j = 0; j < QK_K/64; ++j) {
  5747. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5748. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5749. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5750. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5751. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5752. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5753. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5754. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5755. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5756. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5757. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5758. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5759. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5760. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5761. }
  5762. sumf += d * sumi - dmin * sumi_mins;
  5763. }
  5764. *s = sumf;
  5765. #elif defined __AVX2__
  5766. const __m256i m4 = _mm256_set1_epi8(0xF);
  5767. const __m128i mzero = _mm_setzero_si128();
  5768. const __m256i mone = _mm256_set1_epi8(1);
  5769. __m256 acc = _mm256_setzero_ps();
  5770. float summs = 0.f;
  5771. for (int i = 0; i < nb; ++i) {
  5772. const uint8_t * restrict q5 = x[i].qs;
  5773. const int8_t * restrict q8 = y[i].qs;
  5774. #if QK_K == 256
  5775. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5776. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5777. memcpy(utmp, x[i].scales, 12);
  5778. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5779. const uint32_t uaux = utmp[1] & kmask1;
  5780. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5781. utmp[2] = uaux;
  5782. utmp[0] &= kmask1;
  5783. #else
  5784. // TODO
  5785. const float d = 0, dmin = 0;
  5786. #endif
  5787. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5788. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5789. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5790. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5791. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5792. summs += dmin * _mm_extract_epi32(hsum, 0);
  5793. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5794. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5795. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5796. __m256i hmask = mone;
  5797. __m256i sumi = _mm256_setzero_si256();
  5798. int bit = 0;
  5799. for (int j = 0; j < QK_K/64; ++j) {
  5800. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5801. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5802. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5803. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5804. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5805. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5806. hmask = _mm256_slli_epi16(hmask, 1);
  5807. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5808. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5809. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5810. hmask = _mm256_slli_epi16(hmask, 1);
  5811. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5812. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5813. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5814. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5815. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5816. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5817. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5818. }
  5819. __m256 vd = _mm256_set1_ps(d);
  5820. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5821. }
  5822. *s = hsum_float_8(acc) + summs;
  5823. #elif defined __AVX__
  5824. const __m128i m4 = _mm_set1_epi8(0xF);
  5825. const __m128i mzero = _mm_setzero_si128();
  5826. const __m128i mone = _mm_set1_epi8(1);
  5827. const __m128i m2 = _mm_set1_epi8(2);
  5828. __m256 acc = _mm256_setzero_ps();
  5829. float summs = 0.f;
  5830. for (int i = 0; i < nb; ++i) {
  5831. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5832. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5833. const uint8_t * restrict q5 = x[i].qs;
  5834. const int8_t * restrict q8 = y[i].qs;
  5835. memcpy(utmp, x[i].scales, 12);
  5836. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5837. const uint32_t uaux = utmp[1] & kmask1;
  5838. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5839. utmp[2] = uaux;
  5840. utmp[0] &= kmask1;
  5841. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5842. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5843. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5844. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5845. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5846. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5847. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5848. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5849. summs += dmin * _mm_extract_epi32(hsum, 0);
  5850. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5851. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5852. __m128i hmask = mone;
  5853. __m128i sumi_0 = _mm_setzero_si128();
  5854. __m128i sumi_1 = _mm_setzero_si128();
  5855. int bit = 0;
  5856. __m128i shuffle = _mm_set1_epi16(0x0100);
  5857. for (int j = 0; j < QK_K/64; ++j) {
  5858. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5859. shuffle = _mm_add_epi16(shuffle, m2);
  5860. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5861. shuffle = _mm_add_epi16(shuffle, m2);
  5862. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5863. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5864. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5865. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5866. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5867. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5868. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5869. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5870. hmask = _mm_slli_epi16(hmask, 1);
  5871. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5872. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5873. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5874. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5875. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5876. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5877. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5878. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5879. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5880. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5881. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5882. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5883. hmask = _mm_slli_epi16(hmask, 1);
  5884. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5885. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5886. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5887. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5888. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5889. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5890. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5891. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5892. }
  5893. __m256 vd = _mm256_set1_ps(d);
  5894. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5895. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5896. }
  5897. *s = hsum_float_8(acc) + summs;
  5898. #elif defined __riscv_v_intrinsic
  5899. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5900. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5901. float sumf = 0;
  5902. float sums = 0.0;
  5903. size_t vl;
  5904. for (int i = 0; i < nb; ++i) {
  5905. vl = 8;
  5906. const uint8_t * restrict q5 = x[i].qs;
  5907. const uint8_t * restrict hm = x[i].qh;
  5908. const int8_t * restrict q8 = y[i].qs;
  5909. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5910. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5911. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5912. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5913. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5914. memcpy(utmp, x[i].scales, 12);
  5915. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5916. const uint32_t uaux = utmp[1] & kmask1;
  5917. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5918. utmp[2] = uaux;
  5919. utmp[0] &= kmask1;
  5920. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5921. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5922. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5923. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5924. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5925. vl = 32;
  5926. int32_t aux32 = 0;
  5927. int is = 0;
  5928. uint8_t m = 1;
  5929. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5930. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5931. for (int j = 0; j < QK_K/64; ++j) {
  5932. // load Q5 and Q8
  5933. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5934. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5935. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5936. // compute mask for addition
  5937. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5938. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5939. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5940. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5941. m <<= 1;
  5942. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5943. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5944. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5945. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5946. m <<= 1;
  5947. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5948. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5949. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5950. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5951. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5952. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5953. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5954. q5 += 32; q8 += 64;
  5955. }
  5956. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5957. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5958. }
  5959. *s = sumf+sums;
  5960. #else
  5961. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5962. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5963. int8_t aux8[QK_K];
  5964. int16_t aux16[8];
  5965. float sums [8];
  5966. int32_t aux32[8];
  5967. memset(sums, 0, 8*sizeof(float));
  5968. float sumf = 0;
  5969. for (int i = 0; i < nb; ++i) {
  5970. const uint8_t * restrict q4 = x[i].qs;
  5971. const uint8_t * restrict hm = x[i].qh;
  5972. const int8_t * restrict q8 = y[i].qs;
  5973. memset(aux32, 0, 8*sizeof(int32_t));
  5974. int8_t * restrict a = aux8;
  5975. uint8_t m = 1;
  5976. for (int j = 0; j < QK_K/64; ++j) {
  5977. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5978. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5979. a += 32; m <<= 1;
  5980. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5981. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5982. a += 32; m <<= 1;
  5983. q4 += 32;
  5984. }
  5985. memcpy(utmp, x[i].scales, 12);
  5986. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5987. const uint32_t uaux = utmp[1] & kmask1;
  5988. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5989. utmp[2] = uaux;
  5990. utmp[0] &= kmask1;
  5991. int sumi = 0;
  5992. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5993. a = aux8;
  5994. int is = 0;
  5995. for (int j = 0; j < QK_K/32; ++j) {
  5996. int32_t scale = scales[is++];
  5997. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5998. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5999. q8 += 8; a += 8;
  6000. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6001. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6002. q8 += 8; a += 8;
  6003. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6004. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6005. q8 += 8; a += 8;
  6006. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6007. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6008. q8 += 8; a += 8;
  6009. }
  6010. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6011. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6012. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  6013. sumf -= dmin * sumi;
  6014. }
  6015. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6016. *s = sumf;
  6017. #endif
  6018. }
  6019. #else
  6020. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6021. assert(n % QK_K == 0);
  6022. assert(nrc == 1);
  6023. UNUSED(nrc);
  6024. UNUSED(bx);
  6025. UNUSED(by);
  6026. UNUSED(bs);
  6027. const block_q5_K * restrict x = vx;
  6028. const block_q8_K * restrict y = vy;
  6029. const int nb = n / QK_K;
  6030. #ifdef __ARM_NEON
  6031. const uint8x16_t m4b = vdupq_n_u8(0xf);
  6032. const uint8x16_t mh = vdupq_n_u8(16);
  6033. const int32x4_t mzero = vdupq_n_s32(0);
  6034. ggml_int8x16x4_t q5bytes;
  6035. ggml_uint8x16x4_t q5h;
  6036. float sumf = 0;
  6037. for (int i = 0; i < nb; ++i) {
  6038. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6039. const int8_t * sc = x[i].scales;
  6040. const uint8_t * restrict q5 = x[i].qs;
  6041. const uint8_t * restrict qh = x[i].qh;
  6042. const int8_t * restrict q8 = y[i].qs;
  6043. const uint8x8_t qhbits = vld1_u8(qh);
  6044. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  6045. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6046. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  6047. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  6048. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  6049. q5h.val[2] = vbicq_u8(mh, htmp);
  6050. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  6051. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  6052. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  6053. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  6054. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  6055. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  6056. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  6057. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  6058. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  6059. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6060. }
  6061. *s = sumf;
  6062. #elif defined __AVX2__
  6063. const __m256i m4 = _mm256_set1_epi8(0xF);
  6064. const __m256i mone = _mm256_set1_epi8(1);
  6065. __m256 acc = _mm256_setzero_ps();
  6066. for (int i = 0; i < nb; ++i) {
  6067. const uint8_t * restrict q5 = x[i].qs;
  6068. const int8_t * restrict q8 = y[i].qs;
  6069. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6070. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6071. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  6072. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  6073. int64_t aux64;
  6074. memcpy(&aux64, x[i].qh, 8);
  6075. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  6076. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  6077. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  6078. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  6079. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  6080. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  6081. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6082. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6083. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  6084. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  6085. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  6086. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  6087. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  6088. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  6089. }
  6090. *s = hsum_float_8(acc);
  6091. #elif defined __AVX__
  6092. const __m128i m4 = _mm_set1_epi8(0xF);
  6093. const __m128i mone = _mm_set1_epi8(1);
  6094. __m256 acc = _mm256_setzero_ps();
  6095. for (int i = 0; i < nb; ++i) {
  6096. const uint8_t * restrict q5 = x[i].qs;
  6097. const int8_t * restrict q8 = y[i].qs;
  6098. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6099. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6100. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  6101. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  6102. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  6103. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  6104. int64_t aux64;
  6105. memcpy(&aux64, x[i].qh, 8);
  6106. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  6107. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  6108. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  6109. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  6110. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  6111. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  6112. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  6113. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  6114. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  6115. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  6116. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6117. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6118. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  6119. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  6120. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  6121. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  6122. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  6123. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  6124. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  6125. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  6126. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  6127. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  6128. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  6129. }
  6130. *s = hsum_float_8(acc);
  6131. #elif defined __riscv_v_intrinsic
  6132. float sumf = 0;
  6133. for (int i = 0; i < nb; ++i) {
  6134. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6135. const int8_t * sc = x[i].scales;
  6136. const uint8_t * restrict q5 = x[i].qs;
  6137. const uint8_t * restrict qh = x[i].qh;
  6138. const int8_t * restrict q8 = y[i].qs;
  6139. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6140. // load qh
  6141. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  6142. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  6143. size_t vl = 16;
  6144. // combine both qh_1 and qh_2
  6145. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  6146. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6147. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  6148. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  6149. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6150. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  6151. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  6152. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  6153. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  6154. // load q5
  6155. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  6156. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  6157. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  6158. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  6159. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  6160. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  6161. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  6162. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  6163. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  6164. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  6165. // load Q8 and multiply it with Q5
  6166. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6167. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6168. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6169. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6170. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6171. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6172. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6173. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6174. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  6175. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  6176. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  6177. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  6178. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6179. }
  6180. *s = sumf;
  6181. #else
  6182. int8_t aux8[QK_K];
  6183. int16_t aux16[16];
  6184. float sums [8];
  6185. memset(sums, 0, 8*sizeof(float));
  6186. float sumf = 0;
  6187. for (int i = 0; i < nb; ++i) {
  6188. const uint8_t * restrict q4 = x[i].qs;
  6189. const uint8_t * restrict hm = x[i].qh;
  6190. const int8_t * restrict q8 = y[i].qs;
  6191. int8_t * restrict a = aux8;
  6192. for (int l = 0; l < 32; ++l) {
  6193. a[l+ 0] = q4[l] & 0xF;
  6194. a[l+32] = q4[l] >> 4;
  6195. }
  6196. for (int is = 0; is < 8; ++is) {
  6197. uint8_t m = 1 << is;
  6198. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  6199. }
  6200. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6201. const int8_t * restrict sc = x[i].scales;
  6202. for (int j = 0; j < QK_K/16; ++j) {
  6203. const float dl = d * sc[j];
  6204. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  6205. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  6206. q8 += 16; a += 16;
  6207. }
  6208. }
  6209. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6210. *s = sumf;
  6211. #endif
  6212. }
  6213. #endif
  6214. #if QK_K == 256
  6215. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6216. assert(n % QK_K == 0);
  6217. assert(nrc == 1);
  6218. UNUSED(nrc);
  6219. UNUSED(bx);
  6220. UNUSED(by);
  6221. UNUSED(bs);
  6222. const block_q6_K * restrict x = vx;
  6223. const block_q8_K * restrict y = vy;
  6224. const int nb = n / QK_K;
  6225. #ifdef __ARM_NEON
  6226. float sum = 0;
  6227. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6228. const int32x4_t vzero = vdupq_n_s32(0);
  6229. //const int8x16_t m32s = vdupq_n_s8(32);
  6230. const uint8x16_t mone = vdupq_n_u8(3);
  6231. ggml_int8x16x4_t q6bytes;
  6232. ggml_uint8x16x4_t q6h;
  6233. for (int i = 0; i < nb; ++i) {
  6234. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6235. const uint8_t * restrict q6 = x[i].ql;
  6236. const uint8_t * restrict qh = x[i].qh;
  6237. const int8_t * restrict q8 = y[i].qs;
  6238. const int8_t * restrict scale = x[i].scales;
  6239. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6240. const int8x16_t scales = vld1q_s8(scale);
  6241. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6242. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6243. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6244. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6245. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6246. int32_t isum_mins = vaddvq_s32(prod);
  6247. int32_t isum = 0;
  6248. for (int j = 0; j < QK_K/128; ++j) {
  6249. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6250. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6251. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6252. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6253. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6254. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6255. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6256. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6257. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6258. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6259. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6260. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6261. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6262. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6263. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6264. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6265. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6266. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6267. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6268. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6269. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6270. scale += 4;
  6271. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6272. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6273. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6274. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6275. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6276. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6277. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6278. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6279. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6280. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6281. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6282. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6283. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6284. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6285. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6286. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6287. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6288. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6289. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6290. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6291. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6292. scale += 4;
  6293. }
  6294. //sum += isum * d_all * y[i].d;
  6295. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6296. }
  6297. *s = sum;
  6298. #elif defined __AVX2__
  6299. const __m256i m4 = _mm256_set1_epi8(0xF);
  6300. const __m256i m2 = _mm256_set1_epi8(3);
  6301. const __m256i m32s = _mm256_set1_epi8(32);
  6302. __m256 acc = _mm256_setzero_ps();
  6303. for (int i = 0; i < nb; ++i) {
  6304. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6305. const uint8_t * restrict q4 = x[i].ql;
  6306. const uint8_t * restrict qh = x[i].qh;
  6307. const int8_t * restrict q8 = y[i].qs;
  6308. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6309. __m256i sumi = _mm256_setzero_si256();
  6310. int is = 0;
  6311. for (int j = 0; j < QK_K/128; ++j) {
  6312. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6313. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6314. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6315. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6316. is += 4;
  6317. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6318. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6319. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6320. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6321. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6322. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6323. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6324. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6325. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6326. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6327. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6328. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6329. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6330. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6331. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6332. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6333. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6334. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6335. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6336. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6337. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6338. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6339. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6340. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6341. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6342. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6343. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6344. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6345. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6346. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6347. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6348. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6349. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6350. }
  6351. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6352. }
  6353. *s = hsum_float_8(acc);
  6354. #elif defined __AVX__
  6355. const __m128i m4 = _mm_set1_epi8(0xF);
  6356. const __m128i m3 = _mm_set1_epi8(3);
  6357. const __m128i m32s = _mm_set1_epi8(32);
  6358. const __m128i m2 = _mm_set1_epi8(2);
  6359. __m256 acc = _mm256_setzero_ps();
  6360. for (int i = 0; i < nb; ++i) {
  6361. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6362. const uint8_t * restrict q4 = x[i].ql;
  6363. const uint8_t * restrict qh = x[i].qh;
  6364. const int8_t * restrict q8 = y[i].qs;
  6365. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6366. __m128i sumi_0 = _mm_setzero_si128();
  6367. __m128i sumi_1 = _mm_setzero_si128();
  6368. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6369. for (int j = 0; j < QK_K/128; ++j) {
  6370. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6371. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6372. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6373. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6374. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6375. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6376. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6377. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6378. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6379. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6380. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6381. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6382. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6383. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6384. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6385. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6386. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6387. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6388. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6389. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6390. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6391. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6392. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6393. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6394. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6395. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6396. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6397. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6398. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6399. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6400. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6401. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6402. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6403. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6404. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6405. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6406. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6407. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6408. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6409. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6410. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6411. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6412. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6413. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6414. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6415. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6416. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6417. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6418. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6419. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6420. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6421. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6422. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6423. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6424. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6425. shuffle = _mm_add_epi8(shuffle, m2);
  6426. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6427. shuffle = _mm_add_epi8(shuffle, m2);
  6428. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6429. shuffle = _mm_add_epi8(shuffle, m2);
  6430. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6431. shuffle = _mm_add_epi8(shuffle, m2);
  6432. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6433. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6434. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6435. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6436. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6437. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6438. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6439. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6440. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6441. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6442. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6443. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6444. }
  6445. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6446. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6447. }
  6448. *s = hsum_float_8(acc);
  6449. #elif defined __riscv_v_intrinsic
  6450. float sumf = 0;
  6451. for (int i = 0; i < nb; ++i) {
  6452. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6453. const uint8_t * restrict q6 = x[i].ql;
  6454. const uint8_t * restrict qh = x[i].qh;
  6455. const int8_t * restrict q8 = y[i].qs;
  6456. const int8_t * restrict scale = x[i].scales;
  6457. size_t vl;
  6458. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6459. int sum_t = 0;
  6460. int is = 0;
  6461. for (int j = 0; j < QK_K/128; ++j) {
  6462. vl = 32;
  6463. // load qh
  6464. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6465. // load Q6
  6466. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6467. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6468. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6469. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6470. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6471. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6472. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6473. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6474. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6475. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6476. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6477. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6478. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6479. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6480. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6481. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6482. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6483. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6484. // load Q8 and take product
  6485. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6486. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6487. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6488. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6489. vl = 16;
  6490. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6491. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6492. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6493. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6494. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6495. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6496. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6497. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6498. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6499. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6500. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6501. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6502. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6503. q6 += 64; qh += 32; q8 += 128; is=8;
  6504. }
  6505. sumf += d * sum_t;
  6506. }
  6507. *s = sumf;
  6508. #else
  6509. int8_t aux8[QK_K];
  6510. int16_t aux16[8];
  6511. float sums [8];
  6512. int32_t aux32[8];
  6513. memset(sums, 0, 8*sizeof(float));
  6514. float sumf = 0;
  6515. for (int i = 0; i < nb; ++i) {
  6516. const uint8_t * restrict q4 = x[i].ql;
  6517. const uint8_t * restrict qh = x[i].qh;
  6518. const int8_t * restrict q8 = y[i].qs;
  6519. memset(aux32, 0, 8*sizeof(int32_t));
  6520. int8_t * restrict a = aux8;
  6521. for (int j = 0; j < QK_K; j += 128) {
  6522. for (int l = 0; l < 32; ++l) {
  6523. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6524. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6525. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6526. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6527. }
  6528. a += 128;
  6529. q4 += 64;
  6530. qh += 32;
  6531. }
  6532. a = aux8;
  6533. int is = 0;
  6534. for (int j = 0; j < QK_K/16; ++j) {
  6535. int scale = x[i].scales[is++];
  6536. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6537. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6538. q8 += 8; a += 8;
  6539. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6540. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6541. q8 += 8; a += 8;
  6542. }
  6543. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6544. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6545. }
  6546. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6547. *s = sumf;
  6548. #endif
  6549. }
  6550. #else
  6551. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6552. assert(n % QK_K == 0);
  6553. assert(nrc == 1);
  6554. UNUSED(nrc);
  6555. UNUSED(bx);
  6556. UNUSED(by);
  6557. UNUSED(bs);
  6558. const block_q6_K * restrict x = vx;
  6559. const block_q8_K * restrict y = vy;
  6560. const int nb = n / QK_K;
  6561. #ifdef __ARM_NEON
  6562. float sum = 0;
  6563. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6564. const int8x16_t m32s = vdupq_n_s8(32);
  6565. const int32x4_t vzero = vdupq_n_s32(0);
  6566. const uint8x16_t mone = vdupq_n_u8(3);
  6567. ggml_int8x16x4_t q6bytes;
  6568. ggml_uint8x16x4_t q6h;
  6569. for (int i = 0; i < nb; ++i) {
  6570. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6571. const uint8_t * restrict q6 = x[i].ql;
  6572. const uint8_t * restrict qh = x[i].qh;
  6573. const int8_t * restrict q8 = y[i].qs;
  6574. const int8_t * restrict scale = x[i].scales;
  6575. int32_t isum = 0;
  6576. uint8x16_t qhbits = vld1q_u8(qh);
  6577. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6578. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6579. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6580. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6581. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6582. shifted = vshrq_n_u8(qhbits, 4);
  6583. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6584. shifted = vshrq_n_u8(qhbits, 6);
  6585. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6586. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6587. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6588. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6589. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6590. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6591. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6592. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6593. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6594. sum += isum * d_all * y[i].d;
  6595. }
  6596. *s = sum;
  6597. #elif defined __AVX2__
  6598. const __m256i m4 = _mm256_set1_epi8(0xF);
  6599. const __m256i m2 = _mm256_set1_epi8(3);
  6600. const __m256i m32s = _mm256_set1_epi8(32);
  6601. __m256 acc = _mm256_setzero_ps();
  6602. for (int i = 0; i < nb; ++i) {
  6603. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6604. const uint8_t * restrict q4 = x[i].ql;
  6605. const uint8_t * restrict qh = x[i].qh;
  6606. const int8_t * restrict q8 = y[i].qs;
  6607. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6608. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6609. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6610. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6611. __m256i sumi = _mm256_setzero_si256();
  6612. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6613. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6614. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6615. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6616. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6617. 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);
  6618. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6619. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6620. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6621. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6622. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6623. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6624. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6625. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6626. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6627. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6628. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6629. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6630. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6631. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6632. }
  6633. *s = hsum_float_8(acc);
  6634. #elif defined __AVX__
  6635. const __m128i m4 = _mm_set1_epi8(0xF);
  6636. const __m128i m2 = _mm_set1_epi8(3);
  6637. const __m128i m32s = _mm_set1_epi8(32);
  6638. __m256 acc = _mm256_setzero_ps();
  6639. for (int i = 0; i < nb; ++i) {
  6640. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6641. const uint8_t * restrict q4 = x[i].ql;
  6642. const uint8_t * restrict qh = x[i].qh;
  6643. const int8_t * restrict q8 = y[i].qs;
  6644. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6645. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6646. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6647. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6648. __m128i sumi_0 = _mm_setzero_si128();
  6649. __m128i sumi_1 = _mm_setzero_si128();
  6650. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6651. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6652. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6653. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6654. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6655. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6656. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6657. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6658. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6659. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6660. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6661. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6662. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6663. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6664. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6665. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6666. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6667. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6668. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6669. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6670. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6671. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6672. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6673. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6674. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6675. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6676. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6677. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6678. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6679. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6680. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6681. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6682. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6683. }
  6684. *s = hsum_float_8(acc);
  6685. #elif defined __riscv_v_intrinsic
  6686. float sumf = 0;
  6687. for (int i = 0; i < nb; ++i) {
  6688. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6689. const uint8_t * restrict q6 = x[i].ql;
  6690. const uint8_t * restrict qh = x[i].qh;
  6691. const int8_t * restrict q8 = y[i].qs;
  6692. const int8_t * restrict scale = x[i].scales;
  6693. int32_t isum = 0;
  6694. size_t vl = 16;
  6695. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6696. // load Q6
  6697. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6698. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6699. // load qh
  6700. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6701. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6702. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6703. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6704. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6705. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6706. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6707. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6708. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6709. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6710. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6711. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6712. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6713. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6714. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6715. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6716. // load Q8 and take product
  6717. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6718. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6719. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6720. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6721. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6722. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6723. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6724. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6725. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6726. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6727. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6728. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6729. sumf += isum * d_all * y[i].d;
  6730. }
  6731. *s = sumf;
  6732. #else
  6733. int8_t aux8[QK_K];
  6734. int16_t aux16[8];
  6735. float sums [8];
  6736. int32_t aux32[8];
  6737. memset(sums, 0, 8*sizeof(float));
  6738. float sumf = 0;
  6739. for (int i = 0; i < nb; ++i) {
  6740. const uint8_t * restrict q4 = x[i].ql;
  6741. const uint8_t * restrict qh = x[i].qh;
  6742. const int8_t * restrict q8 = y[i].qs;
  6743. memset(aux32, 0, 8*sizeof(int32_t));
  6744. int8_t * restrict a = aux8;
  6745. for (int l = 0; l < 16; ++l) {
  6746. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6747. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6748. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6749. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6750. }
  6751. int is = 0;
  6752. for (int j = 0; j < QK_K/16; ++j) {
  6753. int scale = x[i].scales[is++];
  6754. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6755. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6756. q8 += 8; a += 8;
  6757. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6758. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6759. q8 += 8; a += 8;
  6760. }
  6761. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6762. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6763. }
  6764. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6765. *s = sumf;
  6766. #endif
  6767. }
  6768. #endif
  6769. #if defined (__AVX2__) || defined (__ARM_NEON)
  6770. static const int8_t keven_signs_q2xs[1024] = {
  6771. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6772. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6773. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6774. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6775. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6776. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6777. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6778. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6779. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6780. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6781. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6782. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6783. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6784. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6785. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6786. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6787. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6788. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6789. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6790. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6791. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6792. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6793. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6794. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6795. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6796. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6797. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6798. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6799. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6800. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6801. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6802. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6803. };
  6804. #endif
  6805. void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6806. assert(n % QK_K == 0);
  6807. assert(nrc == 1);
  6808. UNUSED(nrc);
  6809. UNUSED(bx);
  6810. UNUSED(by);
  6811. UNUSED(bs);
  6812. const block_iq2_xxs * restrict x = vx;
  6813. const block_q8_K * restrict y = vy;
  6814. const int nb = n / QK_K;
  6815. #if defined(__ARM_NEON)
  6816. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6817. uint32_t aux32[4];
  6818. const uint8_t * aux8 = (const uint8_t *)aux32;
  6819. ggml_int8x16x4_t q2u;
  6820. ggml_int8x16x4_t q2s;
  6821. ggml_int8x16x4_t q8b;
  6822. float sumf = 0;
  6823. for (int i = 0; i < nb; ++i) {
  6824. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6825. const uint16_t * restrict q2 = x[i].qs;
  6826. const int8_t * restrict q8 = y[i].qs;
  6827. float sumf1 = 0, sumf2 = 0;
  6828. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6829. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6830. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6831. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6832. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6833. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6834. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6835. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6836. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6837. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6838. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6839. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6840. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6841. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6842. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6843. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);
  6844. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);
  6845. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6846. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6847. }
  6848. sumf += d*(sumf1 + sumf2);
  6849. }
  6850. *s = 0.25f * sumf;
  6851. #elif defined(__AVX2__)
  6852. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6853. uint32_t aux32[4];
  6854. const uint8_t * aux8 = (const uint8_t *)aux32;
  6855. __m256 accumf = _mm256_setzero_ps();
  6856. for (int i = 0; i < nb; ++i) {
  6857. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6858. const uint16_t * restrict q2 = x[i].qs;
  6859. const int8_t * restrict q8 = y[i].qs;
  6860. __m256i sumi1 = _mm256_setzero_si256();
  6861. __m256i sumi2 = _mm256_setzero_si256();
  6862. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6863. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6864. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6865. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6866. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6867. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6868. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6869. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6870. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6871. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6872. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6873. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6874. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6875. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6876. const uint16_t ls1 = aux32[1] >> 28;
  6877. const uint16_t ls2 = aux32[3] >> 28;
  6878. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6879. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6880. sumi1 = _mm256_add_epi32(sumi1, p1);
  6881. sumi2 = _mm256_add_epi32(sumi2, p2);
  6882. }
  6883. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6884. }
  6885. *s = 0.125f * hsum_float_8(accumf);
  6886. #else
  6887. uint32_t aux32[2];
  6888. const uint8_t * aux8 = (const uint8_t *)aux32;
  6889. float sumf = 0.f;
  6890. for (int i = 0; i < nb; ++i) {
  6891. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6892. const uint16_t * restrict q2 = x[i].qs;
  6893. const int8_t * restrict q8 = y[i].qs;
  6894. int32_t bsum = 0;
  6895. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6896. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6897. q2 += 4;
  6898. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6899. int32_t sumi = 0;
  6900. for (int l = 0; l < 4; ++l) {
  6901. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6902. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6903. for (int j = 0; j < 8; ++j) {
  6904. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6905. }
  6906. q8 += 8;
  6907. }
  6908. bsum += sumi * ls;
  6909. }
  6910. sumf += d * bsum;
  6911. }
  6912. *s = 0.125f * sumf;
  6913. #endif
  6914. }
  6915. void ggml_vec_dot_iq2_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6916. assert(n % QK_K == 0);
  6917. assert(nrc == 1);
  6918. UNUSED(nrc);
  6919. UNUSED(bx);
  6920. UNUSED(by);
  6921. UNUSED(bs);
  6922. const block_iq2_xs * restrict x = vx;
  6923. const block_q8_K * restrict y = vy;
  6924. const int nb = n / QK_K;
  6925. #if defined(__ARM_NEON)
  6926. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6927. ggml_int8x16x4_t q2u;
  6928. ggml_int8x16x4_t q2s;
  6929. ggml_int8x16x4_t q8b;
  6930. int32x4x4_t scales32;
  6931. float sumf = 0;
  6932. for (int i = 0; i < nb; ++i) {
  6933. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6934. const uint16_t * restrict q2 = x[i].qs;
  6935. const int8_t * restrict q8 = y[i].qs;
  6936. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6937. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6938. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6939. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6940. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6941. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6942. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6943. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6944. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6945. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6946. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6947. int32x4_t sumi = vdupq_n_s32(0);
  6948. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6949. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6950. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6951. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6952. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6953. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6954. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6955. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6956. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6957. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6958. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6959. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6960. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6961. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6962. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6963. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6964. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6965. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6966. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6967. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6968. q2 += 8;
  6969. }
  6970. sumf += d*vaddvq_s32(sumi);
  6971. }
  6972. *s = 0.125f * sumf;
  6973. #elif defined(__AVX2__)
  6974. const __m256i mone = _mm256_set1_epi8(1);
  6975. static const char block_sign_shuffle_mask_1[32] = {
  6976. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6977. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6978. };
  6979. static const char block_sign_shuffle_mask_2[32] = {
  6980. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6981. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6982. };
  6983. static const uint8_t bit_selector_mask_bytes[32] = {
  6984. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6985. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6986. };
  6987. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6988. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6989. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6990. #if QK_K == 64
  6991. static const uint8_t k_bit_helper[16] = {
  6992. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6993. };
  6994. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6995. const __m128i m511 = _mm_set1_epi16(511);
  6996. typedef union {
  6997. __m128i vec_index;
  6998. uint16_t index[8];
  6999. } index_t;
  7000. index_t idx;
  7001. __m256 accumf = _mm256_setzero_ps();
  7002. for (int i = 0; i < nb; ++i) {
  7003. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7004. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  7005. idx.vec_index = _mm_and_si128(q2_data, m511);
  7006. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  7007. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  7008. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  7009. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7010. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  7011. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  7012. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  7013. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  7014. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  7015. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  7016. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  7017. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  7018. __m256i signs;
  7019. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  7020. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7021. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7022. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  7023. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7024. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7025. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7026. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7027. const __m256i sc1 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[0] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[0] & 0xf)+1));
  7028. const __m256i sc2 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[1] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[1] & 0xf)+1));
  7029. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  7030. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  7031. }
  7032. *s = 0.125f * hsum_float_8(accumf);
  7033. #else
  7034. static const uint8_t k_bit_helper[32] = {
  7035. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7036. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7037. };
  7038. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  7039. const __m256i m511 = _mm256_set1_epi16(511);
  7040. const __m128i m4 = _mm_set1_epi8(0xf);
  7041. const __m128i m1 = _mm_set1_epi8(1);
  7042. uint64_t aux64;
  7043. // somewhat hacky, but gives a significant boost in performance
  7044. __m256i aux_gindex;
  7045. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  7046. __m256 accumf = _mm256_setzero_ps();
  7047. for (int i = 0; i < nb; ++i) {
  7048. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7049. const uint16_t * restrict q2 = x[i].qs;
  7050. const int8_t * restrict q8 = y[i].qs;
  7051. memcpy(&aux64, x[i].scales, 8);
  7052. __m128i stmp = _mm_set1_epi64x(aux64);
  7053. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  7054. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  7055. __m256i sumi1 = _mm256_setzero_si256();
  7056. __m256i sumi2 = _mm256_setzero_si256();
  7057. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7058. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  7059. aux_gindex = _mm256_and_si256(q2_data, m511);
  7060. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  7061. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  7062. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  7063. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7064. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  7065. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7066. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7067. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7068. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7069. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7070. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7071. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7072. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7073. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7074. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7075. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7076. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7077. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  7078. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  7079. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  7080. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  7081. __m256i signs;
  7082. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  7083. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7084. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7085. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  7086. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7087. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7088. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  7089. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7090. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  7091. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  7092. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7093. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  7094. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7095. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7096. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  7097. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  7098. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  7099. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  7100. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  7101. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  7102. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  7103. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  7104. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  7105. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  7106. }
  7107. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7108. }
  7109. *s = 0.125f * hsum_float_8(accumf);
  7110. #endif
  7111. #else
  7112. float sumf = 0.f;
  7113. for (int i = 0; i < nb; ++i) {
  7114. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7115. const uint16_t * restrict q2 = x[i].qs;
  7116. const uint8_t * restrict sc = x[i].scales;
  7117. const int8_t * restrict q8 = y[i].qs;
  7118. int32_t bsum = 0;
  7119. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7120. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  7121. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  7122. int32_t sumi = 0;
  7123. for (int l = 0; l < 2; ++l) {
  7124. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7125. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7126. for (int j = 0; j < 8; ++j) {
  7127. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7128. }
  7129. q8 += 8;
  7130. }
  7131. bsum += sumi * ls1;
  7132. sumi = 0;
  7133. for (int l = 2; l < 4; ++l) {
  7134. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7135. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7136. for (int j = 0; j < 8; ++j) {
  7137. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7138. }
  7139. q8 += 8;
  7140. }
  7141. bsum += sumi * ls2;
  7142. q2 += 4;
  7143. }
  7144. sumf += d * bsum;
  7145. }
  7146. *s = 0.125f * sumf;
  7147. #endif
  7148. }
  7149. void ggml_vec_dot_iq2_s_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7150. assert(n % QK_K == 0);
  7151. assert(nrc == 1);
  7152. UNUSED(nrc);
  7153. UNUSED(bx);
  7154. UNUSED(by);
  7155. UNUSED(bs);
  7156. const block_iq2_s * restrict x = vx;
  7157. const block_q8_K * restrict y = vy;
  7158. const int nb = n / QK_K;
  7159. #if defined(__ARM_NEON)
  7160. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7161. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7162. };
  7163. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7164. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7165. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7166. const uint8x16_t m1 = vdupq_n_u8(1);
  7167. const int32x4_t vzero = vdupq_n_s32(0);
  7168. uint8x16x2_t vs;
  7169. ggml_int8x16x4_t q2s;
  7170. ggml_int8x16x4_t q8b;
  7171. float sumf = 0;
  7172. for (int i = 0; i < nb; ++i) {
  7173. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7174. const uint8_t * restrict qs = x[i].qs;
  7175. const uint8_t * restrict qh = x[i].qh;
  7176. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7177. const int8_t * restrict q8 = y[i].qs;
  7178. int sumi1 = 0, sumi2 = 0;
  7179. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7180. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7181. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  7182. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  7183. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  7184. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  7185. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  7186. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  7187. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  7188. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  7189. qs += 8;
  7190. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7191. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7192. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7193. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7194. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7195. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  7196. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  7197. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7198. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7199. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7200. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7201. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7202. signs += 4;
  7203. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  7204. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  7205. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  7206. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  7207. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  7208. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  7209. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  7210. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  7211. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  7212. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  7213. }
  7214. sumf += d*(sumi1 + sumi2);
  7215. }
  7216. *s = 0.125f * sumf;
  7217. #elif defined(__AVX2__)
  7218. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7219. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7220. };
  7221. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7222. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7223. };
  7224. const __m128i m4 = _mm_set1_epi8(0xf);
  7225. const __m128i m1 = _mm_set1_epi8(1);
  7226. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7227. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7228. uint64_t aux64;
  7229. __m256 accumf = _mm256_setzero_ps();
  7230. for (int i = 0; i < nb; ++i) {
  7231. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7232. const uint8_t * restrict qs = x[i].qs;
  7233. const uint8_t * restrict qh = x[i].qh;
  7234. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7235. const int8_t * restrict q8 = y[i].qs;
  7236. memcpy(&aux64, x[i].scales, 8);
  7237. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7238. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7239. __m256i sumi1 = _mm256_setzero_si256();
  7240. __m256i sumi2 = _mm256_setzero_si256();
  7241. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7242. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7243. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7244. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7245. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7246. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7247. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7248. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7249. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7250. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7251. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7252. qs += 8;
  7253. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7254. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7255. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7256. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7257. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7258. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7259. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7260. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7261. signs += 4;
  7262. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7263. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7264. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7265. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7266. sumi1 = _mm256_add_epi32(sumi1, p1);
  7267. sumi2 = _mm256_add_epi32(sumi2, p2);
  7268. }
  7269. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7270. }
  7271. *s = 0.125f * hsum_float_8(accumf);
  7272. #else
  7273. float sumf = 0;
  7274. for (int i = 0; i < nb; i++) {
  7275. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7276. const int8_t * q8 = y[i].qs;
  7277. const uint8_t * qs = x[i].qs;
  7278. const uint8_t * qh = x[i].qh;
  7279. const uint8_t * signs = qs + QK_K/8;
  7280. int bsum = 0;
  7281. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7282. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7283. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7284. int sumi1 = 0, sumi2 = 0;
  7285. for (int l = 0; l < 2; ++l) {
  7286. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7287. for (int j = 0; j < 8; ++j) {
  7288. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7289. }
  7290. q8 += 8;
  7291. }
  7292. for (int l = 2; l < 4; ++l) {
  7293. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7294. for (int j = 0; j < 8; ++j) {
  7295. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7296. }
  7297. q8 += 8;
  7298. }
  7299. bsum += ls1 * sumi1 + ls2 * sumi2;
  7300. qs += 4;
  7301. signs += 4;
  7302. }
  7303. sumf += d * bsum;
  7304. }
  7305. *s = 0.125f * sumf;
  7306. #endif
  7307. }
  7308. void ggml_vec_dot_iq3_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7309. assert(n % QK_K == 0);
  7310. assert(nrc == 1);
  7311. UNUSED(nrc);
  7312. UNUSED(bx);
  7313. UNUSED(by);
  7314. UNUSED(bs);
  7315. const block_iq3_xxs * restrict x = vx;
  7316. const block_q8_K * restrict y = vy;
  7317. const int nb = n / QK_K;
  7318. #if defined(__ARM_NEON)
  7319. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7320. uint32_t aux32[2];
  7321. ggml_int8x16x4_t q3s;
  7322. ggml_int8x16x4_t q8b;
  7323. float sumf = 0;
  7324. for (int i = 0; i < nb; ++i) {
  7325. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7326. const uint8_t * restrict q3 = x[i].qs;
  7327. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7328. const int8_t * restrict q8 = y[i].qs;
  7329. float sumf1 = 0, sumf2 = 0;
  7330. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7331. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7332. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7333. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7334. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7335. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7336. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7337. q3 += 16;
  7338. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7339. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7340. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7341. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7342. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7343. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7344. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7345. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7346. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7347. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7348. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7349. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7350. }
  7351. sumf += d*(sumf1 + sumf2);
  7352. }
  7353. *s = 0.5f * sumf;
  7354. #elif defined(__AVX2__)
  7355. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7356. uint32_t aux32[2];
  7357. __m256 accumf = _mm256_setzero_ps();
  7358. for (int i = 0; i < nb; ++i) {
  7359. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7360. const uint8_t * restrict q3 = x[i].qs;
  7361. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7362. const int8_t * restrict q8 = y[i].qs;
  7363. __m256i sumi1 = _mm256_setzero_si256();
  7364. __m256i sumi2 = _mm256_setzero_si256();
  7365. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7366. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7367. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7368. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7369. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7370. q3 += 8;
  7371. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7372. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7373. q3 += 8;
  7374. memcpy(aux32, gas, 8); gas += 8;
  7375. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7376. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7377. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7378. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7379. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7380. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7381. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7382. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7383. const uint16_t ls1 = aux32[0] >> 28;
  7384. const uint16_t ls2 = aux32[1] >> 28;
  7385. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7386. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7387. sumi1 = _mm256_add_epi32(sumi1, p1);
  7388. sumi2 = _mm256_add_epi32(sumi2, p2);
  7389. }
  7390. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7391. }
  7392. *s = 0.25f * hsum_float_8(accumf);
  7393. #else
  7394. uint32_t aux32;
  7395. float sumf = 0.f;
  7396. for (int i = 0; i < nb; ++i) {
  7397. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7398. const uint8_t * restrict q3 = x[i].qs;
  7399. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7400. const int8_t * restrict q8 = y[i].qs;
  7401. int32_t bsum = 0;
  7402. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7403. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7404. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7405. int32_t sumi = 0;
  7406. for (int l = 0; l < 4; ++l) {
  7407. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7408. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7409. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7410. for (int j = 0; j < 4; ++j) {
  7411. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7412. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7413. }
  7414. q8 += 8;
  7415. }
  7416. q3 += 8;
  7417. bsum += sumi * ls;
  7418. }
  7419. sumf += d * bsum;
  7420. }
  7421. *s = 0.25f * sumf;
  7422. #endif
  7423. }
  7424. void ggml_vec_dot_iq3_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7425. assert(n % QK_K == 0);
  7426. assert(nrc == 1);
  7427. UNUSED(nrc);
  7428. UNUSED(bx);
  7429. UNUSED(by);
  7430. UNUSED(bs);
  7431. const block_iq3_s * restrict x = vx;
  7432. const block_q8_K * restrict y = vy;
  7433. const int nb = n / QK_K;
  7434. #if defined(__ARM_NEON)
  7435. typedef union {
  7436. uint16x8_t vec_index;
  7437. uint16_t index[8];
  7438. } vec_index_t;
  7439. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7440. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7441. };
  7442. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7443. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7444. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7445. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7446. const int16x8_t hshift = vld1q_s16(k_shift);
  7447. const uint16x8_t m256 = vdupq_n_u16(256);
  7448. const uint8x16_t m1 = vdupq_n_u8(1);
  7449. uint8x16x2_t vs;
  7450. ggml_int8x16x4_t q3s;
  7451. ggml_int8x16x4_t q8b;
  7452. vec_index_t idx;
  7453. #if QK_K == 256
  7454. uint32_t scales32[2];
  7455. const uint8_t * scales8 = (const uint8_t *)scales32;
  7456. #endif
  7457. float sumf = 0;
  7458. for (int i = 0; i < nb; ++i) {
  7459. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7460. const uint8_t * restrict qs = x[i].qs;
  7461. const uint8_t * restrict qh = x[i].qh;
  7462. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7463. const int8_t * restrict q8 = y[i].qs;
  7464. #if QK_K == 256
  7465. memcpy(scales32, x[i].scales, 4);
  7466. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7467. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7468. #endif
  7469. int sumi1 = 0, sumi2 = 0;
  7470. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7471. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7472. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7473. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7474. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7475. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7476. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7477. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7478. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7479. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7480. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7481. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7482. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7483. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7484. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7485. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7486. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7487. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7488. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7489. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7490. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7491. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7492. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7493. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7494. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7495. signs += 4;
  7496. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7497. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7498. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7499. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7500. #if QK_K == 256
  7501. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7502. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7503. #else
  7504. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7505. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7506. #endif
  7507. }
  7508. sumf += d*(sumi1 + sumi2);
  7509. }
  7510. *s = sumf;
  7511. #elif defined(__AVX2__)
  7512. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7513. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7514. };
  7515. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7516. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7517. };
  7518. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7519. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7520. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7521. const __m256i idx_mask = _mm256_set1_epi32(256);
  7522. typedef union {
  7523. __m256i vec[2];
  7524. uint32_t index[16];
  7525. } index_t;
  7526. index_t idx;
  7527. __m256 accumf = _mm256_setzero_ps();
  7528. for (int i = 0; i < nb; ++i) {
  7529. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7530. const uint8_t * restrict qs = x[i].qs;
  7531. const uint8_t * restrict qh = x[i].qh;
  7532. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7533. const int8_t * restrict q8 = y[i].qs;
  7534. __m256i sumi1 = _mm256_setzero_si256();
  7535. __m256i sumi2 = _mm256_setzero_si256();
  7536. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7537. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7538. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7539. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7540. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7541. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7542. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7543. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7544. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7545. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7546. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7547. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7548. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7549. const __m256i q2_1 = _mm256_set_epi32(
  7550. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7551. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7552. );
  7553. const __m256i q2_2 = _mm256_set_epi32(
  7554. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7555. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7556. );
  7557. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7558. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7559. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7560. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7561. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7562. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7563. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7564. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7565. signs += 4;
  7566. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7567. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7568. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7569. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7570. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7571. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7572. sumi1 = _mm256_add_epi32(sumi1, p1);
  7573. sumi2 = _mm256_add_epi32(sumi2, p2);
  7574. }
  7575. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7576. }
  7577. *s = hsum_float_8(accumf);
  7578. #else
  7579. float sumf = 0.f;
  7580. for (int i = 0; i < nb; ++i) {
  7581. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7582. const uint8_t * restrict qs = x[i].qs;
  7583. const uint8_t * restrict qh = x[i].qh;
  7584. const uint8_t * restrict signs = x[i].signs;
  7585. const int8_t * restrict q8 = y[i].qs;
  7586. int32_t bsum = 0;
  7587. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7588. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7589. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7590. int32_t sumi = 0;
  7591. for (int l = 0; l < 4; ++l) {
  7592. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7593. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7594. for (int j = 0; j < 4; ++j) {
  7595. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7596. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7597. }
  7598. q8 += 8;
  7599. }
  7600. qs += 8;
  7601. signs += 4;
  7602. bsum += sumi * ls1;
  7603. sumi = 0;
  7604. for (int l = 0; l < 4; ++l) {
  7605. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7606. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7607. for (int j = 0; j < 4; ++j) {
  7608. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7609. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7610. }
  7611. q8 += 8;
  7612. }
  7613. qs += 8;
  7614. signs += 4;
  7615. bsum += sumi * ls2;
  7616. }
  7617. sumf += d * bsum;
  7618. }
  7619. *s = sumf;
  7620. #endif
  7621. }
  7622. #ifdef __AVX2__
  7623. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7624. const __m256i ax = _mm256_sign_epi8(x, x);
  7625. const __m256i sy = _mm256_sign_epi8(y, x);
  7626. return _mm256_maddubs_epi16(ax, sy);
  7627. }
  7628. #endif
  7629. void ggml_vec_dot_iq1_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7630. assert(n % QK_K == 0);
  7631. assert(nrc == 1);
  7632. UNUSED(nrc);
  7633. UNUSED(bx);
  7634. UNUSED(by);
  7635. UNUSED(bs);
  7636. const block_iq1_s * restrict x = vx;
  7637. const block_q8_K * restrict y = vy;
  7638. const int nb = n / QK_K;
  7639. #if defined __ARM_NEON
  7640. ggml_int8x16x4_t q1b;
  7641. ggml_int8x16x4_t q8b;
  7642. float sumf = 0;
  7643. for (int i = 0; i < nb; ++i) {
  7644. const int8_t * q8 = y[i].qs;
  7645. const uint8_t * qs = x[i].qs;
  7646. const uint16_t * qh = x[i].qh;
  7647. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7648. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7649. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7650. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7651. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7652. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7653. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7654. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7655. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7656. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7657. qs += 8;
  7658. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7659. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
  7660. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
  7661. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7662. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7663. sumi1 += vaddvq_s32(p1) * ls1;
  7664. sumi2 += vaddvq_s32(p2) * ls2;
  7665. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7666. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7667. }
  7668. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7669. }
  7670. *s = sumf;
  7671. #elif defined __AVX2__
  7672. __m256 accum = _mm256_setzero_ps();
  7673. float accum1 = 0;
  7674. for (int i = 0; i < nb; ++i) {
  7675. const int8_t * q8 = y[i].qs;
  7676. const uint8_t * qs = x[i].qs;
  7677. const uint16_t * qh = x[i].qh;
  7678. __m256i sumi = _mm256_setzero_si256();
  7679. int sumi1 = 0;
  7680. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7681. const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
  7682. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7683. const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
  7684. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7685. qs += 8;
  7686. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7687. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7688. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7689. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7690. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7691. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7692. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7693. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7694. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7695. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7696. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7697. }
  7698. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7699. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7700. accum1 += d * sumi1;
  7701. }
  7702. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7703. #else
  7704. float sumf = 0;
  7705. for (int i = 0; i < nb; i++) {
  7706. const int8_t * q8 = y[i].qs;
  7707. const uint8_t * qs = x[i].qs;
  7708. const uint16_t * qh = x[i].qh;
  7709. int sumi = 0, sumi1 = 0;
  7710. for (int ib = 0; ib < QK_K/32; ++ib) {
  7711. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7712. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7713. int lsum = 0;
  7714. for (int l = 0; l < 4; ++l) {
  7715. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7716. for (int j = 0; j < 8; ++j) {
  7717. lsum += q8[j] * grid[j];
  7718. }
  7719. q8 += 8;
  7720. }
  7721. sumi += ls * lsum;
  7722. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7723. qs += 4;
  7724. }
  7725. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7726. }
  7727. *s = sumf;
  7728. #endif
  7729. }
  7730. void ggml_vec_dot_iq1_m_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7731. assert(n % QK_K == 0);
  7732. assert(nrc == 1);
  7733. UNUSED(nrc);
  7734. UNUSED(bx);
  7735. UNUSED(by);
  7736. UNUSED(bs);
  7737. const block_iq1_m * restrict x = vx;
  7738. const block_q8_K * restrict y = vy;
  7739. const int nb = n / QK_K;
  7740. #if QK_K != 64
  7741. iq1m_scale_t scale;
  7742. #endif
  7743. #if defined __ARM_NEON
  7744. #if QK_K == 64
  7745. const int32x4_t mask = vdupq_n_s32(0xf);
  7746. #else
  7747. const int32x4_t mask = vdupq_n_s32(0x7);
  7748. #endif
  7749. const int32x4_t mone = vdupq_n_s32(1);
  7750. const int32x4_t mzero = vdupq_n_s32(0);
  7751. ggml_int8x16x4_t deltas;
  7752. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7753. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7754. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7755. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7756. ggml_int8x16x4_t q1b;
  7757. ggml_int8x16x4_t q8b;
  7758. uint32_t aux32;
  7759. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7760. float sumf = 0;
  7761. for (int i = 0; i < nb; ++i) {
  7762. const int8_t * q8 = y[i].qs;
  7763. const uint8_t * qs = x[i].qs;
  7764. const uint8_t * qh = x[i].qh;
  7765. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7766. #if QK_K != 64
  7767. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7768. #endif
  7769. int32x4_t sumi1 = mzero;
  7770. int32x4_t sumi2 = mzero;
  7771. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7772. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7773. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7774. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7775. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7776. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7777. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7778. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7779. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7780. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7781. const int32x4_t p1 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[0], q8b.val[0]), ggml_vdotq_s32(mzero, q1b.val[1], q8b.val[1]));
  7782. const int32x4_t p2 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[2], q8b.val[2]), ggml_vdotq_s32(mzero, q1b.val[3], q8b.val[3]));
  7783. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7784. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7785. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7786. const int32x4_t p3 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[0]], q8b.val[0]), ggml_vdotq_s32(mzero, deltas.val[aux8[1]], q8b.val[1]));
  7787. const int32x4_t p4 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[2]], q8b.val[2]), ggml_vdotq_s32(mzero, deltas.val[aux8[3]], q8b.val[3]));
  7788. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7789. #if QK_K == 64
  7790. int32x4_t scales_4 = ggml_vld1q_u32(sc[0] >> 0, sc[0] >> 4, sc[0] >> 8, sc[0] >> 12);
  7791. #else
  7792. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7793. #endif
  7794. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7795. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7796. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7797. qs += 8; qh += 4;
  7798. }
  7799. #if QK_K == 64
  7800. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7801. #else
  7802. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7803. #endif
  7804. }
  7805. *s = sumf;
  7806. #elif defined __AVX2__
  7807. #if QK_K == 64
  7808. const __m256i mask = _mm256_set1_epi16(0xf);
  7809. #else
  7810. const __m256i mask = _mm256_set1_epi16(0x7);
  7811. #endif
  7812. const __m256i mone = _mm256_set1_epi16(1);
  7813. __m256 accum1 = _mm256_setzero_ps();
  7814. __m256 accum2 = _mm256_setzero_ps();
  7815. for (int i = 0; i < nb; ++i) {
  7816. const int8_t * q8 = y[i].qs;
  7817. const uint8_t * qs = x[i].qs;
  7818. const uint8_t * qh = x[i].qh;
  7819. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7820. #if QK_K != 64
  7821. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7822. #endif
  7823. __m256i sumi1 = _mm256_setzero_si256();
  7824. __m256i sumi2 = _mm256_setzero_si256();
  7825. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7826. const __m256i q1b_1 = _mm256_set_epi64x(
  7827. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7828. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7829. );
  7830. const __m256i q1b_2 = _mm256_set_epi64x(
  7831. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7832. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7833. );
  7834. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7835. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7836. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7837. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7838. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7839. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7840. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7841. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7842. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7843. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7844. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7845. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7846. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7847. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7848. #if QK_K == 64
  7849. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 4), _mm_set1_epi16(sc[0] >> 0));
  7850. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 12), _mm_set1_epi16(sc[0] >> 8));
  7851. #else
  7852. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7853. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7854. #endif
  7855. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7856. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7857. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7858. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7859. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7860. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7861. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7862. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7863. qs += 8; qh += 4;
  7864. }
  7865. #if QK_K == 64
  7866. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d));
  7867. #else
  7868. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7869. #endif
  7870. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7871. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7872. }
  7873. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7874. #else
  7875. int sum1[2], sum2[2], delta[4];
  7876. float sumf = 0;
  7877. for (int i = 0; i < nb; i++) {
  7878. const int8_t * q8 = y[i].qs;
  7879. const uint8_t * qs = x[i].qs;
  7880. const uint8_t * qh = x[i].qh;
  7881. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7882. #if QK_K != 64
  7883. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7884. #endif
  7885. int sumi1 = 0, sumi2 = 0;
  7886. for (int ib = 0; ib < QK_K/32; ++ib) {
  7887. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7888. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7889. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7890. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7891. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7892. for (int l = 0; l < 4; ++l) {
  7893. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7894. int lsum1 = 0, lsum2 = 0;
  7895. for (int j = 0; j < 8; ++j) {
  7896. lsum1 += q8[j] * grid[j];
  7897. lsum2 += q8[j];
  7898. }
  7899. q8 += 8;
  7900. sum1[l/2] += lsum1;
  7901. sum2[l/2] += lsum2*delta[l];
  7902. }
  7903. #if QK_K == 64
  7904. const int ls1 = 2*((sc[0] >> (8*(ib%2)+0)) & 0xf) + 1;
  7905. const int ls2 = 2*((sc[0] >> (8*(ib%2)+4)) & 0xf) + 1;
  7906. #else
  7907. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7908. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7909. #endif
  7910. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7911. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7912. qs += 4;
  7913. qh += 2;
  7914. }
  7915. #if QK_K == 64
  7916. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7917. #else
  7918. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7919. #endif
  7920. }
  7921. *s = sumf;
  7922. #endif
  7923. }
  7924. void ggml_vec_dot_iq4_nl_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7925. assert(nrc == 1);
  7926. UNUSED(nrc);
  7927. UNUSED(bx);
  7928. UNUSED(by);
  7929. UNUSED(bs);
  7930. assert(n % QK4_NL == 0);
  7931. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7932. const block_iq4_nl * restrict x = vx;
  7933. const block_q8_0 * restrict y = vy;
  7934. const int nb = n / QK4_NL;
  7935. #if defined __ARM_NEON
  7936. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7937. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7938. uint8x16x2_t q4bits;
  7939. int8x16x4_t q4b;
  7940. int8x16x4_t q8b;
  7941. int32x4_t prod_1, prod_2;
  7942. float sumf = 0;
  7943. for (int ib = 0; ib < nb; ib += 2) {
  7944. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7945. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7946. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7947. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7948. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7949. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7950. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7951. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7952. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7953. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7954. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7955. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7956. sumf +=
  7957. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7958. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7959. }
  7960. *s = sumf;
  7961. #elif defined __AVX2__
  7962. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7963. const __m128i m4b = _mm_set1_epi8(0x0f);
  7964. const __m256i mone = _mm256_set1_epi16(1);
  7965. __m256 accum1 = _mm256_setzero_ps();
  7966. __m256 accum2 = _mm256_setzero_ps();
  7967. for (int ib = 0; ib < nb; ib += 2) {
  7968. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7969. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7970. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7971. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7972. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7973. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7974. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7975. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7976. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7977. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7978. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7979. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7980. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7981. _mm256_cvtepi32_ps(p_1), accum1);
  7982. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7983. _mm256_cvtepi32_ps(p_2), accum2);
  7984. y += 2;
  7985. x += 2;
  7986. }
  7987. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7988. #else
  7989. float sumf = 0;
  7990. for (int ib = 0; ib < nb; ++ib) {
  7991. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7992. int sumi1 = 0, sumi2 = 0;
  7993. for (int j = 0; j < QK4_NL/2; ++j) {
  7994. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7995. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7996. }
  7997. sumf += d * (sumi1 + sumi2);
  7998. }
  7999. *s = sumf;
  8000. #endif
  8001. }
  8002. void ggml_vec_dot_iq4_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  8003. assert(nrc == 1);
  8004. UNUSED(nrc);
  8005. UNUSED(bx);
  8006. UNUSED(by);
  8007. UNUSED(bs);
  8008. assert(n % QK_K == 0);
  8009. #if QK_K == 64
  8010. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  8011. #else
  8012. const block_iq4_xs * restrict x = vx;
  8013. const block_q8_K * restrict y = vy;
  8014. const int nb = n / QK_K;
  8015. #if defined __ARM_NEON
  8016. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  8017. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  8018. ggml_uint8x16x2_t q4bits;
  8019. ggml_int8x16x4_t q4b;
  8020. ggml_int8x16x4_t q8b;
  8021. int32x4_t prod_1, prod_2;
  8022. float sumf = 0;
  8023. for (int ibl = 0; ibl < nb; ++ibl) {
  8024. const int8_t * q8 = y[ibl].qs;
  8025. const uint8_t * q4 = x[ibl].qs;
  8026. uint16_t h = x[ibl].scales_h;
  8027. int sumi1 = 0, sumi2 = 0;
  8028. for (int ib = 0; ib < QK_K/64; ++ib) {
  8029. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  8030. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  8031. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  8032. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  8033. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  8034. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  8035. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  8036. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  8037. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  8038. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  8039. h >>= 4;
  8040. sumi1 += vaddvq_s32(prod_1) * ls1;
  8041. sumi2 += vaddvq_s32(prod_2) * ls2;
  8042. }
  8043. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  8044. }
  8045. *s = sumf;
  8046. #elif defined __AVX2__
  8047. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  8048. const __m128i m4b = _mm_set1_epi8(0x0f);
  8049. __m256 accum = _mm256_setzero_ps();
  8050. for (int ibl = 0; ibl < nb; ++ibl) {
  8051. const uint8_t * qs = x[ibl].qs;
  8052. const int8_t * q8 = y[ibl].qs;
  8053. uint16_t sh = x[ibl].scales_h;
  8054. __m256i sumi1 = _mm256_setzero_si256();
  8055. __m256i sumi2 = _mm256_setzero_si256();
  8056. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8057. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  8058. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  8059. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8060. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8061. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  8062. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  8063. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  8064. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  8065. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  8066. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  8067. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  8068. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  8069. sh >>= 4;
  8070. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  8071. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  8072. sumi1 = _mm256_add_epi32(p_1, sumi1);
  8073. sumi2 = _mm256_add_epi32(p_2, sumi2);
  8074. }
  8075. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  8076. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  8077. }
  8078. *s = hsum_float_8(accum);
  8079. #else
  8080. float sumf = 0;
  8081. for (int ibl = 0; ibl < nb; ++ibl) {
  8082. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  8083. uint16_t h = x[ibl].scales_h;
  8084. const uint8_t * qs = x[ibl].qs;
  8085. const int8_t * q8 = y[ibl].qs;
  8086. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8087. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  8088. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  8089. h >>= 4;
  8090. const float d1 = d4d8*(ls1 - 32);
  8091. const float d2 = d4d8*(ls2 - 32);
  8092. int sumi1 = 0, sumi2 = 0;
  8093. for (int j = 0; j < 16; ++j) {
  8094. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  8095. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  8096. }
  8097. sumf += d1 * (sumi1 + sumi2);
  8098. qs += 16;
  8099. q8 += 32;
  8100. sumi1 = sumi2 = 0;
  8101. for (int j = 0; j < 16; ++j) {
  8102. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  8103. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  8104. }
  8105. sumf += d2 * (sumi1 + sumi2);
  8106. qs += 16;
  8107. q8 += 32;
  8108. }
  8109. }
  8110. *s = sumf;
  8111. #endif
  8112. #endif
  8113. }
  8114. // ================================ IQ2 quantization =============================================
  8115. typedef struct {
  8116. uint64_t * grid;
  8117. int * map;
  8118. uint16_t * neighbours;
  8119. } iq2_entry_t;
  8120. static iq2_entry_t iq2_data[4] = {
  8121. {NULL, NULL, NULL},
  8122. {NULL, NULL, NULL},
  8123. {NULL, NULL, NULL},
  8124. {NULL, NULL, NULL},
  8125. };
  8126. static inline int iq2_data_index(enum ggml_type type) {
  8127. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  8128. return type == GGML_TYPE_IQ2_XXS ? 0 :
  8129. type == GGML_TYPE_IQ2_XS ? 1 :
  8130. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  8131. }
  8132. static inline int iq2_grid_size(enum ggml_type type) {
  8133. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  8134. return type == GGML_TYPE_IQ2_XXS ? 256 :
  8135. type == GGML_TYPE_IQ2_XS ? 512 :
  8136. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  8137. }
  8138. static int iq2_compare_func(const void * left, const void * right) {
  8139. const int * l = (const int *)left;
  8140. const int * r = (const int *)right;
  8141. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8142. }
  8143. void iq2xs_init_impl(enum ggml_type type) {
  8144. const int gindex = iq2_data_index(type);
  8145. const int grid_size = iq2_grid_size(type);
  8146. if (iq2_data[gindex].grid) {
  8147. return;
  8148. }
  8149. static const uint16_t kgrid_2bit_256[256] = {
  8150. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  8151. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  8152. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  8153. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  8154. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  8155. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  8156. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  8157. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  8158. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  8159. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  8160. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  8161. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  8162. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  8163. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  8164. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  8165. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  8166. };
  8167. static const uint16_t kgrid_2bit_512[512] = {
  8168. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8169. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  8170. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  8171. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  8172. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  8173. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  8174. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  8175. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  8176. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  8177. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  8178. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  8179. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  8180. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  8181. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  8182. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  8183. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  8184. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  8185. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  8186. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  8187. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  8188. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  8189. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  8190. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  8191. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  8192. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  8193. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  8194. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  8195. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  8196. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  8197. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  8198. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  8199. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  8200. };
  8201. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  8202. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  8203. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  8204. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  8205. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  8206. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  8207. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  8208. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  8209. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  8210. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  8211. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  8212. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  8213. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  8214. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  8215. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  8216. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  8217. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  8218. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  8219. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  8220. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  8221. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  8222. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  8223. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8224. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8225. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8226. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8227. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8228. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8229. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8230. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8231. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8232. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8233. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8234. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8235. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8236. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8237. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8238. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8239. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8240. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8241. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8242. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8243. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8244. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8245. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8246. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8247. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8248. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8249. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8250. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8251. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8252. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8253. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8254. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8255. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8256. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8257. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8258. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8259. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8260. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8261. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8262. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8263. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8264. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8265. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8266. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8267. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8268. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8269. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8270. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8271. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8272. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8273. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8274. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8275. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8276. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8277. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8278. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8279. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8280. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8281. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8282. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8283. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8284. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8285. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8286. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8287. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8288. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8289. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8290. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8291. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8292. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8293. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8294. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8295. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8296. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8297. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8298. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8299. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8300. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8301. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8302. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8303. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8304. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8305. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8306. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8307. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8308. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8309. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8310. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8311. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8312. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8313. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8314. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8315. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8316. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8317. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8318. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8319. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8320. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8321. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8322. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8323. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8324. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8325. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8326. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8327. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8328. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8329. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8330. };
  8331. static const uint16_t kgrid_2bit_1024[1024] = {
  8332. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8333. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8334. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8335. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8336. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8337. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8338. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8339. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8340. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8341. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8342. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8343. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8344. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8345. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8346. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8347. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8348. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8349. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8350. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8351. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8352. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8353. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8354. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8355. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8356. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8357. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8358. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8359. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8360. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8361. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8362. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8363. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8364. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8365. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8366. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8367. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8368. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8369. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8370. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8371. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8372. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8373. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8374. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8375. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8376. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8377. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8378. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8379. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8380. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8381. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8382. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8383. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8384. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8385. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8386. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8387. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8388. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8389. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8390. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8391. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8392. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8393. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8394. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8395. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8396. };
  8397. const int kmap_size = 43692;
  8398. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8399. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8400. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8401. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8402. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8403. uint64_t * kgrid_q2xs;
  8404. int * kmap_q2xs;
  8405. uint16_t * kneighbors_q2xs;
  8406. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8407. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8408. for (int k = 0; k < grid_size; ++k) {
  8409. int8_t * pos = (int8_t *)(the_grid + k);
  8410. for (int i = 0; i < 8; ++i) {
  8411. int l = (kgrid[k] >> 2*i) & 0x3;
  8412. pos[i] = 2*l + 1;
  8413. }
  8414. }
  8415. kgrid_q2xs = the_grid;
  8416. iq2_data[gindex].grid = the_grid;
  8417. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8418. iq2_data[gindex].map = kmap_q2xs;
  8419. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8420. uint64_t aux64;
  8421. uint8_t * aux8 = (uint8_t *)&aux64;
  8422. for (int i = 0; i < grid_size; ++i) {
  8423. aux64 = kgrid_q2xs[i];
  8424. uint16_t index = 0;
  8425. for (int k=0; k<8; ++k) {
  8426. uint16_t q = (aux8[k] - 1)/2;
  8427. index |= (q << 2*k);
  8428. }
  8429. kmap_q2xs[index] = i;
  8430. }
  8431. int8_t pos[8];
  8432. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8433. int num_neighbors = 0, num_not_in_map = 0;
  8434. for (int i = 0; i < kmap_size; ++i) {
  8435. if (kmap_q2xs[i] >= 0) continue;
  8436. ++num_not_in_map;
  8437. for (int k = 0; k < 8; ++k) {
  8438. int l = (i >> 2*k) & 0x3;
  8439. pos[k] = 2*l + 1;
  8440. }
  8441. for (int j = 0; j < grid_size; ++j) {
  8442. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8443. int d2 = 0;
  8444. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8445. dist2[2*j+0] = d2;
  8446. dist2[2*j+1] = j;
  8447. }
  8448. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8449. int n = 0; int d2 = dist2[0];
  8450. int nhave = 1;
  8451. for (int j = 0; j < grid_size; ++j) {
  8452. if (dist2[2*j] > d2) {
  8453. if (nhave == nwant) break;
  8454. d2 = dist2[2*j];
  8455. ++nhave;
  8456. }
  8457. ++n;
  8458. }
  8459. num_neighbors += n;
  8460. }
  8461. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8462. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8463. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8464. int counter = 0;
  8465. for (int i = 0; i < kmap_size; ++i) {
  8466. if (kmap_q2xs[i] >= 0) continue;
  8467. for (int k = 0; k < 8; ++k) {
  8468. int l = (i >> 2*k) & 0x3;
  8469. pos[k] = 2*l + 1;
  8470. }
  8471. for (int j = 0; j < grid_size; ++j) {
  8472. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8473. int d2 = 0;
  8474. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8475. dist2[2*j+0] = d2;
  8476. dist2[2*j+1] = j;
  8477. }
  8478. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8479. kmap_q2xs[i] = -(counter + 1);
  8480. int d2 = dist2[0];
  8481. uint16_t * start = &kneighbors_q2xs[counter++];
  8482. int n = 0, nhave = 1;
  8483. for (int j = 0; j < grid_size; ++j) {
  8484. if (dist2[2*j] > d2) {
  8485. if (nhave == nwant) break;
  8486. d2 = dist2[2*j];
  8487. ++nhave;
  8488. }
  8489. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8490. ++n;
  8491. }
  8492. *start = n;
  8493. }
  8494. free(dist2);
  8495. }
  8496. void iq2xs_free_impl(enum ggml_type type) {
  8497. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  8498. const int gindex = iq2_data_index(type);
  8499. if (iq2_data[gindex].grid) {
  8500. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8501. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8502. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8503. }
  8504. }
  8505. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8506. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8507. int num_neighbors = neighbours[0];
  8508. GGML_ASSERT(num_neighbors > 0);
  8509. float best_d2 = FLT_MAX;
  8510. int grid_index = -1;
  8511. for (int j = 1; j <= num_neighbors; ++j) {
  8512. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8513. float d2 = 0;
  8514. for (int i = 0; i < 8; ++i) {
  8515. float q = pg[i];
  8516. float diff = scale*q - xval[i];
  8517. d2 += weight[i]*diff*diff;
  8518. }
  8519. if (d2 < best_d2) {
  8520. best_d2 = d2; grid_index = neighbours[j];
  8521. }
  8522. }
  8523. GGML_ASSERT(grid_index >= 0);
  8524. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8525. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8526. return grid_index;
  8527. }
  8528. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8529. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8530. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8531. const int * kmap_q2xs = iq2_data[gindex].map;
  8532. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8533. GGML_ASSERT(quant_weights && "missing quantization weights");
  8534. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8535. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8536. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8537. GGML_ASSERT(n%QK_K == 0);
  8538. const int kMaxQ = 3;
  8539. const int nbl = n/QK_K;
  8540. block_iq2_xxs * y = vy;
  8541. float scales[QK_K/32];
  8542. float weight[32];
  8543. float xval[32];
  8544. int8_t L[32];
  8545. int8_t Laux[32];
  8546. float waux[32];
  8547. uint8_t block_signs[4];
  8548. uint32_t q2[2*(QK_K/32)];
  8549. for (int ibl = 0; ibl < nbl; ++ibl) {
  8550. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8551. memset(q2, 0, QK_K/4);
  8552. float max_scale = 0;
  8553. const float * xbl = x + QK_K*ibl;
  8554. float sumx2 = 0;
  8555. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8556. float sigma2 = sumx2/QK_K;
  8557. for (int ib = 0; ib < QK_K/32; ++ib) {
  8558. const float * xb = xbl + 32*ib;
  8559. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8560. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8561. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8562. for (int k = 0; k < 4; ++k) {
  8563. int nflip = 0;
  8564. uint8_t s = 0;
  8565. for (int i = 0; i < 8; ++i) {
  8566. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8567. else {
  8568. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8569. }
  8570. }
  8571. if (nflip%2) {
  8572. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8573. for (int i = 1; i < 8; ++i) {
  8574. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8575. if (ax < min) {
  8576. min = ax; imin = i;
  8577. }
  8578. }
  8579. xval[8*k+imin] = -xval[8*k+imin];
  8580. s ^= (1 << imin);
  8581. }
  8582. block_signs[k] = s & 127;
  8583. }
  8584. float max = xval[0];
  8585. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8586. if (!max) {
  8587. scales[ib] = 0;
  8588. memset(L, 0, 32);
  8589. continue;
  8590. }
  8591. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8592. float eff_max = scale*kMaxQ;
  8593. float best = 0;
  8594. for (int is = -6; is <= 6; ++is) {
  8595. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8596. float this_scale = 1/id;
  8597. for (int k = 0; k < 4; ++k) {
  8598. for (int i = 0; i < 8; ++i) {
  8599. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8600. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8601. }
  8602. uint16_t u = 0;
  8603. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8604. int grid_index = kmap_q2xs[u];
  8605. if (grid_index < 0) {
  8606. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8607. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8608. }
  8609. }
  8610. float sumqx = 0, sumq2 = 0;
  8611. for (int i = 0; i < 32; ++i) {
  8612. float w = weight[i];
  8613. float q = 2*Laux[i] + 1;
  8614. sumqx += w*xval[i]*q;
  8615. sumq2 += w*q*q;
  8616. }
  8617. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8618. scale = sumqx/sumq2; best = scale*sumqx;
  8619. memcpy(L, Laux, 32);
  8620. }
  8621. }
  8622. if (scale > 0) {
  8623. float id = 1/scale;
  8624. for (int k = 0; k < 4; ++k) {
  8625. uint16_t u = 0;
  8626. for (int i = 0; i < 8; ++i) {
  8627. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8628. l = MAX(0, MIN(kMaxQ-1, l));
  8629. u |= (l << 2*i);
  8630. }
  8631. int grid_index = kmap_q2xs[u];
  8632. if (grid_index < 0) {
  8633. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8634. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8635. }
  8636. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8637. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8638. }
  8639. float sumqx = 0, sumq2 = 0;
  8640. for (int i = 0; i < 32; ++i) {
  8641. float w = weight[i];
  8642. float q = 2*L[i] + 1;
  8643. sumqx += w*xval[i]*q;
  8644. sumq2 += w*q*q;
  8645. }
  8646. if (sumq2 > 0) scale = sumqx/sumq2;
  8647. }
  8648. if (scale < 0) {
  8649. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8650. // and correspondingly flip quant signs.
  8651. scale = -scale;
  8652. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8653. }
  8654. for (int k = 0; k < 4; ++k) {
  8655. uint16_t u = 0;
  8656. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8657. int grid_index = kmap_q2xs[u];
  8658. if (grid_index < 0) {
  8659. printf("Oops: found point %u not on grid:", u);
  8660. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8661. printf("\n");
  8662. GGML_ASSERT(false);
  8663. }
  8664. q2[2*ib+0] |= (grid_index << 8*k);
  8665. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8666. }
  8667. GGML_ASSERT(scale >= 0);
  8668. scales[ib] = scale;
  8669. max_scale = MAX(max_scale, scale);
  8670. }
  8671. if (!max_scale) {
  8672. memset(y[ibl].qs, 0, QK_K/4);
  8673. continue;
  8674. }
  8675. float d = max_scale/31;
  8676. y[ibl].d = GGML_FP32_TO_FP16(d);
  8677. float id = 1/d;
  8678. for (int ib = 0; ib < QK_K/32; ++ib) {
  8679. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8680. l = MAX(0, MIN(15, l));
  8681. q2[2*ib+1] |= ((uint32_t)l << 28);
  8682. }
  8683. memcpy(y[ibl].qs, q2, QK_K/4);
  8684. }
  8685. }
  8686. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8687. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8688. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8689. const int * kmap_q2xs = iq2_data[gindex].map;
  8690. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8691. GGML_ASSERT(quant_weights && "missing quantization weights");
  8692. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8693. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8694. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8695. GGML_ASSERT(n%QK_K == 0);
  8696. const int kMaxQ = 3;
  8697. const int nbl = n/QK_K;
  8698. block_iq2_xs * y = vy;
  8699. float scales[QK_K/16];
  8700. float weight[16];
  8701. float xval[16];
  8702. int8_t L[16];
  8703. int8_t Laux[16];
  8704. float waux[16];
  8705. bool is_on_grid[2];
  8706. bool is_on_grid_aux[2];
  8707. uint8_t block_signs[2];
  8708. uint16_t q2[2*(QK_K/16)];
  8709. for (int ibl = 0; ibl < nbl; ++ibl) {
  8710. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8711. memset(q2, 0, QK_K/4);
  8712. memset(y[ibl].scales, 0, QK_K/32);
  8713. float max_scale = 0;
  8714. const float * xbl = x + QK_K*ibl;
  8715. float sumx2 = 0;
  8716. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8717. float sigma2 = sumx2/QK_K;
  8718. for (int ib = 0; ib < QK_K/16; ++ib) {
  8719. const float * xb = xbl + 16*ib;
  8720. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8721. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8722. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8723. for (int k = 0; k < 2; ++k) {
  8724. int nflip = 0;
  8725. uint8_t s = 0;
  8726. for (int i = 0; i < 8; ++i) {
  8727. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8728. else {
  8729. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8730. }
  8731. }
  8732. if (nflip%2) {
  8733. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8734. for (int i = 1; i < 8; ++i) {
  8735. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8736. if (ax < min) {
  8737. min = ax; imin = i;
  8738. }
  8739. }
  8740. xval[8*k+imin] = -xval[8*k+imin];
  8741. s ^= (1 << imin);
  8742. }
  8743. block_signs[k] = s & 127;
  8744. }
  8745. float max = xval[0];
  8746. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8747. if (!max) {
  8748. scales[ib] = 0;
  8749. memset(L, 0, 16);
  8750. continue;
  8751. }
  8752. float best = 0;
  8753. float scale = max/(2*kMaxQ-1);
  8754. is_on_grid[0] = is_on_grid[1] = true;
  8755. for (int is = -9; is <= 9; ++is) {
  8756. float id = (2*kMaxQ-1+is*0.1f)/max;
  8757. float this_scale = 1/id;
  8758. for (int k = 0; k < 2; ++k) {
  8759. for (int i = 0; i < 8; ++i) {
  8760. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8761. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8762. }
  8763. uint16_t u = 0;
  8764. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8765. int grid_index = kmap_q2xs[u];
  8766. is_on_grid_aux[k] = true;
  8767. if (grid_index < 0) {
  8768. is_on_grid_aux[k] = false;
  8769. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8770. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8771. }
  8772. }
  8773. float sumqx = 0, sumq2 = 0;
  8774. for (int i = 0; i < 16; ++i) {
  8775. float w = weight[i];
  8776. float q = 2*Laux[i] + 1;
  8777. sumqx += w*xval[i]*q;
  8778. sumq2 += w*q*q;
  8779. }
  8780. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8781. scale = sumqx/sumq2; best = scale*sumqx;
  8782. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8783. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8784. }
  8785. }
  8786. int n_not_ongrid = 0;
  8787. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8788. if (n_not_ongrid > 0 && scale > 0) {
  8789. float id = 1/scale;
  8790. for (int k = 0; k < 2; ++k) {
  8791. if (is_on_grid[k]) continue;
  8792. uint16_t u = 0;
  8793. for (int i = 0; i < 8; ++i) {
  8794. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8795. l = MAX(0, MIN(kMaxQ-1, l));
  8796. u |= (l << 2*i);
  8797. L[8*k + i] = l;
  8798. }
  8799. int grid_index = kmap_q2xs[u];
  8800. if (grid_index < 0) {
  8801. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8802. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8803. }
  8804. }
  8805. float sumqx = 0, sumq2 = 0;
  8806. for (int i = 0; i < 16; ++i) {
  8807. float w = weight[i];
  8808. float q = 2*L[i] + 1;
  8809. sumqx += w*xval[i]*q;
  8810. sumq2 += w*q*q;
  8811. }
  8812. if (sumq2 > 0) scale = sumqx/sumq2;
  8813. }
  8814. if (scale < 0) {
  8815. scale = -scale;
  8816. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8817. }
  8818. for (int k = 0; k < 2; ++k) {
  8819. uint16_t u = 0;
  8820. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8821. int grid_index = kmap_q2xs[u];
  8822. if (grid_index < 0) {
  8823. printf("Oops: found point %u not on grid:", u);
  8824. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8825. printf("\n");
  8826. GGML_ASSERT(false);
  8827. }
  8828. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8829. }
  8830. GGML_ASSERT(scale >= 0);
  8831. scales[ib] = scale;
  8832. max_scale = MAX(max_scale, scale);
  8833. }
  8834. if (!max_scale) {
  8835. memset(y[ibl].qs, 0, QK_K/4);
  8836. continue;
  8837. }
  8838. float d = max_scale/31;
  8839. y[ibl].d = GGML_FP32_TO_FP16(d);
  8840. float id = 1/d;
  8841. for (int ib = 0; ib < QK_K/16; ++ib) {
  8842. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8843. l = MAX(0, MIN(15, l));
  8844. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8845. else y[ibl].scales[ib/2] |= (l << 4);
  8846. }
  8847. memcpy(y[ibl].qs, q2, QK_K/4);
  8848. }
  8849. }
  8850. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8851. GGML_ASSERT(n_per_row%QK_K == 0);
  8852. int nblock = n_per_row/QK_K;
  8853. char * qrow = (char *)dst;
  8854. for (int row = 0; row < nrow; ++row) {
  8855. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8856. src += n_per_row;
  8857. qrow += nblock*sizeof(block_iq2_xxs);
  8858. }
  8859. return nrow * nblock * sizeof(block_iq2_xxs);
  8860. }
  8861. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8862. GGML_ASSERT(n_per_row%QK_K == 0);
  8863. int nblock = n_per_row/QK_K;
  8864. char * qrow = (char *)dst;
  8865. for (int row = 0; row < nrow; ++row) {
  8866. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8867. src += n_per_row;
  8868. qrow += nblock*sizeof(block_iq2_xs);
  8869. }
  8870. return nrow * nblock * sizeof(block_iq2_xs);
  8871. }
  8872. //
  8873. // ============================================= 3-bit using D4 lattice
  8874. //
  8875. typedef struct {
  8876. uint32_t * grid;
  8877. int * map;
  8878. uint16_t * neighbours;
  8879. } iq3_entry_t;
  8880. static iq3_entry_t iq3_data[2] = {
  8881. {NULL, NULL, NULL},
  8882. {NULL, NULL, NULL},
  8883. };
  8884. static inline int iq3_data_index(int grid_size) {
  8885. (void)grid_size;
  8886. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8887. return grid_size == 256 ? 0 : 1;
  8888. }
  8889. static int iq3_compare_func(const void * left, const void * right) {
  8890. const int * l = (const int *)left;
  8891. const int * r = (const int *)right;
  8892. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8893. }
  8894. void iq3xs_init_impl(int grid_size) {
  8895. const int gindex = iq3_data_index(grid_size);
  8896. if (iq3_data[gindex].grid) {
  8897. return;
  8898. }
  8899. static const uint16_t kgrid_256[256] = {
  8900. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8901. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8902. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8903. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8904. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8905. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8906. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8907. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8908. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8909. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8910. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8911. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8912. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8913. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8914. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8915. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8916. };
  8917. static const uint16_t kgrid_512[512] = {
  8918. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8919. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8920. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8921. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8922. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8923. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8924. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8925. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8926. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8927. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8928. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8929. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8930. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8931. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8932. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8933. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8934. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8935. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8936. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8937. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8938. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8939. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8940. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8941. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8942. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8943. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8944. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8945. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8946. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8947. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8948. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8949. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8950. };
  8951. const int kmap_size = 4096;
  8952. const int nwant = grid_size == 256 ? 2 : 3;
  8953. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8954. uint32_t * kgrid_q3xs;
  8955. int * kmap_q3xs;
  8956. uint16_t * kneighbors_q3xs;
  8957. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8958. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8959. for (int k = 0; k < grid_size; ++k) {
  8960. int8_t * pos = (int8_t *)(the_grid + k);
  8961. for (int i = 0; i < 4; ++i) {
  8962. int l = (kgrid[k] >> 3*i) & 0x7;
  8963. pos[i] = 2*l + 1;
  8964. }
  8965. }
  8966. kgrid_q3xs = the_grid;
  8967. iq3_data[gindex].grid = the_grid;
  8968. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8969. iq3_data[gindex].map = kmap_q3xs;
  8970. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8971. uint32_t aux32;
  8972. uint8_t * aux8 = (uint8_t *)&aux32;
  8973. for (int i = 0; i < grid_size; ++i) {
  8974. aux32 = kgrid_q3xs[i];
  8975. uint16_t index = 0;
  8976. for (int k=0; k<4; ++k) {
  8977. uint16_t q = (aux8[k] - 1)/2;
  8978. index |= (q << 3*k);
  8979. }
  8980. kmap_q3xs[index] = i;
  8981. }
  8982. int8_t pos[4];
  8983. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8984. int num_neighbors = 0, num_not_in_map = 0;
  8985. for (int i = 0; i < kmap_size; ++i) {
  8986. if (kmap_q3xs[i] >= 0) continue;
  8987. ++num_not_in_map;
  8988. for (int k = 0; k < 4; ++k) {
  8989. int l = (i >> 3*k) & 0x7;
  8990. pos[k] = 2*l + 1;
  8991. }
  8992. for (int j = 0; j < grid_size; ++j) {
  8993. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8994. int d2 = 0;
  8995. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8996. dist2[2*j+0] = d2;
  8997. dist2[2*j+1] = j;
  8998. }
  8999. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  9000. int n = 0; int d2 = dist2[0];
  9001. int nhave = 1;
  9002. for (int j = 0; j < grid_size; ++j) {
  9003. if (dist2[2*j] > d2) {
  9004. if (nhave == nwant) break;
  9005. d2 = dist2[2*j];
  9006. ++nhave;
  9007. }
  9008. ++n;
  9009. }
  9010. num_neighbors += n;
  9011. }
  9012. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  9013. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  9014. iq3_data[gindex].neighbours = kneighbors_q3xs;
  9015. int counter = 0;
  9016. for (int i = 0; i < kmap_size; ++i) {
  9017. if (kmap_q3xs[i] >= 0) continue;
  9018. for (int k = 0; k < 4; ++k) {
  9019. int l = (i >> 3*k) & 0x7;
  9020. pos[k] = 2*l + 1;
  9021. }
  9022. for (int j = 0; j < grid_size; ++j) {
  9023. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  9024. int d2 = 0;
  9025. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  9026. dist2[2*j+0] = d2;
  9027. dist2[2*j+1] = j;
  9028. }
  9029. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  9030. kmap_q3xs[i] = -(counter + 1);
  9031. int d2 = dist2[0];
  9032. uint16_t * start = &kneighbors_q3xs[counter++];
  9033. int n = 0, nhave = 1;
  9034. for (int j = 0; j < grid_size; ++j) {
  9035. if (dist2[2*j] > d2) {
  9036. if (nhave == nwant) break;
  9037. d2 = dist2[2*j];
  9038. ++nhave;
  9039. }
  9040. kneighbors_q3xs[counter++] = dist2[2*j+1];
  9041. ++n;
  9042. }
  9043. *start = n;
  9044. }
  9045. free(dist2);
  9046. }
  9047. void iq3xs_free_impl(int grid_size) {
  9048. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  9049. const int gindex = iq3_data_index(grid_size);
  9050. if (iq3_data[gindex].grid) {
  9051. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  9052. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  9053. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  9054. }
  9055. }
  9056. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  9057. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  9058. int num_neighbors = neighbours[0];
  9059. GGML_ASSERT(num_neighbors > 0);
  9060. float best_d2 = FLT_MAX;
  9061. int grid_index = -1;
  9062. for (int j = 1; j <= num_neighbors; ++j) {
  9063. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9064. float d2 = 0;
  9065. for (int i = 0; i < 4; ++i) {
  9066. float q = pg[i];
  9067. float diff = scale*q - xval[i];
  9068. d2 += weight[i]*diff*diff;
  9069. }
  9070. if (d2 < best_d2) {
  9071. best_d2 = d2; grid_index = neighbours[j];
  9072. }
  9073. }
  9074. GGML_ASSERT(grid_index >= 0);
  9075. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9076. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  9077. return grid_index;
  9078. }
  9079. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int n,
  9080. const float * restrict quant_weights) {
  9081. const int gindex = iq3_data_index(grid_size);
  9082. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9083. const int * kmap_q3xs = iq3_data[gindex].map;
  9084. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9085. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9086. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9087. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9088. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9089. GGML_ASSERT(n%QK_K == 0);
  9090. const int kMaxQ = 8;
  9091. const int nbl = n/QK_K;
  9092. ggml_fp16_t * dh;
  9093. uint8_t * qs;
  9094. int block_size;
  9095. if (grid_size == 256) {
  9096. block_iq3_xxs * y = vy;
  9097. dh = &y->d;
  9098. qs = y->qs;
  9099. block_size = sizeof(block_iq3_xxs);
  9100. } else {
  9101. block_iq3_s * y = vy;
  9102. dh = &y->d;
  9103. qs = y->qs;
  9104. block_size = sizeof(block_iq3_s);
  9105. }
  9106. int quant_size = block_size - sizeof(ggml_fp16_t);
  9107. float scales[QK_K/32];
  9108. float weight[32];
  9109. float xval[32];
  9110. int8_t L[32];
  9111. int8_t Laux[32];
  9112. float waux[32];
  9113. bool is_on_grid[8];
  9114. bool is_on_grid_aux[8];
  9115. uint8_t block_signs[8];
  9116. uint8_t q3[3*(QK_K/8)+QK_K/32];
  9117. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  9118. uint8_t * qh = q3 + 3*(QK_K/8);
  9119. for (int ibl = 0; ibl < nbl; ++ibl) {
  9120. dh[0] = GGML_FP32_TO_FP16(0.f);
  9121. memset(q3, 0, 3*QK_K/8+QK_K/32);
  9122. float max_scale = 0;
  9123. const float * xbl = x + QK_K*ibl;
  9124. float sumx2 = 0;
  9125. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9126. float sigma2 = 2*sumx2/QK_K;
  9127. for (int ib = 0; ib < QK_K/32; ++ib) {
  9128. const float * xb = xbl + 32*ib;
  9129. if (quant_weights) {
  9130. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  9131. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9132. } else {
  9133. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  9134. }
  9135. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  9136. for (int k = 0; k < 4; ++k) {
  9137. int nflip = 0;
  9138. uint8_t s = 0;
  9139. for (int i = 0; i < 8; ++i) {
  9140. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9141. else {
  9142. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  9143. }
  9144. }
  9145. if (nflip%2) {
  9146. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  9147. for (int i = 1; i < 8; ++i) {
  9148. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  9149. if (ax < min) {
  9150. min = ax; imin = i;
  9151. }
  9152. }
  9153. xval[8*k+imin] = -xval[8*k+imin];
  9154. s ^= (1 << imin);
  9155. }
  9156. block_signs[k] = s & 127;
  9157. }
  9158. float max = xval[0];
  9159. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  9160. if (!max) {
  9161. scales[ib] = 0;
  9162. memset(L, 0, 32);
  9163. continue;
  9164. }
  9165. float best = 0;
  9166. float scale = max/(2*kMaxQ-1);
  9167. for (int is = -15; is <= 15; ++is) {
  9168. float id = (2*kMaxQ-1+is*0.2f)/max;
  9169. float this_scale = 1/id;
  9170. for (int k = 0; k < 8; ++k) {
  9171. for (int i = 0; i < 4; ++i) {
  9172. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9173. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9174. }
  9175. uint16_t u = 0;
  9176. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9177. int grid_index = kmap_q3xs[u];
  9178. is_on_grid_aux[k] = true;
  9179. if (grid_index < 0) {
  9180. is_on_grid_aux[k] = false;
  9181. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9182. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9183. }
  9184. }
  9185. float sumqx = 0, sumq2 = 0;
  9186. for (int i = 0; i < 32; ++i) {
  9187. float w = weight[i];
  9188. float q = 2*Laux[i] + 1;
  9189. sumqx += w*xval[i]*q;
  9190. sumq2 += w*q*q;
  9191. }
  9192. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9193. scale = sumqx/sumq2; best = scale*sumqx;
  9194. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  9195. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9196. }
  9197. }
  9198. int n_not_ongrid = 0;
  9199. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9200. if (n_not_ongrid > 0 && scale > 0) {
  9201. float id = 1/scale;
  9202. for (int k = 0; k < 8; ++k) {
  9203. if (is_on_grid[k]) continue;
  9204. uint16_t u = 0;
  9205. for (int i = 0; i < 4; ++i) {
  9206. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9207. l = MAX(0, MIN(kMaxQ-1, l));
  9208. u |= (l << 3*i);
  9209. }
  9210. int grid_index = kmap_q3xs[u];
  9211. if (grid_index < 0) {
  9212. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9213. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9214. }
  9215. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9216. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9217. }
  9218. float sumqx = 0, sumq2 = 0;
  9219. for (int i = 0; i < 32; ++i) {
  9220. float w = weight[i];
  9221. float q = 2*L[i] + 1;
  9222. sumqx += w*xval[i]*q;
  9223. sumq2 += w*q*q;
  9224. }
  9225. if (sumq2 > 0) scale = sumqx/sumq2;
  9226. }
  9227. if (scale < 0) {
  9228. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9229. // and correspondingly flip quant signs.
  9230. scale = -scale;
  9231. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9232. }
  9233. for (int k = 0; k < 8; ++k) {
  9234. uint16_t u = 0;
  9235. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9236. int grid_index = kmap_q3xs[u];
  9237. if (grid_index < 0) {
  9238. printf("Oops: found point %u not on grid:", u);
  9239. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9240. printf("\n");
  9241. GGML_ASSERT(false);
  9242. }
  9243. if (grid_size == 256) {
  9244. q3[8*ib+k] = grid_index;
  9245. } else {
  9246. q3[8*ib+k] = grid_index & 255;
  9247. qh[ib] |= ((grid_index >> 8) << k);
  9248. }
  9249. }
  9250. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9251. GGML_ASSERT(scale >= 0);
  9252. scales[ib] = scale;
  9253. max_scale = MAX(max_scale, scale);
  9254. }
  9255. if (!max_scale) {
  9256. memset(qs, 0, quant_size);
  9257. dh += block_size/sizeof(ggml_fp16_t);
  9258. qs += block_size;
  9259. continue;
  9260. }
  9261. float d = max_scale/31;
  9262. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9263. float id = 1/d;
  9264. for (int ib = 0; ib < QK_K/32; ++ib) {
  9265. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9266. l = MAX(0, MIN(15, l));
  9267. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9268. }
  9269. memcpy(qs, q3, quant_size);
  9270. dh += block_size/sizeof(ggml_fp16_t);
  9271. qs += block_size;
  9272. }
  9273. }
  9274. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9275. GGML_ASSERT(n_per_row%QK_K == 0);
  9276. int nblock = n_per_row/QK_K;
  9277. char * qrow = (char *)dst;
  9278. for (int row = 0; row < nrow; ++row) {
  9279. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9280. src += n_per_row;
  9281. qrow += nblock*sizeof(block_iq3_xxs);
  9282. }
  9283. return nrow * nblock * sizeof(block_iq3_xxs);
  9284. }
  9285. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  9286. assert(k % QK_K == 0);
  9287. block_iq3_xxs * restrict y = vy;
  9288. quantize_row_iq3_xxs_reference(x, y, k);
  9289. }
  9290. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  9291. assert(k % QK_K == 0);
  9292. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9293. }
  9294. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9295. const float * restrict quant_weights,
  9296. float * scales,
  9297. float * weight,
  9298. float * xval,
  9299. int8_t * L,
  9300. int8_t * Laux,
  9301. float * waux,
  9302. bool * is_on_grid,
  9303. bool * is_on_grid_aux,
  9304. uint8_t * block_signs) {
  9305. const int gindex = iq3_data_index(512);
  9306. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9307. const int * kmap_q3xs = iq3_data[gindex].map;
  9308. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9309. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9310. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9311. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9312. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9313. GGML_ASSERT(n%QK_K == 0);
  9314. const int kMaxQ = 8;
  9315. const int nbl = n/QK_K;
  9316. block_iq3_s * y = vy;
  9317. const int bs4 = block_size/4;
  9318. const int bs8 = block_size/8;
  9319. for (int ibl = 0; ibl < nbl; ++ibl) {
  9320. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9321. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9322. uint8_t * qs = y[ibl].qs;
  9323. uint8_t * qh = y[ibl].qh;
  9324. uint8_t * signs = y[ibl].signs;
  9325. float max_scale = 0;
  9326. const float * xbl = x + QK_K*ibl;
  9327. float sumx2 = 0;
  9328. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9329. float sigma2 = 2*sumx2/QK_K;
  9330. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9331. const float * xb = xbl + block_size*ib;
  9332. if (quant_weights) {
  9333. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9334. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9335. } else {
  9336. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9337. }
  9338. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9339. for (int k = 0; k < bs8; ++k) {
  9340. uint8_t s = 0;
  9341. for (int i = 0; i < 8; ++i) {
  9342. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9343. else {
  9344. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9345. }
  9346. }
  9347. block_signs[k] = s;
  9348. }
  9349. float max = xval[0];
  9350. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9351. if (!max) {
  9352. scales[ib] = 0;
  9353. continue;
  9354. }
  9355. float best = 0;
  9356. float scale = max/(2*kMaxQ-1);
  9357. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9358. for (int is = -9; is <= 9; ++is) {
  9359. float id = (2*kMaxQ-1+is*0.2f)/max;
  9360. float this_scale = 1/id;
  9361. for (int k = 0; k < bs4; ++k) {
  9362. for (int i = 0; i < 4; ++i) {
  9363. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9364. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9365. }
  9366. uint16_t u = 0;
  9367. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9368. int grid_index = kmap_q3xs[u];
  9369. is_on_grid_aux[k] = true;
  9370. if (grid_index < 0) {
  9371. is_on_grid_aux[k] = false;
  9372. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9373. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9374. }
  9375. }
  9376. float sumqx = 0, sumq2 = 0;
  9377. for (int i = 0; i < block_size; ++i) {
  9378. float w = weight[i];
  9379. float q = 2*Laux[i] + 1;
  9380. sumqx += w*xval[i]*q;
  9381. sumq2 += w*q*q;
  9382. }
  9383. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9384. scale = sumqx/sumq2; best = scale*sumqx;
  9385. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9386. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9387. }
  9388. }
  9389. int n_not_ongrid = 0;
  9390. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9391. if (n_not_ongrid > 0 && scale > 0) {
  9392. float id = 1/scale;
  9393. for (int k = 0; k < bs4; ++k) {
  9394. //if (is_on_grid[k]) continue;
  9395. uint16_t u = 0;
  9396. for (int i = 0; i < 4; ++i) {
  9397. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9398. l = MAX(0, MIN(kMaxQ-1, l));
  9399. u |= (l << 3*i);
  9400. }
  9401. int grid_index = kmap_q3xs[u];
  9402. if (grid_index < 0) {
  9403. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9404. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9405. }
  9406. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9407. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9408. }
  9409. float sumqx = 0, sumq2 = 0;
  9410. for (int i = 0; i < block_size; ++i) {
  9411. float w = weight[i];
  9412. float q = 2*L[i] + 1;
  9413. sumqx += w*xval[i]*q;
  9414. sumq2 += w*q*q;
  9415. }
  9416. if (sumq2 > 0) scale = sumqx/sumq2;
  9417. }
  9418. if (scale < 0) {
  9419. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9420. // and correspondingly flip quant signs.
  9421. scale = -scale;
  9422. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9423. }
  9424. for (int k = 0; k < bs4; ++k) {
  9425. uint16_t u = 0;
  9426. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9427. int grid_index = kmap_q3xs[u];
  9428. if (grid_index < 0) {
  9429. printf("Oops: found point %u not on grid:", u);
  9430. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9431. printf("\n");
  9432. GGML_ASSERT(false);
  9433. }
  9434. qs[k] = grid_index & 255;
  9435. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9436. }
  9437. qs += bs4;
  9438. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9439. signs += bs8;
  9440. GGML_ASSERT(scale >= 0);
  9441. scales[ib] = scale;
  9442. max_scale = MAX(max_scale, scale);
  9443. }
  9444. if (!max_scale) {
  9445. continue;
  9446. }
  9447. float d = max_scale/31;
  9448. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9449. float id = 1/d;
  9450. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9451. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9452. l1 = MAX(0, MIN(15, l1));
  9453. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9454. l2 = MAX(0, MIN(15, l2));
  9455. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9456. }
  9457. }
  9458. }
  9459. #define IQ3S_BLOCK_SIZE 32
  9460. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9461. GGML_ASSERT(n_per_row%QK_K == 0);
  9462. int nblock = n_per_row/QK_K;
  9463. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9464. float weight[IQ3S_BLOCK_SIZE];
  9465. float xval[IQ3S_BLOCK_SIZE];
  9466. int8_t L[IQ3S_BLOCK_SIZE];
  9467. int8_t Laux[IQ3S_BLOCK_SIZE];
  9468. float waux[IQ3S_BLOCK_SIZE];
  9469. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9470. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9471. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9472. char * qrow = (char *)dst;
  9473. for (int row = 0; row < nrow; ++row) {
  9474. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9475. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9476. src += n_per_row;
  9477. qrow += nblock*sizeof(block_iq3_s);
  9478. }
  9479. return nrow * nblock * sizeof(block_iq3_s);
  9480. }
  9481. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int k) {
  9482. assert(k % QK_K == 0);
  9483. block_iq3_s * restrict y = vy;
  9484. quantize_row_iq3_s_reference(x, y, k);
  9485. }
  9486. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int k) {
  9487. assert(k % QK_K == 0);
  9488. quantize_iq3_s(x, y, 1, k, NULL);
  9489. }
  9490. // =================================== 1.5 bpw ===================================================
  9491. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9492. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9493. int num_neighbors = neighbours[0];
  9494. GGML_ASSERT(num_neighbors > 0);
  9495. float best_score = 0;
  9496. int grid_index = -1;
  9497. for (int j = 1; j <= num_neighbors; ++j) {
  9498. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9499. float sumqx = 0, sumq2 = 0;
  9500. for (int i = 0; i < 8; ++i) {
  9501. float q = (pg[i] - 3)/2;
  9502. float w = weight[i];
  9503. sumqx += w*q*xval[i];
  9504. sumq2 += w*q*q;
  9505. }
  9506. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9507. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9508. grid_index = neighbours[j];
  9509. }
  9510. }
  9511. if (grid_index < 0) {
  9512. for (int i = 0; i < ngrid; ++i) {
  9513. const int8_t * grid_i = (const int8_t *)(grid + i);
  9514. float sumqx = 0, sumq2 = 0;
  9515. for (int j = 0; j < 8; ++j) {
  9516. float w = weight[j];
  9517. float q = (grid_i[j] - 3)/2;
  9518. sumqx += w*q*xval[j];
  9519. sumq2 += w*q*q;
  9520. }
  9521. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9522. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9523. grid_index = i;
  9524. }
  9525. }
  9526. }
  9527. if (grid_index < 0) {
  9528. printf("Oops, did not find grid point\n");
  9529. printf("Have %d neighbours\n", num_neighbors);
  9530. for (int j = 1; j <= num_neighbors; ++j) {
  9531. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9532. float sumqx = 0, sumq2 = 0;
  9533. for (int i = 0; i < 8; ++i) {
  9534. float q = (pg[i] - 3)/2;
  9535. float w = weight[i];
  9536. sumqx += w*q*xval[i];
  9537. sumq2 += w*q*q;
  9538. }
  9539. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9540. }
  9541. }
  9542. GGML_ASSERT(grid_index >= 0);
  9543. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9544. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9545. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9546. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9547. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9548. return grid_index;
  9549. }
  9550. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9551. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9552. int num_neighbors = neighbours[0];
  9553. GGML_ASSERT(num_neighbors > 0);
  9554. float best_score = FLT_MAX;
  9555. int grid_index = -1;
  9556. for (int j = 1; j <= num_neighbors; ++j) {
  9557. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9558. float d2 = 0;
  9559. for (int i = 0; i < 8; ++i) {
  9560. float q = xg[(pg[i] - 1)/2];
  9561. float w = weight[i];
  9562. float diff = scale*q - xval[i];
  9563. d2 += w*diff*diff;
  9564. }
  9565. if (d2 < best_score) {
  9566. best_score = d2;
  9567. grid_index = neighbours[j];
  9568. }
  9569. }
  9570. if (grid_index < 0) {
  9571. for (int i = 0; i < ngrid; ++i) {
  9572. const int8_t * grid_i = (const int8_t *)(grid + i);
  9573. float d2 = 0;
  9574. for (int j = 0; j < 8; ++j) {
  9575. float w = weight[j];
  9576. float q = xg[(grid_i[j] - 1)/2];
  9577. float diff = scale*q - xval[i];
  9578. d2 += w*diff*diff;
  9579. }
  9580. if (d2 < best_score) {
  9581. best_score = d2;
  9582. grid_index = i;
  9583. }
  9584. }
  9585. }
  9586. if (grid_index < 0) {
  9587. printf("Oops, did not find grid point\n");
  9588. printf("Have %d neighbours\n", num_neighbors);
  9589. for (int j = 1; j <= num_neighbors; ++j) {
  9590. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9591. float sumqx = 0, sumq2 = 0;
  9592. for (int i = 0; i < 8; ++i) {
  9593. float q = xg[(pg[i] - 1)/2];
  9594. float w = weight[i];
  9595. sumqx += w*q*xval[i];
  9596. sumq2 += w*q*q;
  9597. }
  9598. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9599. }
  9600. }
  9601. GGML_ASSERT(grid_index >= 0);
  9602. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9603. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9604. return grid_index;
  9605. }
  9606. static int iq1_sort_helper(const void * left, const void * right) {
  9607. const float * l = left;
  9608. const float * r = right;
  9609. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9610. }
  9611. #define IQ1S_BLOCK_SIZE 32
  9612. #define IQ1M_BLOCK_SIZE 16
  9613. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights,
  9614. float * scales,
  9615. float * weight,
  9616. float * sumx,
  9617. float * sumw,
  9618. float * pairs,
  9619. int8_t * L,
  9620. uint16_t * index,
  9621. int8_t * shifts) {
  9622. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9623. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9624. const int * kmap_q2xs = iq2_data[gindex].map;
  9625. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9626. GGML_ASSERT(quant_weights && "missing quantization weights");
  9627. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9628. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9629. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9630. GGML_ASSERT(n%QK_K == 0);
  9631. block_iq1_s * y = vy;
  9632. const int nbl = n/QK_K;
  9633. const int block_size = IQ1S_BLOCK_SIZE;
  9634. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9635. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9636. int * idx = (int *)(pairs + 1);
  9637. for (int ibl = 0; ibl < nbl; ++ibl) {
  9638. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9639. memset(y[ibl].qs, 0, QK_K/8);
  9640. memset(y[ibl].qh, 0, QK_K/16);
  9641. float max_scale = 0;
  9642. const float * xbl = x + QK_K*ibl;
  9643. float sumx2 = 0;
  9644. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9645. float sigma2 = 2*sumx2/QK_K;
  9646. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9647. const float * xb = xbl + block_size*ib;
  9648. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9649. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9650. float max = fabsf(xb[0]);
  9651. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9652. if (!max) {
  9653. scales[ib] = 0;
  9654. memset(L, 1, block_size);
  9655. continue;
  9656. }
  9657. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9658. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9659. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9660. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9661. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9662. // for each possible and score for each split.
  9663. for (int j = 0; j < block_size; ++j) {
  9664. pairs[2*j] = xb[j];
  9665. idx[2*j] = j;
  9666. }
  9667. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9668. {
  9669. sumx[0] = sumw[0] = 0;
  9670. for (int j = 0; j < block_size; ++j) {
  9671. int i = idx[2*j];
  9672. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9673. sumw[j+1] = sumw[j] + weight[i];
  9674. }
  9675. }
  9676. float best_score = 0, scale = max;
  9677. int besti1 = -1, besti2 = -1, best_shift = 0;
  9678. for (int i1 = 0; i1 <= block_size; ++i1) {
  9679. for (int i2 = i1; i2 <= block_size; ++i2) {
  9680. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9681. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
  9682. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9683. scale = sumqx/sumq2; best_score = scale*sumqx;
  9684. besti1 = i1; besti2 = i2; best_shift = 1;
  9685. }
  9686. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9687. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
  9688. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9689. scale = sumqx/sumq2; best_score = scale*sumqx;
  9690. besti1 = i1; besti2 = i2; best_shift = -1;
  9691. }
  9692. }
  9693. }
  9694. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9695. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9696. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9697. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9698. if (scale < 0) {
  9699. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9700. scale = -scale; best_shift = -best_shift;
  9701. }
  9702. bool all_on_grid = true;
  9703. const float * xx = best_shift == 1 ? x_p : x_m;
  9704. for (int k = 0; k < block_size/8; ++k) {
  9705. uint16_t u = 0;
  9706. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9707. int grid_index = kmap_q2xs[u];
  9708. if (grid_index < 0) {
  9709. all_on_grid = false;
  9710. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9711. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9712. GGML_ASSERT(grid_index >= 0);
  9713. }
  9714. index[k] = grid_index;
  9715. }
  9716. if (!all_on_grid) {
  9717. float sumqx = 0, sumq2 = 0;
  9718. for (int k = 0; k < block_size/8; ++k) {
  9719. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9720. for (int j = 0; j < 8; ++j) {
  9721. float w = weight[8*k + j];
  9722. float q = xx[(pg[j] - 1)/2];
  9723. sumqx += w*q*xb[8*k+j];
  9724. sumq2 += w*q*q;
  9725. }
  9726. }
  9727. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9728. }
  9729. uint16_t h = 0;
  9730. for (int k = 0; k < block_size/8; ++k) {
  9731. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9732. h |= (index[k] >> 8) << 3*k;
  9733. }
  9734. y[ibl].qh[ib] = h;
  9735. GGML_ASSERT(scale >= 0);
  9736. scales[ib] = scale;
  9737. shifts[ib] = best_shift;
  9738. max_scale = MAX(max_scale, scale);
  9739. }
  9740. if (!max_scale) {
  9741. continue;
  9742. }
  9743. float d = max_scale/15;
  9744. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9745. float id = 1/d;
  9746. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9747. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9748. l = MAX(0, MIN(7, l));
  9749. if (shifts[ib] == -1) l |= 8;
  9750. y[ibl].qh[ib] |= (l << 12);
  9751. }
  9752. }
  9753. }
  9754. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9755. GGML_ASSERT(n_per_row%QK_K == 0);
  9756. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9757. float weight[IQ1S_BLOCK_SIZE];
  9758. int8_t L[IQ1S_BLOCK_SIZE];
  9759. float sumx[IQ1S_BLOCK_SIZE+1];
  9760. float sumw[IQ1S_BLOCK_SIZE+1];
  9761. float pairs[2*IQ1S_BLOCK_SIZE];
  9762. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9763. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9764. int nblock = n_per_row/QK_K;
  9765. char * qrow = (char *)dst;
  9766. for (int row = 0; row < nrow; ++row) {
  9767. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9768. src += n_per_row;
  9769. qrow += nblock*sizeof(block_iq1_s);
  9770. }
  9771. return nrow * nblock * sizeof(block_iq1_s);
  9772. }
  9773. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights,
  9774. float * scales,
  9775. float * weight,
  9776. float * pairs,
  9777. int8_t * L,
  9778. uint16_t * index,
  9779. int8_t * shifts) {
  9780. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9781. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9782. const int * kmap_q2xs = iq2_data[gindex].map;
  9783. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9784. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9785. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9786. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9787. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9788. GGML_ASSERT(n%QK_K == 0);
  9789. block_iq1_m * y = vy;
  9790. const int nbl = n/QK_K;
  9791. const int block_size = IQ1M_BLOCK_SIZE;
  9792. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9793. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9794. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9795. int * idx = (int *)(pairs + 1);
  9796. float sumqx[4], sumq2[4];
  9797. iq1m_scale_t s;
  9798. const float * xx;
  9799. for (int ibl = 0; ibl < nbl; ++ibl) {
  9800. #if QK_K == 64
  9801. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9802. #endif
  9803. memset(y[ibl].qs, 0, QK_K/8);
  9804. memset(y[ibl].qh, 0, QK_K/16);
  9805. memset(y[ibl].scales, 0, QK_K/32);
  9806. float max_scale = 0;
  9807. const float * xbl = x + QK_K*ibl;
  9808. float sumx2 = 0;
  9809. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9810. float sigma2 = 2*sumx2/QK_K;
  9811. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9812. const float * xb = xbl + block_size*ib;
  9813. if (quant_weights) {
  9814. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9815. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9816. } else {
  9817. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9818. }
  9819. float max = fabsf(xb[0]);
  9820. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9821. if (!max) {
  9822. scales[ib] = 0;
  9823. memset(L, 1, block_size);
  9824. continue;
  9825. }
  9826. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9827. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9828. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9829. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9830. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9831. // for each possible and score for each split.
  9832. for (int j = 0; j < block_size; ++j) {
  9833. pairs[2*j] = xb[j];
  9834. idx[2*j] = j;
  9835. }
  9836. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9837. float best_score = 0, scale = max;
  9838. int besti1 = -1, besti2 = -1, best_k = -1;
  9839. // 0: +, +
  9840. // 1: +, -
  9841. // 2: -, +
  9842. // 3: -, -
  9843. for (int i1 = 0; i1 <= block_size; ++i1) {
  9844. for (int i2 = i1; i2 <= block_size; ++i2) {
  9845. memset(sumqx, 0, 4*sizeof(float));
  9846. memset(sumq2, 0, 4*sizeof(float));
  9847. for (int j = 0; j < i1; ++j) {
  9848. int i = idx[2*j];
  9849. if (i < block_size/2) {
  9850. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9851. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9852. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9853. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9854. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9855. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9856. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9857. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9858. } else {
  9859. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9860. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9861. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9862. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9863. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9864. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9865. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9866. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9867. }
  9868. }
  9869. for (int j = i1; j < i2; ++j) {
  9870. int i = idx[2*j];
  9871. if (i < block_size/2) {
  9872. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9873. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9874. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9875. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9876. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9877. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9878. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9879. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9880. } else {
  9881. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9882. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9883. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9884. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9885. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9886. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9887. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9888. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9889. }
  9890. }
  9891. for (int j = i2; j < block_size; ++j) {
  9892. int i = idx[2*j];
  9893. if (i < block_size/2) {
  9894. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9895. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9896. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9897. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9898. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9899. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9900. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9901. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9902. } else {
  9903. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9904. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9905. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9906. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9907. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9908. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9909. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9910. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9911. }
  9912. }
  9913. for (int k = 0; k < 4; ++k) {
  9914. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9915. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9916. besti1 = i1; besti2 = i2; best_k = k;
  9917. }
  9918. }
  9919. }
  9920. }
  9921. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9922. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9923. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9924. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9925. if (scale < 0) {
  9926. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9927. scale = -scale;
  9928. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9929. }
  9930. bool all_on_grid = true;
  9931. for (int k = 0; k < block_size/8; ++k) {
  9932. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9933. else xx = best_k%2 == 0 ? x_p : x_m;
  9934. uint16_t u = 0;
  9935. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9936. int grid_index = kmap_q2xs[u];
  9937. if (grid_index < 0) {
  9938. all_on_grid = false;
  9939. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9940. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9941. GGML_ASSERT(grid_index >= 0);
  9942. }
  9943. index[k] = grid_index;
  9944. }
  9945. if (!all_on_grid) {
  9946. float sumqx_f = 0, sumq2_f = 0;
  9947. for (int k = 0; k < block_size/8; ++k) {
  9948. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9949. else xx = best_k%2 == 0 ? x_p : x_m;
  9950. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9951. for (int j = 0; j < 8; ++j) {
  9952. float w = weight[8*k + j];
  9953. float q = xx[(pg[j] - 1)/2];
  9954. sumqx_f += w*q*xb[8*k+j];
  9955. sumq2_f += w*q*q;
  9956. }
  9957. }
  9958. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9959. }
  9960. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9961. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9962. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9963. GGML_ASSERT(scale >= 0);
  9964. scales[ib] = scale;
  9965. shifts[ib] = best_k;
  9966. max_scale = MAX(max_scale, scale);
  9967. }
  9968. if (!max_scale) {
  9969. continue;
  9970. }
  9971. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9972. #if QK_K == 64
  9973. float d = max_scale/31;
  9974. #else
  9975. float d = max_scale/15;
  9976. #endif
  9977. float id = 1/d;
  9978. float sumqx_f = 0, sumq2_f = 0;
  9979. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9980. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9981. #if QK_K == 64
  9982. l = MAX(0, MIN(15, l));
  9983. sc[ib/4] |= (l << 4*(ib%4));
  9984. #else
  9985. l = MAX(0, MIN(7, l));
  9986. sc[ib/4] |= (l << 3*(ib%4));
  9987. #endif
  9988. y[ibl].qh[ib] |= masks[shifts[ib]];
  9989. const float * xb = xbl + block_size*ib;
  9990. if (quant_weights) {
  9991. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9992. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9993. } else {
  9994. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9995. }
  9996. for (int k = 0; k < block_size/8; ++k) {
  9997. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9998. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9999. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  10000. for (int j = 0; j < 8; ++j) {
  10001. float w = weight[8*k + j];
  10002. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  10003. sumqx_f += w*q*xb[8*k+j];
  10004. sumq2_f += w*q*q;
  10005. }
  10006. }
  10007. }
  10008. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  10009. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  10010. #if QK_K == 64
  10011. y[ibl].d = s.f16;
  10012. #else
  10013. sc[0] |= ((s.u16 & 0x000f) << 12);
  10014. sc[1] |= ((s.u16 & 0x00f0) << 8);
  10015. sc[2] |= ((s.u16 & 0x0f00) << 4);
  10016. sc[3] |= ((s.u16 & 0xf000) << 0);
  10017. #endif
  10018. }
  10019. }
  10020. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10021. GGML_ASSERT(n_per_row%QK_K == 0);
  10022. float scales[QK_K/IQ1M_BLOCK_SIZE];
  10023. float weight[IQ1M_BLOCK_SIZE];
  10024. int8_t L[IQ1M_BLOCK_SIZE];
  10025. float pairs[2*IQ1M_BLOCK_SIZE];
  10026. uint16_t index[IQ1M_BLOCK_SIZE/8];
  10027. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  10028. int nblock = n_per_row/QK_K;
  10029. char * qrow = (char *)dst;
  10030. for (int row = 0; row < nrow; ++row) {
  10031. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  10032. src += n_per_row;
  10033. qrow += nblock*sizeof(block_iq1_m);
  10034. }
  10035. return nrow * nblock * sizeof(block_iq1_m);
  10036. }
  10037. // ============================ 4-bit non-linear quants
  10038. static inline int best_index_int8(int n, const int8_t * val, float x) {
  10039. if (x <= val[0]) return 0;
  10040. if (x >= val[n-1]) return n-1;
  10041. int ml = 0, mu = n-1;
  10042. while (mu-ml > 1) {
  10043. int mav = (ml+mu)/2;
  10044. if (x < val[mav]) mu = mav; else ml = mav;
  10045. }
  10046. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  10047. }
  10048. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  10049. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  10050. float * scales, float * weight, uint8_t * L,
  10051. const int8_t * values,
  10052. const float * quant_weights,
  10053. const int ntry) {
  10054. float sigma2 = 0;
  10055. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  10056. sigma2 *= 2.f/super_block_size;
  10057. memset(q4, 0, super_block_size/2);
  10058. dh[0] = GGML_FP32_TO_FP16(0.f);
  10059. float max_scale = 0, amax_scale = 0;
  10060. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  10061. const float * xb = x + ib*block_size;
  10062. uint8_t * Lb = L + ib*block_size;
  10063. if (quant_weights) {
  10064. const float * qw = quant_weights + ib*block_size;
  10065. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  10066. } else {
  10067. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  10068. }
  10069. float amax = 0, max = 0;
  10070. for (int j = 0; j < block_size; ++j) {
  10071. float ax = fabsf(xb[j]);
  10072. if (ax > amax) {
  10073. amax = ax; max = xb[j];
  10074. }
  10075. }
  10076. if (!amax) {
  10077. scales[ib] = 0;
  10078. continue;
  10079. }
  10080. float d = ntry > 0 ? -max/values[0] : max/values[0];
  10081. float id = 1/d;
  10082. float sumqx = 0, sumq2 = 0;
  10083. for (int j = 0; j < block_size; ++j) {
  10084. float al = id*xb[j];
  10085. int l = best_index_int8(16, values, al);
  10086. Lb[j] = l;
  10087. float q = values[l];
  10088. float w = weight[j];
  10089. sumqx += w*q*xb[j];
  10090. sumq2 += w*q*q;
  10091. }
  10092. d = sumqx/sumq2;
  10093. float best = d*sumqx;
  10094. for (int itry = -ntry; itry <= ntry; ++itry) {
  10095. id = (itry + values[0])/max;
  10096. sumqx = sumq2 = 0;
  10097. for (int j = 0; j < block_size; ++j) {
  10098. float al = id*xb[j];
  10099. int l = best_index_int8(16, values, al);
  10100. float q = values[l];
  10101. float w = weight[j];
  10102. sumqx += w*q*xb[j];
  10103. sumq2 += w*q*q;
  10104. }
  10105. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10106. d = sumqx/sumq2; best = d * sumqx;
  10107. }
  10108. }
  10109. scales[ib] = d;
  10110. float abs_d = fabsf(d);
  10111. if (abs_d > amax_scale) {
  10112. amax_scale = abs_d; max_scale = d;
  10113. }
  10114. }
  10115. if (super_block_size/block_size > 1) {
  10116. int nb = super_block_size/block_size;
  10117. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  10118. float d = -max_scale/32;
  10119. dh[0] = GGML_FP32_TO_FP16(d);
  10120. float id = d ? 1/d : 0.f;
  10121. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  10122. int l = nearest_int(id*scales[ib]);
  10123. l = MAX(-32, MIN(31, l));
  10124. float dl = d * l;
  10125. float idl = dl ? 1/dl : 0.f;
  10126. uint8_t * Lb = L + ib*block_size;
  10127. const float * xb = x + ib*block_size;
  10128. for (int j = 0; j < block_size; ++j) {
  10129. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  10130. }
  10131. l += 32;
  10132. uint8_t l_l = l & 0xf;
  10133. uint8_t l_h = l >> 4;
  10134. if (ib%2 == 0) scales_l[ib/2] = l_l;
  10135. else scales_l[ib/2] |= (l_l << 4);
  10136. scales_h[ib/8] |= (l_h << 2*(ib%8));
  10137. }
  10138. } else {
  10139. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  10140. if (ntry > 0) {
  10141. float id = scales[0] ? 1/scales[0] : 0;
  10142. for (int j = 0; j < super_block_size; ++j) {
  10143. L[j] = best_index_int8(16, values, id*x[j]);
  10144. }
  10145. }
  10146. }
  10147. for (int i = 0; i < super_block_size/32; ++i) {
  10148. for (int j = 0; j < 16; ++j) {
  10149. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  10150. }
  10151. }
  10152. }
  10153. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10154. GGML_ASSERT(n_per_row%QK4_NL == 0);
  10155. int nblock = n_per_row/QK4_NL;
  10156. char * qrow = (char *)dst;
  10157. uint8_t L[QK4_NL];
  10158. float weight[QK4_NL];
  10159. uint16_t unused_h;
  10160. uint8_t * unused_l = NULL;
  10161. float scale;
  10162. for (int row = 0; row < nrow; ++row) {
  10163. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  10164. for (int ibl = 0; ibl < nblock; ++ibl) {
  10165. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  10166. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  10167. &scale, weight, L, kvalues_iq4nl, qw, 7);
  10168. }
  10169. src += n_per_row;
  10170. qrow += nblock*sizeof(block_iq4_nl);
  10171. }
  10172. return nrow * nblock * sizeof(block_iq4_nl);
  10173. }
  10174. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int k) {
  10175. GGML_ASSERT(k%QK4_NL == 0);
  10176. int nblock = k/QK4_NL;
  10177. uint8_t L[QK4_NL];
  10178. float weight[QK4_NL];
  10179. uint16_t unused_h;
  10180. uint8_t * unused_l = NULL;
  10181. float scale;
  10182. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  10183. for (int ibl = 0; ibl < nblock; ++ibl) {
  10184. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  10185. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  10186. }
  10187. }
  10188. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int k) {
  10189. assert(k % QK4_NL == 0);
  10190. quantize_row_iq4_nl(x, y, k);
  10191. }
  10192. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10193. #if QK_K == 64
  10194. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  10195. #else
  10196. GGML_ASSERT(n_per_row%QK_K == 0);
  10197. int nblock = n_per_row/QK_K;
  10198. char * qrow = (char *)dst;
  10199. uint8_t L[QK_K];
  10200. float weight[32];
  10201. float scales[QK_K/32];
  10202. for (int row = 0; row < nrow; ++row) {
  10203. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  10204. for (int ibl = 0; ibl < nblock; ++ibl) {
  10205. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  10206. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  10207. scales, weight, L, kvalues_iq4nl, qw, 7);
  10208. }
  10209. src += n_per_row;
  10210. qrow += nblock*sizeof(block_iq4_xs);
  10211. }
  10212. return nrow * nblock * sizeof(block_iq4_xs);
  10213. #endif
  10214. }
  10215. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int k) {
  10216. assert(k % QK_K == 0);
  10217. block_iq4_xs * restrict y = vy;
  10218. quantize_row_iq4_xs_reference(x, y, k);
  10219. }
  10220. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int k) {
  10221. assert(k % QK_K == 0);
  10222. quantize_iq4_xs(x, y, 1, k, NULL);
  10223. }
  10224. // =============================== 2.5625 bpw
  10225. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  10226. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10227. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10228. const int * kmap_q2xs = iq2_data[gindex].map;
  10229. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10230. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10231. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10232. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10233. GGML_ASSERT(n%QK_K == 0);
  10234. const int kMaxQ = 3;
  10235. const int nbl = n/QK_K;
  10236. block_iq2_s * y = vy;
  10237. float scales[QK_K/16];
  10238. float weight[16];
  10239. float xval[16];
  10240. int8_t L[16];
  10241. int8_t Laux[16];
  10242. float waux[16];
  10243. bool is_on_grid[2];
  10244. bool is_on_grid_aux[2];
  10245. uint8_t block_signs[2];
  10246. for (int ibl = 0; ibl < nbl; ++ibl) {
  10247. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10248. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10249. float max_scale = 0;
  10250. const float * xbl = x + QK_K*ibl;
  10251. float sumx2 = 0;
  10252. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10253. float sigma2 = 2*sumx2/QK_K;
  10254. for (int ib = 0; ib < QK_K/16; ++ib) {
  10255. const float * xb = xbl + 16*ib;
  10256. if (quant_weights) {
  10257. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10258. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10259. } else {
  10260. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10261. }
  10262. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10263. for (int k = 0; k < 2; ++k) {
  10264. uint8_t s = 0;
  10265. for (int i = 0; i < 8; ++i) {
  10266. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10267. else {
  10268. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10269. }
  10270. }
  10271. block_signs[k] = s;
  10272. }
  10273. float max = xval[0];
  10274. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10275. if (!max) {
  10276. scales[ib] = 0;
  10277. continue;
  10278. }
  10279. float best = 0;
  10280. float scale = max/(2*kMaxQ-1);
  10281. is_on_grid[0] = is_on_grid[1] = true;
  10282. for (int is = -9; is <= 9; ++is) {
  10283. float id = (2*kMaxQ-1+is*0.1f)/max;
  10284. float this_scale = 1/id;
  10285. for (int k = 0; k < 2; ++k) {
  10286. for (int i = 0; i < 8; ++i) {
  10287. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10288. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10289. }
  10290. uint16_t u = 0;
  10291. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10292. int grid_index = kmap_q2xs[u];
  10293. is_on_grid_aux[k] = true;
  10294. if (grid_index < 0) {
  10295. is_on_grid_aux[k] = false;
  10296. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10297. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10298. }
  10299. }
  10300. float sumqx = 0, sumq2 = 0;
  10301. for (int i = 0; i < 16; ++i) {
  10302. float w = weight[i];
  10303. float q = 2*Laux[i] + 1;
  10304. sumqx += w*xval[i]*q;
  10305. sumq2 += w*q*q;
  10306. }
  10307. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10308. scale = sumqx/sumq2; best = scale*sumqx;
  10309. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10310. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10311. }
  10312. }
  10313. int n_not_ongrid = 0;
  10314. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10315. if (n_not_ongrid > 0 && scale > 0) {
  10316. float id = 1/scale;
  10317. for (int k = 0; k < 2; ++k) {
  10318. if (is_on_grid[k]) continue;
  10319. uint16_t u = 0;
  10320. for (int i = 0; i < 8; ++i) {
  10321. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10322. l = MAX(0, MIN(kMaxQ-1, l));
  10323. u |= (l << 2*i);
  10324. L[8*k + i] = l;
  10325. }
  10326. int grid_index = kmap_q2xs[u];
  10327. if (grid_index < 0) {
  10328. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10329. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10330. }
  10331. }
  10332. float sumqx = 0, sumq2 = 0;
  10333. for (int i = 0; i < 16; ++i) {
  10334. float w = weight[i];
  10335. float q = 2*L[i] + 1;
  10336. sumqx += w*xval[i]*q;
  10337. sumq2 += w*q*q;
  10338. }
  10339. if (sumq2 > 0) scale = sumqx/sumq2;
  10340. }
  10341. if (scale < 0) {
  10342. scale = -scale;
  10343. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10344. }
  10345. for (int k = 0; k < 2; ++k) {
  10346. uint16_t u = 0;
  10347. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10348. int grid_index = kmap_q2xs[u];
  10349. if (grid_index < 0) {
  10350. printf("Oops: found point %u not on grid:", u);
  10351. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10352. printf("\n");
  10353. GGML_ASSERT(false);
  10354. }
  10355. const int i8 = 2*ib + k;
  10356. y[ibl].qs[i8] = grid_index & 255;
  10357. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10358. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10359. }
  10360. GGML_ASSERT(scale >= 0);
  10361. scales[ib] = scale;
  10362. max_scale = MAX(max_scale, scale);
  10363. }
  10364. if (!max_scale) {
  10365. continue;
  10366. }
  10367. float d = max_scale/31;
  10368. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10369. float id = 1/d;
  10370. for (int ib = 0; ib < QK_K/16; ++ib) {
  10371. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10372. l = MAX(0, MIN(15, l));
  10373. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10374. else y[ibl].scales[ib/2] |= (l << 4);
  10375. }
  10376. }
  10377. }
  10378. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10379. GGML_ASSERT(n_per_row%QK_K == 0);
  10380. int nblock = n_per_row/QK_K;
  10381. char * qrow = (char *)dst;
  10382. for (int row = 0; row < nrow; ++row) {
  10383. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10384. src += n_per_row;
  10385. qrow += nblock*sizeof(block_iq2_s);
  10386. }
  10387. return nrow * nblock * sizeof(block_iq2_s);
  10388. }
  10389. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int k) {
  10390. assert(k % QK_K == 0);
  10391. quantize_iq2_s(x, y, 1, k, NULL);
  10392. }
  10393. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int k) {
  10394. assert(k % QK_K == 0);
  10395. block_iq2_s * restrict y = vy;
  10396. quantize_row_iq2_s_reference(x, y, k);
  10397. }