ggml-quants.c 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687
  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. #define UNUSED GGML_UNUSED
  14. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  15. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  16. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  17. // multiply int8_t, add results pairwise twice
  18. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  19. // Get absolute values of x vectors
  20. const __m128i ax = _mm_sign_epi8(x, x);
  21. // Sign the values of the y vectors
  22. const __m128i sy = _mm_sign_epi8(y, x);
  23. // Perform multiplication and create 16-bit values
  24. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  25. const __m128i ones = _mm_set1_epi16(1);
  26. return _mm_madd_epi16(ones, dot);
  27. }
  28. #if __AVX__ || __AVX2__ || __AVX512F__
  29. // horizontally add 8 floats
  30. static inline float hsum_float_8(const __m256 x) {
  31. __m128 res = _mm256_extractf128_ps(x, 1);
  32. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  33. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  34. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  35. return _mm_cvtss_f32(res);
  36. }
  37. // horizontally add 8 int32_t
  38. static inline int hsum_i32_8(const __m256i a) {
  39. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  40. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  41. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  42. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  43. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  44. }
  45. // horizontally add 4 int32_t
  46. static inline int hsum_i32_4(const __m128i a) {
  47. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  48. const __m128i sum64 = _mm_add_epi32(hi64, a);
  49. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  50. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  51. }
  52. #if defined(__AVX2__) || defined(__AVX512F__)
  53. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  54. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  55. uint32_t x32;
  56. memcpy(&x32, x, sizeof(uint32_t));
  57. const __m256i shuf_mask = _mm256_set_epi64x(
  58. 0x0303030303030303, 0x0202020202020202,
  59. 0x0101010101010101, 0x0000000000000000);
  60. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  61. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  62. bytes = _mm256_or_si256(bytes, bit_mask);
  63. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  64. }
  65. // Unpack 32 4-bit fields into 32 bytes
  66. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  67. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  68. {
  69. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  70. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  71. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  72. return _mm256_and_si256(lowMask, bytes);
  73. }
  74. // add int16_t pairwise and return as float vector
  75. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  76. const __m256i ones = _mm256_set1_epi16(1);
  77. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  78. return _mm256_cvtepi32_ps(summed_pairs);
  79. }
  80. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  81. #if defined(__AVXVNNI__) || (defined(__AVX512VNNI__) && defined(__AVX512VL__))
  82. const __m256i zero = _mm256_setzero_si256();
  83. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  84. return _mm256_cvtepi32_ps(summed_pairs);
  85. #else
  86. // Perform multiplication and create 16-bit values
  87. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  88. return sum_i16_pairs_float(dot);
  89. #endif
  90. }
  91. // multiply int8_t, add results pairwise twice and return as float vector
  92. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  93. #if __AVXVNNIINT8__
  94. const __m256i zero = _mm256_setzero_si256();
  95. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  96. return _mm256_cvtepi32_ps(summed_pairs);
  97. #else
  98. // Get absolute values of x vectors
  99. const __m256i ax = _mm256_sign_epi8(x, x);
  100. // Sign the values of the y vectors
  101. const __m256i sy = _mm256_sign_epi8(y, x);
  102. return mul_sum_us8_pairs_float(ax, sy);
  103. #endif
  104. }
  105. static inline __m128i packNibbles( __m256i bytes )
  106. {
  107. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  108. #if __AVX512F__
  109. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  110. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  111. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  112. #else
  113. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  114. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  115. __m256i low = _mm256_and_si256( lowByte, bytes );
  116. high = _mm256_srli_epi16( high, 4 );
  117. bytes = _mm256_or_si256( low, high );
  118. // Compress uint16_t lanes into bytes
  119. __m128i r0 = _mm256_castsi256_si128( bytes );
  120. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  121. return _mm_packus_epi16( r0, r1 );
  122. #endif
  123. }
  124. #elif defined(__AVX__)
  125. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  126. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  127. uint32_t x32;
  128. memcpy(&x32, x, sizeof(uint32_t));
  129. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  130. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  131. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  132. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  133. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  134. bytesl = _mm_or_si128(bytesl, bit_mask);
  135. bytesh = _mm_or_si128(bytesh, bit_mask);
  136. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  137. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  138. return MM256_SET_M128I(bytesh, bytesl);
  139. }
  140. // Unpack 32 4-bit fields into 32 bytes
  141. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  142. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  143. {
  144. // Load 16 bytes from memory
  145. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  146. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  147. const __m128i lowMask = _mm_set1_epi8(0xF);
  148. tmpl = _mm_and_si128(lowMask, tmpl);
  149. tmph = _mm_and_si128(lowMask, tmph);
  150. return MM256_SET_M128I(tmph, tmpl);
  151. }
  152. // add int16_t pairwise and return as float vector
  153. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  154. const __m128i ones = _mm_set1_epi16(1);
  155. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  156. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  157. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  158. return _mm256_cvtepi32_ps(summed_pairs);
  159. }
  160. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  161. const __m128i axl = _mm256_castsi256_si128(ax);
  162. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  163. const __m128i syl = _mm256_castsi256_si128(sy);
  164. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  165. // Perform multiplication and create 16-bit values
  166. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  167. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  168. return sum_i16_pairs_float(doth, dotl);
  169. }
  170. // multiply int8_t, add results pairwise twice and return as float vector
  171. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  172. const __m128i xl = _mm256_castsi256_si128(x);
  173. const __m128i xh = _mm256_extractf128_si256(x, 1);
  174. const __m128i yl = _mm256_castsi256_si128(y);
  175. const __m128i yh = _mm256_extractf128_si256(y, 1);
  176. // Get absolute values of x vectors
  177. const __m128i axl = _mm_sign_epi8(xl, xl);
  178. const __m128i axh = _mm_sign_epi8(xh, xh);
  179. // Sign the values of the y vectors
  180. const __m128i syl = _mm_sign_epi8(yl, xl);
  181. const __m128i syh = _mm_sign_epi8(yh, xh);
  182. // Perform multiplication and create 16-bit values
  183. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  184. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  185. return sum_i16_pairs_float(doth, dotl);
  186. }
  187. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  188. {
  189. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  190. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  191. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  192. __m128i low = _mm_and_si128( lowByte, bytes1 );
  193. high = _mm_srli_epi16( high, 4 );
  194. bytes1 = _mm_or_si128( low, high );
  195. high = _mm_andnot_si128( lowByte, bytes2 );
  196. low = _mm_and_si128( lowByte, bytes2 );
  197. high = _mm_srli_epi16( high, 4 );
  198. bytes2 = _mm_or_si128( low, high );
  199. return _mm_packus_epi16( bytes1, bytes2);
  200. }
  201. #endif
  202. #elif defined(__SSSE3__)
  203. // horizontally add 4x4 floats
  204. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  205. __m128 res_0 =_mm_hadd_ps(a, b);
  206. __m128 res_1 =_mm_hadd_ps(c, d);
  207. __m128 res =_mm_hadd_ps(res_0, res_1);
  208. res =_mm_hadd_ps(res, res);
  209. res =_mm_hadd_ps(res, res);
  210. return _mm_cvtss_f32(res);
  211. }
  212. #endif // __AVX__ || __AVX2__ || __AVX512F__
  213. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  214. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  215. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  216. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  217. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  218. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  219. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  220. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  221. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  222. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  223. // precomputed tables for expanding 8bits to 8 bytes:
  224. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  225. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  226. #endif
  227. // reference implementation for deterministic creation of model files
  228. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int64_t k) {
  229. static const int qk = QK4_0;
  230. assert(k % qk == 0);
  231. const int nb = k / qk;
  232. for (int i = 0; i < nb; i++) {
  233. float amax = 0.0f; // absolute max
  234. float max = 0.0f;
  235. for (int j = 0; j < qk; j++) {
  236. const float v = x[i*qk + j];
  237. if (amax < fabsf(v)) {
  238. amax = fabsf(v);
  239. max = v;
  240. }
  241. }
  242. const float d = max / -8;
  243. const float id = d ? 1.0f/d : 0.0f;
  244. y[i].d = GGML_FP32_TO_FP16(d);
  245. for (int j = 0; j < qk/2; ++j) {
  246. const float x0 = x[i*qk + 0 + j]*id;
  247. const float x1 = x[i*qk + qk/2 + j]*id;
  248. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  249. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  250. y[i].qs[j] = xi0;
  251. y[i].qs[j] |= xi1 << 4;
  252. }
  253. }
  254. }
  255. void quantize_row_q4_0(const float * restrict x, void * restrict y, int64_t k) {
  256. quantize_row_q4_0_reference(x, y, k);
  257. }
  258. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int64_t k) {
  259. const int qk = QK4_1;
  260. assert(k % qk == 0);
  261. const int nb = k / qk;
  262. for (int i = 0; i < nb; i++) {
  263. float min = FLT_MAX;
  264. float max = -FLT_MAX;
  265. for (int j = 0; j < qk; j++) {
  266. const float v = x[i*qk + j];
  267. if (v < min) min = v;
  268. if (v > max) max = v;
  269. }
  270. const float d = (max - min) / ((1 << 4) - 1);
  271. const float id = d ? 1.0f/d : 0.0f;
  272. y[i].d = GGML_FP32_TO_FP16(d);
  273. y[i].m = GGML_FP32_TO_FP16(min);
  274. for (int j = 0; j < qk/2; ++j) {
  275. const float x0 = (x[i*qk + 0 + j] - min)*id;
  276. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  277. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  278. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  279. y[i].qs[j] = xi0;
  280. y[i].qs[j] |= xi1 << 4;
  281. }
  282. }
  283. }
  284. void quantize_row_q4_1(const float * restrict x, void * restrict y, int64_t k) {
  285. quantize_row_q4_1_reference(x, y, k);
  286. }
  287. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int64_t k) {
  288. static const int qk = QK5_0;
  289. assert(k % qk == 0);
  290. const int nb = k / qk;
  291. for (int i = 0; i < nb; i++) {
  292. float amax = 0.0f; // absolute max
  293. float max = 0.0f;
  294. for (int j = 0; j < qk; j++) {
  295. const float v = x[i*qk + j];
  296. if (amax < fabsf(v)) {
  297. amax = fabsf(v);
  298. max = v;
  299. }
  300. }
  301. const float d = max / -16;
  302. const float id = d ? 1.0f/d : 0.0f;
  303. y[i].d = GGML_FP32_TO_FP16(d);
  304. uint32_t qh = 0;
  305. for (int j = 0; j < qk/2; ++j) {
  306. const float x0 = x[i*qk + 0 + j]*id;
  307. const float x1 = x[i*qk + qk/2 + j]*id;
  308. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  309. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  310. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  311. // get the 5-th bit and store it in qh at the right position
  312. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  313. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  314. }
  315. memcpy(&y[i].qh, &qh, sizeof(qh));
  316. }
  317. }
  318. void quantize_row_q5_0(const float * restrict x, void * restrict y, int64_t k) {
  319. quantize_row_q5_0_reference(x, y, k);
  320. }
  321. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int64_t k) {
  322. const int qk = QK5_1;
  323. assert(k % qk == 0);
  324. const int nb = k / qk;
  325. for (int i = 0; i < nb; i++) {
  326. float min = FLT_MAX;
  327. float max = -FLT_MAX;
  328. for (int j = 0; j < qk; j++) {
  329. const float v = x[i*qk + j];
  330. if (v < min) min = v;
  331. if (v > max) max = v;
  332. }
  333. const float d = (max - min) / ((1 << 5) - 1);
  334. const float id = d ? 1.0f/d : 0.0f;
  335. y[i].d = GGML_FP32_TO_FP16(d);
  336. y[i].m = GGML_FP32_TO_FP16(min);
  337. uint32_t qh = 0;
  338. for (int j = 0; j < qk/2; ++j) {
  339. const float x0 = (x[i*qk + 0 + j] - min)*id;
  340. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  341. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  342. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  343. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  344. // get the 5-th bit and store it in qh at the right position
  345. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  346. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  347. }
  348. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  349. }
  350. }
  351. void quantize_row_q5_1(const float * restrict x, void * restrict y, int64_t k) {
  352. quantize_row_q5_1_reference(x, y, k);
  353. }
  354. // reference implementation for deterministic creation of model files
  355. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int64_t k) {
  356. assert(k % QK8_0 == 0);
  357. const int nb = k / QK8_0;
  358. for (int i = 0; i < nb; i++) {
  359. float amax = 0.0f; // absolute max
  360. for (int j = 0; j < QK8_0; j++) {
  361. const float v = x[i*QK8_0 + j];
  362. amax = MAX(amax, fabsf(v));
  363. }
  364. const float d = amax / ((1 << 7) - 1);
  365. const float id = d ? 1.0f/d : 0.0f;
  366. y[i].d = GGML_FP32_TO_FP16(d);
  367. for (int j = 0; j < QK8_0; ++j) {
  368. const float x0 = x[i*QK8_0 + j]*id;
  369. y[i].qs[j] = roundf(x0);
  370. }
  371. }
  372. }
  373. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int64_t k) {
  374. assert(QK8_0 == 32);
  375. assert(k % QK8_0 == 0);
  376. const int nb = k / QK8_0;
  377. block_q8_0 * restrict y = vy;
  378. #if defined(__ARM_NEON)
  379. for (int i = 0; i < nb; i++) {
  380. float32x4_t srcv [8];
  381. float32x4_t asrcv[8];
  382. float32x4_t amaxv[8];
  383. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  384. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  385. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  386. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  387. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  388. const float amax = vmaxvq_f32(amaxv[0]);
  389. const float d = amax / ((1 << 7) - 1);
  390. const float id = d ? 1.0f/d : 0.0f;
  391. y[i].d = GGML_FP32_TO_FP16(d);
  392. for (int j = 0; j < 8; j++) {
  393. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  394. const int32x4_t vi = vcvtnq_s32_f32(v);
  395. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  396. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  397. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  398. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  399. }
  400. }
  401. #elif defined(__wasm_simd128__)
  402. for (int i = 0; i < nb; i++) {
  403. v128_t srcv [8];
  404. v128_t asrcv[8];
  405. v128_t amaxv[8];
  406. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  407. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  408. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  409. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  410. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  411. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  412. wasm_f32x4_extract_lane(amaxv[0], 1)),
  413. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  414. wasm_f32x4_extract_lane(amaxv[0], 3)));
  415. const float d = amax / ((1 << 7) - 1);
  416. const float id = d ? 1.0f/d : 0.0f;
  417. y[i].d = GGML_FP32_TO_FP16(d);
  418. for (int j = 0; j < 8; j++) {
  419. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  420. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  421. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  422. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  423. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  424. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  425. }
  426. }
  427. #elif defined(__AVX2__) || defined(__AVX__)
  428. for (int i = 0; i < nb; i++) {
  429. // Load elements into 4 AVX vectors
  430. __m256 v0 = _mm256_loadu_ps( x );
  431. __m256 v1 = _mm256_loadu_ps( x + 8 );
  432. __m256 v2 = _mm256_loadu_ps( x + 16 );
  433. __m256 v3 = _mm256_loadu_ps( x + 24 );
  434. x += 32;
  435. // Compute max(abs(e)) for the block
  436. const __m256 signBit = _mm256_set1_ps( -0.0f );
  437. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  438. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  439. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  440. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  441. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  442. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  443. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  444. const float maxScalar = _mm_cvtss_f32( max4 );
  445. // Quantize these floats
  446. const float d = maxScalar / 127.f;
  447. y[i].d = GGML_FP32_TO_FP16(d);
  448. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  449. const __m256 mul = _mm256_set1_ps( id );
  450. // Apply the multiplier
  451. v0 = _mm256_mul_ps( v0, mul );
  452. v1 = _mm256_mul_ps( v1, mul );
  453. v2 = _mm256_mul_ps( v2, mul );
  454. v3 = _mm256_mul_ps( v3, mul );
  455. // Round to nearest integer
  456. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  457. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  458. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  459. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  460. // Convert floats to integers
  461. __m256i i0 = _mm256_cvtps_epi32( v0 );
  462. __m256i i1 = _mm256_cvtps_epi32( v1 );
  463. __m256i i2 = _mm256_cvtps_epi32( v2 );
  464. __m256i i3 = _mm256_cvtps_epi32( v3 );
  465. #if defined(__AVX2__)
  466. // Convert int32 to int16
  467. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  468. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  469. // Convert int16 to int8
  470. 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
  471. // We got our precious signed bytes, but the order is now wrong
  472. // These AVX2 pack instructions process 16-byte pieces independently
  473. // The following instruction is fixing the order
  474. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  475. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  476. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  477. #else
  478. // Since we don't have in AVX some necessary functions,
  479. // we split the registers in half and call AVX2 analogs from SSE
  480. __m128i ni0 = _mm256_castsi256_si128( i0 );
  481. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  482. __m128i ni2 = _mm256_castsi256_si128( i1 );
  483. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  484. __m128i ni4 = _mm256_castsi256_si128( i2 );
  485. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  486. __m128i ni6 = _mm256_castsi256_si128( i3 );
  487. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  488. // Convert int32 to int16
  489. ni0 = _mm_packs_epi32( ni0, ni1 );
  490. ni2 = _mm_packs_epi32( ni2, ni3 );
  491. ni4 = _mm_packs_epi32( ni4, ni5 );
  492. ni6 = _mm_packs_epi32( ni6, ni7 );
  493. // Convert int16 to int8
  494. ni0 = _mm_packs_epi16( ni0, ni2 );
  495. ni4 = _mm_packs_epi16( ni4, ni6 );
  496. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  497. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  498. #endif
  499. }
  500. #elif defined(__riscv_v_intrinsic)
  501. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  502. for (int i = 0; i < nb; i++) {
  503. // load elements
  504. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  505. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  506. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  507. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  508. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  509. const float d = amax / ((1 << 7) - 1);
  510. const float id = d ? 1.0f/d : 0.0f;
  511. y[i].d = GGML_FP32_TO_FP16(d);
  512. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  513. // convert to integer
  514. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  515. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  516. // store result
  517. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  518. }
  519. #else
  520. GGML_UNUSED(nb);
  521. // scalar
  522. quantize_row_q8_0_reference(x, y, k);
  523. #endif
  524. }
  525. // reference implementation for deterministic creation of model files
  526. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int64_t k) {
  527. assert(QK8_1 == 32);
  528. assert(k % QK8_1 == 0);
  529. const int nb = k / QK8_1;
  530. for (int i = 0; i < nb; i++) {
  531. float amax = 0.0f; // absolute max
  532. for (int j = 0; j < QK8_1; j++) {
  533. const float v = x[i*QK8_1 + j];
  534. amax = MAX(amax, fabsf(v));
  535. }
  536. const float d = amax / ((1 << 7) - 1);
  537. const float id = d ? 1.0f/d : 0.0f;
  538. y[i].d = GGML_FP32_TO_FP16(d);
  539. int sum = 0;
  540. for (int j = 0; j < QK8_1/2; ++j) {
  541. const float v0 = x[i*QK8_1 + j]*id;
  542. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  543. y[i].qs[ j] = roundf(v0);
  544. y[i].qs[QK8_1/2 + j] = roundf(v1);
  545. sum += y[i].qs[ j];
  546. sum += y[i].qs[QK8_1/2 + j];
  547. }
  548. y[i].s = GGML_FP32_TO_FP16(sum*d);
  549. }
  550. }
  551. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int64_t k) {
  552. assert(k % QK8_1 == 0);
  553. const int nb = k / QK8_1;
  554. block_q8_1 * restrict y = vy;
  555. #if defined(__ARM_NEON)
  556. for (int i = 0; i < nb; i++) {
  557. float32x4_t srcv [8];
  558. float32x4_t asrcv[8];
  559. float32x4_t amaxv[8];
  560. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  561. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  562. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  563. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  564. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  565. const float amax = vmaxvq_f32(amaxv[0]);
  566. const float d = amax / ((1 << 7) - 1);
  567. const float id = d ? 1.0f/d : 0.0f;
  568. y[i].d = GGML_FP32_TO_FP16(d);
  569. int32x4_t accv = vdupq_n_s32(0);
  570. for (int j = 0; j < 8; j++) {
  571. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  572. const int32x4_t vi = vcvtnq_s32_f32(v);
  573. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  574. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  575. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  576. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  577. accv = vaddq_s32(accv, vi);
  578. }
  579. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  580. }
  581. #elif defined(__wasm_simd128__)
  582. for (int i = 0; i < nb; i++) {
  583. v128_t srcv [8];
  584. v128_t asrcv[8];
  585. v128_t amaxv[8];
  586. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  587. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  588. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  589. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  590. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  591. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  592. wasm_f32x4_extract_lane(amaxv[0], 1)),
  593. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  594. wasm_f32x4_extract_lane(amaxv[0], 3)));
  595. const float d = amax / ((1 << 7) - 1);
  596. const float id = d ? 1.0f/d : 0.0f;
  597. y[i].d = GGML_FP32_TO_FP16(d);
  598. v128_t accv = wasm_i32x4_splat(0);
  599. for (int j = 0; j < 8; j++) {
  600. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  601. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  602. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  603. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  604. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  605. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  606. accv = wasm_i32x4_add(accv, vi);
  607. }
  608. y[i].s = GGML_FP32_TO_FP16(
  609. d * (wasm_i32x4_extract_lane(accv, 0) +
  610. wasm_i32x4_extract_lane(accv, 1) +
  611. wasm_i32x4_extract_lane(accv, 2) +
  612. wasm_i32x4_extract_lane(accv, 3)));
  613. }
  614. #elif defined(__AVX2__) || defined(__AVX__)
  615. for (int i = 0; i < nb; i++) {
  616. // Load elements into 4 AVX vectors
  617. __m256 v0 = _mm256_loadu_ps( x );
  618. __m256 v1 = _mm256_loadu_ps( x + 8 );
  619. __m256 v2 = _mm256_loadu_ps( x + 16 );
  620. __m256 v3 = _mm256_loadu_ps( x + 24 );
  621. x += 32;
  622. // Compute max(abs(e)) for the block
  623. const __m256 signBit = _mm256_set1_ps( -0.0f );
  624. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  625. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  626. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  627. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  628. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  629. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  630. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  631. const float maxScalar = _mm_cvtss_f32( max4 );
  632. // Quantize these floats
  633. const float d = maxScalar / 127.f;
  634. y[i].d = GGML_FP32_TO_FP16(d);
  635. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  636. const __m256 mul = _mm256_set1_ps( id );
  637. // Apply the multiplier
  638. v0 = _mm256_mul_ps( v0, mul );
  639. v1 = _mm256_mul_ps( v1, mul );
  640. v2 = _mm256_mul_ps( v2, mul );
  641. v3 = _mm256_mul_ps( v3, mul );
  642. // Round to nearest integer
  643. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  644. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  645. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  646. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  647. // Convert floats to integers
  648. __m256i i0 = _mm256_cvtps_epi32( v0 );
  649. __m256i i1 = _mm256_cvtps_epi32( v1 );
  650. __m256i i2 = _mm256_cvtps_epi32( v2 );
  651. __m256i i3 = _mm256_cvtps_epi32( v3 );
  652. #if defined(__AVX2__)
  653. // Compute the sum of the quants and set y[i].s
  654. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  655. // Convert int32 to int16
  656. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  657. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  658. // Convert int16 to int8
  659. 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
  660. // We got our precious signed bytes, but the order is now wrong
  661. // These AVX2 pack instructions process 16-byte pieces independently
  662. // The following instruction is fixing the order
  663. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  664. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  665. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  666. #else
  667. // Since we don't have in AVX some necessary functions,
  668. // we split the registers in half and call AVX2 analogs from SSE
  669. __m128i ni0 = _mm256_castsi256_si128( i0 );
  670. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  671. __m128i ni2 = _mm256_castsi256_si128( i1 );
  672. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  673. __m128i ni4 = _mm256_castsi256_si128( i2 );
  674. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  675. __m128i ni6 = _mm256_castsi256_si128( i3 );
  676. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  677. // Compute the sum of the quants and set y[i].s
  678. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  679. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  680. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  681. // Convert int32 to int16
  682. ni0 = _mm_packs_epi32( ni0, ni1 );
  683. ni2 = _mm_packs_epi32( ni2, ni3 );
  684. ni4 = _mm_packs_epi32( ni4, ni5 );
  685. ni6 = _mm_packs_epi32( ni6, ni7 );
  686. // Convert int16 to int8
  687. ni0 = _mm_packs_epi16( ni0, ni2 );
  688. ni4 = _mm_packs_epi16( ni4, ni6 );
  689. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  690. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  691. #endif
  692. }
  693. #elif defined(__riscv_v_intrinsic)
  694. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  695. for (int i = 0; i < nb; i++) {
  696. // load elements
  697. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  698. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  699. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  700. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  701. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  702. const float d = amax / ((1 << 7) - 1);
  703. const float id = d ? 1.0f/d : 0.0f;
  704. y[i].d = GGML_FP32_TO_FP16(d);
  705. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  706. // convert to integer
  707. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  708. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  709. // store result
  710. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  711. // compute sum for y[i].s
  712. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  713. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  714. // set y[i].s
  715. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  716. y[i].s = GGML_FP32_TO_FP16(sum*d);
  717. }
  718. #else
  719. GGML_UNUSED(nb);
  720. // scalar
  721. quantize_row_q8_1_reference(x, y, k);
  722. #endif
  723. }
  724. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int64_t k) {
  725. static const int qk = QK4_0;
  726. assert(k % qk == 0);
  727. const int nb = k / qk;
  728. for (int i = 0; i < nb; i++) {
  729. const float d = GGML_FP16_TO_FP32(x[i].d);
  730. for (int j = 0; j < qk/2; ++j) {
  731. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  732. const int x1 = (x[i].qs[j] >> 4) - 8;
  733. y[i*qk + j + 0 ] = x0*d;
  734. y[i*qk + j + qk/2] = x1*d;
  735. }
  736. }
  737. }
  738. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int64_t k) {
  739. static const int qk = QK4_1;
  740. assert(k % qk == 0);
  741. const int nb = k / qk;
  742. for (int i = 0; i < nb; i++) {
  743. const float d = GGML_FP16_TO_FP32(x[i].d);
  744. const float m = GGML_FP16_TO_FP32(x[i].m);
  745. for (int j = 0; j < qk/2; ++j) {
  746. const int x0 = (x[i].qs[j] & 0x0F);
  747. const int x1 = (x[i].qs[j] >> 4);
  748. y[i*qk + j + 0 ] = x0*d + m;
  749. y[i*qk + j + qk/2] = x1*d + m;
  750. }
  751. }
  752. }
  753. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int64_t k) {
  754. static const int qk = QK5_0;
  755. assert(k % qk == 0);
  756. const int nb = k / qk;
  757. for (int i = 0; i < nb; i++) {
  758. const float d = GGML_FP16_TO_FP32(x[i].d);
  759. uint32_t qh;
  760. memcpy(&qh, x[i].qh, sizeof(qh));
  761. for (int j = 0; j < qk/2; ++j) {
  762. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  763. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  764. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  765. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  766. y[i*qk + j + 0 ] = x0*d;
  767. y[i*qk + j + qk/2] = x1*d;
  768. }
  769. }
  770. }
  771. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int64_t k) {
  772. static const int qk = QK5_1;
  773. assert(k % qk == 0);
  774. const int nb = k / qk;
  775. for (int i = 0; i < nb; i++) {
  776. const float d = GGML_FP16_TO_FP32(x[i].d);
  777. const float m = GGML_FP16_TO_FP32(x[i].m);
  778. uint32_t qh;
  779. memcpy(&qh, x[i].qh, sizeof(qh));
  780. for (int j = 0; j < qk/2; ++j) {
  781. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  782. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  783. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  784. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  785. y[i*qk + j + 0 ] = x0*d + m;
  786. y[i*qk + j + qk/2] = x1*d + m;
  787. }
  788. }
  789. }
  790. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int64_t k) {
  791. static const int qk = QK8_0;
  792. assert(k % qk == 0);
  793. const int nb = k / qk;
  794. for (int i = 0; i < nb; i++) {
  795. const float d = GGML_FP16_TO_FP32(x[i].d);
  796. for (int j = 0; j < qk; ++j) {
  797. y[i*qk + j] = x[i].qs[j]*d;
  798. }
  799. }
  800. }
  801. //
  802. // 2-6 bit quantization in super-blocks
  803. //
  804. //
  805. // ===================== Helper functions
  806. //
  807. static inline int nearest_int(float fval) {
  808. assert(fval <= 4194303.f);
  809. float val = fval + 12582912.f;
  810. int i; memcpy(&i, &val, sizeof(int));
  811. return (i & 0x007fffff) - 0x00400000;
  812. }
  813. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  814. const float * restrict qw) {
  815. float max = 0;
  816. float amax = 0;
  817. for (int i = 0; i < n; ++i) {
  818. float ax = fabsf(x[i]);
  819. if (ax > amax) { amax = ax; max = x[i]; }
  820. }
  821. if (amax < 1e-30f) { // all zero
  822. for (int i = 0; i < n; ++i) {
  823. L[i] = 0;
  824. }
  825. return 0.f;
  826. }
  827. float iscale = -nmax / max;
  828. if (rmse_type == 0) {
  829. for (int i = 0; i < n; ++i) {
  830. int l = nearest_int(iscale * x[i]);
  831. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  832. }
  833. return 1/iscale;
  834. }
  835. bool return_early = false;
  836. if (rmse_type < 0) {
  837. rmse_type = -rmse_type;
  838. return_early = true;
  839. }
  840. float sumlx = 0;
  841. float suml2 = 0;
  842. #ifdef HAVE_BUGGY_APPLE_LINKER
  843. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  844. for (volatile int i = 0; i < n; ++i) {
  845. #else
  846. for (int i = 0; i < n; ++i) {
  847. #endif
  848. int l = nearest_int(iscale * x[i]);
  849. l = MAX(-nmax, MIN(nmax-1, l));
  850. L[i] = l + nmax;
  851. 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]));
  852. sumlx += w*x[i]*l;
  853. suml2 += w*l*l;
  854. }
  855. float scale = sumlx/suml2;
  856. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  857. float best = scale * sumlx;
  858. for (int is = -9; is <= 9; ++is) {
  859. if (is == 0) {
  860. continue;
  861. }
  862. iscale = -(nmax + 0.1f*is) / max;
  863. sumlx = suml2 = 0;
  864. for (int i = 0; i < n; ++i) {
  865. int l = nearest_int(iscale * x[i]);
  866. l = MAX(-nmax, MIN(nmax-1, l));
  867. 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]));
  868. sumlx += w*x[i]*l;
  869. suml2 += w*l*l;
  870. }
  871. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  872. for (int i = 0; i < n; ++i) {
  873. int l = nearest_int(iscale * x[i]);
  874. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  875. }
  876. scale = sumlx/suml2; best = scale*sumlx;
  877. }
  878. }
  879. return scale;
  880. }
  881. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  882. float max = 0;
  883. float amax = 0;
  884. for (int i = 0; i < n; ++i) {
  885. float ax = fabsf(x[i]);
  886. if (ax > amax) { amax = ax; max = x[i]; }
  887. }
  888. if (!amax) { // all zero
  889. for (int i = 0; i < n; ++i) { L[i] = 0; }
  890. return 0.f;
  891. }
  892. float iscale = -nmax / max;
  893. if (do_rmse) {
  894. float sumlx = 0;
  895. float suml2 = 0;
  896. for (int i = 0; i < n; ++i) {
  897. int l = nearest_int(iscale * x[i]);
  898. l = MAX(-nmax, MIN(nmax-1, l));
  899. L[i] = l;
  900. float w = x[i]*x[i];
  901. sumlx += w*x[i]*l;
  902. suml2 += w*l*l;
  903. }
  904. for (int itry = 0; itry < 5; ++itry) {
  905. int n_changed = 0;
  906. for (int i = 0; i < n; ++i) {
  907. float w = x[i]*x[i];
  908. float slx = sumlx - w*x[i]*L[i];
  909. if (slx > 0) {
  910. float sl2 = suml2 - w*L[i]*L[i];
  911. int new_l = nearest_int(x[i] * sl2 / slx);
  912. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  913. if (new_l != L[i]) {
  914. slx += w*x[i]*new_l;
  915. sl2 += w*new_l*new_l;
  916. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  917. L[i] = new_l; sumlx = slx; suml2 = sl2;
  918. ++n_changed;
  919. }
  920. }
  921. }
  922. }
  923. if (!n_changed) {
  924. break;
  925. }
  926. }
  927. for (int i = 0; i < n; ++i) {
  928. L[i] += nmax;
  929. }
  930. return sumlx / suml2;
  931. }
  932. for (int i = 0; i < n; ++i) {
  933. int l = nearest_int(iscale * x[i]);
  934. l = MAX(-nmax, MIN(nmax-1, l));
  935. L[i] = l + nmax;
  936. }
  937. return 1/iscale;
  938. }
  939. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  940. int ntry, float alpha) {
  941. float min = x[0];
  942. float max = x[0];
  943. for (int i = 1; i < n; ++i) {
  944. if (x[i] < min) min = x[i];
  945. if (x[i] > max) max = x[i];
  946. }
  947. if (max == min) {
  948. for (int i = 0; i < n; ++i) L[i] = 0;
  949. *the_min = 0;
  950. return 0.f;
  951. }
  952. if (min > 0) min = 0;
  953. float iscale = nmax/(max - min);
  954. float scale = 1/iscale;
  955. for (int itry = 0; itry < ntry; ++itry) {
  956. float sumlx = 0; int suml2 = 0;
  957. bool did_change = false;
  958. for (int i = 0; i < n; ++i) {
  959. int l = nearest_int(iscale*(x[i] - min));
  960. l = MAX(0, MIN(nmax, l));
  961. if (l != L[i]) {
  962. L[i] = l;
  963. did_change = true;
  964. }
  965. sumlx += (x[i] - min)*l;
  966. suml2 += l*l;
  967. }
  968. scale = sumlx/suml2;
  969. float sum = 0;
  970. for (int i = 0; i < n; ++i) {
  971. sum += x[i] - scale*L[i];
  972. }
  973. min = alpha*min + (1 - alpha)*sum/n;
  974. if (min > 0) min = 0;
  975. iscale = 1/scale;
  976. if (!did_change) break;
  977. }
  978. *the_min = -min;
  979. return scale;
  980. }
  981. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  982. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  983. float rmin, float rdelta, int nstep, bool use_mad) {
  984. float min = x[0];
  985. float max = x[0];
  986. float sum_w = weights[0];
  987. float sum_x = sum_w * x[0];
  988. #ifdef HAVE_BUGGY_APPLE_LINKER
  989. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  990. for (volatile int i = 1; i < n; ++i) {
  991. #else
  992. for (int i = 1; i < n; ++i) {
  993. #endif
  994. if (x[i] < min) min = x[i];
  995. if (x[i] > max) max = x[i];
  996. float w = weights[i];
  997. sum_w += w;
  998. sum_x += w * x[i];
  999. }
  1000. if (min > 0) min = 0;
  1001. if (max == min) {
  1002. for (int i = 0; i < n; ++i) L[i] = 0;
  1003. *the_min = -min;
  1004. return 0.f;
  1005. }
  1006. float iscale = nmax/(max - min);
  1007. float scale = 1/iscale;
  1008. float best_mad = 0;
  1009. for (int i = 0; i < n; ++i) {
  1010. int l = nearest_int(iscale*(x[i] - min));
  1011. L[i] = MAX(0, MIN(nmax, l));
  1012. float diff = scale * L[i] + min - x[i];
  1013. diff = use_mad ? fabsf(diff) : diff * diff;
  1014. float w = weights[i];
  1015. best_mad += w * diff;
  1016. }
  1017. if (nstep < 1) {
  1018. *the_min = -min;
  1019. return scale;
  1020. }
  1021. for (int is = 0; is <= nstep; ++is) {
  1022. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1023. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1024. for (int i = 0; i < n; ++i) {
  1025. int l = nearest_int(iscale*(x[i] - min));
  1026. l = MAX(0, MIN(nmax, l));
  1027. Laux[i] = l;
  1028. float w = weights[i];
  1029. sum_l += w*l;
  1030. sum_l2 += w*l*l;
  1031. sum_xl += w*l*x[i];
  1032. }
  1033. float D = sum_w * sum_l2 - sum_l * sum_l;
  1034. if (D > 0) {
  1035. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1036. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1037. if (this_min > 0) {
  1038. this_min = 0;
  1039. this_scale = sum_xl / sum_l2;
  1040. }
  1041. float mad = 0;
  1042. for (int i = 0; i < n; ++i) {
  1043. float diff = this_scale * Laux[i] + this_min - x[i];
  1044. diff = use_mad ? fabsf(diff) : diff * diff;
  1045. float w = weights[i];
  1046. mad += w * diff;
  1047. }
  1048. if (mad < best_mad) {
  1049. for (int i = 0; i < n; ++i) {
  1050. L[i] = Laux[i];
  1051. }
  1052. best_mad = mad;
  1053. scale = this_scale;
  1054. min = this_min;
  1055. }
  1056. }
  1057. }
  1058. *the_min = -min;
  1059. return scale;
  1060. }
  1061. #if QK_K == 256
  1062. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1063. if (j < 4) {
  1064. *d = q[j] & 63; *m = q[j + 4] & 63;
  1065. } else {
  1066. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1067. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1068. }
  1069. }
  1070. #endif
  1071. //========================- 2-bit (de)-quantization
  1072. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int64_t k) {
  1073. assert(k % QK_K == 0);
  1074. const int nb = k / QK_K;
  1075. uint8_t L[QK_K];
  1076. uint8_t Laux[16];
  1077. float weights[16];
  1078. float mins[QK_K/16];
  1079. float scales[QK_K/16];
  1080. const float q4scale = 15.f;
  1081. for (int i = 0; i < nb; i++) {
  1082. float max_scale = 0; // as we are deducting the min, scales are always positive
  1083. float max_min = 0;
  1084. for (int j = 0; j < QK_K/16; ++j) {
  1085. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1086. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1087. float scale = scales[j];
  1088. if (scale > max_scale) {
  1089. max_scale = scale;
  1090. }
  1091. float min = mins[j];
  1092. if (min > max_min) {
  1093. max_min = min;
  1094. }
  1095. }
  1096. if (max_scale > 0) {
  1097. float iscale = q4scale/max_scale;
  1098. for (int j = 0; j < QK_K/16; ++j) {
  1099. int l = nearest_int(iscale*scales[j]);
  1100. y[i].scales[j] = l;
  1101. }
  1102. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1103. } else {
  1104. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1105. y[i].d = GGML_FP32_TO_FP16(0.f);
  1106. }
  1107. if (max_min > 0) {
  1108. float iscale = q4scale/max_min;
  1109. for (int j = 0; j < QK_K/16; ++j) {
  1110. int l = nearest_int(iscale*mins[j]);
  1111. y[i].scales[j] |= (l << 4);
  1112. }
  1113. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1114. } else {
  1115. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1116. }
  1117. for (int j = 0; j < QK_K/16; ++j) {
  1118. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1119. if (!d) continue;
  1120. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1121. for (int ii = 0; ii < 16; ++ii) {
  1122. int l = nearest_int((x[16*j + ii] + dm)/d);
  1123. l = MAX(0, MIN(3, l));
  1124. L[16*j + ii] = l;
  1125. }
  1126. }
  1127. #if QK_K == 256
  1128. for (int j = 0; j < QK_K; j += 128) {
  1129. for (int l = 0; l < 32; ++l) {
  1130. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1131. }
  1132. }
  1133. #else
  1134. for (int l = 0; l < 16; ++l) {
  1135. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1136. }
  1137. #endif
  1138. x += QK_K;
  1139. }
  1140. }
  1141. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int64_t k) {
  1142. assert(k % QK_K == 0);
  1143. const int nb = k / QK_K;
  1144. for (int i = 0; i < nb; i++) {
  1145. const float d = GGML_FP16_TO_FP32(x[i].d);
  1146. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1147. const uint8_t * q = x[i].qs;
  1148. #if QK_K == 256
  1149. int is = 0;
  1150. float dl, ml;
  1151. for (int n = 0; n < QK_K; n += 128) {
  1152. int shift = 0;
  1153. for (int j = 0; j < 4; ++j) {
  1154. uint8_t sc = x[i].scales[is++];
  1155. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1156. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1157. sc = x[i].scales[is++];
  1158. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1159. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1160. shift += 2;
  1161. }
  1162. q += 32;
  1163. }
  1164. #else
  1165. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1166. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1167. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1168. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1169. for (int l = 0; l < 16; ++l) {
  1170. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1171. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1172. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1173. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1174. }
  1175. y += QK_K;
  1176. #endif
  1177. }
  1178. }
  1179. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int64_t k) {
  1180. quantize_row_q2_K_reference(x, vy, k);
  1181. }
  1182. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1183. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1184. float rmin, float rdelta, int nstep, bool use_mad) {
  1185. float min = x[0];
  1186. float max = x[0];
  1187. float sum_w = weights ? weights[0] : x[0]*x[0];
  1188. float sum_x = sum_w * x[0];
  1189. #ifdef HAVE_BUGGY_APPLE_LINKER
  1190. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1191. for (volatile int i = 1; i < n; ++i) {
  1192. #else
  1193. for (int i = 1; i < n; ++i) {
  1194. #endif
  1195. if (x[i] < min) min = x[i];
  1196. if (x[i] > max) max = x[i];
  1197. float w = weights ? weights[i] : x[i]*x[i];
  1198. sum_w += w;
  1199. sum_x += w * x[i];
  1200. }
  1201. if (min > 0) {
  1202. min = 0;
  1203. }
  1204. if (max <= min) {
  1205. memset(L, 0, n);
  1206. *the_min = -min;
  1207. return 0.f;
  1208. }
  1209. float iscale = nmax/(max - min);
  1210. float scale = 1/iscale;
  1211. float best_mad = 0;
  1212. for (int i = 0; i < n; ++i) {
  1213. int l = nearest_int(iscale*(x[i] - min));
  1214. L[i] = MAX(0, MIN(nmax, l));
  1215. float diff = scale * L[i] + min - x[i];
  1216. diff = use_mad ? fabsf(diff) : diff*diff;
  1217. float w = weights ? weights[i] : x[i]*x[i];
  1218. best_mad += w * diff;
  1219. }
  1220. if (nstep < 1) {
  1221. *the_min = -min;
  1222. return scale;
  1223. }
  1224. for (int is = 0; is <= nstep; ++is) {
  1225. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1226. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1227. for (int i = 0; i < n; ++i) {
  1228. int l = nearest_int(iscale*(x[i] - min));
  1229. l = MAX(0, MIN(nmax, l));
  1230. Laux[i] = l;
  1231. float w = weights ? weights[i] : x[i]*x[i];
  1232. sum_l += w*l;
  1233. sum_l2 += w*l*l;
  1234. sum_xl += w*l*x[i];
  1235. }
  1236. float D = sum_w * sum_l2 - sum_l * sum_l;
  1237. if (D > 0) {
  1238. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1239. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1240. if (this_min > 0) {
  1241. this_min = 0;
  1242. this_scale = sum_xl / sum_l2;
  1243. }
  1244. float mad = 0;
  1245. for (int i = 0; i < n; ++i) {
  1246. float diff = this_scale * Laux[i] + this_min - x[i];
  1247. diff = use_mad ? fabsf(diff) : diff*diff;
  1248. float w = weights ? weights[i] : x[i]*x[i];
  1249. mad += w * diff;
  1250. }
  1251. if (mad < best_mad) {
  1252. for (int i = 0; i < n; ++i) {
  1253. L[i] = Laux[i];
  1254. }
  1255. best_mad = mad;
  1256. scale = this_scale;
  1257. min = this_min;
  1258. }
  1259. }
  1260. }
  1261. *the_min = -min;
  1262. return scale;
  1263. }
  1264. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1265. float max = 0;
  1266. for (int i = 0; i < n; ++i) {
  1267. max = MAX(max, x[i]);
  1268. }
  1269. if (!max) { // all zero
  1270. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1271. return 0.f;
  1272. }
  1273. float iscale = nmax / max;
  1274. for (int i = 0; i < n; ++i) {
  1275. L[i] = nearest_int(iscale * x[i]);
  1276. }
  1277. float scale = 1/iscale;
  1278. float best_mse = 0;
  1279. for (int i = 0; i < n; ++i) {
  1280. float diff = x[i] - scale*L[i];
  1281. float w = quant_weights[i];
  1282. best_mse += w*diff*diff;
  1283. }
  1284. for (int is = -4; is <= 4; ++is) {
  1285. if (is == 0) continue;
  1286. float iscale_is = (0.1f*is + nmax)/max;
  1287. float scale_is = 1/iscale_is;
  1288. float mse = 0;
  1289. for (int i = 0; i < n; ++i) {
  1290. int l = nearest_int(iscale_is*x[i]);
  1291. l = MIN(nmax, l);
  1292. float diff = x[i] - scale_is*l;
  1293. float w = quant_weights[i];
  1294. mse += w*diff*diff;
  1295. }
  1296. if (mse < best_mse) {
  1297. best_mse = mse;
  1298. iscale = iscale_is;
  1299. }
  1300. }
  1301. float sumlx = 0;
  1302. float suml2 = 0;
  1303. for (int i = 0; i < n; ++i) {
  1304. int l = nearest_int(iscale * x[i]);
  1305. l = MIN(nmax, l);
  1306. L[i] = l;
  1307. float w = quant_weights[i];
  1308. sumlx += w*x[i]*l;
  1309. suml2 += w*l*l;
  1310. }
  1311. for (int itry = 0; itry < 5; ++itry) {
  1312. int n_changed = 0;
  1313. for (int i = 0; i < n; ++i) {
  1314. float w = quant_weights[i];
  1315. float slx = sumlx - w*x[i]*L[i];
  1316. float sl2 = suml2 - w*L[i]*L[i];
  1317. if (slx > 0 && sl2 > 0) {
  1318. int new_l = nearest_int(x[i] * sl2 / slx);
  1319. new_l = MIN(nmax, new_l);
  1320. if (new_l != L[i]) {
  1321. slx += w*x[i]*new_l;
  1322. sl2 += w*new_l*new_l;
  1323. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1324. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1325. ++n_changed;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. if (!n_changed) {
  1331. break;
  1332. }
  1333. }
  1334. return sumlx / suml2;
  1335. }
  1336. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1337. GGML_ASSERT(quant_weights);
  1338. assert(k % QK_K == 0);
  1339. const int nb = k / QK_K;
  1340. const bool requantize = true;
  1341. uint8_t L[QK_K];
  1342. uint8_t Laux[16];
  1343. float mins[QK_K/16];
  1344. float scales[QK_K/16];
  1345. float sw[QK_K/16];
  1346. float weight[16];
  1347. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1348. for (int i = 0; i < nb; i++) {
  1349. memset(sw, 0, QK_K/16*sizeof(float));
  1350. float sumx2 = 0;
  1351. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1352. float sigma2 = sumx2/QK_K;
  1353. for (int j = 0; j < QK_K/16; ++j) {
  1354. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1355. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1356. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1357. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1358. }
  1359. float dm, mm;
  1360. #if QK_K == 64
  1361. float max_scale = 0, max_min = 0;
  1362. for (int j = 0; j < QK_K/16; ++j) {
  1363. max_scale = MAX(max_scale, scales[j]);
  1364. max_min = MAX(max_min, mins[j]);
  1365. }
  1366. dm = max_scale/15;
  1367. mm = max_min/15;
  1368. if (max_scale) {
  1369. float id = 1/dm;
  1370. for (int j = 0; j < QK_K/16; ++j) {
  1371. int l = nearest_int(id*scales[j]);
  1372. Ls[j] = MAX(0, MIN(15, l));
  1373. }
  1374. } else {
  1375. memset(Ls, 0, QK_K/16);
  1376. }
  1377. if (max_min) {
  1378. float id = 1/mm;
  1379. for (int j = 0; j < QK_K/16; ++j) {
  1380. int l = nearest_int(id*mins[j]);
  1381. Lm[j] = MAX(0, MIN(15, l));
  1382. }
  1383. } else {
  1384. memset(Lm, 0, QK_K/16);
  1385. }
  1386. #else
  1387. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1388. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1389. #endif
  1390. y[i].d = GGML_FP32_TO_FP16(dm);
  1391. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1392. dm = GGML_FP16_TO_FP32(y[i].d);
  1393. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1394. for (int j = 0; j < QK_K/16; ++j) {
  1395. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1396. }
  1397. if (requantize) {
  1398. for (int j = 0; j < QK_K/16; ++j) {
  1399. const float d = dm * (y[i].scales[j] & 0xF);
  1400. if (!d) continue;
  1401. const float m = mm * (y[i].scales[j] >> 4);
  1402. for (int ii = 0; ii < 16; ++ii) {
  1403. int l = nearest_int((x[16*j + ii] + m)/d);
  1404. l = MAX(0, MIN(3, l));
  1405. L[16*j + ii] = l;
  1406. }
  1407. }
  1408. }
  1409. #if QK_K == 256
  1410. for (int j = 0; j < QK_K; j += 128) {
  1411. for (int l = 0; l < 32; ++l) {
  1412. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1413. }
  1414. }
  1415. #else
  1416. for (int l = 0; l < 16; ++l) {
  1417. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1418. }
  1419. #endif
  1420. x += QK_K;
  1421. }
  1422. }
  1423. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1424. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1425. if (!quant_weights) {
  1426. quantize_row_q2_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1427. }
  1428. else {
  1429. char * qrow = (char *)dst;
  1430. for (int64_t row = 0; row < nrow; ++row) {
  1431. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1432. src += n_per_row;
  1433. qrow += row_size;
  1434. }
  1435. }
  1436. return nrow * row_size;
  1437. }
  1438. //========================= 3-bit (de)-quantization
  1439. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int64_t k) {
  1440. assert(k % QK_K == 0);
  1441. const int nb = k / QK_K;
  1442. int8_t L[QK_K];
  1443. float scales[QK_K / 16];
  1444. for (int i = 0; i < nb; i++) {
  1445. float max_scale = 0;
  1446. float amax = 0;
  1447. for (int j = 0; j < QK_K/16; ++j) {
  1448. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1449. float scale = fabsf(scales[j]);
  1450. if (scale > amax) {
  1451. amax = scale; max_scale = scales[j];
  1452. }
  1453. }
  1454. #if QK_K == 256
  1455. memset(y[i].scales, 0, 12);
  1456. if (max_scale) {
  1457. float iscale = -32.f/max_scale;
  1458. for (int j = 0; j < QK_K/16; ++j) {
  1459. int8_t l = nearest_int(iscale*scales[j]);
  1460. l = MAX(-32, MIN(31, l)) + 32;
  1461. if (j < 8) {
  1462. y[i].scales[j] = l & 0xF;
  1463. } else {
  1464. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1465. }
  1466. l >>= 4;
  1467. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1468. }
  1469. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1470. } else {
  1471. y[i].d = GGML_FP32_TO_FP16(0.f);
  1472. }
  1473. int8_t sc;
  1474. for (int j = 0; j < QK_K/16; ++j) {
  1475. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1476. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1477. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1478. if (!d) {
  1479. continue;
  1480. }
  1481. for (int ii = 0; ii < 16; ++ii) {
  1482. int l = nearest_int(x[16*j + ii]/d);
  1483. l = MAX(-4, MIN(3, l));
  1484. L[16*j + ii] = l + 4;
  1485. }
  1486. }
  1487. #else
  1488. if (max_scale) {
  1489. float iscale = -8.f/max_scale;
  1490. for (int j = 0; j < QK_K/16; j+=2) {
  1491. int l1 = nearest_int(iscale*scales[j]);
  1492. l1 = 8 + MAX(-8, MIN(7, l1));
  1493. int l2 = nearest_int(iscale*scales[j+1]);
  1494. l2 = 8 + MAX(-8, MIN(7, l2));
  1495. y[i].scales[j/2] = l1 | (l2 << 4);
  1496. }
  1497. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1498. } else {
  1499. for (int j = 0; j < QK_K/16; j+=2) {
  1500. y[i].scales[j/2] = 0;
  1501. }
  1502. y[i].d = GGML_FP32_TO_FP16(0.f);
  1503. }
  1504. for (int j = 0; j < QK_K/16; ++j) {
  1505. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1506. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1507. if (!d) {
  1508. continue;
  1509. }
  1510. for (int ii = 0; ii < 16; ++ii) {
  1511. int l = nearest_int(x[16*j + ii]/d);
  1512. l = MAX(-4, MIN(3, l));
  1513. L[16*j + ii] = l + 4;
  1514. }
  1515. }
  1516. #endif
  1517. memset(y[i].hmask, 0, QK_K/8);
  1518. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1519. int m = 0;
  1520. uint8_t hm = 1;
  1521. for (int j = 0; j < QK_K; ++j) {
  1522. if (L[j] > 3) {
  1523. y[i].hmask[m] |= hm;
  1524. L[j] -= 4;
  1525. }
  1526. if (++m == QK_K/8) {
  1527. m = 0; hm <<= 1;
  1528. }
  1529. }
  1530. #if QK_K == 256
  1531. for (int j = 0; j < QK_K; j += 128) {
  1532. for (int l = 0; l < 32; ++l) {
  1533. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1534. }
  1535. }
  1536. #else
  1537. for (int l = 0; l < 16; ++l) {
  1538. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1539. }
  1540. #endif
  1541. x += QK_K;
  1542. }
  1543. }
  1544. #if QK_K == 256
  1545. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1546. assert(k % QK_K == 0);
  1547. const int nb = k / QK_K;
  1548. const uint32_t kmask1 = 0x03030303;
  1549. const uint32_t kmask2 = 0x0f0f0f0f;
  1550. uint32_t aux[4];
  1551. const int8_t * scales = (const int8_t*)aux;
  1552. for (int i = 0; i < nb; i++) {
  1553. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1554. const uint8_t * restrict q = x[i].qs;
  1555. const uint8_t * restrict hm = x[i].hmask;
  1556. uint8_t m = 1;
  1557. memcpy(aux, x[i].scales, 12);
  1558. uint32_t tmp = aux[2];
  1559. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1560. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1561. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1562. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1563. int is = 0;
  1564. float dl;
  1565. for (int n = 0; n < QK_K; n += 128) {
  1566. int shift = 0;
  1567. for (int j = 0; j < 4; ++j) {
  1568. dl = d_all * (scales[is++] - 32);
  1569. for (int l = 0; l < 16; ++l) {
  1570. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1571. }
  1572. dl = d_all * (scales[is++] - 32);
  1573. for (int l = 0; l < 16; ++l) {
  1574. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1575. }
  1576. shift += 2;
  1577. m <<= 1;
  1578. }
  1579. q += 32;
  1580. }
  1581. }
  1582. }
  1583. #else
  1584. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1585. assert(k % QK_K == 0);
  1586. assert(QK_K == 64);
  1587. const int nb = k / QK_K;
  1588. for (int i = 0; i < nb; i++) {
  1589. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1590. const uint8_t * restrict q = x[i].qs;
  1591. const uint8_t * restrict hm = x[i].hmask;
  1592. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1593. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1594. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1595. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1596. for (int l=0; l<8; ++l) {
  1597. uint8_t h = hm[l];
  1598. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1599. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1600. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1601. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1602. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1603. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1604. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1605. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1606. }
  1607. y += QK_K;
  1608. }
  1609. }
  1610. #endif
  1611. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int64_t k) {
  1612. quantize_row_q3_K_reference(x, vy, k);
  1613. }
  1614. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int64_t n_per_row, const float * restrict quant_weights) {
  1615. #if QK_K != 256
  1616. (void)quant_weights;
  1617. quantize_row_q3_K_reference(x, y, n_per_row);
  1618. #else
  1619. assert(n_per_row % QK_K == 0);
  1620. const int nb = n_per_row / QK_K;
  1621. int8_t L[QK_K];
  1622. float scales[QK_K / 16];
  1623. float weight[16];
  1624. float sw[QK_K / 16];
  1625. int8_t Ls[QK_K / 16];
  1626. for (int i = 0; i < nb; i++) {
  1627. float sumx2 = 0;
  1628. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1629. float sigma2 = 2*sumx2/QK_K;
  1630. for (int j = 0; j < QK_K/16; ++j) {
  1631. if (quant_weights) {
  1632. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1633. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1634. } else {
  1635. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1636. }
  1637. float sumw = 0;
  1638. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1639. sw[j] = sumw;
  1640. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1641. }
  1642. memset(y[i].scales, 0, 12);
  1643. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1644. for (int j = 0; j < QK_K/16; ++j) {
  1645. int l = Ls[j];
  1646. if (j < 8) {
  1647. y[i].scales[j] = l & 0xF;
  1648. } else {
  1649. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1650. }
  1651. l >>= 4;
  1652. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1653. }
  1654. y[i].d = GGML_FP32_TO_FP16(d_block);
  1655. int8_t sc;
  1656. for (int j = 0; j < QK_K/16; ++j) {
  1657. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1658. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1659. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1660. if (!d) {
  1661. continue;
  1662. }
  1663. for (int ii = 0; ii < 16; ++ii) {
  1664. int l = nearest_int(x[16*j + ii]/d);
  1665. l = MAX(-4, MIN(3, l));
  1666. L[16*j + ii] = l + 4;
  1667. }
  1668. }
  1669. memset(y[i].hmask, 0, QK_K/8);
  1670. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1671. int m = 0;
  1672. uint8_t hm = 1;
  1673. for (int j = 0; j < QK_K; ++j) {
  1674. if (L[j] > 3) {
  1675. y[i].hmask[m] |= hm;
  1676. L[j] -= 4;
  1677. }
  1678. if (++m == QK_K/8) {
  1679. m = 0; hm <<= 1;
  1680. }
  1681. }
  1682. for (int j = 0; j < QK_K; j += 128) {
  1683. for (int l = 0; l < 32; ++l) {
  1684. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1685. }
  1686. }
  1687. x += QK_K;
  1688. }
  1689. #endif
  1690. }
  1691. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1692. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1693. if (!quant_weights) {
  1694. quantize_row_q3_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1695. }
  1696. else {
  1697. char * qrow = (char *)dst;
  1698. for (int64_t row = 0; row < nrow; ++row) {
  1699. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1700. src += n_per_row;
  1701. qrow += row_size;
  1702. }
  1703. }
  1704. return nrow * row_size;
  1705. }
  1706. // ====================== 4-bit (de)-quantization
  1707. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int64_t k) {
  1708. assert(k % QK_K == 0);
  1709. const int nb = k / QK_K;
  1710. uint8_t L[QK_K];
  1711. uint8_t Laux[32];
  1712. float weights[32];
  1713. float mins[QK_K/32];
  1714. float scales[QK_K/32];
  1715. for (int i = 0; i < nb; i++) {
  1716. float max_scale = 0; // as we are deducting the min, scales are always positive
  1717. float max_min = 0;
  1718. for (int j = 0; j < QK_K/32; ++j) {
  1719. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1720. float sum_x2 = 0;
  1721. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1722. float av_x = sqrtf(sum_x2/32);
  1723. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1724. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1725. float scale = scales[j];
  1726. if (scale > max_scale) {
  1727. max_scale = scale;
  1728. }
  1729. float min = mins[j];
  1730. if (min > max_min) {
  1731. max_min = min;
  1732. }
  1733. }
  1734. #if QK_K == 256
  1735. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1736. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1737. for (int j = 0; j < QK_K/32; ++j) {
  1738. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1739. uint8_t lm = nearest_int(inv_min*mins[j]);
  1740. ls = MIN(63, ls);
  1741. lm = MIN(63, lm);
  1742. if (j < 4) {
  1743. y[i].scales[j] = ls;
  1744. y[i].scales[j+4] = lm;
  1745. } else {
  1746. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1747. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1748. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1749. }
  1750. }
  1751. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1752. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1753. uint8_t sc, m;
  1754. for (int j = 0; j < QK_K/32; ++j) {
  1755. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1756. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1757. if (!d) continue;
  1758. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1759. for (int ii = 0; ii < 32; ++ii) {
  1760. int l = nearest_int((x[32*j + ii] + dm)/d);
  1761. l = MAX(0, MIN(15, l));
  1762. L[32*j + ii] = l;
  1763. }
  1764. }
  1765. #else
  1766. const float s_factor = 15.f;
  1767. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1768. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1769. int d1 = nearest_int(inv_scale*scales[0]);
  1770. int m1 = nearest_int(inv_min*mins[0]);
  1771. int d2 = nearest_int(inv_scale*scales[1]);
  1772. int m2 = nearest_int(inv_min*mins[1]);
  1773. y[i].scales[0] = d1 | (m1 << 4);
  1774. y[i].scales[1] = d2 | (m2 << 4);
  1775. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1776. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1777. float sumlx = 0;
  1778. int suml2 = 0;
  1779. for (int j = 0; j < QK_K/32; ++j) {
  1780. const uint8_t sd = y[i].scales[j] & 0xF;
  1781. const uint8_t sm = y[i].scales[j] >> 4;
  1782. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1783. if (!d) continue;
  1784. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1785. for (int ii = 0; ii < 32; ++ii) {
  1786. int l = nearest_int((x[32*j + ii] + m)/d);
  1787. l = MAX(0, MIN(15, l));
  1788. L[32*j + ii] = l;
  1789. sumlx += (x[32*j + ii] + m)*l*sd;
  1790. suml2 += l*l*sd*sd;
  1791. }
  1792. }
  1793. if (suml2) {
  1794. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1795. }
  1796. #endif
  1797. uint8_t * q = y[i].qs;
  1798. for (int j = 0; j < QK_K; j += 64) {
  1799. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1800. q += 32;
  1801. }
  1802. x += QK_K;
  1803. }
  1804. }
  1805. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int64_t k) {
  1806. assert(k % QK_K == 0);
  1807. const int nb = k / QK_K;
  1808. for (int i = 0; i < nb; i++) {
  1809. const uint8_t * q = x[i].qs;
  1810. #if QK_K == 256
  1811. const float d = GGML_FP16_TO_FP32(x[i].d);
  1812. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1813. int is = 0;
  1814. uint8_t sc, m;
  1815. for (int j = 0; j < QK_K; j += 64) {
  1816. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1817. const float d1 = d * sc; const float m1 = min * m;
  1818. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1819. const float d2 = d * sc; const float m2 = min * m;
  1820. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1821. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1822. q += 32; is += 2;
  1823. }
  1824. #else
  1825. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1826. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1827. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1828. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1829. for (int l = 0; l < 32; ++l) {
  1830. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1831. y[l+32] = d2 * (q[l] >> 4) - m2;
  1832. }
  1833. y += QK_K;
  1834. #endif
  1835. }
  1836. }
  1837. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int64_t k) {
  1838. assert(k % QK_K == 0);
  1839. block_q4_K * restrict y = vy;
  1840. quantize_row_q4_K_reference(x, y, k);
  1841. }
  1842. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  1843. #if QK_K != 256
  1844. (void)quant_weights;
  1845. quantize_row_q4_K_reference(x, y, n_per_row);
  1846. #else
  1847. assert(n_per_row % QK_K == 0);
  1848. const int64_t nb = n_per_row / QK_K;
  1849. uint8_t L[QK_K];
  1850. uint8_t Laux[32];
  1851. uint8_t Ls[QK_K/32];
  1852. uint8_t Lm[QK_K/32];
  1853. float weights[32];
  1854. float sw[QK_K/32];
  1855. float mins[QK_K/32];
  1856. float scales[QK_K/32];
  1857. for (int i = 0; i < nb; i++) {
  1858. float sum_x2 = 0;
  1859. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1860. float sigma2 = 2*sum_x2/QK_K;
  1861. float av_x = sqrtf(sigma2);
  1862. for (int j = 0; j < QK_K/32; ++j) {
  1863. if (quant_weights) {
  1864. const float * qw = quant_weights + QK_K*i + 32*j;
  1865. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1866. } else {
  1867. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1868. }
  1869. float sumw = 0;
  1870. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1871. sw[j] = sumw;
  1872. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1873. }
  1874. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1875. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1876. for (int j = 0; j < QK_K/32; ++j) {
  1877. uint8_t ls = Ls[j];
  1878. uint8_t lm = Lm[j];
  1879. if (j < 4) {
  1880. y[i].scales[j] = ls;
  1881. y[i].scales[j+4] = lm;
  1882. } else {
  1883. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1884. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1885. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1886. }
  1887. }
  1888. y[i].d = GGML_FP32_TO_FP16(d_block);
  1889. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1890. uint8_t sc, m;
  1891. for (int j = 0; j < QK_K/32; ++j) {
  1892. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1893. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1894. if (!d) continue;
  1895. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1896. for (int ii = 0; ii < 32; ++ii) {
  1897. int l = nearest_int((x[32*j + ii] + dm)/d);
  1898. l = MAX(0, MIN(15, l));
  1899. L[32*j + ii] = l;
  1900. }
  1901. }
  1902. uint8_t * q = y[i].qs;
  1903. for (int j = 0; j < QK_K; j += 64) {
  1904. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1905. q += 32;
  1906. }
  1907. x += QK_K;
  1908. }
  1909. #endif
  1910. }
  1911. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1912. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  1913. if (!quant_weights) {
  1914. quantize_row_q4_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1915. }
  1916. else {
  1917. char * qrow = (char *)dst;
  1918. for (int64_t row = 0; row < nrow; ++row) {
  1919. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  1920. src += n_per_row;
  1921. qrow += row_size;
  1922. }
  1923. }
  1924. return nrow * row_size;
  1925. }
  1926. // ====================== 5-bit (de)-quantization
  1927. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int64_t k) {
  1928. assert(k % QK_K == 0);
  1929. const int64_t nb = k / QK_K;
  1930. #if QK_K == 256
  1931. uint8_t L[QK_K];
  1932. float mins[QK_K/32];
  1933. float scales[QK_K/32];
  1934. float weights[32];
  1935. uint8_t Laux[32];
  1936. #else
  1937. int8_t L[QK_K];
  1938. float scales[QK_K/16];
  1939. #endif
  1940. for (int i = 0; i < nb; i++) {
  1941. #if QK_K == 256
  1942. float max_scale = 0; // as we are deducting the min, scales are always positive
  1943. float max_min = 0;
  1944. for (int j = 0; j < QK_K/32; ++j) {
  1945. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1946. float sum_x2 = 0;
  1947. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1948. float av_x = sqrtf(sum_x2/32);
  1949. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1950. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1951. float scale = scales[j];
  1952. if (scale > max_scale) {
  1953. max_scale = scale;
  1954. }
  1955. float min = mins[j];
  1956. if (min > max_min) {
  1957. max_min = min;
  1958. }
  1959. }
  1960. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1961. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1962. for (int j = 0; j < QK_K/32; ++j) {
  1963. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1964. uint8_t lm = nearest_int(inv_min*mins[j]);
  1965. ls = MIN(63, ls);
  1966. lm = MIN(63, lm);
  1967. if (j < 4) {
  1968. y[i].scales[j] = ls;
  1969. y[i].scales[j+4] = lm;
  1970. } else {
  1971. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1972. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1973. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1974. }
  1975. }
  1976. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1977. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1978. uint8_t sc, m;
  1979. for (int j = 0; j < QK_K/32; ++j) {
  1980. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1981. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1982. if (!d) continue;
  1983. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1984. for (int ii = 0; ii < 32; ++ii) {
  1985. int l = nearest_int((x[32*j + ii] + dm)/d);
  1986. l = MAX(0, MIN(31, l));
  1987. L[32*j + ii] = l;
  1988. }
  1989. }
  1990. uint8_t * restrict qh = y[i].qh;
  1991. uint8_t * restrict ql = y[i].qs;
  1992. memset(qh, 0, QK_K/8);
  1993. uint8_t m1 = 1, m2 = 2;
  1994. for (int n = 0; n < QK_K; n += 64) {
  1995. for (int j = 0; j < 32; ++j) {
  1996. int l1 = L[n + j];
  1997. if (l1 > 15) {
  1998. l1 -= 16; qh[j] |= m1;
  1999. }
  2000. int l2 = L[n + j + 32];
  2001. if (l2 > 15) {
  2002. l2 -= 16; qh[j] |= m2;
  2003. }
  2004. ql[j] = l1 | (l2 << 4);
  2005. }
  2006. m1 <<= 2; m2 <<= 2;
  2007. ql += 32;
  2008. }
  2009. #else
  2010. float max_scale = 0, amax = 0;
  2011. for (int j = 0; j < QK_K/16; ++j) {
  2012. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2013. float abs_scale = fabsf(scales[j]);
  2014. if (abs_scale > amax) {
  2015. amax = abs_scale;
  2016. max_scale = scales[j];
  2017. }
  2018. }
  2019. float iscale = -128.f/max_scale;
  2020. for (int j = 0; j < QK_K/16; ++j) {
  2021. int l = nearest_int(iscale*scales[j]);
  2022. y[i].scales[j] = MAX(-128, MIN(127, l));
  2023. }
  2024. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2025. for (int j = 0; j < QK_K/16; ++j) {
  2026. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2027. if (!d) continue;
  2028. for (int ii = 0; ii < 16; ++ii) {
  2029. int l = nearest_int(x[16*j + ii]/d);
  2030. l = MAX(-16, MIN(15, l));
  2031. L[16*j + ii] = l + 16;
  2032. }
  2033. }
  2034. uint8_t * restrict qh = y[i].qh;
  2035. uint8_t * restrict ql = y[i].qs;
  2036. memset(qh, 0, QK_K/8);
  2037. for (int j = 0; j < 32; ++j) {
  2038. int jm = j%8;
  2039. int is = j/8;
  2040. int l1 = L[j];
  2041. if (l1 > 15) {
  2042. l1 -= 16; qh[jm] |= (1 << is);
  2043. }
  2044. int l2 = L[j + 32];
  2045. if (l2 > 15) {
  2046. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2047. }
  2048. ql[j] = l1 | (l2 << 4);
  2049. }
  2050. #endif
  2051. x += QK_K;
  2052. }
  2053. }
  2054. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int64_t k) {
  2055. assert(k % QK_K == 0);
  2056. const int64_t nb = k / QK_K;
  2057. for (int i = 0; i < nb; i++) {
  2058. const uint8_t * ql = x[i].qs;
  2059. const uint8_t * qh = x[i].qh;
  2060. #if QK_K == 256
  2061. const float d = GGML_FP16_TO_FP32(x[i].d);
  2062. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2063. int is = 0;
  2064. uint8_t sc, m;
  2065. uint8_t u1 = 1, u2 = 2;
  2066. for (int j = 0; j < QK_K; j += 64) {
  2067. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2068. const float d1 = d * sc; const float m1 = min * m;
  2069. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2070. const float d2 = d * sc; const float m2 = min * m;
  2071. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2072. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2073. ql += 32; is += 2;
  2074. u1 <<= 2; u2 <<= 2;
  2075. }
  2076. #else
  2077. float d = GGML_FP16_TO_FP32(x[i].d);
  2078. const int8_t * restrict s = x[i].scales;
  2079. for (int l = 0; l < 8; ++l) {
  2080. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2081. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2082. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2083. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2084. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2085. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2086. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2087. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2088. }
  2089. y += QK_K;
  2090. #endif
  2091. }
  2092. }
  2093. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int64_t k) {
  2094. assert(k % QK_K == 0);
  2095. block_q5_K * restrict y = vy;
  2096. quantize_row_q5_K_reference(x, y, k);
  2097. }
  2098. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2099. #if QK_K != 256
  2100. (void)quant_weights;
  2101. quantize_row_q5_K_reference(x, y, n_per_row);
  2102. #else
  2103. assert(n_per_row % QK_K == 0);
  2104. const int64_t nb = n_per_row / QK_K;
  2105. uint8_t L[QK_K];
  2106. uint8_t Laux[32];
  2107. uint8_t Ls[QK_K/32];
  2108. uint8_t Lm[QK_K/32];
  2109. float mins[QK_K/32];
  2110. float scales[QK_K/32];
  2111. float sw[QK_K/32];
  2112. float weights[32];
  2113. for (int i = 0; i < nb; i++) {
  2114. float sum_x2 = 0;
  2115. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2116. float sigma2 = 2*sum_x2/QK_K;
  2117. float av_x = sqrtf(sigma2);
  2118. for (int j = 0; j < QK_K/32; ++j) {
  2119. if (quant_weights) {
  2120. const float * qw = quant_weights + QK_K*i + 32*j;
  2121. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2122. } else {
  2123. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2124. }
  2125. float sumw = 0;
  2126. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2127. sw[j] = sumw;
  2128. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2129. }
  2130. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2131. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2132. for (int j = 0; j < QK_K/32; ++j) {
  2133. uint8_t ls = Ls[j];
  2134. uint8_t lm = Lm[j];
  2135. ls = MIN(63, ls);
  2136. lm = MIN(63, lm);
  2137. if (j < 4) {
  2138. y[i].scales[j] = ls;
  2139. y[i].scales[j+4] = lm;
  2140. } else {
  2141. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2142. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2143. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2144. }
  2145. }
  2146. y[i].d = GGML_FP32_TO_FP16(d_block);
  2147. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2148. uint8_t sc, m;
  2149. for (int j = 0; j < QK_K/32; ++j) {
  2150. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2151. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2152. if (!d) continue;
  2153. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2154. for (int ii = 0; ii < 32; ++ii) {
  2155. int l = nearest_int((x[32*j + ii] + dm)/d);
  2156. l = MAX(0, MIN(31, l));
  2157. L[32*j + ii] = l;
  2158. }
  2159. }
  2160. uint8_t * restrict qh = y[i].qh;
  2161. uint8_t * restrict ql = y[i].qs;
  2162. memset(qh, 0, QK_K/8);
  2163. uint8_t m1 = 1, m2 = 2;
  2164. for (int n = 0; n < QK_K; n += 64) {
  2165. for (int j = 0; j < 32; ++j) {
  2166. int l1 = L[n + j];
  2167. if (l1 > 15) {
  2168. l1 -= 16; qh[j] |= m1;
  2169. }
  2170. int l2 = L[n + j + 32];
  2171. if (l2 > 15) {
  2172. l2 -= 16; qh[j] |= m2;
  2173. }
  2174. ql[j] = l1 | (l2 << 4);
  2175. }
  2176. m1 <<= 2; m2 <<= 2;
  2177. ql += 32;
  2178. }
  2179. x += QK_K;
  2180. }
  2181. #endif
  2182. }
  2183. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2184. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2185. if (!quant_weights) {
  2186. quantize_row_q5_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2187. }
  2188. else {
  2189. char * qrow = (char *)dst;
  2190. for (int64_t row = 0; row < nrow; ++row) {
  2191. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2192. src += n_per_row;
  2193. qrow += row_size;
  2194. }
  2195. }
  2196. return nrow * row_size;
  2197. }
  2198. // ====================== 6-bit (de)-quantization
  2199. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int64_t k) {
  2200. assert(k % QK_K == 0);
  2201. const int64_t nb = k / QK_K;
  2202. int8_t L[QK_K];
  2203. float scales[QK_K/16];
  2204. for (int i = 0; i < nb; i++) {
  2205. float max_scale = 0;
  2206. float max_abs_scale = 0;
  2207. for (int ib = 0; ib < QK_K/16; ++ib) {
  2208. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2209. scales[ib] = scale;
  2210. const float abs_scale = fabsf(scale);
  2211. if (abs_scale > max_abs_scale) {
  2212. max_abs_scale = abs_scale;
  2213. max_scale = scale;
  2214. }
  2215. }
  2216. if (!max_abs_scale) {
  2217. memset(&y[i], 0, sizeof(block_q6_K));
  2218. y[i].d = GGML_FP32_TO_FP16(0.f);
  2219. x += QK_K;
  2220. continue;
  2221. }
  2222. float iscale = -128.f/max_scale;
  2223. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2224. for (int ib = 0; ib < QK_K/16; ++ib) {
  2225. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2226. }
  2227. for (int j = 0; j < QK_K/16; ++j) {
  2228. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2229. if (!d) {
  2230. continue;
  2231. }
  2232. for (int ii = 0; ii < 16; ++ii) {
  2233. int l = nearest_int(x[16*j + ii]/d);
  2234. l = MAX(-32, MIN(31, l));
  2235. L[16*j + ii] = l + 32;
  2236. }
  2237. }
  2238. uint8_t * restrict ql = y[i].ql;
  2239. uint8_t * restrict qh = y[i].qh;
  2240. #if QK_K == 256
  2241. for (int j = 0; j < QK_K; j += 128) {
  2242. for (int l = 0; l < 32; ++l) {
  2243. const uint8_t q1 = L[j + l + 0] & 0xF;
  2244. const uint8_t q2 = L[j + l + 32] & 0xF;
  2245. const uint8_t q3 = L[j + l + 64] & 0xF;
  2246. const uint8_t q4 = L[j + l + 96] & 0xF;
  2247. ql[l+ 0] = q1 | (q3 << 4);
  2248. ql[l+32] = q2 | (q4 << 4);
  2249. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2250. }
  2251. ql += 64;
  2252. qh += 32;
  2253. }
  2254. #else
  2255. for (int l = 0; l < 32; ++l) {
  2256. const uint8_t q1 = L[l + 0] & 0xF;
  2257. const uint8_t q2 = L[l + 32] & 0xF;
  2258. ql[l] = q1 | (q2 << 4);
  2259. }
  2260. for (int l = 0; l < 16; ++l) {
  2261. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2262. }
  2263. #endif
  2264. x += QK_K;
  2265. }
  2266. }
  2267. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int64_t k) {
  2268. assert(k % QK_K == 0);
  2269. const int64_t nb = k / QK_K;
  2270. for (int i = 0; i < nb; i++) {
  2271. const float d = GGML_FP16_TO_FP32(x[i].d);
  2272. const uint8_t * restrict ql = x[i].ql;
  2273. const uint8_t * restrict qh = x[i].qh;
  2274. const int8_t * restrict sc = x[i].scales;
  2275. #if QK_K == 256
  2276. for (int n = 0; n < QK_K; n += 128) {
  2277. for (int l = 0; l < 32; ++l) {
  2278. int is = l/16;
  2279. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2280. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2281. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2282. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2283. y[l + 0] = d * sc[is + 0] * q1;
  2284. y[l + 32] = d * sc[is + 2] * q2;
  2285. y[l + 64] = d * sc[is + 4] * q3;
  2286. y[l + 96] = d * sc[is + 6] * q4;
  2287. }
  2288. y += 128;
  2289. ql += 64;
  2290. qh += 32;
  2291. sc += 8;
  2292. }
  2293. #else
  2294. for (int l = 0; l < 16; ++l) {
  2295. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2296. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2297. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2298. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2299. y[l+ 0] = d * sc[0] * q1;
  2300. y[l+16] = d * sc[1] * q2;
  2301. y[l+32] = d * sc[2] * q3;
  2302. y[l+48] = d * sc[3] * q4;
  2303. }
  2304. y += 64;
  2305. #endif
  2306. }
  2307. }
  2308. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int64_t k) {
  2309. assert(k % QK_K == 0);
  2310. block_q6_K * restrict y = vy;
  2311. quantize_row_q6_K_reference(x, y, k);
  2312. }
  2313. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2314. #if QK_K != 256
  2315. (void)quant_weights;
  2316. quantize_row_q6_K_reference(x, y, n_per_row);
  2317. #else
  2318. assert(n_per_row % QK_K == 0);
  2319. const int64_t nb = n_per_row / QK_K;
  2320. int8_t L[QK_K];
  2321. float scales[QK_K/16];
  2322. //float weights[16];
  2323. for (int i = 0; i < nb; i++) {
  2324. //float sum_x2 = 0;
  2325. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2326. //float sigma2 = sum_x2/QK_K;
  2327. float max_scale = 0;
  2328. float max_abs_scale = 0;
  2329. for (int ib = 0; ib < QK_K/16; ++ib) {
  2330. float scale;
  2331. if (quant_weights) {
  2332. const float * qw = quant_weights + QK_K*i + 16*ib;
  2333. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2334. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2335. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2336. } else {
  2337. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2338. }
  2339. scales[ib] = scale;
  2340. const float abs_scale = fabsf(scale);
  2341. if (abs_scale > max_abs_scale) {
  2342. max_abs_scale = abs_scale;
  2343. max_scale = scale;
  2344. }
  2345. }
  2346. if (!max_abs_scale) {
  2347. memset(&y[i], 0, sizeof(block_q6_K));
  2348. y[i].d = GGML_FP32_TO_FP16(0.f);
  2349. x += QK_K;
  2350. continue;
  2351. }
  2352. float iscale = -128.f/max_scale;
  2353. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2354. for (int ib = 0; ib < QK_K/16; ++ib) {
  2355. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2356. }
  2357. for (int j = 0; j < QK_K/16; ++j) {
  2358. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2359. if (!d) {
  2360. continue;
  2361. }
  2362. for (int ii = 0; ii < 16; ++ii) {
  2363. int l = nearest_int(x[16*j + ii]/d);
  2364. l = MAX(-32, MIN(31, l));
  2365. L[16*j + ii] = l + 32;
  2366. }
  2367. }
  2368. uint8_t * restrict ql = y[i].ql;
  2369. uint8_t * restrict qh = y[i].qh;
  2370. for (int j = 0; j < QK_K; j += 128) {
  2371. for (int l = 0; l < 32; ++l) {
  2372. const uint8_t q1 = L[j + l + 0] & 0xF;
  2373. const uint8_t q2 = L[j + l + 32] & 0xF;
  2374. const uint8_t q3 = L[j + l + 64] & 0xF;
  2375. const uint8_t q4 = L[j + l + 96] & 0xF;
  2376. ql[l+ 0] = q1 | (q3 << 4);
  2377. ql[l+32] = q2 | (q4 << 4);
  2378. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2379. }
  2380. ql += 64;
  2381. qh += 32;
  2382. }
  2383. x += QK_K;
  2384. }
  2385. #endif
  2386. }
  2387. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2388. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2389. if (!quant_weights) {
  2390. quantize_row_q6_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2391. }
  2392. else {
  2393. char * qrow = (char *)dst;
  2394. for (int64_t row = 0; row < nrow; ++row) {
  2395. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2396. src += n_per_row;
  2397. qrow += row_size;
  2398. }
  2399. }
  2400. return nrow * row_size;
  2401. }
  2402. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2403. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2404. if (!quant_weights) {
  2405. quantize_row_q4_0_reference(x, y, n_per_row);
  2406. return;
  2407. }
  2408. float weight[QK4_0];
  2409. int8_t L[QK4_0];
  2410. float sum_x2 = 0;
  2411. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2412. float sigma2 = sum_x2/n_per_row;
  2413. const int64_t nb = n_per_row/QK4_0;
  2414. for (int ib = 0; ib < nb; ++ib) {
  2415. const float * xb = x + QK4_0 * ib;
  2416. const float * qw = quant_weights + QK4_0 * ib;
  2417. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2418. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2419. y[ib].d = GGML_FP32_TO_FP16(d);
  2420. for (int j = 0; j < 16; ++j) {
  2421. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2422. }
  2423. }
  2424. }
  2425. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2426. if (!quant_weights) {
  2427. quantize_row_q4_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2428. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2429. }
  2430. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2431. char * qrow = (char *)dst;
  2432. for (int64_t row = 0; row < nrow; ++row) {
  2433. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2434. src += n_per_row;
  2435. qrow += row_size;
  2436. }
  2437. return nrow * row_size;
  2438. }
  2439. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2440. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2441. if (!quant_weights) {
  2442. quantize_row_q4_1_reference(x, y, n_per_row);
  2443. return;
  2444. }
  2445. float weight[QK4_1];
  2446. uint8_t L[QK4_1], Laux[QK4_1];
  2447. float sum_x2 = 0;
  2448. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2449. float sigma2 = sum_x2/n_per_row;
  2450. const int64_t nb = n_per_row/QK4_1;
  2451. for (int ib = 0; ib < nb; ++ib) {
  2452. const float * xb = x + QK4_1 * ib;
  2453. const float * qw = quant_weights + QK4_1 * ib;
  2454. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2455. float min;
  2456. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2457. y[ib].d = GGML_FP32_TO_FP16(d);
  2458. y[ib].m = GGML_FP32_TO_FP16(-min);
  2459. for (int j = 0; j < 16; ++j) {
  2460. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2461. }
  2462. }
  2463. }
  2464. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2465. if (!quant_weights) {
  2466. quantize_row_q4_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2467. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2468. }
  2469. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2470. char * qrow = (char *)dst;
  2471. for (int64_t row = 0; row < nrow; ++row) {
  2472. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2473. src += n_per_row;
  2474. qrow += row_size;
  2475. }
  2476. return nrow * row_size;
  2477. }
  2478. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2479. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2480. if (!quant_weights) {
  2481. quantize_row_q5_0_reference(x, y, n_per_row);
  2482. return;
  2483. }
  2484. float weight[QK5_0];
  2485. int8_t L[QK5_0];
  2486. float sum_x2 = 0;
  2487. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2488. float sigma2 = sum_x2/n_per_row;
  2489. const int64_t nb = n_per_row/QK5_0;
  2490. for (int ib = 0; ib < nb; ++ib) {
  2491. const float * xb = x + QK5_0 * ib;
  2492. const float * qw = quant_weights + QK5_0 * ib;
  2493. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2494. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2495. y[ib].d = GGML_FP32_TO_FP16(d);
  2496. uint32_t qh = 0;
  2497. for (int j = 0; j < 16; ++j) {
  2498. const uint8_t xi0 = L[j];
  2499. const uint8_t xi1 = L[j+16];
  2500. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2501. // get the 5-th bit and store it in qh at the right position
  2502. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2503. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2504. }
  2505. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2506. }
  2507. }
  2508. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2509. if (!quant_weights) {
  2510. quantize_row_q5_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2511. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2512. }
  2513. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2514. char * qrow = (char *)dst;
  2515. for (int64_t row = 0; row < nrow; ++row) {
  2516. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2517. src += n_per_row;
  2518. qrow += row_size;
  2519. }
  2520. return nrow * row_size;
  2521. }
  2522. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2523. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2524. if (!quant_weights) {
  2525. quantize_row_q5_1_reference(x, y, n_per_row);
  2526. return;
  2527. }
  2528. float weight[QK5_1];
  2529. uint8_t L[QK5_1], Laux[QK5_1];
  2530. float sum_x2 = 0;
  2531. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2532. float sigma2 = sum_x2/n_per_row;
  2533. const int64_t nb = n_per_row/QK5_1;
  2534. for (int ib = 0; ib < nb; ++ib) {
  2535. const float * xb = x + QK5_1 * ib;
  2536. const float * qw = quant_weights + QK5_1 * ib;
  2537. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2538. float min;
  2539. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2540. y[ib].d = GGML_FP32_TO_FP16(d);
  2541. y[ib].m = GGML_FP32_TO_FP16(-min);
  2542. uint32_t qh = 0;
  2543. for (int j = 0; j < 16; ++j) {
  2544. const uint8_t xi0 = L[j];
  2545. const uint8_t xi1 = L[j+16];
  2546. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2547. // get the 5-th bit and store it in qh at the right position
  2548. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2549. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2550. }
  2551. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2552. }
  2553. }
  2554. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2555. if (!quant_weights) {
  2556. quantize_row_q5_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2557. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2558. }
  2559. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2560. char * qrow = (char *)dst;
  2561. for (int64_t row = 0; row < nrow; ++row) {
  2562. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2563. src += n_per_row;
  2564. qrow += row_size;
  2565. }
  2566. return nrow * row_size;
  2567. }
  2568. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2569. (void)quant_weights; // not used
  2570. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2571. quantize_row_q8_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2572. return nrow * row_size;
  2573. }
  2574. // ====================== "True" 2-bit (de)-quantization
  2575. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int64_t k) {
  2576. assert(k % QK_K == 0);
  2577. const int64_t nb = k / QK_K;
  2578. uint32_t aux32[2];
  2579. const uint8_t * aux8 = (const uint8_t *)aux32;
  2580. for (int i = 0; i < nb; i++) {
  2581. const float d = GGML_FP16_TO_FP32(x[i].d);
  2582. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2583. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2584. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2585. for (int l = 0; l < 4; ++l) {
  2586. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2587. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2588. for (int j = 0; j < 8; ++j) {
  2589. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2590. }
  2591. y += 8;
  2592. }
  2593. }
  2594. }
  2595. }
  2596. // ====================== 2.3125 bpw (de)-quantization
  2597. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int64_t k) {
  2598. assert(k % QK_K == 0);
  2599. const int64_t nb = k / QK_K;
  2600. float db[2];
  2601. for (int i = 0; i < nb; i++) {
  2602. const float d = GGML_FP16_TO_FP32(x[i].d);
  2603. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2604. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2605. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2606. for (int l = 0; l < 4; ++l) {
  2607. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2608. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2609. for (int j = 0; j < 8; ++j) {
  2610. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2611. }
  2612. y += 8;
  2613. }
  2614. }
  2615. }
  2616. }
  2617. // ====================== 2.5625 bpw (de)-quantization
  2618. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int64_t k) {
  2619. assert(k % QK_K == 0);
  2620. const int64_t nb = k / QK_K;
  2621. float db[2];
  2622. for (int i = 0; i < nb; i++) {
  2623. const float d = GGML_FP16_TO_FP32(x[i].d);
  2624. const uint8_t * qs = x[i].qs;
  2625. const uint8_t * qh = x[i].qh;
  2626. const uint8_t * signs = qs + QK_K/8;
  2627. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2628. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2629. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2630. for (int l = 0; l < 4; ++l) {
  2631. const float dl = db[l/2];
  2632. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2633. for (int j = 0; j < 8; ++j) {
  2634. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2635. }
  2636. y += 8;
  2637. }
  2638. qs += 4;
  2639. signs += 4;
  2640. }
  2641. }
  2642. }
  2643. // ====================== 3.0625 bpw (de)-quantization
  2644. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int64_t k) {
  2645. assert(k % QK_K == 0);
  2646. const int64_t nb = k / QK_K;
  2647. uint32_t aux32;
  2648. for (int i = 0; i < nb; i++) {
  2649. const float d = GGML_FP16_TO_FP32(x[i].d);
  2650. const uint8_t * qs = x[i].qs;
  2651. const uint8_t * scales_and_signs = qs + QK_K/4;
  2652. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2653. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2654. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2655. for (int l = 0; l < 4; ++l) {
  2656. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2657. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2658. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2659. for (int j = 0; j < 4; ++j) {
  2660. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2661. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2662. }
  2663. y += 8;
  2664. }
  2665. qs += 8;
  2666. }
  2667. }
  2668. }
  2669. // ====================== 3.3125 bpw (de)-quantization
  2670. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int64_t k) {
  2671. assert(k % QK_K == 0);
  2672. const int64_t nb = k / QK_K;
  2673. for (int i = 0; i < nb; i++) {
  2674. const float d = GGML_FP16_TO_FP32(x[i].d);
  2675. const uint8_t * qs = x[i].qs;
  2676. const uint8_t * qh = x[i].qh;
  2677. const uint8_t * signs = x[i].signs;
  2678. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2679. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2680. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2681. for (int l = 0; l < 4; ++l) {
  2682. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2683. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2684. for (int j = 0; j < 4; ++j) {
  2685. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2686. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2687. }
  2688. y += 8;
  2689. }
  2690. qs += 8;
  2691. signs += 4;
  2692. for (int l = 0; l < 4; ++l) {
  2693. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2694. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2695. for (int j = 0; j < 4; ++j) {
  2696. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2697. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2698. }
  2699. y += 8;
  2700. }
  2701. qh += 2;
  2702. qs += 8;
  2703. signs += 4;
  2704. }
  2705. }
  2706. }
  2707. // ====================== 1.5625 bpw (de)-quantization
  2708. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int64_t k) {
  2709. assert(k % QK_K == 0);
  2710. const int64_t nb = k / QK_K;
  2711. for (int i = 0; i < nb; i++) {
  2712. const float d = GGML_FP16_TO_FP32(x[i].d);
  2713. const uint8_t * qs = x[i].qs;
  2714. const uint16_t * qh = x[i].qh;
  2715. for (int ib = 0; ib < QK_K/32; ++ib) {
  2716. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2717. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2718. for (int l = 0; l < 4; ++l) {
  2719. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2720. for (int j = 0; j < 8; ++j) {
  2721. y[j] = dl * (grid[j] + delta);
  2722. }
  2723. y += 8;
  2724. }
  2725. qs += 4;
  2726. }
  2727. }
  2728. }
  2729. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int64_t k) {
  2730. assert(k % QK_K == 0);
  2731. const int64_t nb = k / QK_K;
  2732. float delta[4];
  2733. uint16_t idx[4];
  2734. #if QK_K != 64
  2735. iq1m_scale_t scale;
  2736. #endif
  2737. for (int i = 0; i < nb; i++) {
  2738. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2739. #if QK_K == 64
  2740. const float d = GGML_FP16_TO_FP32(x[i].d);
  2741. #else
  2742. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2743. const float d = GGML_FP16_TO_FP32(scale.f16);
  2744. #endif
  2745. const uint8_t * qs = x[i].qs;
  2746. const uint8_t * qh = x[i].qh;
  2747. for (int ib = 0; ib < QK_K/32; ++ib) {
  2748. #if QK_K == 64
  2749. const float dl1 = d * (2*((sc[ib/2] >> (8*(ib%2)+0)) & 0xf) + 1);
  2750. const float dl2 = d * (2*((sc[ib/2] >> (8*(ib%2)+4)) & 0xf) + 1);
  2751. #else
  2752. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2753. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2754. #endif
  2755. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2756. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2757. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2758. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2759. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2760. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2761. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2762. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2763. for (int l = 0; l < 2; ++l) {
  2764. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2765. for (int j = 0; j < 8; ++j) {
  2766. y[j] = dl1 * (grid[j] + delta[l]);
  2767. }
  2768. y += 8;
  2769. }
  2770. for (int l = 2; l < 4; ++l) {
  2771. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2772. for (int j = 0; j < 8; ++j) {
  2773. y[j] = dl2 * (grid[j] + delta[l]);
  2774. }
  2775. y += 8;
  2776. }
  2777. qs += 4;
  2778. qh += 2;
  2779. }
  2780. }
  2781. }
  2782. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2783. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int64_t k) {
  2784. assert(k % QK4_NL == 0);
  2785. const int64_t nb = k / QK4_NL;
  2786. for (int i = 0; i < nb; i++) {
  2787. const uint8_t * qs = x[i].qs;
  2788. const float d = GGML_FP16_TO_FP32(x[i].d);
  2789. for (int j = 0; j < QK4_NL/2; ++j) {
  2790. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2791. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2792. }
  2793. y += QK4_NL;
  2794. qs += QK4_NL/2;
  2795. }
  2796. }
  2797. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int64_t k) {
  2798. assert(k % QK_K == 0);
  2799. #if QK_K == 64
  2800. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2801. #else
  2802. const int64_t nb = k / QK_K;
  2803. for (int i = 0; i < nb; i++) {
  2804. const uint8_t * qs = x[i].qs;
  2805. const float d = GGML_FP16_TO_FP32(x[i].d);
  2806. for (int ib = 0; ib < QK_K/32; ++ib) {
  2807. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2808. const float dl = d * (ls - 32);
  2809. for (int j = 0; j < 16; ++j) {
  2810. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2811. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2812. }
  2813. y += 32;
  2814. qs += 16;
  2815. }
  2816. }
  2817. #endif
  2818. }
  2819. //===================================== Q8_K ==============================================
  2820. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int64_t k) {
  2821. assert(k % QK_K == 0);
  2822. const int64_t nb = k / QK_K;
  2823. for (int i = 0; i < nb; i++) {
  2824. float max = 0;
  2825. float amax = 0;
  2826. for (int j = 0; j < QK_K; ++j) {
  2827. float ax = fabsf(x[j]);
  2828. if (ax > amax) {
  2829. amax = ax; max = x[j];
  2830. }
  2831. }
  2832. if (!amax) {
  2833. y[i].d = 0;
  2834. memset(y[i].qs, 0, QK_K);
  2835. x += QK_K;
  2836. continue;
  2837. }
  2838. //const float iscale = -128.f/max;
  2839. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2840. const float iscale = -127.f/max;
  2841. for (int j = 0; j < QK_K; ++j) {
  2842. int v = nearest_int(iscale*x[j]);
  2843. y[i].qs[j] = MIN(127, v);
  2844. }
  2845. for (int j = 0; j < QK_K/16; ++j) {
  2846. int sum = 0;
  2847. for (int ii = 0; ii < 16; ++ii) {
  2848. sum += y[i].qs[j*16 + ii];
  2849. }
  2850. y[i].bsums[j] = sum;
  2851. }
  2852. y[i].d = 1/iscale;
  2853. x += QK_K;
  2854. }
  2855. }
  2856. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int64_t k) {
  2857. assert(k % QK_K == 0);
  2858. const int64_t nb = k / QK_K;
  2859. for (int i = 0; i < nb; i++) {
  2860. for (int j = 0; j < QK_K; ++j) {
  2861. *y++ = x[i].d * x[i].qs[j];
  2862. }
  2863. }
  2864. }
  2865. void quantize_row_q8_K(const float * restrict x, void * restrict y, int64_t k) {
  2866. quantize_row_q8_K_reference(x, y, k);
  2867. }
  2868. //===================================== Dot ptoducts =================================
  2869. //
  2870. // Helper functions
  2871. //
  2872. #if __AVX__ || __AVX2__ || __AVX512F__
  2873. // shuffles to pick the required scales in dot products
  2874. static inline __m256i get_scale_shuffle_q3k(int i) {
  2875. static const uint8_t k_shuffle[128] = {
  2876. 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,
  2877. 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,
  2878. 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,
  2879. 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,
  2880. };
  2881. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2882. }
  2883. static inline __m256i get_scale_shuffle_k4(int i) {
  2884. static const uint8_t k_shuffle[256] = {
  2885. 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,
  2886. 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,
  2887. 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,
  2888. 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,
  2889. 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,
  2890. 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,
  2891. 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,
  2892. 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
  2893. };
  2894. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2895. }
  2896. static inline __m128i get_scale_shuffle(int i) {
  2897. static const uint8_t k_shuffle[128] = {
  2898. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2899. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2900. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2901. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2902. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2903. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2904. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2905. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2906. };
  2907. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2908. }
  2909. #endif
  2910. 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) {
  2911. const int qk = QK8_0;
  2912. const int nb = n / qk;
  2913. assert(n % qk == 0);
  2914. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2915. assert((nrc == 2) || (nrc == 1));
  2916. #else
  2917. assert(nrc == 1);
  2918. #endif
  2919. UNUSED(nrc);
  2920. UNUSED(bx);
  2921. UNUSED(by);
  2922. UNUSED(bs);
  2923. const block_q4_0 * restrict x = vx;
  2924. const block_q8_0 * restrict y = vy;
  2925. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2926. if (nrc == 2) {
  2927. const block_q4_0 * restrict vx0 = vx;
  2928. const block_q4_0 * restrict vx1 = vx + bx;
  2929. const block_q8_0 * restrict vy0 = vy;
  2930. const block_q8_0 * restrict vy1 = vy + by;
  2931. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2932. for (int i = 0; i < nb; i++) {
  2933. const block_q4_0 * restrict b_x0 = &vx0[i];
  2934. const block_q4_0 * restrict b_x1 = &vx1[i];
  2935. const block_q8_0 * restrict b_y0 = &vy0[i];
  2936. const block_q8_0 * restrict b_y1 = &vy1[i];
  2937. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2938. const int8x16_t s8b = vdupq_n_s8(0x8);
  2939. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  2940. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  2941. // 4-bit -> 8-bit
  2942. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2943. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2944. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2945. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2946. // sub 8
  2947. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  2948. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  2949. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  2950. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  2951. // load y
  2952. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  2953. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  2954. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  2955. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  2956. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  2957. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  2958. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  2959. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  2960. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2961. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2962. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2963. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2964. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2965. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2966. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2967. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2968. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  2969. l1, r1)), l2, r2)), l3, r3))), scale);
  2970. }
  2971. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  2972. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  2973. vst1_f32(s, vget_low_f32(sumv2));
  2974. vst1_f32(s + bs, vget_high_f32(sumv2));
  2975. return;
  2976. }
  2977. #endif
  2978. #if defined(__ARM_NEON)
  2979. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2980. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2981. assert(nb % 2 == 0); // TODO: handle odd nb
  2982. for (int i = 0; i < nb; i += 2) {
  2983. const block_q4_0 * restrict x0 = &x[i + 0];
  2984. const block_q4_0 * restrict x1 = &x[i + 1];
  2985. const block_q8_0 * restrict y0 = &y[i + 0];
  2986. const block_q8_0 * restrict y1 = &y[i + 1];
  2987. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2988. const int8x16_t s8b = vdupq_n_s8(0x8);
  2989. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2990. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2991. // 4-bit -> 8-bit
  2992. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2993. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2994. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2995. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2996. // sub 8
  2997. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2998. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2999. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3000. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3001. // load y
  3002. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3003. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3004. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3005. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3006. // dot product into int32x4_t
  3007. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3008. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3009. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3010. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3011. }
  3012. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3013. #elif defined(__AVX2__)
  3014. // Initialize accumulator with zeros
  3015. __m256 acc = _mm256_setzero_ps();
  3016. // Main loop
  3017. for (int i = 0; i < nb; ++i) {
  3018. /* Compute combined scale for the block */
  3019. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3020. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3021. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3022. const __m256i off = _mm256_set1_epi8( 8 );
  3023. qx = _mm256_sub_epi8( qx, off );
  3024. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3025. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3026. /* Multiply q with scale and accumulate */
  3027. acc = _mm256_fmadd_ps( d, q, acc );
  3028. }
  3029. *s = hsum_float_8(acc);
  3030. #elif defined(__AVX__)
  3031. // Initialize accumulator with zeros
  3032. __m256 acc = _mm256_setzero_ps();
  3033. // Main loop
  3034. for (int i = 0; i < nb; ++i) {
  3035. // Compute combined scale for the block
  3036. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3037. const __m128i lowMask = _mm_set1_epi8(0xF);
  3038. const __m128i off = _mm_set1_epi8(8);
  3039. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3040. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3041. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3042. bx_0 = _mm_sub_epi8(bx_0, off);
  3043. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3044. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3045. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3046. bx_0 = _mm_sub_epi8(bx_0, off);
  3047. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3048. // Convert int32_t to float
  3049. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3050. // Apply the scale, and accumulate
  3051. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3052. }
  3053. *s = hsum_float_8(acc);
  3054. #elif defined(__SSSE3__)
  3055. // set constants
  3056. const __m128i lowMask = _mm_set1_epi8(0xF);
  3057. const __m128i off = _mm_set1_epi8(8);
  3058. // Initialize accumulator with zeros
  3059. __m128 acc_0 = _mm_setzero_ps();
  3060. __m128 acc_1 = _mm_setzero_ps();
  3061. __m128 acc_2 = _mm_setzero_ps();
  3062. __m128 acc_3 = _mm_setzero_ps();
  3063. // First round without accumulation
  3064. {
  3065. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3066. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3067. // Compute combined scale for the block 0 and 1
  3068. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3069. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3070. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3071. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3072. bx_0 = _mm_sub_epi8(bx_0, off);
  3073. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3074. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3075. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3076. bx_1 = _mm_sub_epi8(bx_1, off);
  3077. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3078. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3079. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3080. // Compute combined scale for the block 2 and 3
  3081. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3082. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3083. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3084. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3085. bx_2 = _mm_sub_epi8(bx_2, off);
  3086. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3087. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3088. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3089. bx_3 = _mm_sub_epi8(bx_3, off);
  3090. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3091. // Convert int32_t to float
  3092. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3093. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3094. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3095. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3096. // Apply the scale
  3097. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3098. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3099. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3100. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3101. }
  3102. assert(nb % 2 == 0); // TODO: handle odd nb
  3103. // Main loop
  3104. for (int i = 2; i < nb; i+=2) {
  3105. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3106. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3107. // Compute combined scale for the block 0 and 1
  3108. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3109. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3110. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3111. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3112. bx_0 = _mm_sub_epi8(bx_0, off);
  3113. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3114. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3115. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3116. bx_1 = _mm_sub_epi8(bx_1, off);
  3117. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3118. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3119. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3120. // Compute combined scale for the block 2 and 3
  3121. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3122. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3123. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3124. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3125. bx_2 = _mm_sub_epi8(bx_2, off);
  3126. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3127. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3128. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3129. bx_3 = _mm_sub_epi8(bx_3, off);
  3130. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3131. // Convert int32_t to float
  3132. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3133. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3134. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3135. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3136. // Apply the scale
  3137. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3138. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3139. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3140. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3141. // Acummulate
  3142. acc_0 = _mm_add_ps(p0_d, acc_0);
  3143. acc_1 = _mm_add_ps(p1_d, acc_1);
  3144. acc_2 = _mm_add_ps(p2_d, acc_2);
  3145. acc_3 = _mm_add_ps(p3_d, acc_3);
  3146. }
  3147. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3148. #elif defined(__riscv_v_intrinsic)
  3149. float sumf = 0.0;
  3150. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3151. for (int i = 0; i < nb; i++) {
  3152. // load elements
  3153. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3154. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3155. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3156. // mask and store lower part of x, and then upper part
  3157. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3158. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3159. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3160. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3161. // subtract offset
  3162. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3163. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3164. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3165. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3166. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3167. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3168. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3169. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3170. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3171. }
  3172. *s = sumf;
  3173. #else
  3174. // scalar
  3175. float sumf = 0.0;
  3176. for (int i = 0; i < nb; i++) {
  3177. int sumi = 0;
  3178. for (int j = 0; j < qk/2; ++j) {
  3179. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3180. const int v1 = (x[i].qs[j] >> 4) - 8;
  3181. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3182. }
  3183. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3184. }
  3185. *s = sumf;
  3186. #endif
  3187. }
  3188. 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) {
  3189. const int qk = QK8_1;
  3190. const int nb = n / qk;
  3191. assert(n % qk == 0);
  3192. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3193. assert((nrc == 2) || (nrc == 1));
  3194. #else
  3195. assert(nrc == 1);
  3196. #endif
  3197. UNUSED(nrc);
  3198. UNUSED(bx);
  3199. UNUSED(by);
  3200. UNUSED(bs);
  3201. const block_q4_1 * restrict x = vx;
  3202. const block_q8_1 * restrict y = vy;
  3203. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3204. if (nrc == 2) {
  3205. const block_q4_1 * restrict vx0 = vx;
  3206. const block_q4_1 * restrict vx1 = vx + bx;
  3207. const block_q8_1 * restrict vy0 = vy;
  3208. const block_q8_1 * restrict vy1 = vy + by;
  3209. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3210. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3211. for (int i = 0; i < nb; i++) {
  3212. const block_q4_1 * restrict b_x0 = &vx0[i];
  3213. const block_q4_1 * restrict b_x1 = &vx1[i];
  3214. const block_q8_1 * restrict b_y0 = &vy0[i];
  3215. const block_q8_1 * restrict b_y1 = &vy1[i];
  3216. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3217. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3218. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3219. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3220. summs0 += summs_t;
  3221. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3222. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3223. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3224. // 4-bit -> 8-bit
  3225. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3226. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3227. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3228. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3229. // load y
  3230. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3231. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3232. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3233. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3234. // mmla into int32x4_t
  3235. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3236. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3237. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3238. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3239. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3240. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3241. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3242. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3243. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3244. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3245. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3246. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3247. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3248. l1, r1)), l2, r2)), l3, r3))), scale);
  3249. }
  3250. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3251. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3252. sumv2 = sumv2 + summs0;
  3253. vst1_f32(s, vget_low_f32(sumv2));
  3254. vst1_f32(s + bs, vget_high_f32(sumv2));
  3255. return;
  3256. }
  3257. #endif
  3258. // TODO: add WASM SIMD
  3259. #if defined(__ARM_NEON)
  3260. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3261. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3262. float summs = 0;
  3263. assert(nb % 2 == 0); // TODO: handle odd nb
  3264. for (int i = 0; i < nb; i += 2) {
  3265. const block_q4_1 * restrict x0 = &x[i + 0];
  3266. const block_q4_1 * restrict x1 = &x[i + 1];
  3267. const block_q8_1 * restrict y0 = &y[i + 0];
  3268. const block_q8_1 * restrict y1 = &y[i + 1];
  3269. 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);
  3270. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3271. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3272. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3273. // 4-bit -> 8-bit
  3274. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3275. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3276. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3277. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3278. // load y
  3279. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3280. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3281. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3282. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3283. // dot product into int32x4_t
  3284. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3285. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3286. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3287. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3288. }
  3289. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3290. #elif defined(__AVX2__) || defined(__AVX__)
  3291. // Initialize accumulator with zeros
  3292. __m256 acc = _mm256_setzero_ps();
  3293. float summs = 0;
  3294. // Main loop
  3295. for (int i = 0; i < nb; ++i) {
  3296. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3297. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3298. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3299. const __m256 d0v = _mm256_set1_ps( d0 );
  3300. const __m256 d1v = _mm256_set1_ps( d1 );
  3301. // Compute combined scales
  3302. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3303. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3304. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3305. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3306. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3307. // Accumulate d0*d1*x*y
  3308. #if defined(__AVX2__)
  3309. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3310. #else
  3311. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3312. #endif
  3313. }
  3314. *s = hsum_float_8(acc) + summs;
  3315. #elif defined(__riscv_v_intrinsic)
  3316. float sumf = 0.0;
  3317. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3318. for (int i = 0; i < nb; i++) {
  3319. // load elements
  3320. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3321. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3322. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3323. // mask and store lower part of x, and then upper part
  3324. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3325. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3326. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3327. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3328. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3329. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3330. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3331. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3332. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3333. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3334. 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);
  3335. }
  3336. *s = sumf;
  3337. #else
  3338. // scalar
  3339. float sumf = 0.0;
  3340. for (int i = 0; i < nb; i++) {
  3341. int sumi = 0;
  3342. for (int j = 0; j < qk/2; ++j) {
  3343. const int v0 = (x[i].qs[j] & 0x0F);
  3344. const int v1 = (x[i].qs[j] >> 4);
  3345. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3346. }
  3347. 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);
  3348. }
  3349. *s = sumf;
  3350. #endif
  3351. }
  3352. 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) {
  3353. const int qk = QK8_0;
  3354. const int nb = n / qk;
  3355. assert(n % qk == 0);
  3356. assert(qk == QK5_0);
  3357. assert(nrc == 1);
  3358. UNUSED(nrc);
  3359. UNUSED(bx);
  3360. UNUSED(by);
  3361. UNUSED(bs);
  3362. const block_q5_0 * restrict x = vx;
  3363. const block_q8_0 * restrict y = vy;
  3364. #if defined(__ARM_NEON)
  3365. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3366. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3367. uint32_t qh0;
  3368. uint32_t qh1;
  3369. uint64_t tmp0[4];
  3370. uint64_t tmp1[4];
  3371. assert(nb % 2 == 0); // TODO: handle odd nb
  3372. for (int i = 0; i < nb; i += 2) {
  3373. const block_q5_0 * restrict x0 = &x[i];
  3374. const block_q5_0 * restrict x1 = &x[i + 1];
  3375. const block_q8_0 * restrict y0 = &y[i];
  3376. const block_q8_0 * restrict y1 = &y[i + 1];
  3377. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3378. // extract the 5th bit via lookup table ((!b) << 4)
  3379. memcpy(&qh0, x0->qh, sizeof(qh0));
  3380. memcpy(&qh1, x1->qh, sizeof(qh1));
  3381. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3382. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3383. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3384. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3385. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3386. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3387. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3388. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3389. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3390. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3391. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3392. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3393. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3394. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3395. // 4-bit -> 8-bit
  3396. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3397. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3398. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3399. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3400. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3401. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3402. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3403. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3404. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3405. // load y
  3406. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3407. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3408. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3409. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3410. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3411. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3412. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3413. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3414. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3415. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3416. }
  3417. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3418. #elif defined(__wasm_simd128__)
  3419. v128_t sumv = wasm_f32x4_splat(0.0f);
  3420. uint32_t qh;
  3421. uint64_t tmp[4];
  3422. // TODO: check if unrolling this is better
  3423. for (int i = 0; i < nb; ++i) {
  3424. const block_q5_0 * restrict x0 = &x[i];
  3425. const block_q8_0 * restrict y0 = &y[i];
  3426. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3427. // extract the 5th bit
  3428. memcpy(&qh, x0->qh, sizeof(qh));
  3429. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3430. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3431. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3432. tmp[3] = table_b2b_1[(qh >> 24) ];
  3433. const v128_t qhl = wasm_v128_load(tmp + 0);
  3434. const v128_t qhh = wasm_v128_load(tmp + 2);
  3435. const v128_t v0 = wasm_v128_load(x0->qs);
  3436. // 4-bit -> 8-bit
  3437. const v128_t v0l = wasm_v128_and (v0, m4b);
  3438. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3439. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3440. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3441. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3442. // load y
  3443. const v128_t v1l = wasm_v128_load(y0->qs);
  3444. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3445. // int8x16 -> int16x8
  3446. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3447. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3448. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3449. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3450. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3451. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3452. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3453. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3454. // dot product
  3455. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3456. wasm_i32x4_add(
  3457. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3458. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3459. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3460. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3461. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3462. }
  3463. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3464. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3465. #elif defined(__AVX2__)
  3466. // Initialize accumulator with zeros
  3467. __m256 acc = _mm256_setzero_ps();
  3468. // Main loop
  3469. for (int i = 0; i < nb; i++) {
  3470. /* Compute combined scale for the block */
  3471. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3472. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3473. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3474. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3475. qx = _mm256_or_si256(qx, bxhi);
  3476. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3477. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3478. /* Multiply q with scale and accumulate */
  3479. acc = _mm256_fmadd_ps(d, q, acc);
  3480. }
  3481. *s = hsum_float_8(acc);
  3482. #elif defined(__AVX__)
  3483. // Initialize accumulator with zeros
  3484. __m256 acc = _mm256_setzero_ps();
  3485. __m128i mask = _mm_set1_epi8((char)0xF0);
  3486. // Main loop
  3487. for (int i = 0; i < nb; i++) {
  3488. /* Compute combined scale for the block */
  3489. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3490. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3491. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3492. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3493. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3494. bxhil = _mm_andnot_si128(bxhil, mask);
  3495. bxhih = _mm_andnot_si128(bxhih, mask);
  3496. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3497. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3498. bxl = _mm_or_si128(bxl, bxhil);
  3499. bxh = _mm_or_si128(bxh, bxhih);
  3500. bx_0 = MM256_SET_M128I(bxh, bxl);
  3501. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3502. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3503. /* Multiply q with scale and accumulate */
  3504. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3505. }
  3506. *s = hsum_float_8(acc);
  3507. #elif defined(__riscv_v_intrinsic)
  3508. float sumf = 0.0;
  3509. uint32_t qh;
  3510. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3511. // These temporary registers are for masking and shift operations
  3512. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3513. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3514. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3515. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3516. for (int i = 0; i < nb; i++) {
  3517. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3518. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3519. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3520. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3521. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3522. // ((qh & (1u << (j + 16))) >> (j + 12));
  3523. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3524. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3525. // narrowing
  3526. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3527. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3528. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3529. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3530. // load
  3531. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3532. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3533. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3534. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3535. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3536. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3537. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3538. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3539. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3540. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3541. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3542. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3543. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3544. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3545. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3546. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3547. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3548. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3549. }
  3550. *s = sumf;
  3551. #else
  3552. // scalar
  3553. float sumf = 0.0;
  3554. for (int i = 0; i < nb; i++) {
  3555. uint32_t qh;
  3556. memcpy(&qh, x[i].qh, sizeof(qh));
  3557. int sumi = 0;
  3558. for (int j = 0; j < qk/2; ++j) {
  3559. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3560. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3561. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3562. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3563. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3564. }
  3565. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3566. }
  3567. *s = sumf;
  3568. #endif
  3569. }
  3570. 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) {
  3571. const int qk = QK8_1;
  3572. const int nb = n / qk;
  3573. assert(n % qk == 0);
  3574. assert(qk == QK5_1);
  3575. assert(nrc == 1);
  3576. UNUSED(nrc);
  3577. UNUSED(bx);
  3578. UNUSED(by);
  3579. UNUSED(bs);
  3580. const block_q5_1 * restrict x = vx;
  3581. const block_q8_1 * restrict y = vy;
  3582. #if defined(__ARM_NEON)
  3583. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3584. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3585. float summs0 = 0.0f;
  3586. float summs1 = 0.0f;
  3587. uint32_t qh0;
  3588. uint32_t qh1;
  3589. uint64_t tmp0[4];
  3590. uint64_t tmp1[4];
  3591. assert(nb % 2 == 0); // TODO: handle odd nb
  3592. for (int i = 0; i < nb; i += 2) {
  3593. const block_q5_1 * restrict x0 = &x[i];
  3594. const block_q5_1 * restrict x1 = &x[i + 1];
  3595. const block_q8_1 * restrict y0 = &y[i];
  3596. const block_q8_1 * restrict y1 = &y[i + 1];
  3597. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3598. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3599. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3600. // extract the 5th bit via lookup table ((b) << 4)
  3601. memcpy(&qh0, x0->qh, sizeof(qh0));
  3602. memcpy(&qh1, x1->qh, sizeof(qh1));
  3603. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3604. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3605. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3606. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3607. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3608. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3609. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3610. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3611. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3612. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3613. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3614. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3615. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3616. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3617. // 4-bit -> 8-bit
  3618. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3619. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3620. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3621. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3622. // add high bit
  3623. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3624. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3625. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3626. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3627. // load y
  3628. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3629. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3630. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3631. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3632. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3633. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3634. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3635. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3636. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3637. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3638. }
  3639. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3640. #elif defined(__wasm_simd128__)
  3641. v128_t sumv = wasm_f32x4_splat(0.0f);
  3642. float summs = 0.0f;
  3643. uint32_t qh;
  3644. uint64_t tmp[4];
  3645. // TODO: check if unrolling this is better
  3646. for (int i = 0; i < nb; ++i) {
  3647. const block_q5_1 * restrict x0 = &x[i];
  3648. const block_q8_1 * restrict y0 = &y[i];
  3649. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  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_0[(qh >> 0) & 0xFF];
  3654. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3655. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3656. tmp[3] = table_b2b_0[(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
  3664. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3665. const v128_t v0hf = wasm_v128_or(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,
  3680. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(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) + summs;
  3689. #elif defined(__AVX2__)
  3690. // Initialize accumulator with zeros
  3691. __m256 acc = _mm256_setzero_ps();
  3692. float summs = 0.0f;
  3693. // Main loop
  3694. for (int i = 0; i < nb; i++) {
  3695. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3696. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3697. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3698. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3699. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3700. qx = _mm256_or_si256(qx, bxhi);
  3701. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3702. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3703. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3704. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3705. }
  3706. *s = hsum_float_8(acc) + summs;
  3707. #elif defined(__AVX__)
  3708. // Initialize accumulator with zeros
  3709. __m256 acc = _mm256_setzero_ps();
  3710. __m128i mask = _mm_set1_epi8(0x10);
  3711. float summs = 0.0f;
  3712. // Main loop
  3713. for (int i = 0; i < nb; i++) {
  3714. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3715. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3716. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3717. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3718. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3719. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3720. bxhil = _mm_and_si128(bxhil, mask);
  3721. bxhih = _mm_and_si128(bxhih, mask);
  3722. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3723. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3724. bxl = _mm_or_si128(bxl, bxhil);
  3725. bxh = _mm_or_si128(bxh, bxhih);
  3726. bx_0 = MM256_SET_M128I(bxh, bxl);
  3727. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3728. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3729. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3730. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3731. }
  3732. *s = hsum_float_8(acc) + summs;
  3733. #elif defined(__riscv_v_intrinsic)
  3734. float sumf = 0.0;
  3735. uint32_t qh;
  3736. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3737. // temporary registers for shift operations
  3738. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3739. vuint32m2_t vt_2 = __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. // load qh
  3743. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3744. // ((qh >> (j + 0)) << 4) & 0x10;
  3745. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3746. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3747. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3748. // ((qh >> (j + 12)) ) & 0x10;
  3749. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3750. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3751. // narrowing
  3752. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3753. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3754. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3755. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3756. // load
  3757. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3758. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3759. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3760. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3761. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3762. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3763. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3764. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3765. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  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 + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  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 >> (j + 0)) << 4) & 0x10;
  3784. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3785. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3786. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  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 + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3790. }
  3791. *s = sumf;
  3792. #endif
  3793. }
  3794. 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) {
  3795. const int qk = QK8_0;
  3796. const int nb = n / qk;
  3797. assert(n % qk == 0);
  3798. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3799. assert((nrc == 2) || (nrc == 1));
  3800. #else
  3801. assert(nrc == 1);
  3802. #endif
  3803. UNUSED(nrc);
  3804. UNUSED(bx);
  3805. UNUSED(by);
  3806. UNUSED(bs);
  3807. const block_q8_0 * restrict x = vx;
  3808. const block_q8_0 * restrict y = vy;
  3809. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3810. if (nrc == 2) {
  3811. const block_q8_0 * restrict vx0 = vx;
  3812. const block_q8_0 * restrict vx1 = vx + bx;
  3813. const block_q8_0 * restrict vy0 = vy;
  3814. const block_q8_0 * restrict vy1 = vy + by;
  3815. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3816. for (int i = 0; i < nb; i++) {
  3817. const block_q8_0 * restrict b_x0 = &vx0[i];
  3818. const block_q8_0 * restrict b_y0 = &vy0[i];
  3819. const block_q8_0 * restrict b_x1 = &vx1[i];
  3820. const block_q8_0 * restrict b_y1 = &vy1[i];
  3821. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3822. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3823. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3824. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3825. // load y
  3826. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3827. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3828. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3829. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3830. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3831. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3832. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3833. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3834. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3835. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3836. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3837. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3838. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3839. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3840. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3841. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3842. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3843. l1, r1)), l2, r2)), l3, r3))), scale);
  3844. }
  3845. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3846. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3847. vst1_f32(s, vget_low_f32(sumv2));
  3848. vst1_f32(s + bs, vget_high_f32(sumv2));
  3849. return;
  3850. }
  3851. #endif
  3852. #if defined(__ARM_NEON)
  3853. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3854. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3855. assert(nb % 2 == 0); // TODO: handle odd nb
  3856. for (int i = 0; i < nb; i += 2) {
  3857. const block_q8_0 * restrict x0 = &x[i + 0];
  3858. const block_q8_0 * restrict x1 = &x[i + 1];
  3859. const block_q8_0 * restrict y0 = &y[i + 0];
  3860. const block_q8_0 * restrict y1 = &y[i + 1];
  3861. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3862. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3863. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3864. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3865. // load y
  3866. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3867. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3868. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3869. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3870. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3871. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3872. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3873. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3874. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3875. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3876. }
  3877. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3878. #elif defined(__AVX2__) || defined(__AVX__)
  3879. // Initialize accumulator with zeros
  3880. __m256 acc = _mm256_setzero_ps();
  3881. // Main loop
  3882. for (int i = 0; i < nb; ++i) {
  3883. // Compute combined scale for the block
  3884. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3885. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3886. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3887. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3888. // Multiply q with scale and accumulate
  3889. #if defined(__AVX2__)
  3890. acc = _mm256_fmadd_ps( d, q, acc );
  3891. #else
  3892. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3893. #endif
  3894. }
  3895. *s = hsum_float_8(acc);
  3896. #elif defined(__riscv_v_intrinsic)
  3897. float sumf = 0.0;
  3898. size_t vl = __riscv_vsetvl_e8m1(qk);
  3899. for (int i = 0; i < nb; i++) {
  3900. // load elements
  3901. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3902. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3903. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  3904. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3905. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3906. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3907. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3908. }
  3909. *s = sumf;
  3910. #else
  3911. // scalar
  3912. float sumf = 0.0;
  3913. for (int i = 0; i < nb; i++) {
  3914. int sumi = 0;
  3915. for (int j = 0; j < qk; j++) {
  3916. sumi += x[i].qs[j]*y[i].qs[j];
  3917. }
  3918. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3919. }
  3920. *s = sumf;
  3921. #endif
  3922. }
  3923. #if QK_K == 256
  3924. 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) {
  3925. assert(nrc == 1);
  3926. UNUSED(nrc);
  3927. UNUSED(bx);
  3928. UNUSED(by);
  3929. UNUSED(bs);
  3930. const block_q2_K * restrict x = vx;
  3931. const block_q8_K * restrict y = vy;
  3932. const int nb = n / QK_K;
  3933. #ifdef __ARM_NEON
  3934. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3935. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3936. const int32x4_t vzero = vdupq_n_s32(0);
  3937. ggml_int8x16x2_t q2bytes;
  3938. uint8_t aux[16];
  3939. float sum = 0;
  3940. for (int i = 0; i < nb; ++i) {
  3941. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3942. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3943. const uint8_t * restrict q2 = x[i].qs;
  3944. const int8_t * restrict q8 = y[i].qs;
  3945. const uint8_t * restrict sc = x[i].scales;
  3946. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3947. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3948. vst1q_u8(aux, scales);
  3949. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3950. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3951. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3952. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3953. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3954. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3955. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3956. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3957. int isum = 0;
  3958. int is = 0;
  3959. // We use this macro instead of a function call because for some reason
  3960. // the code runs 2-3% slower, even if the function is declared inline
  3961. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3962. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3963. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3964. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3965. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3966. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3967. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3968. MULTIPLY_ACCUM_WITH_SCALE((index));
  3969. for (int j = 0; j < QK_K/128; ++j) {
  3970. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3971. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3972. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3973. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3974. MULTIPLY_ACCUM_WITH_SCALE(0);
  3975. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3976. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3977. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3978. is += 8;
  3979. }
  3980. sum += d * isum;
  3981. }
  3982. *s = sum;
  3983. #elif defined __AVX2__
  3984. const __m256i m3 = _mm256_set1_epi8(3);
  3985. const __m128i m4 = _mm_set1_epi8(0xF);
  3986. __m256 acc = _mm256_setzero_ps();
  3987. for (int i = 0; i < nb; ++i) {
  3988. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3989. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3990. const uint8_t * restrict q2 = x[i].qs;
  3991. const int8_t * restrict q8 = y[i].qs;
  3992. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3993. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3994. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3995. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3996. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3997. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3998. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3999. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4000. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4001. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4002. __m256i sumi = _mm256_setzero_si256();
  4003. for (int j = 0; j < QK_K/128; ++j) {
  4004. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4005. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4006. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4007. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4008. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4009. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4010. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4011. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4012. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4013. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4014. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4015. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4016. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4017. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4018. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4019. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4020. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4021. p0 = _mm256_add_epi32(p0, p1);
  4022. p2 = _mm256_add_epi32(p2, p3);
  4023. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4024. }
  4025. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4026. }
  4027. *s = hsum_float_8(acc);
  4028. #elif defined __AVX__
  4029. const __m128i m3 = _mm_set1_epi8(0x3);
  4030. const __m128i m4 = _mm_set1_epi8(0xF);
  4031. const __m128i m2 = _mm_set1_epi8(0x2);
  4032. __m256 acc = _mm256_setzero_ps();
  4033. for (int i = 0; i < nb; ++i) {
  4034. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4035. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4036. const uint8_t * restrict q2 = x[i].qs;
  4037. const int8_t * restrict q8 = y[i].qs;
  4038. // load mins and scales from block_q2_K.scales[QK_K/16]
  4039. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4040. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4041. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4042. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4043. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4044. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4045. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4046. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4047. // sumf += -dmin * summs in 32bits*8
  4048. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4049. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4050. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4051. const __m128i scales[2] = { scales_0, scales_1 };
  4052. __m128i sumi_0 = _mm_setzero_si128();
  4053. __m128i sumi_1 = _mm_setzero_si128();
  4054. for (int j = 0; j < QK_K/128; ++j) {
  4055. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4056. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4057. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4058. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4059. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4060. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4061. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4062. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4063. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4064. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4065. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4066. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4067. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4068. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4069. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4070. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4071. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4072. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4073. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4074. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4075. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4076. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4077. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4078. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4079. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4080. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4081. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4082. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4083. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4084. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4085. __m128i shuffle = _mm_set1_epi16(0x0100);
  4086. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4087. shuffle = _mm_add_epi16(shuffle, m2);
  4088. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4089. shuffle = _mm_add_epi16(shuffle, m2);
  4090. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4091. shuffle = _mm_add_epi16(shuffle, m2);
  4092. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4093. shuffle = _mm_add_epi16(shuffle, m2);
  4094. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4095. shuffle = _mm_add_epi16(shuffle, m2);
  4096. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4097. shuffle = _mm_add_epi16(shuffle, m2);
  4098. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4099. shuffle = _mm_add_epi16(shuffle, m2);
  4100. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4101. p0 = _mm_add_epi32(p0, p1);
  4102. p2 = _mm_add_epi32(p2, p3);
  4103. p4 = _mm_add_epi32(p4, p5);
  4104. p6 = _mm_add_epi32(p6, p7);
  4105. // isum in 32bits*4*2
  4106. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4107. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4108. }
  4109. // sumf += dall * isum - dmin * summs in 32bits
  4110. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4111. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4112. }
  4113. *s = hsum_float_8(acc);
  4114. #elif defined __riscv_v_intrinsic
  4115. float sumf = 0;
  4116. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4117. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4118. for (int i = 0; i < nb; ++i) {
  4119. const uint8_t * q2 = x[i].qs;
  4120. const int8_t * q8 = y[i].qs;
  4121. const uint8_t * sc = x[i].scales;
  4122. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4123. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4124. size_t vl = 16;
  4125. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4126. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4127. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4128. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4129. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4130. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4131. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4132. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4133. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4134. vl = 32;
  4135. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4136. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4137. uint8_t is=0;
  4138. int isum=0;
  4139. for (int j = 0; j < QK_K/128; ++j) {
  4140. // load Q2
  4141. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4142. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4143. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4144. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4145. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4146. // duplicate scale elements for product
  4147. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4148. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4149. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4150. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4151. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4152. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4153. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4154. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4155. // load Q8
  4156. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4157. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4158. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4159. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4160. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4161. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4162. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4163. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4164. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4165. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4166. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4167. q2+=32; q8+=128; is=8;
  4168. }
  4169. sumf += dall * isum;
  4170. }
  4171. *s = sumf;
  4172. #else
  4173. float sumf = 0;
  4174. for (int i = 0; i < nb; ++i) {
  4175. const uint8_t * q2 = x[i].qs;
  4176. const int8_t * q8 = y[i].qs;
  4177. const uint8_t * sc = x[i].scales;
  4178. int summs = 0;
  4179. for (int j = 0; j < 16; ++j) {
  4180. summs += y[i].bsums[j] * (sc[j] >> 4);
  4181. }
  4182. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4183. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4184. int isum = 0;
  4185. int is = 0;
  4186. int d;
  4187. for (int k = 0; k < QK_K/128; ++k) {
  4188. int shift = 0;
  4189. for (int j = 0; j < 4; ++j) {
  4190. d = sc[is++] & 0xF;
  4191. int isuml = 0;
  4192. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4193. isum += d * isuml;
  4194. d = sc[is++] & 0xF;
  4195. isuml = 0;
  4196. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4197. isum += d * isuml;
  4198. shift += 2;
  4199. q8 += 32;
  4200. }
  4201. q2 += 32;
  4202. }
  4203. sumf += dall * isum - dmin * summs;
  4204. }
  4205. *s = sumf;
  4206. #endif
  4207. }
  4208. #else
  4209. 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) {
  4210. assert(nrc == 1);
  4211. UNUSED(nrc);
  4212. UNUSED(bx);
  4213. UNUSED(by);
  4214. UNUSED(bs);
  4215. const block_q2_K * restrict x = vx;
  4216. const block_q8_K * restrict y = vy;
  4217. const int nb = n / QK_K;
  4218. #ifdef __ARM_NEON
  4219. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4220. const int32x4_t vzero = vdupq_n_s32(0);
  4221. ggml_int8x16x4_t q2bytes;
  4222. uint32_t aux32[2];
  4223. const uint8_t * scales = (const uint8_t *)aux32;
  4224. float sum = 0;
  4225. for (int i = 0; i < nb; ++i) {
  4226. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4227. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4228. const uint8_t * restrict q2 = x[i].qs;
  4229. const int8_t * restrict q8 = y[i].qs;
  4230. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4231. aux32[0] = sc[0] & 0x0f0f0f0f;
  4232. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4233. 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]);
  4234. int isum1 = 0, isum2 = 0;
  4235. const uint8x16_t q2bits = vld1q_u8(q2);
  4236. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4237. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4238. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4239. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4240. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4241. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4242. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4243. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4244. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4245. sum += d * (isum1 + isum2);
  4246. }
  4247. *s = sum;
  4248. #elif defined __AVX2__
  4249. const __m256i m3 = _mm256_set1_epi8(3);
  4250. __m256 acc = _mm256_setzero_ps();
  4251. uint32_t ud, um;
  4252. const uint8_t * restrict db = (const uint8_t *)&ud;
  4253. const uint8_t * restrict mb = (const uint8_t *)&um;
  4254. float summs = 0;
  4255. // TODO: optimize this
  4256. for (int i = 0; i < nb; ++i) {
  4257. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4258. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4259. const uint8_t * restrict q2 = x[i].qs;
  4260. const int8_t * restrict q8 = y[i].qs;
  4261. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4262. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4263. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4264. 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];
  4265. summs += dmin * smin;
  4266. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4267. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4268. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4269. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4270. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4271. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4272. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4273. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4274. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4275. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4276. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4277. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4278. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4279. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4280. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4281. }
  4282. *s = hsum_float_8(acc) + summs;
  4283. #elif defined __AVX__
  4284. const __m128i m3 = _mm_set1_epi8(3);
  4285. __m256 acc = _mm256_setzero_ps();
  4286. uint32_t ud, um;
  4287. const uint8_t * restrict db = (const uint8_t *)&ud;
  4288. const uint8_t * restrict mb = (const uint8_t *)&um;
  4289. float summs = 0;
  4290. // TODO: optimize this
  4291. for (int i = 0; i < nb; ++i) {
  4292. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4293. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4294. const uint8_t * restrict q2 = x[i].qs;
  4295. const int8_t * restrict q8 = y[i].qs;
  4296. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4297. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4298. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4299. 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];
  4300. summs += dmin * smin;
  4301. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4302. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4303. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4304. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4305. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4306. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4307. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4308. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4309. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4310. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4311. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4312. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4313. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4314. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4315. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4316. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4317. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4318. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4319. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4320. }
  4321. *s = hsum_float_8(acc) + summs;
  4322. #elif defined __riscv_v_intrinsic
  4323. uint32_t aux32[2];
  4324. const uint8_t * scales = (const uint8_t *)aux32;
  4325. float sumf = 0;
  4326. for (int i = 0; i < nb; ++i) {
  4327. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4328. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4329. const uint8_t * restrict q2 = x[i].qs;
  4330. const int8_t * restrict q8 = y[i].qs;
  4331. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4332. aux32[0] = sc[0] & 0x0f0f0f0f;
  4333. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4334. 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]);
  4335. int isum1 = 0;
  4336. int isum2 = 0;
  4337. size_t vl = 16;
  4338. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4339. // load Q2
  4340. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4341. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4342. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4343. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4344. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4345. // load Q8, and take product with Q2
  4346. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4347. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4348. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4349. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4350. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4351. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4352. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4353. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4354. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4355. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4356. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4357. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4358. sumf += d * (isum1 + isum2);
  4359. }
  4360. *s = sumf;
  4361. #else
  4362. float sumf = 0;
  4363. int isum[QK_K/16];
  4364. for (int i = 0; i < nb; ++i) {
  4365. const uint8_t * q2 = x[i].qs;
  4366. const int8_t * q8 = y[i].qs;
  4367. const uint8_t * sc = x[i].scales;
  4368. int summs = 0;
  4369. for (int j = 0; j < QK_K/16; ++j) {
  4370. summs += y[i].bsums[j] * (sc[j] >> 4);
  4371. }
  4372. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4373. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4374. memset(isum, 0, (QK_K/16)*sizeof(int));
  4375. for (int l = 0; l < 16; ++l) {
  4376. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4377. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4378. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4379. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4380. }
  4381. for (int l = 0; l < QK_K/16; ++l) {
  4382. isum[l] *= (sc[l] & 0xF);
  4383. }
  4384. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4385. }
  4386. *s = sumf;
  4387. #endif
  4388. }
  4389. #endif
  4390. #if QK_K == 256
  4391. 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) {
  4392. assert(n % QK_K == 0);
  4393. assert(nrc == 1);
  4394. UNUSED(nrc);
  4395. UNUSED(bx);
  4396. UNUSED(by);
  4397. UNUSED(bs);
  4398. const uint32_t kmask1 = 0x03030303;
  4399. const uint32_t kmask2 = 0x0f0f0f0f;
  4400. const block_q3_K * restrict x = vx;
  4401. const block_q8_K * restrict y = vy;
  4402. const int nb = n / QK_K;
  4403. #ifdef __ARM_NEON
  4404. uint32_t aux[3];
  4405. uint32_t utmp[4];
  4406. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4407. const int32x4_t vzero = vdupq_n_s32(0);
  4408. const uint8x16_t m0 = vdupq_n_u8(1);
  4409. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4410. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4411. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4412. const int8_t m32 = 32;
  4413. ggml_int8x16x4_t q3bytes;
  4414. float sum = 0;
  4415. for (int i = 0; i < nb; ++i) {
  4416. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4417. const uint8_t * restrict q3 = x[i].qs;
  4418. const uint8_t * restrict qh = x[i].hmask;
  4419. const int8_t * restrict q8 = y[i].qs;
  4420. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4421. ggml_uint8x16x4_t q3h;
  4422. int32_t isum = 0;
  4423. // Set up scales
  4424. memcpy(aux, x[i].scales, 12);
  4425. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4426. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4427. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4428. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4429. int8_t * scale = (int8_t *)utmp;
  4430. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4431. for (int j = 0; j < QK_K/128; ++j) {
  4432. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4433. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4434. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4435. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4436. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4437. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4438. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4439. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4440. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4441. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4442. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4443. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4444. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4445. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4446. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4447. scale += 4;
  4448. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4449. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4450. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4451. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4452. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4453. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4454. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4455. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4456. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4457. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4458. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4459. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4460. scale += 4;
  4461. if (j == 0) {
  4462. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4463. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4464. }
  4465. }
  4466. sum += d * isum;
  4467. }
  4468. *s = sum;
  4469. #elif defined __AVX2__
  4470. const __m256i m3 = _mm256_set1_epi8(3);
  4471. const __m256i mone = _mm256_set1_epi8(1);
  4472. const __m128i m32 = _mm_set1_epi8(32);
  4473. __m256 acc = _mm256_setzero_ps();
  4474. uint32_t aux[3];
  4475. for (int i = 0; i < nb; ++i) {
  4476. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4477. const uint8_t * restrict q3 = x[i].qs;
  4478. const int8_t * restrict q8 = y[i].qs;
  4479. // Set up scales
  4480. memcpy(aux, x[i].scales, 12);
  4481. __m128i scales128 = _mm_set_epi32(
  4482. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4483. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4484. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4485. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4486. scales128 = _mm_sub_epi8(scales128, m32);
  4487. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4488. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4489. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4490. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4491. // high bit
  4492. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4493. // integer accumulator
  4494. __m256i sumi = _mm256_setzero_si256();
  4495. int bit = 0;
  4496. int is = 0;
  4497. for (int j = 0; j < QK_K/128; ++j) {
  4498. // load low 2 bits
  4499. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4500. // prepare low and high bits
  4501. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4502. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4503. ++bit;
  4504. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4505. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4506. ++bit;
  4507. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4508. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4509. ++bit;
  4510. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4511. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4512. ++bit;
  4513. // load Q8 quants
  4514. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4515. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4516. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4517. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4518. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4519. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4520. // and 2 if the high bit was set)
  4521. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4522. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4523. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4524. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4525. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4526. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4527. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4528. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4529. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4530. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4531. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4532. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4533. // multiply with scales
  4534. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4535. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4536. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4537. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4538. // accumulate
  4539. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4540. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4541. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4542. }
  4543. // multiply with block scale and accumulate
  4544. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4545. }
  4546. *s = hsum_float_8(acc);
  4547. #elif defined __AVX__
  4548. const __m128i m3 = _mm_set1_epi8(3);
  4549. const __m128i mone = _mm_set1_epi8(1);
  4550. const __m128i m32 = _mm_set1_epi8(32);
  4551. const __m128i m2 = _mm_set1_epi8(2);
  4552. __m256 acc = _mm256_setzero_ps();
  4553. const uint32_t *aux;
  4554. for (int i = 0; i < nb; ++i) {
  4555. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4556. const uint8_t * restrict q3 = x[i].qs;
  4557. const int8_t * restrict q8 = y[i].qs;
  4558. // Set up scales
  4559. aux = (const uint32_t *)x[i].scales;
  4560. __m128i scales128 = _mm_set_epi32(
  4561. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4562. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4563. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4564. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4565. scales128 = _mm_sub_epi8(scales128, m32);
  4566. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4567. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4568. const __m128i scales[2] = { scales_0, scales_1 };
  4569. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4570. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4571. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4572. // integer accumulator
  4573. __m128i sumi_0 = _mm_setzero_si128();
  4574. __m128i sumi_1 = _mm_setzero_si128();
  4575. for (int j = 0; j < QK_K/128; ++j) {
  4576. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4577. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4578. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4579. // prepare low and high bits
  4580. const int bit = j << 2;
  4581. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4582. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4583. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4584. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4585. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4586. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4587. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4588. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4589. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4590. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4591. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4592. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4593. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4594. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4595. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4596. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4597. // load Q8 quants from block_q8_K.qs[QK_K]
  4598. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4599. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4600. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4601. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4602. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4603. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4604. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4605. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4606. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4607. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4608. // and 2 if the high bit was set)
  4609. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4610. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4611. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4612. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4613. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4614. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4615. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4616. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4617. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4618. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4619. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4620. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4621. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4622. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4623. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4624. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4625. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4626. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4627. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4628. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4629. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4630. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4631. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4632. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4633. // multiply with scales
  4634. __m128i shuffle = _mm_set1_epi16(0x0100);
  4635. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4636. shuffle = _mm_add_epi16(shuffle, m2);
  4637. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4638. shuffle = _mm_add_epi16(shuffle, m2);
  4639. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4640. shuffle = _mm_add_epi16(shuffle, m2);
  4641. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4642. shuffle = _mm_add_epi16(shuffle, m2);
  4643. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4644. shuffle = _mm_add_epi16(shuffle, m2);
  4645. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4646. shuffle = _mm_add_epi16(shuffle, m2);
  4647. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4648. shuffle = _mm_add_epi16(shuffle, m2);
  4649. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4650. // accumulate
  4651. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4652. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4653. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4654. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4655. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4656. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4657. }
  4658. // multiply with block scale and accumulate
  4659. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4660. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4661. }
  4662. *s = hsum_float_8(acc);
  4663. #elif defined __riscv_v_intrinsic
  4664. uint32_t aux[3];
  4665. uint32_t utmp[4];
  4666. float sumf = 0;
  4667. for (int i = 0; i < nb; ++i) {
  4668. const uint8_t * restrict q3 = x[i].qs;
  4669. const uint8_t * restrict qh = x[i].hmask;
  4670. const int8_t * restrict q8 = y[i].qs;
  4671. memcpy(aux, x[i].scales, 12);
  4672. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4673. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4674. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4675. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4676. int8_t * scale = (int8_t *)utmp;
  4677. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4678. size_t vl = 32;
  4679. uint8_t m = 1;
  4680. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4681. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4682. int sum_t = 0;
  4683. for (int j = 0; j < QK_K; j += 128) {
  4684. vl = 32;
  4685. // load Q3
  4686. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4687. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4688. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4689. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4690. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4691. // compute mask for subtraction
  4692. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4693. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4694. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4695. m <<= 1;
  4696. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4697. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4698. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4699. m <<= 1;
  4700. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4701. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4702. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4703. m <<= 1;
  4704. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4705. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4706. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4707. m <<= 1;
  4708. // load Q8 and take product with Q3
  4709. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4710. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4711. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4712. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4713. vl = 16;
  4714. // retrieve lane to multiply with scale
  4715. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4716. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4717. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4718. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4719. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4720. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4721. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4722. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4723. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4724. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4725. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4726. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4727. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4728. q3 += 32; q8 += 128; scale += 8;
  4729. }
  4730. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4731. sumf += d*sum_t;
  4732. }
  4733. *s = sumf;
  4734. #else
  4735. // scalar version
  4736. // This function is written like this so the compiler can manage to vectorize most of it
  4737. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4738. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4739. // The ideal situation would be if we could just write the code once, and the compiler would
  4740. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4741. // write vectorized versions for AVX, ARM_NEON, etc.
  4742. int8_t aux8[QK_K];
  4743. int16_t aux16[8];
  4744. float sums [8];
  4745. int32_t aux32[8];
  4746. memset(sums, 0, 8*sizeof(float));
  4747. uint32_t auxs[4];
  4748. const int8_t * scales = (const int8_t*)auxs;
  4749. float sumf = 0;
  4750. for (int i = 0; i < nb; ++i) {
  4751. const uint8_t * restrict q3 = x[i].qs;
  4752. const uint8_t * restrict hm = x[i].hmask;
  4753. const int8_t * restrict q8 = y[i].qs;
  4754. memset(aux32, 0, 8*sizeof(int32_t));
  4755. int8_t * restrict a = aux8;
  4756. uint8_t m = 1;
  4757. for (int j = 0; j < QK_K; j += 128) {
  4758. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4759. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4760. a += 32; m <<= 1;
  4761. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4762. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4763. a += 32; m <<= 1;
  4764. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4765. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4766. a += 32; m <<= 1;
  4767. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4768. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4769. a += 32; m <<= 1;
  4770. q3 += 32;
  4771. }
  4772. a = aux8;
  4773. memcpy(auxs, x[i].scales, 12);
  4774. uint32_t tmp = auxs[2];
  4775. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4776. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4777. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4778. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4779. for (int j = 0; j < QK_K/16; ++j) {
  4780. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4781. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4782. q8 += 8; a += 8;
  4783. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4784. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4785. q8 += 8; a += 8;
  4786. }
  4787. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4788. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4789. }
  4790. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4791. *s = sumf;
  4792. #endif
  4793. }
  4794. #else
  4795. 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) {
  4796. assert(n % QK_K == 0);
  4797. assert(nrc == 1);
  4798. UNUSED(nrc);
  4799. UNUSED(bx);
  4800. UNUSED(by);
  4801. UNUSED(bs);
  4802. const block_q3_K * restrict x = vx;
  4803. const block_q8_K * restrict y = vy;
  4804. const int nb = n / QK_K;
  4805. #ifdef __ARM_NEON
  4806. const int32x4_t vzero = vdupq_n_s32(0);
  4807. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4808. const uint8x16_t mh = vdupq_n_u8(4);
  4809. ggml_int8x16x4_t q3bytes;
  4810. uint16_t aux16[2];
  4811. int8_t * scales = (int8_t *)aux16;
  4812. float sum = 0;
  4813. for (int i = 0; i < nb; ++i) {
  4814. ggml_uint8x16x4_t q3h;
  4815. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4816. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4817. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4818. const uint16_t a = *(const uint16_t *)x[i].scales;
  4819. aux16[0] = a & 0x0f0f;
  4820. aux16[1] = (a >> 4) & 0x0f0f;
  4821. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4822. 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]);
  4823. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4824. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4825. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4826. q3h.val[1] = vandq_u8(mh, htmp);
  4827. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4828. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4829. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4830. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4831. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4832. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4833. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4834. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4835. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4836. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4837. sum += d * isum;
  4838. }
  4839. *s = sum;
  4840. #elif defined __AVX2__
  4841. const __m256i m3 = _mm256_set1_epi8(3);
  4842. const __m256i m1 = _mm256_set1_epi8(1);
  4843. __m256 acc = _mm256_setzero_ps();
  4844. uint64_t aux64;
  4845. uint16_t aux16[2];
  4846. const int8_t * aux8 = (const int8_t *)aux16;
  4847. for (int i = 0; i < nb; ++i) {
  4848. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4849. const uint8_t * restrict q3 = x[i].qs;
  4850. const int8_t * restrict q8 = y[i].qs;
  4851. const uint16_t a = *(const uint16_t *)x[i].scales;
  4852. aux16[0] = a & 0x0f0f;
  4853. aux16[1] = (a >> 4) & 0x0f0f;
  4854. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4855. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4856. memcpy(&aux64, x[i].hmask, 8);
  4857. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4858. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4859. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4860. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4861. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4862. // load low 2 bits
  4863. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4864. // prepare low and high bits
  4865. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4866. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4867. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4868. // load Q8 quants
  4869. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4870. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4871. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4872. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4873. // and 2 if the high bit was set)
  4874. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4875. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4876. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4877. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4878. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4879. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4880. // multiply with scales
  4881. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4882. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4883. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4884. // multiply with block scale and accumulate
  4885. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4886. }
  4887. *s = hsum_float_8(acc);
  4888. #elif defined __AVX__
  4889. const __m128i m3 = _mm_set1_epi8(3);
  4890. const __m128i m1 = _mm_set1_epi8(1);
  4891. __m256 acc = _mm256_setzero_ps();
  4892. uint64_t aux64;
  4893. uint16_t aux16[2];
  4894. const int8_t * aux8 = (const int8_t *)aux16;
  4895. for (int i = 0; i < nb; ++i) {
  4896. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4897. const uint8_t * restrict q3 = x[i].qs;
  4898. const int8_t * restrict q8 = y[i].qs;
  4899. const uint16_t a = *(const uint16_t *)x[i].scales;
  4900. aux16[0] = a & 0x0f0f;
  4901. aux16[1] = (a >> 4) & 0x0f0f;
  4902. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4903. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4904. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4905. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4906. memcpy(&aux64, x[i].hmask, 8);
  4907. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4908. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4909. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4910. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4911. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4912. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4913. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4914. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4915. // load low 2 bits
  4916. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4917. // prepare low and high bits
  4918. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4919. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4920. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4921. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4922. // load Q8 quants
  4923. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4924. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4925. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4926. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4927. // and 2 if the high bit was set)
  4928. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4929. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4930. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4931. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4932. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4933. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4934. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4935. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4936. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4937. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4938. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4939. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4940. // multiply with scales
  4941. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4942. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4943. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4944. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4945. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4946. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4947. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4948. // multiply with block scale and accumulate
  4949. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4950. }
  4951. *s = hsum_float_8(acc);
  4952. #elif defined __riscv_v_intrinsic
  4953. uint16_t aux16[2];
  4954. int8_t * scales = (int8_t *)aux16;
  4955. float sumf = 0;
  4956. for (int i = 0; i < nb; ++i) {
  4957. const uint8_t * restrict q3 = x[i].qs;
  4958. const int8_t * restrict q8 = y[i].qs;
  4959. const uint16_t a = *(const uint16_t *)x[i].scales;
  4960. aux16[0] = a & 0x0f0f;
  4961. aux16[1] = (a >> 4) & 0x0f0f;
  4962. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4963. 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]);
  4964. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4965. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4966. // load qh
  4967. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4968. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4969. size_t vl = 16;
  4970. // extend and combine both qh_x1 and qh_x2
  4971. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4972. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4973. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4974. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4975. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4976. // load Q3
  4977. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4978. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4979. 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);
  4980. 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);
  4981. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4982. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4983. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4984. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4985. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4986. // load Q8 and take product with Q3
  4987. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4988. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4989. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4990. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4991. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4992. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4993. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4994. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4995. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4996. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4997. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4998. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4999. sumf += d * isum;
  5000. }
  5001. *s = sumf;
  5002. #else
  5003. int8_t aux8[QK_K];
  5004. int16_t aux16[8];
  5005. float sums [8];
  5006. int32_t aux32[8];
  5007. int32_t scales[4];
  5008. memset(sums, 0, 8*sizeof(float));
  5009. float sumf = 0;
  5010. for (int i = 0; i < nb; ++i) {
  5011. const uint8_t * restrict q3 = x[i].qs;
  5012. const uint8_t * restrict hm = x[i].hmask;
  5013. const int8_t * restrict q8 = y[i].qs;
  5014. int8_t * restrict a = aux8;
  5015. for (int l = 0; l < 8; ++l) {
  5016. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5017. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5018. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5019. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5020. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5021. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5022. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5023. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5024. }
  5025. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5026. scales[1] = (x[i].scales[0] >> 4) - 8;
  5027. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5028. scales[3] = (x[i].scales[1] >> 4) - 8;
  5029. memset(aux32, 0, 8*sizeof(int32_t));
  5030. for (int j = 0; j < QK_K/16; ++j) {
  5031. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5032. q8 += 8; a += 8;
  5033. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5034. q8 += 8; a += 8;
  5035. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5036. }
  5037. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5038. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5039. }
  5040. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5041. *s = sumf;
  5042. #endif
  5043. }
  5044. #endif
  5045. #if QK_K == 256
  5046. 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) {
  5047. assert(n % QK_K == 0);
  5048. assert(nrc == 1);
  5049. UNUSED(nrc);
  5050. UNUSED(bx);
  5051. UNUSED(by);
  5052. UNUSED(bs);
  5053. const block_q4_K * restrict x = vx;
  5054. const block_q8_K * restrict y = vy;
  5055. const int nb = n / QK_K;
  5056. static const uint32_t kmask1 = 0x3f3f3f3f;
  5057. static const uint32_t kmask2 = 0x0f0f0f0f;
  5058. static const uint32_t kmask3 = 0x03030303;
  5059. uint32_t utmp[4];
  5060. #ifdef __ARM_NEON
  5061. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5062. const int32x4_t mzero = vdupq_n_s32(0);
  5063. ggml_int8x16x2_t q4bytes;
  5064. ggml_int8x16x2_t q8bytes;
  5065. float sumf = 0;
  5066. for (int i = 0; i < nb; ++i) {
  5067. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5068. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5069. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5070. memcpy(utmp, x[i].scales, 12);
  5071. uint32x2_t mins8 = { 0 };
  5072. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5073. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5074. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5075. utmp[0] &= kmask1;
  5076. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5077. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5078. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5079. sumf -= dmin * vaddvq_s32(prod);
  5080. const uint8_t * scales = (const uint8_t *)utmp;
  5081. const uint8_t * restrict q4 = x[i].qs;
  5082. const int8_t * restrict q8 = y[i].qs;
  5083. int32_t sumi1 = 0;
  5084. int32_t sumi2 = 0;
  5085. for (int j = 0; j < QK_K/64; ++j) {
  5086. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5087. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5088. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5089. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5090. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5091. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5092. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5093. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5094. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5095. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5096. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5097. }
  5098. sumf += d * (sumi1 + sumi2);
  5099. }
  5100. *s = sumf;
  5101. #elif defined __AVX2__
  5102. const __m256i m4 = _mm256_set1_epi8(0xF);
  5103. __m256 acc = _mm256_setzero_ps();
  5104. __m128 acc_m = _mm_setzero_ps();
  5105. for (int i = 0; i < nb; ++i) {
  5106. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5107. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5108. memcpy(utmp, x[i].scales, 12);
  5109. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5110. const uint32_t uaux = utmp[1] & kmask1;
  5111. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5112. utmp[2] = uaux;
  5113. utmp[0] &= kmask1;
  5114. const uint8_t * restrict q4 = x[i].qs;
  5115. const int8_t * restrict q8 = y[i].qs;
  5116. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5117. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5118. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5119. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5120. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5121. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5122. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5123. __m256i sumi = _mm256_setzero_si256();
  5124. for (int j = 0; j < QK_K/64; ++j) {
  5125. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5126. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5127. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5128. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5129. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5130. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5131. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5132. p16l = _mm256_madd_epi16(scale_l, p16l);
  5133. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5134. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5135. p16h = _mm256_madd_epi16(scale_h, p16h);
  5136. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5137. sumi = _mm256_add_epi32(sumi, sumj);
  5138. }
  5139. __m256 vd = _mm256_set1_ps(d);
  5140. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5141. }
  5142. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5143. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5144. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5145. #elif defined __AVX__
  5146. const __m128i m4 = _mm_set1_epi8(0xF);
  5147. const __m128i m2 = _mm_set1_epi8(0x2);
  5148. __m256 acc = _mm256_setzero_ps();
  5149. __m128 acc_m = _mm_setzero_ps();
  5150. for (int i = 0; i < nb; ++i) {
  5151. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5152. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5153. const uint8_t * restrict q4 = x[i].qs;
  5154. const int8_t * restrict q8 = y[i].qs;
  5155. memcpy(utmp, x[i].scales, 12);
  5156. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5157. const uint32_t uaux = utmp[1] & kmask1;
  5158. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5159. utmp[2] = uaux;
  5160. utmp[0] &= kmask1;
  5161. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5162. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5163. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5164. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5165. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5166. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5167. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5168. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5169. __m128i sumi_0 = _mm_setzero_si128();
  5170. __m128i sumi_1 = _mm_setzero_si128();
  5171. __m128i shuffle = _mm_set1_epi16(0x0100);
  5172. for (int j = 0; j < QK_K/64; ++j) {
  5173. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5174. shuffle = _mm_add_epi16(shuffle, m2);
  5175. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5176. shuffle = _mm_add_epi16(shuffle, m2);
  5177. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5178. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5179. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5180. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5181. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5182. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5183. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5184. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5185. p16l = _mm_madd_epi16(scale_l, p16l);
  5186. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5187. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5188. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5189. p16l = _mm_madd_epi16(scale_l, p16l);
  5190. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5191. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5192. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5193. p16h = _mm_madd_epi16(scale_h, p16h);
  5194. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5195. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5196. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5197. p16h = _mm_madd_epi16(scale_h, p16h);
  5198. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5199. }
  5200. __m256 vd = _mm256_set1_ps(d);
  5201. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5202. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5203. }
  5204. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5205. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5206. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5207. #elif defined __riscv_v_intrinsic
  5208. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5209. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5210. float sumf = 0;
  5211. for (int i = 0; i < nb; ++i) {
  5212. size_t vl = 8;
  5213. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5214. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5215. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5216. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5217. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5218. memcpy(utmp, x[i].scales, 12);
  5219. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5220. const uint32_t uaux = utmp[1] & kmask1;
  5221. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5222. utmp[2] = uaux;
  5223. utmp[0] &= kmask1;
  5224. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5225. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5226. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5227. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5228. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5229. const uint8_t * restrict q4 = x[i].qs;
  5230. const int8_t * restrict q8 = y[i].qs;
  5231. vl = 32;
  5232. int32_t sum_1 = 0;
  5233. int32_t sum_2 = 0;
  5234. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5235. for (int j = 0; j < QK_K/64; ++j) {
  5236. // load Q4
  5237. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5238. // load Q8 and multiply it with lower Q4 nibble
  5239. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5240. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5241. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5242. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5243. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5244. // load Q8 and multiply it with upper Q4 nibble
  5245. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5246. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5247. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5248. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5249. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5250. q4 += 32; q8 += 64;
  5251. }
  5252. sumf += d*(sum_1 + sum_2);
  5253. }
  5254. *s = sumf;
  5255. #else
  5256. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5257. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5258. int8_t aux8[QK_K];
  5259. int16_t aux16[8];
  5260. float sums [8];
  5261. int32_t aux32[8];
  5262. memset(sums, 0, 8*sizeof(float));
  5263. float sumf = 0;
  5264. for (int i = 0; i < nb; ++i) {
  5265. const uint8_t * restrict q4 = x[i].qs;
  5266. const int8_t * restrict q8 = y[i].qs;
  5267. memset(aux32, 0, 8*sizeof(int32_t));
  5268. int8_t * restrict a = aux8;
  5269. for (int j = 0; j < QK_K/64; ++j) {
  5270. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5271. a += 32;
  5272. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5273. a += 32; q4 += 32;
  5274. }
  5275. memcpy(utmp, x[i].scales, 12);
  5276. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5277. const uint32_t uaux = utmp[1] & kmask1;
  5278. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5279. utmp[2] = uaux;
  5280. utmp[0] &= kmask1;
  5281. int sumi = 0;
  5282. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5283. a = aux8;
  5284. int is = 0;
  5285. for (int j = 0; j < QK_K/32; ++j) {
  5286. int32_t scale = scales[is++];
  5287. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5288. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5289. q8 += 8; a += 8;
  5290. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5291. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5292. q8 += 8; a += 8;
  5293. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5294. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5295. q8 += 8; a += 8;
  5296. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5297. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5298. q8 += 8; a += 8;
  5299. }
  5300. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5301. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5302. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5303. sumf -= dmin * sumi;
  5304. }
  5305. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5306. *s = sumf;
  5307. #endif
  5308. }
  5309. #else
  5310. 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) {
  5311. assert(n % QK_K == 0);
  5312. assert(nrc == 1);
  5313. UNUSED(nrc);
  5314. UNUSED(bx);
  5315. UNUSED(by);
  5316. UNUSED(bs);
  5317. const block_q4_K * restrict x = vx;
  5318. const block_q8_K * restrict y = vy;
  5319. const int nb = n / QK_K;
  5320. #ifdef __ARM_NEON
  5321. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5322. const int32x4_t mzero = vdupq_n_s32(0);
  5323. float sumf = 0;
  5324. ggml_int8x16x2_t q4bytes;
  5325. ggml_int8x16x4_t q8bytes;
  5326. float sum_mins = 0.f;
  5327. uint16_t aux16[2];
  5328. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5329. for (int i = 0; i < nb; ++i) {
  5330. const uint8_t * restrict q4 = x[i].qs;
  5331. const int8_t * restrict q8 = y[i].qs;
  5332. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5333. aux16[0] = a[0] & 0x0f0f;
  5334. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5335. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5336. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5337. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5338. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5339. q8bytes = ggml_vld1q_s8_x4(q8);
  5340. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5341. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5342. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5343. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5344. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5345. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5346. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5347. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5348. sumf += d * (sumi1 + sumi2);
  5349. }
  5350. *s = sumf - sum_mins;
  5351. #elif defined __AVX2__
  5352. const __m256i m4 = _mm256_set1_epi8(0xF);
  5353. __m256 acc = _mm256_setzero_ps();
  5354. float summs = 0;
  5355. uint16_t aux16[2];
  5356. const uint8_t * scales = (const uint8_t *)aux16;
  5357. for (int i = 0; i < nb; ++i) {
  5358. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5359. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5360. const __m256 vd = _mm256_set1_ps(d);
  5361. const uint16_t * a = (const uint16_t *)x[i].scales;
  5362. aux16[0] = a[0] & 0x0f0f;
  5363. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5364. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5365. const uint8_t * restrict q4 = x[i].qs;
  5366. const int8_t * restrict q8 = y[i].qs;
  5367. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5368. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5369. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5370. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5371. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5372. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5373. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5374. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5375. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5376. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5377. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5378. }
  5379. *s = hsum_float_8(acc) - summs;
  5380. #elif defined __AVX__
  5381. const __m128i m4 = _mm_set1_epi8(0xF);
  5382. __m256 acc = _mm256_setzero_ps();
  5383. float summs = 0;
  5384. uint16_t aux16[2];
  5385. const uint8_t * scales = (const uint8_t *)aux16;
  5386. for (int i = 0; i < nb; ++i) {
  5387. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5388. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5389. const __m256 vd = _mm256_set1_ps(d);
  5390. const uint16_t * a = (const uint16_t *)x[i].scales;
  5391. aux16[0] = a[0] & 0x0f0f;
  5392. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5393. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5394. const uint8_t * restrict q4 = x[i].qs;
  5395. const int8_t * restrict q8 = y[i].qs;
  5396. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5397. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5398. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5399. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5400. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5401. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5402. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5403. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5404. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5405. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5406. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5407. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5408. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5409. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5410. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5411. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5412. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5413. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5414. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5415. }
  5416. *s = hsum_float_8(acc) - summs;
  5417. #elif defined __riscv_v_intrinsic
  5418. uint16_t s16[2];
  5419. const uint8_t * restrict scales = (const uint8_t *)s16;
  5420. float sumf = 0;
  5421. for (int i = 0; i < nb; ++i) {
  5422. const uint8_t * restrict q4 = x[i].qs;
  5423. const int8_t * restrict q8 = y[i].qs;
  5424. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5425. s16[0] = b[0] & 0x0f0f;
  5426. s16[1] = (b[0] >> 4) & 0x0f0f;
  5427. 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]));
  5428. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5429. size_t vl = 32;
  5430. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5431. // load Q4
  5432. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5433. // load Q8 and multiply it with lower Q4 nibble
  5434. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5435. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5436. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5437. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5438. // load Q8 and multiply it with upper Q4 nibble
  5439. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5440. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5441. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5442. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5443. }
  5444. *s = sumf;
  5445. #else
  5446. uint8_t aux8[QK_K];
  5447. int16_t aux16[16];
  5448. float sums [8];
  5449. memset(sums, 0, 8*sizeof(float));
  5450. uint16_t s16[2];
  5451. const uint8_t * restrict scales = (const uint8_t *)s16;
  5452. float sumf = 0;
  5453. for (int i = 0; i < nb; ++i) {
  5454. const uint8_t * restrict q4 = x[i].qs;
  5455. const int8_t * restrict q8 = y[i].qs;
  5456. uint8_t * restrict a = aux8;
  5457. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5458. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5459. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5460. s16[0] = b[0] & 0x0f0f;
  5461. s16[1] = (b[0] >> 4) & 0x0f0f;
  5462. 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]));
  5463. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5464. for (int j = 0; j < QK_K/32; ++j) {
  5465. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5466. q8 += 16; a += 16;
  5467. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5468. q8 += 16; a += 16;
  5469. const float dl = d * scales[j];
  5470. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5471. }
  5472. }
  5473. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5474. *s = sumf;
  5475. #endif
  5476. }
  5477. #endif
  5478. #if QK_K == 256
  5479. 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) {
  5480. assert(n % QK_K == 0);
  5481. assert(nrc == 1);
  5482. UNUSED(nrc);
  5483. UNUSED(bx);
  5484. UNUSED(by);
  5485. UNUSED(bs);
  5486. const block_q5_K * restrict x = vx;
  5487. const block_q8_K * restrict y = vy;
  5488. const int nb = n / QK_K;
  5489. static const uint32_t kmask1 = 0x3f3f3f3f;
  5490. static const uint32_t kmask2 = 0x0f0f0f0f;
  5491. static const uint32_t kmask3 = 0x03030303;
  5492. uint32_t utmp[4];
  5493. #ifdef __ARM_NEON
  5494. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5495. const uint8x16_t mone = vdupq_n_u8(1);
  5496. const uint8x16_t mtwo = vdupq_n_u8(2);
  5497. const int32x4_t mzero = vdupq_n_s32(0);
  5498. ggml_int8x16x4_t q5bytes;
  5499. float sumf = 0;
  5500. for (int i = 0; i < nb; ++i) {
  5501. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5502. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5503. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5504. memcpy(utmp, x[i].scales, 12);
  5505. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5506. const uint32_t uaux = utmp[1] & kmask1;
  5507. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5508. utmp[2] = uaux;
  5509. utmp[0] &= kmask1;
  5510. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5511. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5512. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5513. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5514. int32_t sumi_mins = vaddvq_s32(prod);
  5515. const uint8_t * scales = (const uint8_t *)utmp;
  5516. const uint8_t * restrict q5 = x[i].qs;
  5517. const uint8_t * restrict qh = x[i].qh;
  5518. const int8_t * restrict q8 = y[i].qs;
  5519. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5520. ggml_uint8x16x4_t q5h;
  5521. int32_t sumi = 0;
  5522. for (int j = 0; j < QK_K/64; ++j) {
  5523. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5524. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5525. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5526. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5527. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5528. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5529. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5530. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5531. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5532. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5533. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5534. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5535. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5536. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5537. }
  5538. sumf += d * sumi - dmin * sumi_mins;
  5539. }
  5540. *s = sumf;
  5541. #elif defined __AVX2__
  5542. const __m256i m4 = _mm256_set1_epi8(0xF);
  5543. const __m128i mzero = _mm_setzero_si128();
  5544. const __m256i mone = _mm256_set1_epi8(1);
  5545. __m256 acc = _mm256_setzero_ps();
  5546. float summs = 0.f;
  5547. for (int i = 0; i < nb; ++i) {
  5548. const uint8_t * restrict q5 = x[i].qs;
  5549. const int8_t * restrict q8 = y[i].qs;
  5550. #if QK_K == 256
  5551. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5552. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5553. memcpy(utmp, x[i].scales, 12);
  5554. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5555. const uint32_t uaux = utmp[1] & kmask1;
  5556. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5557. utmp[2] = uaux;
  5558. utmp[0] &= kmask1;
  5559. #else
  5560. // TODO
  5561. const float d = 0, dmin = 0;
  5562. #endif
  5563. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5564. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5565. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5566. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5567. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5568. summs += dmin * _mm_extract_epi32(hsum, 0);
  5569. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5570. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5571. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5572. __m256i hmask = mone;
  5573. __m256i sumi = _mm256_setzero_si256();
  5574. int bit = 0;
  5575. for (int j = 0; j < QK_K/64; ++j) {
  5576. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5577. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5578. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5579. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5580. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5581. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5582. hmask = _mm256_slli_epi16(hmask, 1);
  5583. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5584. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5585. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5586. hmask = _mm256_slli_epi16(hmask, 1);
  5587. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5588. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5589. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5590. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5591. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5592. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5593. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5594. }
  5595. __m256 vd = _mm256_set1_ps(d);
  5596. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5597. }
  5598. *s = hsum_float_8(acc) + summs;
  5599. #elif defined __AVX__
  5600. const __m128i m4 = _mm_set1_epi8(0xF);
  5601. const __m128i mzero = _mm_setzero_si128();
  5602. const __m128i mone = _mm_set1_epi8(1);
  5603. const __m128i m2 = _mm_set1_epi8(2);
  5604. __m256 acc = _mm256_setzero_ps();
  5605. float summs = 0.f;
  5606. for (int i = 0; i < nb; ++i) {
  5607. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5608. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5609. const uint8_t * restrict q5 = x[i].qs;
  5610. const int8_t * restrict q8 = y[i].qs;
  5611. memcpy(utmp, x[i].scales, 12);
  5612. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5613. const uint32_t uaux = utmp[1] & kmask1;
  5614. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5615. utmp[2] = uaux;
  5616. utmp[0] &= kmask1;
  5617. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5618. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5619. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5620. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5621. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5622. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5623. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5624. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5625. summs += dmin * _mm_extract_epi32(hsum, 0);
  5626. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5627. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5628. __m128i hmask = mone;
  5629. __m128i sumi_0 = _mm_setzero_si128();
  5630. __m128i sumi_1 = _mm_setzero_si128();
  5631. int bit = 0;
  5632. __m128i shuffle = _mm_set1_epi16(0x0100);
  5633. for (int j = 0; j < QK_K/64; ++j) {
  5634. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5635. shuffle = _mm_add_epi16(shuffle, m2);
  5636. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5637. shuffle = _mm_add_epi16(shuffle, m2);
  5638. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5639. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5640. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5641. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5642. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5643. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5644. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5645. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5646. hmask = _mm_slli_epi16(hmask, 1);
  5647. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5648. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5649. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5650. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5651. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5652. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5653. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5654. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5655. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5656. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5657. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5658. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5659. hmask = _mm_slli_epi16(hmask, 1);
  5660. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5661. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5662. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5663. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5664. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5665. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5666. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5667. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5668. }
  5669. __m256 vd = _mm256_set1_ps(d);
  5670. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5671. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5672. }
  5673. *s = hsum_float_8(acc) + summs;
  5674. #elif defined __riscv_v_intrinsic
  5675. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5676. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5677. float sumf = 0;
  5678. float sums = 0.0;
  5679. size_t vl;
  5680. for (int i = 0; i < nb; ++i) {
  5681. vl = 8;
  5682. const uint8_t * restrict q5 = x[i].qs;
  5683. const uint8_t * restrict hm = x[i].qh;
  5684. const int8_t * restrict q8 = y[i].qs;
  5685. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5686. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5687. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5688. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5689. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5690. memcpy(utmp, x[i].scales, 12);
  5691. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5692. const uint32_t uaux = utmp[1] & kmask1;
  5693. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5694. utmp[2] = uaux;
  5695. utmp[0] &= kmask1;
  5696. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5697. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5698. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5699. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5700. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5701. vl = 32;
  5702. int32_t aux32 = 0;
  5703. int is = 0;
  5704. uint8_t m = 1;
  5705. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5706. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5707. for (int j = 0; j < QK_K/64; ++j) {
  5708. // load Q5 and Q8
  5709. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5710. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5711. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5712. // compute mask for addition
  5713. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5714. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5715. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5716. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5717. m <<= 1;
  5718. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5719. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5720. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5721. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5722. m <<= 1;
  5723. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5724. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5725. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5726. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5727. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5728. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5729. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5730. q5 += 32; q8 += 64;
  5731. }
  5732. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5733. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5734. }
  5735. *s = sumf+sums;
  5736. #else
  5737. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5738. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5739. int8_t aux8[QK_K];
  5740. int16_t aux16[8];
  5741. float sums [8];
  5742. int32_t aux32[8];
  5743. memset(sums, 0, 8*sizeof(float));
  5744. float sumf = 0;
  5745. for (int i = 0; i < nb; ++i) {
  5746. const uint8_t * restrict q4 = x[i].qs;
  5747. const uint8_t * restrict hm = x[i].qh;
  5748. const int8_t * restrict q8 = y[i].qs;
  5749. memset(aux32, 0, 8*sizeof(int32_t));
  5750. int8_t * restrict a = aux8;
  5751. uint8_t m = 1;
  5752. for (int j = 0; j < QK_K/64; ++j) {
  5753. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5754. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5755. a += 32; m <<= 1;
  5756. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5757. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5758. a += 32; m <<= 1;
  5759. q4 += 32;
  5760. }
  5761. memcpy(utmp, x[i].scales, 12);
  5762. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5763. const uint32_t uaux = utmp[1] & kmask1;
  5764. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5765. utmp[2] = uaux;
  5766. utmp[0] &= kmask1;
  5767. int sumi = 0;
  5768. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5769. a = aux8;
  5770. int is = 0;
  5771. for (int j = 0; j < QK_K/32; ++j) {
  5772. int32_t scale = scales[is++];
  5773. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5774. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5775. q8 += 8; a += 8;
  5776. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5777. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5778. q8 += 8; a += 8;
  5779. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5780. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5781. q8 += 8; a += 8;
  5782. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5783. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5784. q8 += 8; a += 8;
  5785. }
  5786. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5787. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5788. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5789. sumf -= dmin * sumi;
  5790. }
  5791. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5792. *s = sumf;
  5793. #endif
  5794. }
  5795. #else
  5796. 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) {
  5797. assert(n % QK_K == 0);
  5798. assert(nrc == 1);
  5799. UNUSED(nrc);
  5800. UNUSED(bx);
  5801. UNUSED(by);
  5802. UNUSED(bs);
  5803. const block_q5_K * restrict x = vx;
  5804. const block_q8_K * restrict y = vy;
  5805. const int nb = n / QK_K;
  5806. #ifdef __ARM_NEON
  5807. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5808. const uint8x16_t mh = vdupq_n_u8(16);
  5809. const int32x4_t mzero = vdupq_n_s32(0);
  5810. ggml_int8x16x4_t q5bytes;
  5811. ggml_uint8x16x4_t q5h;
  5812. float sumf = 0;
  5813. for (int i = 0; i < nb; ++i) {
  5814. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5815. const int8_t * sc = x[i].scales;
  5816. const uint8_t * restrict q5 = x[i].qs;
  5817. const uint8_t * restrict qh = x[i].qh;
  5818. const int8_t * restrict q8 = y[i].qs;
  5819. const uint8x8_t qhbits = vld1_u8(qh);
  5820. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5821. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5822. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5823. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5824. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5825. q5h.val[2] = vbicq_u8(mh, htmp);
  5826. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5827. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5828. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5829. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5830. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5831. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5832. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5833. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5834. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5835. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5836. }
  5837. *s = sumf;
  5838. #elif defined __AVX2__
  5839. const __m256i m4 = _mm256_set1_epi8(0xF);
  5840. const __m256i mone = _mm256_set1_epi8(1);
  5841. __m256 acc = _mm256_setzero_ps();
  5842. for (int i = 0; i < nb; ++i) {
  5843. const uint8_t * restrict q5 = x[i].qs;
  5844. const int8_t * restrict q8 = y[i].qs;
  5845. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5846. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5847. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5848. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5849. int64_t aux64;
  5850. memcpy(&aux64, x[i].qh, 8);
  5851. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5852. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5853. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5854. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5855. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5856. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5857. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5858. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5859. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5860. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5861. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5862. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5863. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5864. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5865. }
  5866. *s = hsum_float_8(acc);
  5867. #elif defined __AVX__
  5868. const __m128i m4 = _mm_set1_epi8(0xF);
  5869. const __m128i mone = _mm_set1_epi8(1);
  5870. __m256 acc = _mm256_setzero_ps();
  5871. for (int i = 0; i < nb; ++i) {
  5872. const uint8_t * restrict q5 = x[i].qs;
  5873. const int8_t * restrict q8 = y[i].qs;
  5874. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5875. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5876. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5877. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5878. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5879. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5880. int64_t aux64;
  5881. memcpy(&aux64, x[i].qh, 8);
  5882. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5883. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5884. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5885. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5886. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5887. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5888. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5889. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5890. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5891. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5892. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5893. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5894. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5895. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5896. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5897. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5898. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5899. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5900. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5901. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5902. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5903. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5904. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5905. }
  5906. *s = hsum_float_8(acc);
  5907. #elif defined __riscv_v_intrinsic
  5908. float sumf = 0;
  5909. for (int i = 0; i < nb; ++i) {
  5910. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5911. const int8_t * sc = x[i].scales;
  5912. const uint8_t * restrict q5 = x[i].qs;
  5913. const uint8_t * restrict qh = x[i].qh;
  5914. const int8_t * restrict q8 = y[i].qs;
  5915. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5916. // load qh
  5917. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5918. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5919. size_t vl = 16;
  5920. // combine both qh_1 and qh_2
  5921. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5922. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5923. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5924. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5925. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5926. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5927. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5928. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5929. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5930. // load q5
  5931. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5932. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5933. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5934. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5935. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5936. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5937. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5938. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5939. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5940. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5941. // load Q8 and multiply it with Q5
  5942. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5943. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5944. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5945. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5946. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5947. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5948. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5949. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5950. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5951. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5952. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5953. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5954. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5955. }
  5956. *s = sumf;
  5957. #else
  5958. int8_t aux8[QK_K];
  5959. int16_t aux16[16];
  5960. float sums [8];
  5961. memset(sums, 0, 8*sizeof(float));
  5962. float sumf = 0;
  5963. for (int i = 0; i < nb; ++i) {
  5964. const uint8_t * restrict q4 = x[i].qs;
  5965. const uint8_t * restrict hm = x[i].qh;
  5966. const int8_t * restrict q8 = y[i].qs;
  5967. int8_t * restrict a = aux8;
  5968. for (int l = 0; l < 32; ++l) {
  5969. a[l+ 0] = q4[l] & 0xF;
  5970. a[l+32] = q4[l] >> 4;
  5971. }
  5972. for (int is = 0; is < 8; ++is) {
  5973. uint8_t m = 1 << is;
  5974. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5975. }
  5976. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5977. const int8_t * restrict sc = x[i].scales;
  5978. for (int j = 0; j < QK_K/16; ++j) {
  5979. const float dl = d * sc[j];
  5980. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5981. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5982. q8 += 16; a += 16;
  5983. }
  5984. }
  5985. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5986. *s = sumf;
  5987. #endif
  5988. }
  5989. #endif
  5990. #if QK_K == 256
  5991. 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) {
  5992. assert(n % QK_K == 0);
  5993. assert(nrc == 1);
  5994. UNUSED(nrc);
  5995. UNUSED(bx);
  5996. UNUSED(by);
  5997. UNUSED(bs);
  5998. const block_q6_K * restrict x = vx;
  5999. const block_q8_K * restrict y = vy;
  6000. const int nb = n / QK_K;
  6001. #ifdef __ARM_NEON
  6002. float sum = 0;
  6003. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6004. const int32x4_t vzero = vdupq_n_s32(0);
  6005. //const int8x16_t m32s = vdupq_n_s8(32);
  6006. const uint8x16_t mone = vdupq_n_u8(3);
  6007. ggml_int8x16x4_t q6bytes;
  6008. ggml_uint8x16x4_t q6h;
  6009. for (int i = 0; i < nb; ++i) {
  6010. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6011. const uint8_t * restrict q6 = x[i].ql;
  6012. const uint8_t * restrict qh = x[i].qh;
  6013. const int8_t * restrict q8 = y[i].qs;
  6014. const int8_t * restrict scale = x[i].scales;
  6015. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6016. const int8x16_t scales = vld1q_s8(scale);
  6017. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6018. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6019. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6020. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6021. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6022. int32_t isum_mins = vaddvq_s32(prod);
  6023. int32_t isum = 0;
  6024. for (int j = 0; j < QK_K/128; ++j) {
  6025. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6026. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6027. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6028. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6029. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6030. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6031. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6032. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6033. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6034. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6035. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6036. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6037. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6038. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6039. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6040. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6041. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6042. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6043. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6044. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6045. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6046. scale += 4;
  6047. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6048. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6049. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6050. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6051. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6052. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6053. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6054. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6055. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6056. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6057. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6058. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6059. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6060. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6061. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6062. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6063. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6064. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6065. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6066. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6067. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6068. scale += 4;
  6069. }
  6070. //sum += isum * d_all * y[i].d;
  6071. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6072. }
  6073. *s = sum;
  6074. #elif defined __AVX2__
  6075. const __m256i m4 = _mm256_set1_epi8(0xF);
  6076. const __m256i m2 = _mm256_set1_epi8(3);
  6077. const __m256i m32s = _mm256_set1_epi8(32);
  6078. __m256 acc = _mm256_setzero_ps();
  6079. for (int i = 0; i < nb; ++i) {
  6080. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6081. const uint8_t * restrict q4 = x[i].ql;
  6082. const uint8_t * restrict qh = x[i].qh;
  6083. const int8_t * restrict q8 = y[i].qs;
  6084. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6085. __m256i sumi = _mm256_setzero_si256();
  6086. int is = 0;
  6087. for (int j = 0; j < QK_K/128; ++j) {
  6088. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6089. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6090. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6091. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6092. is += 4;
  6093. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6094. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6095. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6096. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6097. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6098. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6099. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6100. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6101. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6102. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6103. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6104. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6105. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6106. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6107. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6108. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6109. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6110. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6111. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6112. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6113. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6114. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6115. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6116. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6117. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6118. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6119. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6120. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6121. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6122. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6123. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6124. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6125. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6126. }
  6127. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6128. }
  6129. *s = hsum_float_8(acc);
  6130. #elif defined __AVX__
  6131. const __m128i m4 = _mm_set1_epi8(0xF);
  6132. const __m128i m3 = _mm_set1_epi8(3);
  6133. const __m128i m32s = _mm_set1_epi8(32);
  6134. const __m128i m2 = _mm_set1_epi8(2);
  6135. __m256 acc = _mm256_setzero_ps();
  6136. for (int i = 0; i < nb; ++i) {
  6137. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6138. const uint8_t * restrict q4 = x[i].ql;
  6139. const uint8_t * restrict qh = x[i].qh;
  6140. const int8_t * restrict q8 = y[i].qs;
  6141. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6142. __m128i sumi_0 = _mm_setzero_si128();
  6143. __m128i sumi_1 = _mm_setzero_si128();
  6144. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6145. for (int j = 0; j < QK_K/128; ++j) {
  6146. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6147. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6148. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6149. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6150. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6151. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6152. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6153. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6154. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6155. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6156. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6157. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6158. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6159. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6160. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6161. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6162. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6163. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6164. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6165. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6166. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6167. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6168. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6169. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6170. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6171. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6172. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6173. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6174. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6175. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6176. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6177. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6178. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6179. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6180. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6181. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6182. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6183. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6184. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6185. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6186. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6187. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6188. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6189. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6190. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6191. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6192. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6193. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6194. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6195. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6196. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6197. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6198. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6199. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6200. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6201. shuffle = _mm_add_epi8(shuffle, m2);
  6202. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6203. shuffle = _mm_add_epi8(shuffle, m2);
  6204. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6205. shuffle = _mm_add_epi8(shuffle, m2);
  6206. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6207. shuffle = _mm_add_epi8(shuffle, m2);
  6208. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6209. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6210. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6211. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6212. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6213. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6214. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6215. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6216. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6217. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6218. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6219. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6220. }
  6221. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6222. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6223. }
  6224. *s = hsum_float_8(acc);
  6225. #elif defined __riscv_v_intrinsic
  6226. float sumf = 0;
  6227. for (int i = 0; i < nb; ++i) {
  6228. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6229. const uint8_t * restrict q6 = x[i].ql;
  6230. const uint8_t * restrict qh = x[i].qh;
  6231. const int8_t * restrict q8 = y[i].qs;
  6232. const int8_t * restrict scale = x[i].scales;
  6233. size_t vl;
  6234. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6235. int sum_t = 0;
  6236. int is = 0;
  6237. for (int j = 0; j < QK_K/128; ++j) {
  6238. vl = 32;
  6239. // load qh
  6240. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6241. // load Q6
  6242. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6243. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6244. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6245. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6246. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6247. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6248. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6249. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6250. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6251. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6252. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6253. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6254. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6255. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6256. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6257. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6258. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6259. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6260. // load Q8 and take product
  6261. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6262. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6263. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6264. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6265. vl = 16;
  6266. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6267. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6268. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6269. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6270. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6271. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6272. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6273. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6274. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6275. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6276. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6277. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6278. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6279. q6 += 64; qh += 32; q8 += 128; is=8;
  6280. }
  6281. sumf += d * sum_t;
  6282. }
  6283. *s = sumf;
  6284. #else
  6285. int8_t aux8[QK_K];
  6286. int16_t aux16[8];
  6287. float sums [8];
  6288. int32_t aux32[8];
  6289. memset(sums, 0, 8*sizeof(float));
  6290. float sumf = 0;
  6291. for (int i = 0; i < nb; ++i) {
  6292. const uint8_t * restrict q4 = x[i].ql;
  6293. const uint8_t * restrict qh = x[i].qh;
  6294. const int8_t * restrict q8 = y[i].qs;
  6295. memset(aux32, 0, 8*sizeof(int32_t));
  6296. int8_t * restrict a = aux8;
  6297. for (int j = 0; j < QK_K; j += 128) {
  6298. for (int l = 0; l < 32; ++l) {
  6299. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6300. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6301. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6302. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6303. }
  6304. a += 128;
  6305. q4 += 64;
  6306. qh += 32;
  6307. }
  6308. a = aux8;
  6309. int is = 0;
  6310. for (int j = 0; j < QK_K/16; ++j) {
  6311. int scale = x[i].scales[is++];
  6312. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6313. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6314. q8 += 8; a += 8;
  6315. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6316. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6317. q8 += 8; a += 8;
  6318. }
  6319. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6320. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6321. }
  6322. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6323. *s = sumf;
  6324. #endif
  6325. }
  6326. #else
  6327. 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) {
  6328. assert(n % QK_K == 0);
  6329. assert(nrc == 1);
  6330. UNUSED(nrc);
  6331. UNUSED(bx);
  6332. UNUSED(by);
  6333. UNUSED(bs);
  6334. const block_q6_K * restrict x = vx;
  6335. const block_q8_K * restrict y = vy;
  6336. const int nb = n / QK_K;
  6337. #ifdef __ARM_NEON
  6338. float sum = 0;
  6339. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6340. const int8x16_t m32s = vdupq_n_s8(32);
  6341. const int32x4_t vzero = vdupq_n_s32(0);
  6342. const uint8x16_t mone = vdupq_n_u8(3);
  6343. ggml_int8x16x4_t q6bytes;
  6344. ggml_uint8x16x4_t q6h;
  6345. for (int i = 0; i < nb; ++i) {
  6346. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6347. const uint8_t * restrict q6 = x[i].ql;
  6348. const uint8_t * restrict qh = x[i].qh;
  6349. const int8_t * restrict q8 = y[i].qs;
  6350. const int8_t * restrict scale = x[i].scales;
  6351. int32_t isum = 0;
  6352. uint8x16_t qhbits = vld1q_u8(qh);
  6353. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6354. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6355. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6356. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6357. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6358. shifted = vshrq_n_u8(qhbits, 4);
  6359. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6360. shifted = vshrq_n_u8(qhbits, 6);
  6361. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6362. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6363. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6364. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6365. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6366. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6367. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6368. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6369. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6370. sum += isum * d_all * y[i].d;
  6371. }
  6372. *s = sum;
  6373. #elif defined __AVX2__
  6374. const __m256i m4 = _mm256_set1_epi8(0xF);
  6375. const __m256i m2 = _mm256_set1_epi8(3);
  6376. const __m256i m32s = _mm256_set1_epi8(32);
  6377. __m256 acc = _mm256_setzero_ps();
  6378. for (int i = 0; i < nb; ++i) {
  6379. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6380. const uint8_t * restrict q4 = x[i].ql;
  6381. const uint8_t * restrict qh = x[i].qh;
  6382. const int8_t * restrict q8 = y[i].qs;
  6383. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6384. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6385. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6386. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6387. __m256i sumi = _mm256_setzero_si256();
  6388. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6389. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6390. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6391. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6392. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6393. 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);
  6394. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6395. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6396. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6397. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6398. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6399. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6400. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6401. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6402. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6403. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6404. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6405. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6406. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6407. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6408. }
  6409. *s = hsum_float_8(acc);
  6410. #elif defined __AVX__
  6411. const __m128i m4 = _mm_set1_epi8(0xF);
  6412. const __m128i m2 = _mm_set1_epi8(3);
  6413. const __m128i m32s = _mm_set1_epi8(32);
  6414. __m256 acc = _mm256_setzero_ps();
  6415. for (int i = 0; i < nb; ++i) {
  6416. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6417. const uint8_t * restrict q4 = x[i].ql;
  6418. const uint8_t * restrict qh = x[i].qh;
  6419. const int8_t * restrict q8 = y[i].qs;
  6420. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6421. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6422. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6423. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6424. __m128i sumi_0 = _mm_setzero_si128();
  6425. __m128i sumi_1 = _mm_setzero_si128();
  6426. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6427. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6428. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6429. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6430. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6431. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6432. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6433. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6434. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6435. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6436. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6437. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6438. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6439. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6440. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6441. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6442. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6443. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6444. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6445. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6446. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6447. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6448. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6449. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6450. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6451. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6452. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6453. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6454. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6455. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6456. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6457. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6458. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6459. }
  6460. *s = hsum_float_8(acc);
  6461. #elif defined __riscv_v_intrinsic
  6462. float sumf = 0;
  6463. for (int i = 0; i < nb; ++i) {
  6464. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6465. const uint8_t * restrict q6 = x[i].ql;
  6466. const uint8_t * restrict qh = x[i].qh;
  6467. const int8_t * restrict q8 = y[i].qs;
  6468. const int8_t * restrict scale = x[i].scales;
  6469. int32_t isum = 0;
  6470. size_t vl = 16;
  6471. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6472. // load Q6
  6473. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6474. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6475. // load qh
  6476. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6477. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6478. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6479. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6480. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6481. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6482. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6483. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6484. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6485. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6486. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6487. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6488. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6489. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6490. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6491. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6492. // load Q8 and take product
  6493. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6494. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6495. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6496. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6497. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6498. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6499. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6500. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6501. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6502. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6503. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6504. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6505. sumf += isum * d_all * y[i].d;
  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 l = 0; l < 16; ++l) {
  6522. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6523. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6524. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6525. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6526. }
  6527. int is = 0;
  6528. for (int j = 0; j < QK_K/16; ++j) {
  6529. int scale = x[i].scales[is++];
  6530. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6531. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6532. q8 += 8; a += 8;
  6533. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6534. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6535. q8 += 8; a += 8;
  6536. }
  6537. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6538. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6539. }
  6540. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6541. *s = sumf;
  6542. #endif
  6543. }
  6544. #endif
  6545. #if defined (__AVX2__) || defined (__ARM_NEON)
  6546. static const int8_t keven_signs_q2xs[1024] = {
  6547. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6548. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6549. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6550. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6551. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6552. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6553. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6554. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6555. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6556. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6557. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6558. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6559. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6560. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6561. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6562. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6563. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6564. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6565. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6566. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6567. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6568. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6569. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6570. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6571. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6572. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6573. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6574. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6575. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6576. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6577. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6578. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6579. };
  6580. #endif
  6581. 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) {
  6582. assert(n % QK_K == 0);
  6583. assert(nrc == 1);
  6584. UNUSED(nrc);
  6585. UNUSED(bx);
  6586. UNUSED(by);
  6587. UNUSED(bs);
  6588. const block_iq2_xxs * restrict x = vx;
  6589. const block_q8_K * restrict y = vy;
  6590. const int nb = n / QK_K;
  6591. #if defined(__ARM_NEON)
  6592. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6593. uint32_t aux32[4];
  6594. const uint8_t * aux8 = (const uint8_t *)aux32;
  6595. ggml_int8x16x4_t q2u;
  6596. ggml_int8x16x4_t q2s;
  6597. ggml_int8x16x4_t q8b;
  6598. float sumf = 0;
  6599. for (int i = 0; i < nb; ++i) {
  6600. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6601. const uint16_t * restrict q2 = x[i].qs;
  6602. const int8_t * restrict q8 = y[i].qs;
  6603. float sumf1 = 0, sumf2 = 0;
  6604. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6605. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6606. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6607. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6608. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6609. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6610. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6611. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6612. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6613. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6614. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6615. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6616. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6617. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6618. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6619. 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]);
  6620. 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]);
  6621. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6622. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6623. }
  6624. sumf += d*(sumf1 + sumf2);
  6625. }
  6626. *s = 0.25f * sumf;
  6627. #elif defined(__AVX2__)
  6628. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6629. uint32_t aux32[4];
  6630. const uint8_t * aux8 = (const uint8_t *)aux32;
  6631. __m256 accumf = _mm256_setzero_ps();
  6632. for (int i = 0; i < nb; ++i) {
  6633. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6634. const uint16_t * restrict q2 = x[i].qs;
  6635. const int8_t * restrict q8 = y[i].qs;
  6636. __m256i sumi1 = _mm256_setzero_si256();
  6637. __m256i sumi2 = _mm256_setzero_si256();
  6638. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6639. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6640. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6641. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6642. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6643. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6644. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6645. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6646. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6647. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6648. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6649. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6650. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6651. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6652. const uint16_t ls1 = aux32[1] >> 28;
  6653. const uint16_t ls2 = aux32[3] >> 28;
  6654. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6655. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6656. sumi1 = _mm256_add_epi32(sumi1, p1);
  6657. sumi2 = _mm256_add_epi32(sumi2, p2);
  6658. }
  6659. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6660. }
  6661. *s = 0.125f * hsum_float_8(accumf);
  6662. #else
  6663. uint32_t aux32[2];
  6664. const uint8_t * aux8 = (const uint8_t *)aux32;
  6665. float sumf = 0.f;
  6666. for (int i = 0; i < nb; ++i) {
  6667. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6668. const uint16_t * restrict q2 = x[i].qs;
  6669. const int8_t * restrict q8 = y[i].qs;
  6670. int32_t bsum = 0;
  6671. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6672. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6673. q2 += 4;
  6674. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6675. int32_t sumi = 0;
  6676. for (int l = 0; l < 4; ++l) {
  6677. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6678. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6679. for (int j = 0; j < 8; ++j) {
  6680. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6681. }
  6682. q8 += 8;
  6683. }
  6684. bsum += sumi * ls;
  6685. }
  6686. sumf += d * bsum;
  6687. }
  6688. *s = 0.125f * sumf;
  6689. #endif
  6690. }
  6691. 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) {
  6692. assert(n % QK_K == 0);
  6693. assert(nrc == 1);
  6694. UNUSED(nrc);
  6695. UNUSED(bx);
  6696. UNUSED(by);
  6697. UNUSED(bs);
  6698. const block_iq2_xs * restrict x = vx;
  6699. const block_q8_K * restrict y = vy;
  6700. const int nb = n / QK_K;
  6701. #if defined(__ARM_NEON)
  6702. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6703. ggml_int8x16x4_t q2u;
  6704. ggml_int8x16x4_t q2s;
  6705. ggml_int8x16x4_t q8b;
  6706. int32x4x4_t scales32;
  6707. float sumf = 0;
  6708. for (int i = 0; i < nb; ++i) {
  6709. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6710. const uint16_t * restrict q2 = x[i].qs;
  6711. const int8_t * restrict q8 = y[i].qs;
  6712. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6713. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6714. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6715. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6716. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6717. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6718. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6719. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6720. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6721. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6722. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6723. int32x4_t sumi = vdupq_n_s32(0);
  6724. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6725. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6726. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6727. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6728. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6729. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6730. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6731. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6732. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6733. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6734. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6735. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6736. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6737. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6738. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6739. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6740. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6741. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6742. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6743. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6744. q2 += 8;
  6745. }
  6746. sumf += d*vaddvq_s32(sumi);
  6747. }
  6748. *s = 0.125f * sumf;
  6749. #elif defined(__AVX2__)
  6750. const __m256i mone = _mm256_set1_epi8(1);
  6751. static const char block_sign_shuffle_mask_1[32] = {
  6752. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6753. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6754. };
  6755. static const char block_sign_shuffle_mask_2[32] = {
  6756. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6757. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6758. };
  6759. static const uint8_t bit_selector_mask_bytes[32] = {
  6760. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6761. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6762. };
  6763. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6764. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6765. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6766. #if QK_K == 64
  6767. static const uint8_t k_bit_helper[16] = {
  6768. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6769. };
  6770. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6771. const __m128i m511 = _mm_set1_epi16(511);
  6772. typedef union {
  6773. __m128i vec_index;
  6774. uint16_t index[8];
  6775. } index_t;
  6776. index_t idx;
  6777. __m256 accumf = _mm256_setzero_ps();
  6778. for (int i = 0; i < nb; ++i) {
  6779. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6780. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6781. idx.vec_index = _mm_and_si128(q2_data, m511);
  6782. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6783. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6784. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6785. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6786. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6787. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6788. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6789. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6790. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6791. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6792. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6793. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6794. __m256i signs;
  6795. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6796. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6797. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6798. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6799. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6800. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6801. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6802. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6803. 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));
  6804. 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));
  6805. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6806. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6807. }
  6808. *s = 0.125f * hsum_float_8(accumf);
  6809. #else
  6810. static const uint8_t k_bit_helper[32] = {
  6811. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6812. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6813. };
  6814. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6815. const __m256i m511 = _mm256_set1_epi16(511);
  6816. const __m128i m4 = _mm_set1_epi8(0xf);
  6817. const __m128i m1 = _mm_set1_epi8(1);
  6818. uint64_t aux64;
  6819. // somewhat hacky, but gives a significant boost in performance
  6820. __m256i aux_gindex;
  6821. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6822. __m256 accumf = _mm256_setzero_ps();
  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. memcpy(&aux64, x[i].scales, 8);
  6828. __m128i stmp = _mm_set1_epi64x(aux64);
  6829. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6830. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6831. __m256i sumi1 = _mm256_setzero_si256();
  6832. __m256i sumi2 = _mm256_setzero_si256();
  6833. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6834. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6835. aux_gindex = _mm256_and_si256(q2_data, m511);
  6836. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6837. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6838. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6839. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6840. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6841. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6842. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6843. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6844. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6845. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6846. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6847. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6848. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6849. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6850. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6851. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6852. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6853. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6854. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6855. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  6856. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  6857. __m256i signs;
  6858. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6859. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6860. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6861. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6862. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6863. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6864. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6865. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6866. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6867. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6868. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6869. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6870. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6871. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6872. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6873. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6874. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6875. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6876. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6877. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6878. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6879. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6880. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6881. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  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. #endif
  6887. #else
  6888. float sumf = 0.f;
  6889. for (int i = 0; i < nb; ++i) {
  6890. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6891. const uint16_t * restrict q2 = x[i].qs;
  6892. const uint8_t * restrict sc = x[i].scales;
  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. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6897. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6898. int32_t sumi = 0;
  6899. for (int l = 0; l < 2; ++l) {
  6900. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6901. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6902. for (int j = 0; j < 8; ++j) {
  6903. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6904. }
  6905. q8 += 8;
  6906. }
  6907. bsum += sumi * ls1;
  6908. sumi = 0;
  6909. for (int l = 2; l < 4; ++l) {
  6910. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6911. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6912. for (int j = 0; j < 8; ++j) {
  6913. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6914. }
  6915. q8 += 8;
  6916. }
  6917. bsum += sumi * ls2;
  6918. q2 += 4;
  6919. }
  6920. sumf += d * bsum;
  6921. }
  6922. *s = 0.125f * sumf;
  6923. #endif
  6924. }
  6925. 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) {
  6926. assert(n % QK_K == 0);
  6927. assert(nrc == 1);
  6928. UNUSED(nrc);
  6929. UNUSED(bx);
  6930. UNUSED(by);
  6931. UNUSED(bs);
  6932. const block_iq2_s * restrict x = vx;
  6933. const block_q8_K * restrict y = vy;
  6934. const int nb = n / QK_K;
  6935. #if defined(__ARM_NEON)
  6936. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6937. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6938. };
  6939. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  6940. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  6941. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  6942. const uint8x16_t m1 = vdupq_n_u8(1);
  6943. const int32x4_t vzero = vdupq_n_s32(0);
  6944. uint8x16x2_t vs;
  6945. ggml_int8x16x4_t q2s;
  6946. ggml_int8x16x4_t q8b;
  6947. float sumf = 0;
  6948. for (int i = 0; i < nb; ++i) {
  6949. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6950. const uint8_t * restrict qs = x[i].qs;
  6951. const uint8_t * restrict qh = x[i].qh;
  6952. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  6953. const int8_t * restrict q8 = y[i].qs;
  6954. int sumi1 = 0, sumi2 = 0;
  6955. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6956. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6957. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  6958. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  6959. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  6960. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  6961. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  6962. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  6963. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  6964. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  6965. qs += 8;
  6966. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  6967. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6968. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6969. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6970. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6971. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  6972. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  6973. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  6974. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6975. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6976. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6977. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6978. signs += 4;
  6979. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  6980. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  6981. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  6982. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  6983. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  6984. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  6985. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  6986. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  6987. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  6988. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  6989. }
  6990. sumf += d*(sumi1 + sumi2);
  6991. }
  6992. *s = 0.125f * sumf;
  6993. #elif defined(__AVX2__)
  6994. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6995. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6996. };
  6997. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6998. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6999. };
  7000. const __m128i m4 = _mm_set1_epi8(0xf);
  7001. const __m128i m1 = _mm_set1_epi8(1);
  7002. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7003. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7004. uint64_t aux64;
  7005. __m256 accumf = _mm256_setzero_ps();
  7006. for (int i = 0; i < nb; ++i) {
  7007. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7008. const uint8_t * restrict qs = x[i].qs;
  7009. const uint8_t * restrict qh = x[i].qh;
  7010. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7011. const int8_t * restrict q8 = y[i].qs;
  7012. memcpy(&aux64, x[i].scales, 8);
  7013. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7014. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7015. __m256i sumi1 = _mm256_setzero_si256();
  7016. __m256i sumi2 = _mm256_setzero_si256();
  7017. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7018. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7019. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7020. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7021. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7022. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7023. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7024. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7025. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7026. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7027. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7028. qs += 8;
  7029. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7030. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7031. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7032. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7033. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7034. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7035. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7036. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7037. signs += 4;
  7038. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7039. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7040. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7041. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7042. sumi1 = _mm256_add_epi32(sumi1, p1);
  7043. sumi2 = _mm256_add_epi32(sumi2, p2);
  7044. }
  7045. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7046. }
  7047. *s = 0.125f * hsum_float_8(accumf);
  7048. #else
  7049. float sumf = 0;
  7050. for (int i = 0; i < nb; i++) {
  7051. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7052. const int8_t * q8 = y[i].qs;
  7053. const uint8_t * qs = x[i].qs;
  7054. const uint8_t * qh = x[i].qh;
  7055. const uint8_t * signs = qs + QK_K/8;
  7056. int bsum = 0;
  7057. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7058. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7059. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7060. int sumi1 = 0, sumi2 = 0;
  7061. for (int l = 0; l < 2; ++l) {
  7062. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7063. for (int j = 0; j < 8; ++j) {
  7064. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7065. }
  7066. q8 += 8;
  7067. }
  7068. for (int l = 2; l < 4; ++l) {
  7069. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7070. for (int j = 0; j < 8; ++j) {
  7071. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7072. }
  7073. q8 += 8;
  7074. }
  7075. bsum += ls1 * sumi1 + ls2 * sumi2;
  7076. qs += 4;
  7077. signs += 4;
  7078. }
  7079. sumf += d * bsum;
  7080. }
  7081. *s = 0.125f * sumf;
  7082. #endif
  7083. }
  7084. 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) {
  7085. assert(n % QK_K == 0);
  7086. assert(nrc == 1);
  7087. UNUSED(nrc);
  7088. UNUSED(bx);
  7089. UNUSED(by);
  7090. UNUSED(bs);
  7091. const block_iq3_xxs * restrict x = vx;
  7092. const block_q8_K * restrict y = vy;
  7093. const int nb = n / QK_K;
  7094. #if defined(__ARM_NEON)
  7095. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7096. uint32_t aux32[2];
  7097. ggml_int8x16x4_t q3s;
  7098. ggml_int8x16x4_t q8b;
  7099. float sumf = 0;
  7100. for (int i = 0; i < nb; ++i) {
  7101. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7102. const uint8_t * restrict q3 = x[i].qs;
  7103. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7104. const int8_t * restrict q8 = y[i].qs;
  7105. float sumf1 = 0, sumf2 = 0;
  7106. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7107. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7108. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7109. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7110. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7111. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7112. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7113. q3 += 16;
  7114. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7115. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7116. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7117. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7118. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7119. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7120. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7121. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7122. 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]);
  7123. 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]);
  7124. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7125. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7126. }
  7127. sumf += d*(sumf1 + sumf2);
  7128. }
  7129. *s = 0.5f * sumf;
  7130. #elif defined(__AVX2__)
  7131. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7132. uint32_t aux32[2];
  7133. __m256 accumf = _mm256_setzero_ps();
  7134. for (int i = 0; i < nb; ++i) {
  7135. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7136. const uint8_t * restrict q3 = x[i].qs;
  7137. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7138. const int8_t * restrict q8 = y[i].qs;
  7139. __m256i sumi1 = _mm256_setzero_si256();
  7140. __m256i sumi2 = _mm256_setzero_si256();
  7141. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7142. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7143. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7144. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7145. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7146. q3 += 8;
  7147. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7148. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7149. q3 += 8;
  7150. memcpy(aux32, gas, 8); gas += 8;
  7151. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7152. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7153. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7154. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7155. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7156. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7157. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7158. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7159. const uint16_t ls1 = aux32[0] >> 28;
  7160. const uint16_t ls2 = aux32[1] >> 28;
  7161. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7162. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7163. sumi1 = _mm256_add_epi32(sumi1, p1);
  7164. sumi2 = _mm256_add_epi32(sumi2, p2);
  7165. }
  7166. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7167. }
  7168. *s = 0.25f * hsum_float_8(accumf);
  7169. #else
  7170. uint32_t aux32;
  7171. float sumf = 0.f;
  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 q3 = x[i].qs;
  7175. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7176. const int8_t * restrict q8 = y[i].qs;
  7177. int32_t bsum = 0;
  7178. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7179. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7180. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7181. int32_t sumi = 0;
  7182. for (int l = 0; l < 4; ++l) {
  7183. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7184. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7185. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7186. for (int j = 0; j < 4; ++j) {
  7187. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7188. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7189. }
  7190. q8 += 8;
  7191. }
  7192. q3 += 8;
  7193. bsum += sumi * ls;
  7194. }
  7195. sumf += d * bsum;
  7196. }
  7197. *s = 0.25f * sumf;
  7198. #endif
  7199. }
  7200. 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) {
  7201. assert(n % QK_K == 0);
  7202. assert(nrc == 1);
  7203. UNUSED(nrc);
  7204. UNUSED(bx);
  7205. UNUSED(by);
  7206. UNUSED(bs);
  7207. const block_iq3_s * restrict x = vx;
  7208. const block_q8_K * restrict y = vy;
  7209. const int nb = n / QK_K;
  7210. #if defined(__ARM_NEON)
  7211. typedef union {
  7212. uint16x8_t vec_index;
  7213. uint16_t index[8];
  7214. } vec_index_t;
  7215. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7216. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7217. };
  7218. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7219. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7220. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7221. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7222. const int16x8_t hshift = vld1q_s16(k_shift);
  7223. const uint16x8_t m256 = vdupq_n_u16(256);
  7224. const uint8x16_t m1 = vdupq_n_u8(1);
  7225. uint8x16x2_t vs;
  7226. ggml_int8x16x4_t q3s;
  7227. ggml_int8x16x4_t q8b;
  7228. vec_index_t idx;
  7229. #if QK_K == 256
  7230. uint32_t scales32[2];
  7231. const uint8_t * scales8 = (const uint8_t *)scales32;
  7232. #endif
  7233. float sumf = 0;
  7234. for (int i = 0; i < nb; ++i) {
  7235. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7236. const uint8_t * restrict qs = x[i].qs;
  7237. const uint8_t * restrict qh = x[i].qh;
  7238. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7239. const int8_t * restrict q8 = y[i].qs;
  7240. #if QK_K == 256
  7241. memcpy(scales32, x[i].scales, 4);
  7242. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7243. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7244. #endif
  7245. int sumi1 = 0, sumi2 = 0;
  7246. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7247. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7248. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7249. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7250. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7251. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7252. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7253. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7254. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7255. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7256. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7257. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7258. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7259. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7260. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7261. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7262. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7263. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7264. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7265. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7266. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7267. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7268. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7269. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7270. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7271. signs += 4;
  7272. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7273. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7274. 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]);
  7275. 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]);
  7276. #if QK_K == 256
  7277. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7278. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7279. #else
  7280. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7281. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7282. #endif
  7283. }
  7284. sumf += d*(sumi1 + sumi2);
  7285. }
  7286. *s = sumf;
  7287. #elif defined(__AVX2__)
  7288. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7289. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7290. };
  7291. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7292. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7293. };
  7294. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7295. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7296. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7297. const __m256i idx_mask = _mm256_set1_epi32(256);
  7298. typedef union {
  7299. __m256i vec[2];
  7300. uint32_t index[16];
  7301. } index_t;
  7302. index_t idx;
  7303. __m256 accumf = _mm256_setzero_ps();
  7304. for (int i = 0; i < nb; ++i) {
  7305. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7306. const uint8_t * restrict qs = x[i].qs;
  7307. const uint8_t * restrict qh = x[i].qh;
  7308. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7309. const int8_t * restrict q8 = y[i].qs;
  7310. __m256i sumi1 = _mm256_setzero_si256();
  7311. __m256i sumi2 = _mm256_setzero_si256();
  7312. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7313. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7314. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7315. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7316. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7317. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7318. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7319. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7320. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7321. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7322. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7323. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7324. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7325. const __m256i q2_1 = _mm256_set_epi32(
  7326. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7327. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7328. );
  7329. const __m256i q2_2 = _mm256_set_epi32(
  7330. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7331. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7332. );
  7333. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7334. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7335. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7336. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7337. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7338. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7339. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7340. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7341. signs += 4;
  7342. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7343. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7344. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7345. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7346. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7347. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7348. sumi1 = _mm256_add_epi32(sumi1, p1);
  7349. sumi2 = _mm256_add_epi32(sumi2, p2);
  7350. }
  7351. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7352. }
  7353. *s = hsum_float_8(accumf);
  7354. #else
  7355. float sumf = 0.f;
  7356. for (int i = 0; i < nb; ++i) {
  7357. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7358. const uint8_t * restrict qs = x[i].qs;
  7359. const uint8_t * restrict qh = x[i].qh;
  7360. const uint8_t * restrict signs = x[i].signs;
  7361. const int8_t * restrict q8 = y[i].qs;
  7362. int32_t bsum = 0;
  7363. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7364. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7365. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7366. int32_t sumi = 0;
  7367. for (int l = 0; l < 4; ++l) {
  7368. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7369. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7370. for (int j = 0; j < 4; ++j) {
  7371. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7372. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7373. }
  7374. q8 += 8;
  7375. }
  7376. qs += 8;
  7377. signs += 4;
  7378. bsum += sumi * ls1;
  7379. sumi = 0;
  7380. for (int l = 0; l < 4; ++l) {
  7381. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7382. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7383. for (int j = 0; j < 4; ++j) {
  7384. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7385. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7386. }
  7387. q8 += 8;
  7388. }
  7389. qs += 8;
  7390. signs += 4;
  7391. bsum += sumi * ls2;
  7392. }
  7393. sumf += d * bsum;
  7394. }
  7395. *s = sumf;
  7396. #endif
  7397. }
  7398. #ifdef __AVX2__
  7399. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7400. const __m256i ax = _mm256_sign_epi8(x, x);
  7401. const __m256i sy = _mm256_sign_epi8(y, x);
  7402. return _mm256_maddubs_epi16(ax, sy);
  7403. }
  7404. #endif
  7405. 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) {
  7406. assert(n % QK_K == 0);
  7407. assert(nrc == 1);
  7408. UNUSED(nrc);
  7409. UNUSED(bx);
  7410. UNUSED(by);
  7411. UNUSED(bs);
  7412. const block_iq1_s * restrict x = vx;
  7413. const block_q8_K * restrict y = vy;
  7414. const int nb = n / QK_K;
  7415. #if defined __ARM_NEON
  7416. ggml_int8x16x4_t q1b;
  7417. ggml_int8x16x4_t q8b;
  7418. float sumf = 0;
  7419. for (int i = 0; i < nb; ++i) {
  7420. const int8_t * q8 = y[i].qs;
  7421. const uint8_t * qs = x[i].qs;
  7422. const uint16_t * qh = x[i].qh;
  7423. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7424. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7425. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7426. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7427. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7428. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7429. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7430. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7431. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7432. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7433. qs += 8;
  7434. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7435. 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]);
  7436. 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]);
  7437. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7438. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7439. sumi1 += vaddvq_s32(p1) * ls1;
  7440. sumi2 += vaddvq_s32(p2) * ls2;
  7441. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7442. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7443. }
  7444. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7445. }
  7446. *s = sumf;
  7447. #elif defined __AVX2__
  7448. __m256 accum = _mm256_setzero_ps();
  7449. float accum1 = 0;
  7450. for (int i = 0; i < nb; ++i) {
  7451. const int8_t * q8 = y[i].qs;
  7452. const uint8_t * qs = x[i].qs;
  7453. const uint16_t * qh = x[i].qh;
  7454. __m256i sumi = _mm256_setzero_si256();
  7455. int sumi1 = 0;
  7456. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7457. 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)],
  7458. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7459. 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)],
  7460. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7461. qs += 8;
  7462. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7463. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7464. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7465. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7466. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7467. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7468. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7469. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7470. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7471. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7472. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7473. }
  7474. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7475. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7476. accum1 += d * sumi1;
  7477. }
  7478. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7479. #else
  7480. float sumf = 0;
  7481. for (int i = 0; i < nb; i++) {
  7482. const int8_t * q8 = y[i].qs;
  7483. const uint8_t * qs = x[i].qs;
  7484. const uint16_t * qh = x[i].qh;
  7485. int sumi = 0, sumi1 = 0;
  7486. for (int ib = 0; ib < QK_K/32; ++ib) {
  7487. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7488. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7489. int lsum = 0;
  7490. for (int l = 0; l < 4; ++l) {
  7491. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7492. for (int j = 0; j < 8; ++j) {
  7493. lsum += q8[j] * grid[j];
  7494. }
  7495. q8 += 8;
  7496. }
  7497. sumi += ls * lsum;
  7498. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7499. qs += 4;
  7500. }
  7501. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7502. }
  7503. *s = sumf;
  7504. #endif
  7505. }
  7506. 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) {
  7507. assert(n % QK_K == 0);
  7508. assert(nrc == 1);
  7509. UNUSED(nrc);
  7510. UNUSED(bx);
  7511. UNUSED(by);
  7512. UNUSED(bs);
  7513. const block_iq1_m * restrict x = vx;
  7514. const block_q8_K * restrict y = vy;
  7515. const int nb = n / QK_K;
  7516. #if QK_K != 64
  7517. iq1m_scale_t scale;
  7518. #endif
  7519. #if defined __ARM_NEON
  7520. #if QK_K == 64
  7521. const int32x4_t mask = vdupq_n_s32(0xf);
  7522. #else
  7523. const int32x4_t mask = vdupq_n_s32(0x7);
  7524. #endif
  7525. const int32x4_t mone = vdupq_n_s32(1);
  7526. const int32x4_t mzero = vdupq_n_s32(0);
  7527. ggml_int8x16x4_t deltas;
  7528. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7529. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7530. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7531. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7532. ggml_int8x16x4_t q1b;
  7533. ggml_int8x16x4_t q8b;
  7534. uint32_t aux32;
  7535. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7536. float sumf = 0;
  7537. for (int i = 0; i < nb; ++i) {
  7538. const int8_t * q8 = y[i].qs;
  7539. const uint8_t * qs = x[i].qs;
  7540. const uint8_t * qh = x[i].qh;
  7541. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7542. #if QK_K != 64
  7543. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7544. #endif
  7545. int32x4_t sumi1 = mzero;
  7546. int32x4_t sumi2 = mzero;
  7547. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7548. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7549. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7550. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7551. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7552. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7553. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7554. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7555. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7556. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7557. 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]));
  7558. 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]));
  7559. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7560. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7561. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7562. 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]));
  7563. 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]));
  7564. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7565. #if QK_K == 64
  7566. int32x4_t scales_4 = ggml_vld1q_u32(sc[0] >> 0, sc[0] >> 4, sc[0] >> 8, sc[0] >> 12);
  7567. #else
  7568. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7569. #endif
  7570. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7571. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7572. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7573. qs += 8; qh += 4;
  7574. }
  7575. #if QK_K == 64
  7576. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7577. #else
  7578. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7579. #endif
  7580. }
  7581. *s = sumf;
  7582. #elif defined __AVX2__
  7583. #if QK_K == 64
  7584. const __m256i mask = _mm256_set1_epi16(0xf);
  7585. #else
  7586. const __m256i mask = _mm256_set1_epi16(0x7);
  7587. #endif
  7588. const __m256i mone = _mm256_set1_epi16(1);
  7589. __m256 accum1 = _mm256_setzero_ps();
  7590. __m256 accum2 = _mm256_setzero_ps();
  7591. for (int i = 0; i < nb; ++i) {
  7592. const int8_t * q8 = y[i].qs;
  7593. const uint8_t * qs = x[i].qs;
  7594. const uint8_t * qh = x[i].qh;
  7595. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7596. #if QK_K != 64
  7597. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7598. #endif
  7599. __m256i sumi1 = _mm256_setzero_si256();
  7600. __m256i sumi2 = _mm256_setzero_si256();
  7601. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7602. const __m256i q1b_1 = _mm256_set_epi64x(
  7603. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7604. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7605. );
  7606. const __m256i q1b_2 = _mm256_set_epi64x(
  7607. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7608. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7609. );
  7610. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7611. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7612. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7613. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7614. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7615. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7616. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7617. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7618. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7619. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7620. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7621. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7622. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7623. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7624. #if QK_K == 64
  7625. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 4), _mm_set1_epi16(sc[0] >> 0));
  7626. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 12), _mm_set1_epi16(sc[0] >> 8));
  7627. #else
  7628. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7629. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7630. #endif
  7631. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7632. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7633. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7634. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7635. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7636. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7637. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7638. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7639. qs += 8; qh += 4;
  7640. }
  7641. #if QK_K == 64
  7642. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d));
  7643. #else
  7644. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7645. #endif
  7646. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7647. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7648. }
  7649. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7650. #else
  7651. int sum1[2], sum2[2], delta[4];
  7652. float sumf = 0;
  7653. for (int i = 0; i < nb; i++) {
  7654. const int8_t * q8 = y[i].qs;
  7655. const uint8_t * qs = x[i].qs;
  7656. const uint8_t * qh = x[i].qh;
  7657. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7658. #if QK_K != 64
  7659. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7660. #endif
  7661. int sumi1 = 0, sumi2 = 0;
  7662. for (int ib = 0; ib < QK_K/32; ++ib) {
  7663. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7664. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7665. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7666. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7667. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7668. for (int l = 0; l < 4; ++l) {
  7669. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7670. int lsum1 = 0, lsum2 = 0;
  7671. for (int j = 0; j < 8; ++j) {
  7672. lsum1 += q8[j] * grid[j];
  7673. lsum2 += q8[j];
  7674. }
  7675. q8 += 8;
  7676. sum1[l/2] += lsum1;
  7677. sum2[l/2] += lsum2*delta[l];
  7678. }
  7679. #if QK_K == 64
  7680. const int ls1 = 2*((sc[0] >> (8*(ib%2)+0)) & 0xf) + 1;
  7681. const int ls2 = 2*((sc[0] >> (8*(ib%2)+4)) & 0xf) + 1;
  7682. #else
  7683. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7684. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7685. #endif
  7686. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7687. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7688. qs += 4;
  7689. qh += 2;
  7690. }
  7691. #if QK_K == 64
  7692. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7693. #else
  7694. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7695. #endif
  7696. }
  7697. *s = sumf;
  7698. #endif
  7699. }
  7700. 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) {
  7701. assert(nrc == 1);
  7702. UNUSED(nrc);
  7703. UNUSED(bx);
  7704. UNUSED(by);
  7705. UNUSED(bs);
  7706. assert(n % QK4_NL == 0);
  7707. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7708. const block_iq4_nl * restrict x = vx;
  7709. const block_q8_0 * restrict y = vy;
  7710. const int nb = n / QK4_NL;
  7711. #if defined __ARM_NEON
  7712. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7713. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7714. uint8x16x2_t q4bits;
  7715. int8x16x4_t q4b;
  7716. int8x16x4_t q8b;
  7717. int32x4_t prod_1, prod_2;
  7718. float sumf = 0;
  7719. for (int ib = 0; ib < nb; ib += 2) {
  7720. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7721. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7722. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7723. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7724. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7725. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7726. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7727. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7728. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7729. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7730. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7731. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7732. sumf +=
  7733. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7734. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7735. }
  7736. *s = sumf;
  7737. #elif defined __AVX2__
  7738. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7739. const __m128i m4b = _mm_set1_epi8(0x0f);
  7740. const __m256i mone = _mm256_set1_epi16(1);
  7741. __m256 accum1 = _mm256_setzero_ps();
  7742. __m256 accum2 = _mm256_setzero_ps();
  7743. for (int ib = 0; ib < nb; ib += 2) {
  7744. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7745. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7746. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7747. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7748. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7749. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7750. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7751. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7752. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7753. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7754. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7755. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7756. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7757. _mm256_cvtepi32_ps(p_1), accum1);
  7758. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7759. _mm256_cvtepi32_ps(p_2), accum2);
  7760. y += 2;
  7761. x += 2;
  7762. }
  7763. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7764. #else
  7765. float sumf = 0;
  7766. for (int ib = 0; ib < nb; ++ib) {
  7767. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7768. int sumi1 = 0, sumi2 = 0;
  7769. for (int j = 0; j < QK4_NL/2; ++j) {
  7770. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7771. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7772. }
  7773. sumf += d * (sumi1 + sumi2);
  7774. }
  7775. *s = sumf;
  7776. #endif
  7777. }
  7778. 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) {
  7779. assert(nrc == 1);
  7780. UNUSED(nrc);
  7781. UNUSED(bx);
  7782. UNUSED(by);
  7783. UNUSED(bs);
  7784. assert(n % QK_K == 0);
  7785. #if QK_K == 64
  7786. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7787. #else
  7788. const block_iq4_xs * restrict x = vx;
  7789. const block_q8_K * restrict y = vy;
  7790. const int nb = n / QK_K;
  7791. #if defined __ARM_NEON
  7792. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7793. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7794. ggml_uint8x16x2_t q4bits;
  7795. ggml_int8x16x4_t q4b;
  7796. ggml_int8x16x4_t q8b;
  7797. int32x4_t prod_1, prod_2;
  7798. float sumf = 0;
  7799. for (int ibl = 0; ibl < nb; ++ibl) {
  7800. const int8_t * q8 = y[ibl].qs;
  7801. const uint8_t * q4 = x[ibl].qs;
  7802. uint16_t h = x[ibl].scales_h;
  7803. int sumi1 = 0, sumi2 = 0;
  7804. for (int ib = 0; ib < QK_K/64; ++ib) {
  7805. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7806. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7807. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7808. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7809. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7810. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7811. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7812. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7813. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7814. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7815. h >>= 4;
  7816. sumi1 += vaddvq_s32(prod_1) * ls1;
  7817. sumi2 += vaddvq_s32(prod_2) * ls2;
  7818. }
  7819. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7820. }
  7821. *s = sumf;
  7822. #elif defined __AVX2__
  7823. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7824. const __m128i m4b = _mm_set1_epi8(0x0f);
  7825. __m256 accum = _mm256_setzero_ps();
  7826. for (int ibl = 0; ibl < nb; ++ibl) {
  7827. const uint8_t * qs = x[ibl].qs;
  7828. const int8_t * q8 = y[ibl].qs;
  7829. uint16_t sh = x[ibl].scales_h;
  7830. __m256i sumi1 = _mm256_setzero_si256();
  7831. __m256i sumi2 = _mm256_setzero_si256();
  7832. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7833. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7834. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7835. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7836. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7837. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7838. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7839. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7840. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7841. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7842. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7843. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7844. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7845. sh >>= 4;
  7846. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7847. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7848. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7849. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7850. }
  7851. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7852. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7853. }
  7854. *s = hsum_float_8(accum);
  7855. #else
  7856. float sumf = 0;
  7857. for (int ibl = 0; ibl < nb; ++ibl) {
  7858. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7859. uint16_t h = x[ibl].scales_h;
  7860. const uint8_t * qs = x[ibl].qs;
  7861. const int8_t * q8 = y[ibl].qs;
  7862. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7863. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7864. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7865. h >>= 4;
  7866. const float d1 = d4d8*(ls1 - 32);
  7867. const float d2 = d4d8*(ls2 - 32);
  7868. int sumi1 = 0, sumi2 = 0;
  7869. for (int j = 0; j < 16; ++j) {
  7870. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7871. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7872. }
  7873. sumf += d1 * (sumi1 + sumi2);
  7874. qs += 16;
  7875. q8 += 32;
  7876. sumi1 = sumi2 = 0;
  7877. for (int j = 0; j < 16; ++j) {
  7878. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7879. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7880. }
  7881. sumf += d2 * (sumi1 + sumi2);
  7882. qs += 16;
  7883. q8 += 32;
  7884. }
  7885. }
  7886. *s = sumf;
  7887. #endif
  7888. #endif
  7889. }
  7890. // ================================ IQ2 quantization =============================================
  7891. typedef struct {
  7892. uint64_t * grid;
  7893. int * map;
  7894. uint16_t * neighbours;
  7895. } iq2_entry_t;
  7896. static iq2_entry_t iq2_data[4] = {
  7897. {NULL, NULL, NULL},
  7898. {NULL, NULL, NULL},
  7899. {NULL, NULL, NULL},
  7900. {NULL, NULL, NULL},
  7901. };
  7902. static inline int iq2_data_index(enum ggml_type type) {
  7903. 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);
  7904. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7905. type == GGML_TYPE_IQ2_XS ? 1 :
  7906. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  7907. }
  7908. static inline int iq2_grid_size(enum ggml_type type) {
  7909. 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);
  7910. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7911. type == GGML_TYPE_IQ2_XS ? 512 :
  7912. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  7913. }
  7914. static int iq2_compare_func(const void * left, const void * right) {
  7915. const int * l = (const int *)left;
  7916. const int * r = (const int *)right;
  7917. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7918. }
  7919. void iq2xs_init_impl(enum ggml_type type) {
  7920. const int gindex = iq2_data_index(type);
  7921. const int grid_size = iq2_grid_size(type);
  7922. if (iq2_data[gindex].grid) {
  7923. return;
  7924. }
  7925. static const uint16_t kgrid_2bit_256[256] = {
  7926. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7927. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7928. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7929. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7930. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7931. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7932. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7933. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7934. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7935. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7936. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7937. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7938. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7939. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7940. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7941. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7942. };
  7943. static const uint16_t kgrid_2bit_512[512] = {
  7944. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7945. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7946. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7947. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7948. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7949. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7950. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7951. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7952. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7953. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7954. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7955. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7956. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7957. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7958. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7959. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7960. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7961. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7962. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7963. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7964. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7965. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7966. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7967. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7968. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7969. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7970. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7971. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7972. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7973. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7974. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7975. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7976. };
  7977. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7978. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7979. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7980. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7981. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7982. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7983. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7984. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7985. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7986. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7987. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7988. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7989. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7990. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7991. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7992. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7993. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7994. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  7995. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  7996. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  7997. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  7998. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  7999. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8000. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8001. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8002. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8003. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8004. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8005. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8006. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8007. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8008. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8009. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8010. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8011. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8012. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8013. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8014. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8015. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8016. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8017. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8018. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8019. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8020. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8021. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8022. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8023. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8024. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8025. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8026. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8027. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8028. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8029. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8030. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8031. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8032. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8033. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8034. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8035. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8036. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8037. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8038. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8039. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8040. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8041. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8042. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8043. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8044. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8045. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8046. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8047. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8048. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8049. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8050. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8051. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8052. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8053. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8054. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8055. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8056. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8057. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8058. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8059. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8060. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8061. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8062. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8063. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8064. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8065. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8066. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8067. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8068. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8069. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8070. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8071. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8072. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8073. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8074. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8075. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8076. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8077. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8078. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8079. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8080. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8081. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8082. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8083. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8084. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8085. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8086. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8087. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8088. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8089. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8090. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8091. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8092. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8093. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8094. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8095. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8096. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8097. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8098. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8099. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8100. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8101. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8102. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8103. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8104. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8105. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8106. };
  8107. static const uint16_t kgrid_2bit_1024[1024] = {
  8108. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8109. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8110. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8111. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8112. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8113. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8114. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8115. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8116. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8117. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8118. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8119. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8120. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8121. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8122. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8123. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8124. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8125. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8126. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8127. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8128. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8129. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8130. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8131. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8132. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8133. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8134. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8135. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8136. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8137. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8138. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8139. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8140. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8141. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8142. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8143. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8144. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8145. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8146. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8147. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8148. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8149. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8150. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8151. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8152. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8153. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8154. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8155. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8156. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8157. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8158. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8159. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8160. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8161. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8162. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8163. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8164. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8165. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8166. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8167. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8168. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8169. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8170. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8171. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8172. };
  8173. const int kmap_size = 43692;
  8174. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8175. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8176. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8177. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8178. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8179. uint64_t * kgrid_q2xs;
  8180. int * kmap_q2xs;
  8181. uint16_t * kneighbors_q2xs;
  8182. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8183. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8184. for (int k = 0; k < grid_size; ++k) {
  8185. int8_t * pos = (int8_t *)(the_grid + k);
  8186. for (int i = 0; i < 8; ++i) {
  8187. int l = (kgrid[k] >> 2*i) & 0x3;
  8188. pos[i] = 2*l + 1;
  8189. }
  8190. }
  8191. kgrid_q2xs = the_grid;
  8192. iq2_data[gindex].grid = the_grid;
  8193. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8194. iq2_data[gindex].map = kmap_q2xs;
  8195. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8196. uint64_t aux64;
  8197. uint8_t * aux8 = (uint8_t *)&aux64;
  8198. for (int i = 0; i < grid_size; ++i) {
  8199. aux64 = kgrid_q2xs[i];
  8200. uint16_t index = 0;
  8201. for (int k=0; k<8; ++k) {
  8202. uint16_t q = (aux8[k] - 1)/2;
  8203. index |= (q << 2*k);
  8204. }
  8205. kmap_q2xs[index] = i;
  8206. }
  8207. int8_t pos[8];
  8208. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8209. int num_neighbors = 0, num_not_in_map = 0;
  8210. for (int i = 0; i < kmap_size; ++i) {
  8211. if (kmap_q2xs[i] >= 0) continue;
  8212. ++num_not_in_map;
  8213. for (int k = 0; k < 8; ++k) {
  8214. int l = (i >> 2*k) & 0x3;
  8215. pos[k] = 2*l + 1;
  8216. }
  8217. for (int j = 0; j < grid_size; ++j) {
  8218. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8219. int d2 = 0;
  8220. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8221. dist2[2*j+0] = d2;
  8222. dist2[2*j+1] = j;
  8223. }
  8224. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8225. int n = 0; int d2 = dist2[0];
  8226. int nhave = 1;
  8227. for (int j = 0; j < grid_size; ++j) {
  8228. if (dist2[2*j] > d2) {
  8229. if (nhave == nwant) break;
  8230. d2 = dist2[2*j];
  8231. ++nhave;
  8232. }
  8233. ++n;
  8234. }
  8235. num_neighbors += n;
  8236. }
  8237. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8238. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8239. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8240. int counter = 0;
  8241. for (int i = 0; i < kmap_size; ++i) {
  8242. if (kmap_q2xs[i] >= 0) continue;
  8243. for (int k = 0; k < 8; ++k) {
  8244. int l = (i >> 2*k) & 0x3;
  8245. pos[k] = 2*l + 1;
  8246. }
  8247. for (int j = 0; j < grid_size; ++j) {
  8248. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8249. int d2 = 0;
  8250. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8251. dist2[2*j+0] = d2;
  8252. dist2[2*j+1] = j;
  8253. }
  8254. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8255. kmap_q2xs[i] = -(counter + 1);
  8256. int d2 = dist2[0];
  8257. uint16_t * start = &kneighbors_q2xs[counter++];
  8258. int n = 0, nhave = 1;
  8259. for (int j = 0; j < grid_size; ++j) {
  8260. if (dist2[2*j] > d2) {
  8261. if (nhave == nwant) break;
  8262. d2 = dist2[2*j];
  8263. ++nhave;
  8264. }
  8265. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8266. ++n;
  8267. }
  8268. *start = n;
  8269. }
  8270. free(dist2);
  8271. }
  8272. void iq2xs_free_impl(enum ggml_type type) {
  8273. 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);
  8274. const int gindex = iq2_data_index(type);
  8275. if (iq2_data[gindex].grid) {
  8276. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8277. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8278. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8279. }
  8280. }
  8281. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8282. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8283. int num_neighbors = neighbours[0];
  8284. GGML_ASSERT(num_neighbors > 0);
  8285. float best_d2 = FLT_MAX;
  8286. int grid_index = -1;
  8287. for (int j = 1; j <= num_neighbors; ++j) {
  8288. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8289. float d2 = 0;
  8290. for (int i = 0; i < 8; ++i) {
  8291. float q = pg[i];
  8292. float diff = scale*q - xval[i];
  8293. d2 += weight[i]*diff*diff;
  8294. }
  8295. if (d2 < best_d2) {
  8296. best_d2 = d2; grid_index = neighbours[j];
  8297. }
  8298. }
  8299. GGML_ASSERT(grid_index >= 0);
  8300. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8301. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8302. return grid_index;
  8303. }
  8304. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8305. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8306. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8307. const int * kmap_q2xs = iq2_data[gindex].map;
  8308. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8309. GGML_ASSERT(quant_weights && "missing quantization weights");
  8310. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8311. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8312. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8313. GGML_ASSERT(n%QK_K == 0);
  8314. const int kMaxQ = 3;
  8315. const int64_t nbl = n/QK_K;
  8316. block_iq2_xxs * y = vy;
  8317. float scales[QK_K/32];
  8318. float weight[32];
  8319. float xval[32];
  8320. int8_t L[32];
  8321. int8_t Laux[32];
  8322. float waux[32];
  8323. uint8_t block_signs[4];
  8324. uint32_t q2[2*(QK_K/32)];
  8325. for (int ibl = 0; ibl < nbl; ++ibl) {
  8326. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8327. memset(q2, 0, QK_K/4);
  8328. float max_scale = 0;
  8329. const float * xbl = x + QK_K*ibl;
  8330. float sumx2 = 0;
  8331. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8332. float sigma2 = sumx2/QK_K;
  8333. for (int ib = 0; ib < QK_K/32; ++ib) {
  8334. const float * xb = xbl + 32*ib;
  8335. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8336. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8337. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8338. for (int k = 0; k < 4; ++k) {
  8339. int nflip = 0;
  8340. uint8_t s = 0;
  8341. for (int i = 0; i < 8; ++i) {
  8342. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8343. else {
  8344. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8345. }
  8346. }
  8347. if (nflip%2) {
  8348. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8349. for (int i = 1; i < 8; ++i) {
  8350. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8351. if (ax < min) {
  8352. min = ax; imin = i;
  8353. }
  8354. }
  8355. xval[8*k+imin] = -xval[8*k+imin];
  8356. s ^= (1 << imin);
  8357. }
  8358. block_signs[k] = s & 127;
  8359. }
  8360. float max = xval[0];
  8361. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8362. if (!max) {
  8363. scales[ib] = 0;
  8364. memset(L, 0, 32);
  8365. continue;
  8366. }
  8367. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8368. float eff_max = scale*kMaxQ;
  8369. float best = 0;
  8370. for (int is = -6; is <= 6; ++is) {
  8371. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8372. float this_scale = 1/id;
  8373. for (int k = 0; k < 4; ++k) {
  8374. for (int i = 0; i < 8; ++i) {
  8375. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8376. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8377. }
  8378. uint16_t u = 0;
  8379. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8380. int grid_index = kmap_q2xs[u];
  8381. if (grid_index < 0) {
  8382. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8383. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8384. }
  8385. }
  8386. float sumqx = 0, sumq2 = 0;
  8387. for (int i = 0; i < 32; ++i) {
  8388. float w = weight[i];
  8389. float q = 2*Laux[i] + 1;
  8390. sumqx += w*xval[i]*q;
  8391. sumq2 += w*q*q;
  8392. }
  8393. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8394. scale = sumqx/sumq2; best = scale*sumqx;
  8395. memcpy(L, Laux, 32);
  8396. }
  8397. }
  8398. if (scale > 0) {
  8399. float id = 1/scale;
  8400. for (int k = 0; k < 4; ++k) {
  8401. uint16_t u = 0;
  8402. for (int i = 0; i < 8; ++i) {
  8403. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8404. l = MAX(0, MIN(kMaxQ-1, l));
  8405. u |= (l << 2*i);
  8406. }
  8407. int grid_index = kmap_q2xs[u];
  8408. if (grid_index < 0) {
  8409. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8410. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8411. }
  8412. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8413. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8414. }
  8415. float sumqx = 0, sumq2 = 0;
  8416. for (int i = 0; i < 32; ++i) {
  8417. float w = weight[i];
  8418. float q = 2*L[i] + 1;
  8419. sumqx += w*xval[i]*q;
  8420. sumq2 += w*q*q;
  8421. }
  8422. if (sumq2 > 0) scale = sumqx/sumq2;
  8423. }
  8424. if (scale < 0) {
  8425. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8426. // and correspondingly flip quant signs.
  8427. scale = -scale;
  8428. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8429. }
  8430. for (int k = 0; k < 4; ++k) {
  8431. uint16_t u = 0;
  8432. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8433. int grid_index = kmap_q2xs[u];
  8434. if (grid_index < 0) {
  8435. printf("Oops: found point %u not on grid:", u);
  8436. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8437. printf("\n");
  8438. GGML_ASSERT(false);
  8439. }
  8440. q2[2*ib+0] |= (grid_index << 8*k);
  8441. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8442. }
  8443. GGML_ASSERT(scale >= 0);
  8444. scales[ib] = scale;
  8445. max_scale = MAX(max_scale, scale);
  8446. }
  8447. if (!max_scale) {
  8448. memset(y[ibl].qs, 0, QK_K/4);
  8449. continue;
  8450. }
  8451. float d = max_scale/31;
  8452. y[ibl].d = GGML_FP32_TO_FP16(d);
  8453. float id = 1/d;
  8454. for (int ib = 0; ib < QK_K/32; ++ib) {
  8455. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8456. l = MAX(0, MIN(15, l));
  8457. q2[2*ib+1] |= ((uint32_t)l << 28);
  8458. }
  8459. memcpy(y[ibl].qs, q2, QK_K/4);
  8460. }
  8461. }
  8462. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8463. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8464. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8465. const int * kmap_q2xs = iq2_data[gindex].map;
  8466. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8467. GGML_ASSERT(quant_weights && "missing quantization weights");
  8468. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8469. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8470. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8471. GGML_ASSERT(n%QK_K == 0);
  8472. const int kMaxQ = 3;
  8473. const int64_t nbl = n/QK_K;
  8474. block_iq2_xs * y = vy;
  8475. float scales[QK_K/16];
  8476. float weight[16];
  8477. float xval[16];
  8478. int8_t L[16];
  8479. int8_t Laux[16];
  8480. float waux[16];
  8481. bool is_on_grid[2];
  8482. bool is_on_grid_aux[2];
  8483. uint8_t block_signs[2];
  8484. uint16_t q2[2*(QK_K/16)];
  8485. for (int ibl = 0; ibl < nbl; ++ibl) {
  8486. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8487. memset(q2, 0, QK_K/4);
  8488. memset(y[ibl].scales, 0, QK_K/32);
  8489. float max_scale = 0;
  8490. const float * xbl = x + QK_K*ibl;
  8491. float sumx2 = 0;
  8492. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8493. float sigma2 = sumx2/QK_K;
  8494. for (int ib = 0; ib < QK_K/16; ++ib) {
  8495. const float * xb = xbl + 16*ib;
  8496. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8497. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8498. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8499. for (int k = 0; k < 2; ++k) {
  8500. int nflip = 0;
  8501. uint8_t s = 0;
  8502. for (int i = 0; i < 8; ++i) {
  8503. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8504. else {
  8505. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8506. }
  8507. }
  8508. if (nflip%2) {
  8509. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8510. for (int i = 1; i < 8; ++i) {
  8511. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8512. if (ax < min) {
  8513. min = ax; imin = i;
  8514. }
  8515. }
  8516. xval[8*k+imin] = -xval[8*k+imin];
  8517. s ^= (1 << imin);
  8518. }
  8519. block_signs[k] = s & 127;
  8520. }
  8521. float max = xval[0];
  8522. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8523. if (!max) {
  8524. scales[ib] = 0;
  8525. memset(L, 0, 16);
  8526. continue;
  8527. }
  8528. float best = 0;
  8529. float scale = max/(2*kMaxQ-1);
  8530. is_on_grid[0] = is_on_grid[1] = true;
  8531. for (int is = -9; is <= 9; ++is) {
  8532. float id = (2*kMaxQ-1+is*0.1f)/max;
  8533. float this_scale = 1/id;
  8534. for (int k = 0; k < 2; ++k) {
  8535. for (int i = 0; i < 8; ++i) {
  8536. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8537. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8538. }
  8539. uint16_t u = 0;
  8540. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8541. int grid_index = kmap_q2xs[u];
  8542. is_on_grid_aux[k] = true;
  8543. if (grid_index < 0) {
  8544. is_on_grid_aux[k] = false;
  8545. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8546. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8547. }
  8548. }
  8549. float sumqx = 0, sumq2 = 0;
  8550. for (int i = 0; i < 16; ++i) {
  8551. float w = weight[i];
  8552. float q = 2*Laux[i] + 1;
  8553. sumqx += w*xval[i]*q;
  8554. sumq2 += w*q*q;
  8555. }
  8556. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8557. scale = sumqx/sumq2; best = scale*sumqx;
  8558. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8559. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8560. }
  8561. }
  8562. int n_not_ongrid = 0;
  8563. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8564. if (n_not_ongrid > 0 && scale > 0) {
  8565. float id = 1/scale;
  8566. for (int k = 0; k < 2; ++k) {
  8567. if (is_on_grid[k]) continue;
  8568. uint16_t u = 0;
  8569. for (int i = 0; i < 8; ++i) {
  8570. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8571. l = MAX(0, MIN(kMaxQ-1, l));
  8572. u |= (l << 2*i);
  8573. L[8*k + i] = l;
  8574. }
  8575. int grid_index = kmap_q2xs[u];
  8576. if (grid_index < 0) {
  8577. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8578. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8579. }
  8580. }
  8581. float sumqx = 0, sumq2 = 0;
  8582. for (int i = 0; i < 16; ++i) {
  8583. float w = weight[i];
  8584. float q = 2*L[i] + 1;
  8585. sumqx += w*xval[i]*q;
  8586. sumq2 += w*q*q;
  8587. }
  8588. if (sumq2 > 0) scale = sumqx/sumq2;
  8589. }
  8590. if (scale < 0) {
  8591. scale = -scale;
  8592. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8593. }
  8594. for (int k = 0; k < 2; ++k) {
  8595. uint16_t u = 0;
  8596. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8597. int grid_index = kmap_q2xs[u];
  8598. if (grid_index < 0) {
  8599. printf("Oops: found point %u not on grid:", u);
  8600. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8601. printf("\n");
  8602. GGML_ASSERT(false);
  8603. }
  8604. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8605. }
  8606. GGML_ASSERT(scale >= 0);
  8607. scales[ib] = scale;
  8608. max_scale = MAX(max_scale, scale);
  8609. }
  8610. if (!max_scale) {
  8611. memset(y[ibl].qs, 0, QK_K/4);
  8612. continue;
  8613. }
  8614. float d = max_scale/31;
  8615. y[ibl].d = GGML_FP32_TO_FP16(d);
  8616. float id = 1/d;
  8617. for (int ib = 0; ib < QK_K/16; ++ib) {
  8618. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8619. l = MAX(0, MIN(15, l));
  8620. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8621. else y[ibl].scales[ib/2] |= (l << 4);
  8622. }
  8623. memcpy(y[ibl].qs, q2, QK_K/4);
  8624. }
  8625. }
  8626. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8627. GGML_ASSERT(n_per_row%QK_K == 0);
  8628. int64_t nblock = n_per_row/QK_K;
  8629. char * qrow = (char *)dst;
  8630. for (int64_t row = 0; row < nrow; ++row) {
  8631. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8632. src += n_per_row;
  8633. qrow += nblock*sizeof(block_iq2_xxs);
  8634. }
  8635. return nrow * nblock * sizeof(block_iq2_xxs);
  8636. }
  8637. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8638. GGML_ASSERT(n_per_row%QK_K == 0);
  8639. int64_t nblock = n_per_row/QK_K;
  8640. char * qrow = (char *)dst;
  8641. for (int64_t row = 0; row < nrow; ++row) {
  8642. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8643. src += n_per_row;
  8644. qrow += nblock*sizeof(block_iq2_xs);
  8645. }
  8646. return nrow * nblock * sizeof(block_iq2_xs);
  8647. }
  8648. //
  8649. // ============================================= 3-bit using D4 lattice
  8650. //
  8651. typedef struct {
  8652. uint32_t * grid;
  8653. int * map;
  8654. uint16_t * neighbours;
  8655. } iq3_entry_t;
  8656. static iq3_entry_t iq3_data[2] = {
  8657. {NULL, NULL, NULL},
  8658. {NULL, NULL, NULL},
  8659. };
  8660. static inline int iq3_data_index(int grid_size) {
  8661. (void)grid_size;
  8662. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8663. return grid_size == 256 ? 0 : 1;
  8664. }
  8665. static int iq3_compare_func(const void * left, const void * right) {
  8666. const int * l = (const int *)left;
  8667. const int * r = (const int *)right;
  8668. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8669. }
  8670. void iq3xs_init_impl(int grid_size) {
  8671. const int gindex = iq3_data_index(grid_size);
  8672. if (iq3_data[gindex].grid) {
  8673. return;
  8674. }
  8675. static const uint16_t kgrid_256[256] = {
  8676. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8677. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8678. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8679. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8680. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8681. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8682. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8683. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8684. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8685. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8686. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8687. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8688. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8689. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8690. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8691. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8692. };
  8693. static const uint16_t kgrid_512[512] = {
  8694. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8695. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8696. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8697. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8698. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8699. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8700. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8701. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8702. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8703. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8704. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8705. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8706. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8707. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8708. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8709. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8710. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8711. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8712. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8713. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8714. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8715. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8716. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8717. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8718. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8719. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8720. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8721. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8722. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8723. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8724. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8725. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8726. };
  8727. const int kmap_size = 4096;
  8728. const int nwant = grid_size == 256 ? 2 : 3;
  8729. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8730. uint32_t * kgrid_q3xs;
  8731. int * kmap_q3xs;
  8732. uint16_t * kneighbors_q3xs;
  8733. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8734. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8735. for (int k = 0; k < grid_size; ++k) {
  8736. int8_t * pos = (int8_t *)(the_grid + k);
  8737. for (int i = 0; i < 4; ++i) {
  8738. int l = (kgrid[k] >> 3*i) & 0x7;
  8739. pos[i] = 2*l + 1;
  8740. }
  8741. }
  8742. kgrid_q3xs = the_grid;
  8743. iq3_data[gindex].grid = the_grid;
  8744. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8745. iq3_data[gindex].map = kmap_q3xs;
  8746. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8747. uint32_t aux32;
  8748. uint8_t * aux8 = (uint8_t *)&aux32;
  8749. for (int i = 0; i < grid_size; ++i) {
  8750. aux32 = kgrid_q3xs[i];
  8751. uint16_t index = 0;
  8752. for (int k=0; k<4; ++k) {
  8753. uint16_t q = (aux8[k] - 1)/2;
  8754. index |= (q << 3*k);
  8755. }
  8756. kmap_q3xs[index] = i;
  8757. }
  8758. int8_t pos[4];
  8759. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8760. int num_neighbors = 0, num_not_in_map = 0;
  8761. for (int i = 0; i < kmap_size; ++i) {
  8762. if (kmap_q3xs[i] >= 0) continue;
  8763. ++num_not_in_map;
  8764. for (int k = 0; k < 4; ++k) {
  8765. int l = (i >> 3*k) & 0x7;
  8766. pos[k] = 2*l + 1;
  8767. }
  8768. for (int j = 0; j < grid_size; ++j) {
  8769. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8770. int d2 = 0;
  8771. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8772. dist2[2*j+0] = d2;
  8773. dist2[2*j+1] = j;
  8774. }
  8775. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8776. int n = 0; int d2 = dist2[0];
  8777. int nhave = 1;
  8778. for (int j = 0; j < grid_size; ++j) {
  8779. if (dist2[2*j] > d2) {
  8780. if (nhave == nwant) break;
  8781. d2 = dist2[2*j];
  8782. ++nhave;
  8783. }
  8784. ++n;
  8785. }
  8786. num_neighbors += n;
  8787. }
  8788. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8789. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8790. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8791. int counter = 0;
  8792. for (int i = 0; i < kmap_size; ++i) {
  8793. if (kmap_q3xs[i] >= 0) continue;
  8794. for (int k = 0; k < 4; ++k) {
  8795. int l = (i >> 3*k) & 0x7;
  8796. pos[k] = 2*l + 1;
  8797. }
  8798. for (int j = 0; j < grid_size; ++j) {
  8799. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8800. int d2 = 0;
  8801. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8802. dist2[2*j+0] = d2;
  8803. dist2[2*j+1] = j;
  8804. }
  8805. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8806. kmap_q3xs[i] = -(counter + 1);
  8807. int d2 = dist2[0];
  8808. uint16_t * start = &kneighbors_q3xs[counter++];
  8809. int n = 0, nhave = 1;
  8810. for (int j = 0; j < grid_size; ++j) {
  8811. if (dist2[2*j] > d2) {
  8812. if (nhave == nwant) break;
  8813. d2 = dist2[2*j];
  8814. ++nhave;
  8815. }
  8816. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8817. ++n;
  8818. }
  8819. *start = n;
  8820. }
  8821. free(dist2);
  8822. }
  8823. void iq3xs_free_impl(int grid_size) {
  8824. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8825. const int gindex = iq3_data_index(grid_size);
  8826. if (iq3_data[gindex].grid) {
  8827. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8828. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8829. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8830. }
  8831. }
  8832. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8833. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8834. int num_neighbors = neighbours[0];
  8835. GGML_ASSERT(num_neighbors > 0);
  8836. float best_d2 = FLT_MAX;
  8837. int grid_index = -1;
  8838. for (int j = 1; j <= num_neighbors; ++j) {
  8839. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8840. float d2 = 0;
  8841. for (int i = 0; i < 4; ++i) {
  8842. float q = pg[i];
  8843. float diff = scale*q - xval[i];
  8844. d2 += weight[i]*diff*diff;
  8845. }
  8846. if (d2 < best_d2) {
  8847. best_d2 = d2; grid_index = neighbours[j];
  8848. }
  8849. }
  8850. GGML_ASSERT(grid_index >= 0);
  8851. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8852. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8853. return grid_index;
  8854. }
  8855. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int64_t n,
  8856. const float * restrict quant_weights) {
  8857. const int gindex = iq3_data_index(grid_size);
  8858. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8859. const int * kmap_q3xs = iq3_data[gindex].map;
  8860. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8861. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8862. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8863. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8864. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8865. GGML_ASSERT(n%QK_K == 0);
  8866. const int kMaxQ = 8;
  8867. const int64_t nbl = n/QK_K;
  8868. ggml_fp16_t * dh;
  8869. uint8_t * qs;
  8870. int block_size;
  8871. if (grid_size == 256) {
  8872. block_iq3_xxs * y = vy;
  8873. dh = &y->d;
  8874. qs = y->qs;
  8875. block_size = sizeof(block_iq3_xxs);
  8876. } else {
  8877. block_iq3_s * y = vy;
  8878. dh = &y->d;
  8879. qs = y->qs;
  8880. block_size = sizeof(block_iq3_s);
  8881. }
  8882. int quant_size = block_size - sizeof(ggml_fp16_t);
  8883. float scales[QK_K/32];
  8884. float weight[32];
  8885. float xval[32];
  8886. int8_t L[32];
  8887. int8_t Laux[32];
  8888. float waux[32];
  8889. bool is_on_grid[8];
  8890. bool is_on_grid_aux[8];
  8891. uint8_t block_signs[8];
  8892. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8893. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8894. uint8_t * qh = q3 + 3*(QK_K/8);
  8895. for (int ibl = 0; ibl < nbl; ++ibl) {
  8896. dh[0] = GGML_FP32_TO_FP16(0.f);
  8897. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8898. float max_scale = 0;
  8899. const float * xbl = x + QK_K*ibl;
  8900. float sumx2 = 0;
  8901. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8902. float sigma2 = 2*sumx2/QK_K;
  8903. for (int ib = 0; ib < QK_K/32; ++ib) {
  8904. const float * xb = xbl + 32*ib;
  8905. if (quant_weights) {
  8906. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8907. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8908. } else {
  8909. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8910. }
  8911. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8912. for (int k = 0; k < 4; ++k) {
  8913. int nflip = 0;
  8914. uint8_t s = 0;
  8915. for (int i = 0; i < 8; ++i) {
  8916. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8917. else {
  8918. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8919. }
  8920. }
  8921. if (nflip%2) {
  8922. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8923. for (int i = 1; i < 8; ++i) {
  8924. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8925. if (ax < min) {
  8926. min = ax; imin = i;
  8927. }
  8928. }
  8929. xval[8*k+imin] = -xval[8*k+imin];
  8930. s ^= (1 << imin);
  8931. }
  8932. block_signs[k] = s & 127;
  8933. }
  8934. float max = xval[0];
  8935. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8936. if (!max) {
  8937. scales[ib] = 0;
  8938. memset(L, 0, 32);
  8939. continue;
  8940. }
  8941. float best = 0;
  8942. float scale = max/(2*kMaxQ-1);
  8943. for (int is = -15; is <= 15; ++is) {
  8944. float id = (2*kMaxQ-1+is*0.2f)/max;
  8945. float this_scale = 1/id;
  8946. for (int k = 0; k < 8; ++k) {
  8947. for (int i = 0; i < 4; ++i) {
  8948. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8949. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8950. }
  8951. uint16_t u = 0;
  8952. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8953. int grid_index = kmap_q3xs[u];
  8954. is_on_grid_aux[k] = true;
  8955. if (grid_index < 0) {
  8956. is_on_grid_aux[k] = false;
  8957. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8958. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8959. }
  8960. }
  8961. float sumqx = 0, sumq2 = 0;
  8962. for (int i = 0; i < 32; ++i) {
  8963. float w = weight[i];
  8964. float q = 2*Laux[i] + 1;
  8965. sumqx += w*xval[i]*q;
  8966. sumq2 += w*q*q;
  8967. }
  8968. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8969. scale = sumqx/sumq2; best = scale*sumqx;
  8970. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8971. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8972. }
  8973. }
  8974. int n_not_ongrid = 0;
  8975. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8976. if (n_not_ongrid > 0 && scale > 0) {
  8977. float id = 1/scale;
  8978. for (int k = 0; k < 8; ++k) {
  8979. if (is_on_grid[k]) continue;
  8980. uint16_t u = 0;
  8981. for (int i = 0; i < 4; ++i) {
  8982. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8983. l = MAX(0, MIN(kMaxQ-1, l));
  8984. u |= (l << 3*i);
  8985. }
  8986. int grid_index = kmap_q3xs[u];
  8987. if (grid_index < 0) {
  8988. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8989. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8990. }
  8991. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8992. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8993. }
  8994. float sumqx = 0, sumq2 = 0;
  8995. for (int i = 0; i < 32; ++i) {
  8996. float w = weight[i];
  8997. float q = 2*L[i] + 1;
  8998. sumqx += w*xval[i]*q;
  8999. sumq2 += w*q*q;
  9000. }
  9001. if (sumq2 > 0) scale = sumqx/sumq2;
  9002. }
  9003. if (scale < 0) {
  9004. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9005. // and correspondingly flip quant signs.
  9006. scale = -scale;
  9007. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9008. }
  9009. for (int k = 0; k < 8; ++k) {
  9010. uint16_t u = 0;
  9011. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9012. int grid_index = kmap_q3xs[u];
  9013. if (grid_index < 0) {
  9014. printf("Oops: found point %u not on grid:", u);
  9015. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9016. printf("\n");
  9017. GGML_ASSERT(false);
  9018. }
  9019. if (grid_size == 256) {
  9020. q3[8*ib+k] = grid_index;
  9021. } else {
  9022. q3[8*ib+k] = grid_index & 255;
  9023. qh[ib] |= ((grid_index >> 8) << k);
  9024. }
  9025. }
  9026. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9027. GGML_ASSERT(scale >= 0);
  9028. scales[ib] = scale;
  9029. max_scale = MAX(max_scale, scale);
  9030. }
  9031. if (!max_scale) {
  9032. memset(qs, 0, quant_size);
  9033. dh += block_size/sizeof(ggml_fp16_t);
  9034. qs += block_size;
  9035. continue;
  9036. }
  9037. float d = max_scale/31;
  9038. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9039. float id = 1/d;
  9040. for (int ib = 0; ib < QK_K/32; ++ib) {
  9041. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9042. l = MAX(0, MIN(15, l));
  9043. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9044. }
  9045. memcpy(qs, q3, quant_size);
  9046. dh += block_size/sizeof(ggml_fp16_t);
  9047. qs += block_size;
  9048. }
  9049. }
  9050. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9051. GGML_ASSERT(n_per_row%QK_K == 0);
  9052. int64_t nblock = n_per_row/QK_K;
  9053. char * qrow = (char *)dst;
  9054. for (int64_t row = 0; row < nrow; ++row) {
  9055. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9056. src += n_per_row;
  9057. qrow += nblock*sizeof(block_iq3_xxs);
  9058. }
  9059. return nrow * nblock * sizeof(block_iq3_xxs);
  9060. }
  9061. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int64_t k) {
  9062. assert(k % QK_K == 0);
  9063. block_iq3_xxs * restrict y = vy;
  9064. quantize_row_iq3_xxs_reference(x, y, k);
  9065. }
  9066. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int64_t k) {
  9067. assert(k % QK_K == 0);
  9068. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9069. }
  9070. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9071. const float * restrict quant_weights,
  9072. float * scales,
  9073. float * weight,
  9074. float * xval,
  9075. int8_t * L,
  9076. int8_t * Laux,
  9077. float * waux,
  9078. bool * is_on_grid,
  9079. bool * is_on_grid_aux,
  9080. uint8_t * block_signs) {
  9081. const int gindex = iq3_data_index(512);
  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 int64_t nbl = n/QK_K;
  9092. block_iq3_s * y = vy;
  9093. const int bs4 = block_size/4;
  9094. const int bs8 = block_size/8;
  9095. for (int ibl = 0; ibl < nbl; ++ibl) {
  9096. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9097. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9098. uint8_t * qs = y[ibl].qs;
  9099. uint8_t * qh = y[ibl].qh;
  9100. uint8_t * signs = y[ibl].signs;
  9101. float max_scale = 0;
  9102. const float * xbl = x + QK_K*ibl;
  9103. float sumx2 = 0;
  9104. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9105. float sigma2 = 2*sumx2/QK_K;
  9106. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9107. const float * xb = xbl + block_size*ib;
  9108. if (quant_weights) {
  9109. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9110. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9111. } else {
  9112. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9113. }
  9114. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9115. for (int k = 0; k < bs8; ++k) {
  9116. uint8_t s = 0;
  9117. for (int i = 0; i < 8; ++i) {
  9118. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9119. else {
  9120. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9121. }
  9122. }
  9123. block_signs[k] = s;
  9124. }
  9125. float max = xval[0];
  9126. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9127. if (!max) {
  9128. scales[ib] = 0;
  9129. continue;
  9130. }
  9131. float best = 0;
  9132. float scale = max/(2*kMaxQ-1);
  9133. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9134. for (int is = -9; is <= 9; ++is) {
  9135. float id = (2*kMaxQ-1+is*0.2f)/max;
  9136. float this_scale = 1/id;
  9137. for (int k = 0; k < bs4; ++k) {
  9138. for (int i = 0; i < 4; ++i) {
  9139. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9140. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9141. }
  9142. uint16_t u = 0;
  9143. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9144. int grid_index = kmap_q3xs[u];
  9145. is_on_grid_aux[k] = true;
  9146. if (grid_index < 0) {
  9147. is_on_grid_aux[k] = false;
  9148. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9149. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9150. }
  9151. }
  9152. float sumqx = 0, sumq2 = 0;
  9153. for (int i = 0; i < block_size; ++i) {
  9154. float w = weight[i];
  9155. float q = 2*Laux[i] + 1;
  9156. sumqx += w*xval[i]*q;
  9157. sumq2 += w*q*q;
  9158. }
  9159. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9160. scale = sumqx/sumq2; best = scale*sumqx;
  9161. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9162. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9163. }
  9164. }
  9165. int n_not_ongrid = 0;
  9166. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9167. if (n_not_ongrid > 0 && scale > 0) {
  9168. float id = 1/scale;
  9169. for (int k = 0; k < bs4; ++k) {
  9170. //if (is_on_grid[k]) continue;
  9171. uint16_t u = 0;
  9172. for (int i = 0; i < 4; ++i) {
  9173. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9174. l = MAX(0, MIN(kMaxQ-1, l));
  9175. u |= (l << 3*i);
  9176. }
  9177. int grid_index = kmap_q3xs[u];
  9178. if (grid_index < 0) {
  9179. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9180. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9181. }
  9182. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9183. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9184. }
  9185. float sumqx = 0, sumq2 = 0;
  9186. for (int i = 0; i < block_size; ++i) {
  9187. float w = weight[i];
  9188. float q = 2*L[i] + 1;
  9189. sumqx += w*xval[i]*q;
  9190. sumq2 += w*q*q;
  9191. }
  9192. if (sumq2 > 0) scale = sumqx/sumq2;
  9193. }
  9194. if (scale < 0) {
  9195. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9196. // and correspondingly flip quant signs.
  9197. scale = -scale;
  9198. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9199. }
  9200. for (int k = 0; k < bs4; ++k) {
  9201. uint16_t u = 0;
  9202. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9203. int grid_index = kmap_q3xs[u];
  9204. if (grid_index < 0) {
  9205. printf("Oops: found point %u not on grid:", u);
  9206. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9207. printf("\n");
  9208. GGML_ASSERT(false);
  9209. }
  9210. qs[k] = grid_index & 255;
  9211. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9212. }
  9213. qs += bs4;
  9214. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9215. signs += bs8;
  9216. GGML_ASSERT(scale >= 0);
  9217. scales[ib] = scale;
  9218. max_scale = MAX(max_scale, scale);
  9219. }
  9220. if (!max_scale) {
  9221. continue;
  9222. }
  9223. float d = max_scale/31;
  9224. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9225. float id = 1/d;
  9226. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9227. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9228. l1 = MAX(0, MIN(15, l1));
  9229. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9230. l2 = MAX(0, MIN(15, l2));
  9231. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9232. }
  9233. }
  9234. }
  9235. #define IQ3S_BLOCK_SIZE 32
  9236. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9237. GGML_ASSERT(n_per_row%QK_K == 0);
  9238. int64_t nblock = n_per_row/QK_K;
  9239. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9240. float weight[IQ3S_BLOCK_SIZE];
  9241. float xval[IQ3S_BLOCK_SIZE];
  9242. int8_t L[IQ3S_BLOCK_SIZE];
  9243. int8_t Laux[IQ3S_BLOCK_SIZE];
  9244. float waux[IQ3S_BLOCK_SIZE];
  9245. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9246. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9247. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9248. char * qrow = (char *)dst;
  9249. for (int64_t row = 0; row < nrow; ++row) {
  9250. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9251. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9252. src += n_per_row;
  9253. qrow += nblock*sizeof(block_iq3_s);
  9254. }
  9255. return nrow * nblock * sizeof(block_iq3_s);
  9256. }
  9257. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int64_t k) {
  9258. assert(k % QK_K == 0);
  9259. block_iq3_s * restrict y = vy;
  9260. quantize_row_iq3_s_reference(x, y, k);
  9261. }
  9262. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int64_t k) {
  9263. assert(k % QK_K == 0);
  9264. quantize_iq3_s(x, y, 1, k, NULL);
  9265. }
  9266. // =================================== 1.5 bpw ===================================================
  9267. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9268. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9269. int num_neighbors = neighbours[0];
  9270. GGML_ASSERT(num_neighbors > 0);
  9271. float best_score = 0;
  9272. int grid_index = -1;
  9273. for (int j = 1; j <= num_neighbors; ++j) {
  9274. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9275. float sumqx = 0, sumq2 = 0;
  9276. for (int i = 0; i < 8; ++i) {
  9277. float q = (pg[i] - 3)/2;
  9278. float w = weight[i];
  9279. sumqx += w*q*xval[i];
  9280. sumq2 += w*q*q;
  9281. }
  9282. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9283. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9284. grid_index = neighbours[j];
  9285. }
  9286. }
  9287. if (grid_index < 0) {
  9288. for (int i = 0; i < ngrid; ++i) {
  9289. const int8_t * grid_i = (const int8_t *)(grid + i);
  9290. float sumqx = 0, sumq2 = 0;
  9291. for (int j = 0; j < 8; ++j) {
  9292. float w = weight[j];
  9293. float q = (grid_i[j] - 3)/2;
  9294. sumqx += w*q*xval[j];
  9295. sumq2 += w*q*q;
  9296. }
  9297. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9298. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9299. grid_index = i;
  9300. }
  9301. }
  9302. }
  9303. if (grid_index < 0) {
  9304. printf("Oops, did not find grid point\n");
  9305. printf("Have %d neighbours\n", num_neighbors);
  9306. for (int j = 1; j <= num_neighbors; ++j) {
  9307. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9308. float sumqx = 0, sumq2 = 0;
  9309. for (int i = 0; i < 8; ++i) {
  9310. float q = (pg[i] - 3)/2;
  9311. float w = weight[i];
  9312. sumqx += w*q*xval[i];
  9313. sumq2 += w*q*q;
  9314. }
  9315. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9316. }
  9317. }
  9318. GGML_ASSERT(grid_index >= 0);
  9319. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9320. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9321. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9322. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9323. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9324. return grid_index;
  9325. }
  9326. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9327. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9328. int num_neighbors = neighbours[0];
  9329. GGML_ASSERT(num_neighbors > 0);
  9330. float best_score = FLT_MAX;
  9331. int grid_index = -1;
  9332. for (int j = 1; j <= num_neighbors; ++j) {
  9333. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9334. float d2 = 0;
  9335. for (int i = 0; i < 8; ++i) {
  9336. float q = xg[(pg[i] - 1)/2];
  9337. float w = weight[i];
  9338. float diff = scale*q - xval[i];
  9339. d2 += w*diff*diff;
  9340. }
  9341. if (d2 < best_score) {
  9342. best_score = d2;
  9343. grid_index = neighbours[j];
  9344. }
  9345. }
  9346. if (grid_index < 0) {
  9347. for (int i = 0; i < ngrid; ++i) {
  9348. const int8_t * grid_i = (const int8_t *)(grid + i);
  9349. float d2 = 0;
  9350. for (int j = 0; j < 8; ++j) {
  9351. float w = weight[j];
  9352. float q = xg[(grid_i[j] - 1)/2];
  9353. float diff = scale*q - xval[i];
  9354. d2 += w*diff*diff;
  9355. }
  9356. if (d2 < best_score) {
  9357. best_score = d2;
  9358. grid_index = i;
  9359. }
  9360. }
  9361. }
  9362. if (grid_index < 0) {
  9363. printf("Oops, did not find grid point\n");
  9364. printf("Have %d neighbours\n", num_neighbors);
  9365. for (int j = 1; j <= num_neighbors; ++j) {
  9366. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9367. float sumqx = 0, sumq2 = 0;
  9368. for (int i = 0; i < 8; ++i) {
  9369. float q = xg[(pg[i] - 1)/2];
  9370. float w = weight[i];
  9371. sumqx += w*q*xval[i];
  9372. sumq2 += w*q*q;
  9373. }
  9374. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9375. }
  9376. }
  9377. GGML_ASSERT(grid_index >= 0);
  9378. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9379. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9380. return grid_index;
  9381. }
  9382. static int iq1_sort_helper(const void * left, const void * right) {
  9383. const float * l = left;
  9384. const float * r = right;
  9385. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9386. }
  9387. #define IQ1S_BLOCK_SIZE 32
  9388. #define IQ1M_BLOCK_SIZE 16
  9389. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9390. float * scales,
  9391. float * weight,
  9392. float * sumx,
  9393. float * sumw,
  9394. float * pairs,
  9395. int8_t * L,
  9396. uint16_t * index,
  9397. int8_t * shifts) {
  9398. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9399. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9400. const int * kmap_q2xs = iq2_data[gindex].map;
  9401. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9402. GGML_ASSERT(quant_weights && "missing quantization weights");
  9403. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9404. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9405. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9406. GGML_ASSERT(n%QK_K == 0);
  9407. block_iq1_s * y = vy;
  9408. const int64_t nbl = n/QK_K;
  9409. const int block_size = IQ1S_BLOCK_SIZE;
  9410. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9411. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9412. int * idx = (int *)(pairs + 1);
  9413. for (int ibl = 0; ibl < nbl; ++ibl) {
  9414. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9415. memset(y[ibl].qs, 0, QK_K/8);
  9416. memset(y[ibl].qh, 0, QK_K/16);
  9417. float max_scale = 0;
  9418. const float * xbl = x + QK_K*ibl;
  9419. float sumx2 = 0;
  9420. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9421. float sigma2 = 2*sumx2/QK_K;
  9422. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9423. const float * xb = xbl + block_size*ib;
  9424. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9425. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9426. float max = fabsf(xb[0]);
  9427. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9428. if (!max) {
  9429. scales[ib] = 0;
  9430. memset(L, 1, block_size);
  9431. continue;
  9432. }
  9433. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9434. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9435. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9436. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9437. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9438. // for each possible and score for each split.
  9439. for (int j = 0; j < block_size; ++j) {
  9440. pairs[2*j] = xb[j];
  9441. idx[2*j] = j;
  9442. }
  9443. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9444. {
  9445. sumx[0] = sumw[0] = 0;
  9446. for (int j = 0; j < block_size; ++j) {
  9447. int i = idx[2*j];
  9448. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9449. sumw[j+1] = sumw[j] + weight[i];
  9450. }
  9451. }
  9452. float best_score = 0, scale = max;
  9453. int besti1 = -1, besti2 = -1, best_shift = 0;
  9454. for (int i1 = 0; i1 <= block_size; ++i1) {
  9455. for (int i2 = i1; i2 <= block_size; ++i2) {
  9456. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9457. 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];
  9458. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9459. scale = sumqx/sumq2; best_score = scale*sumqx;
  9460. besti1 = i1; besti2 = i2; best_shift = 1;
  9461. }
  9462. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9463. 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];
  9464. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9465. scale = sumqx/sumq2; best_score = scale*sumqx;
  9466. besti1 = i1; besti2 = i2; best_shift = -1;
  9467. }
  9468. }
  9469. }
  9470. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9471. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9472. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9473. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9474. if (scale < 0) {
  9475. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9476. scale = -scale; best_shift = -best_shift;
  9477. }
  9478. bool all_on_grid = true;
  9479. const float * xx = best_shift == 1 ? x_p : x_m;
  9480. for (int k = 0; k < block_size/8; ++k) {
  9481. uint16_t u = 0;
  9482. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9483. int grid_index = kmap_q2xs[u];
  9484. if (grid_index < 0) {
  9485. all_on_grid = false;
  9486. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9487. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9488. GGML_ASSERT(grid_index >= 0);
  9489. }
  9490. index[k] = grid_index;
  9491. }
  9492. if (!all_on_grid) {
  9493. float sumqx = 0, sumq2 = 0;
  9494. for (int k = 0; k < block_size/8; ++k) {
  9495. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9496. for (int j = 0; j < 8; ++j) {
  9497. float w = weight[8*k + j];
  9498. float q = xx[(pg[j] - 1)/2];
  9499. sumqx += w*q*xb[8*k+j];
  9500. sumq2 += w*q*q;
  9501. }
  9502. }
  9503. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9504. }
  9505. uint16_t h = 0;
  9506. for (int k = 0; k < block_size/8; ++k) {
  9507. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9508. h |= (index[k] >> 8) << 3*k;
  9509. }
  9510. y[ibl].qh[ib] = h;
  9511. GGML_ASSERT(scale >= 0);
  9512. scales[ib] = scale;
  9513. shifts[ib] = best_shift;
  9514. max_scale = MAX(max_scale, scale);
  9515. }
  9516. if (!max_scale) {
  9517. continue;
  9518. }
  9519. float d = max_scale/15;
  9520. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9521. float id = 1/d;
  9522. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9523. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9524. l = MAX(0, MIN(7, l));
  9525. if (shifts[ib] == -1) l |= 8;
  9526. y[ibl].qh[ib] |= (l << 12);
  9527. }
  9528. }
  9529. }
  9530. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9531. GGML_ASSERT(n_per_row%QK_K == 0);
  9532. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9533. float weight[IQ1S_BLOCK_SIZE];
  9534. int8_t L[IQ1S_BLOCK_SIZE];
  9535. float sumx[IQ1S_BLOCK_SIZE+1];
  9536. float sumw[IQ1S_BLOCK_SIZE+1];
  9537. float pairs[2*IQ1S_BLOCK_SIZE];
  9538. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9539. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9540. int64_t nblock = n_per_row/QK_K;
  9541. char * qrow = (char *)dst;
  9542. for (int64_t row = 0; row < nrow; ++row) {
  9543. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9544. src += n_per_row;
  9545. qrow += nblock*sizeof(block_iq1_s);
  9546. }
  9547. return nrow * nblock * sizeof(block_iq1_s);
  9548. }
  9549. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9550. float * scales,
  9551. float * weight,
  9552. float * pairs,
  9553. int8_t * L,
  9554. uint16_t * index,
  9555. int8_t * shifts) {
  9556. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9557. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9558. const int * kmap_q2xs = iq2_data[gindex].map;
  9559. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9560. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9561. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9562. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9563. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9564. GGML_ASSERT(n%QK_K == 0);
  9565. block_iq1_m * y = vy;
  9566. const int64_t nbl = n/QK_K;
  9567. const int block_size = IQ1M_BLOCK_SIZE;
  9568. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9569. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9570. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9571. int * idx = (int *)(pairs + 1);
  9572. float sumqx[4], sumq2[4];
  9573. iq1m_scale_t s;
  9574. const float * xx;
  9575. for (int ibl = 0; ibl < nbl; ++ibl) {
  9576. #if QK_K == 64
  9577. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9578. #endif
  9579. memset(y[ibl].qs, 0, QK_K/8);
  9580. memset(y[ibl].qh, 0, QK_K/16);
  9581. memset(y[ibl].scales, 0, QK_K/32);
  9582. float max_scale = 0;
  9583. const float * xbl = x + QK_K*ibl;
  9584. float sumx2 = 0;
  9585. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9586. float sigma2 = 2*sumx2/QK_K;
  9587. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9588. const float * xb = xbl + block_size*ib;
  9589. if (quant_weights) {
  9590. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9591. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9592. } else {
  9593. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9594. }
  9595. float max = fabsf(xb[0]);
  9596. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9597. if (!max) {
  9598. scales[ib] = 0;
  9599. memset(L, 1, block_size);
  9600. continue;
  9601. }
  9602. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9603. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9604. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9605. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9606. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9607. // for each possible and score for each split.
  9608. for (int j = 0; j < block_size; ++j) {
  9609. pairs[2*j] = xb[j];
  9610. idx[2*j] = j;
  9611. }
  9612. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9613. float best_score = 0, scale = max;
  9614. int besti1 = -1, besti2 = -1, best_k = -1;
  9615. // 0: +, +
  9616. // 1: +, -
  9617. // 2: -, +
  9618. // 3: -, -
  9619. for (int i1 = 0; i1 <= block_size; ++i1) {
  9620. for (int i2 = i1; i2 <= block_size; ++i2) {
  9621. memset(sumqx, 0, 4*sizeof(float));
  9622. memset(sumq2, 0, 4*sizeof(float));
  9623. for (int j = 0; j < i1; ++j) {
  9624. int i = idx[2*j];
  9625. if (i < block_size/2) {
  9626. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9627. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9628. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9629. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9630. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9631. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9632. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9633. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9634. } else {
  9635. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9636. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9637. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9638. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9639. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9640. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9641. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9642. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9643. }
  9644. }
  9645. for (int j = i1; j < i2; ++j) {
  9646. int i = idx[2*j];
  9647. if (i < block_size/2) {
  9648. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9649. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9650. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9651. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9652. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9653. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9654. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9655. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9656. } else {
  9657. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9658. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9659. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9660. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9661. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9662. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9663. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9664. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9665. }
  9666. }
  9667. for (int j = i2; j < block_size; ++j) {
  9668. int i = idx[2*j];
  9669. if (i < block_size/2) {
  9670. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9671. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9672. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9673. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9674. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9675. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9676. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9677. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9678. } else {
  9679. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9680. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9681. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9682. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9683. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9684. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9685. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9686. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9687. }
  9688. }
  9689. for (int k = 0; k < 4; ++k) {
  9690. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9691. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9692. besti1 = i1; besti2 = i2; best_k = k;
  9693. }
  9694. }
  9695. }
  9696. }
  9697. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9698. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9699. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9700. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9701. if (scale < 0) {
  9702. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9703. scale = -scale;
  9704. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9705. }
  9706. bool all_on_grid = true;
  9707. for (int k = 0; k < block_size/8; ++k) {
  9708. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9709. else xx = best_k%2 == 0 ? x_p : x_m;
  9710. uint16_t u = 0;
  9711. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9712. int grid_index = kmap_q2xs[u];
  9713. if (grid_index < 0) {
  9714. all_on_grid = false;
  9715. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9716. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9717. GGML_ASSERT(grid_index >= 0);
  9718. }
  9719. index[k] = grid_index;
  9720. }
  9721. if (!all_on_grid) {
  9722. float sumqx_f = 0, sumq2_f = 0;
  9723. for (int k = 0; k < block_size/8; ++k) {
  9724. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9725. else xx = best_k%2 == 0 ? x_p : x_m;
  9726. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9727. for (int j = 0; j < 8; ++j) {
  9728. float w = weight[8*k + j];
  9729. float q = xx[(pg[j] - 1)/2];
  9730. sumqx_f += w*q*xb[8*k+j];
  9731. sumq2_f += w*q*q;
  9732. }
  9733. }
  9734. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9735. }
  9736. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9737. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9738. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9739. GGML_ASSERT(scale >= 0);
  9740. scales[ib] = scale;
  9741. shifts[ib] = best_k;
  9742. max_scale = MAX(max_scale, scale);
  9743. }
  9744. if (!max_scale) {
  9745. continue;
  9746. }
  9747. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9748. #if QK_K == 64
  9749. float d = max_scale/31;
  9750. #else
  9751. float d = max_scale/15;
  9752. #endif
  9753. float id = 1/d;
  9754. float sumqx_f = 0, sumq2_f = 0;
  9755. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9756. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9757. #if QK_K == 64
  9758. l = MAX(0, MIN(15, l));
  9759. sc[ib/4] |= (l << 4*(ib%4));
  9760. #else
  9761. l = MAX(0, MIN(7, l));
  9762. sc[ib/4] |= (l << 3*(ib%4));
  9763. #endif
  9764. y[ibl].qh[ib] |= masks[shifts[ib]];
  9765. const float * xb = xbl + block_size*ib;
  9766. if (quant_weights) {
  9767. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9768. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9769. } else {
  9770. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9771. }
  9772. for (int k = 0; k < block_size/8; ++k) {
  9773. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9774. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9775. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  9776. for (int j = 0; j < 8; ++j) {
  9777. float w = weight[8*k + j];
  9778. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  9779. sumqx_f += w*q*xb[8*k+j];
  9780. sumq2_f += w*q*q;
  9781. }
  9782. }
  9783. }
  9784. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  9785. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  9786. #if QK_K == 64
  9787. y[ibl].d = s.f16;
  9788. #else
  9789. sc[0] |= ((s.u16 & 0x000f) << 12);
  9790. sc[1] |= ((s.u16 & 0x00f0) << 8);
  9791. sc[2] |= ((s.u16 & 0x0f00) << 4);
  9792. sc[3] |= ((s.u16 & 0xf000) << 0);
  9793. #endif
  9794. }
  9795. }
  9796. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9797. GGML_ASSERT(n_per_row%QK_K == 0);
  9798. float scales[QK_K/IQ1M_BLOCK_SIZE];
  9799. float weight[IQ1M_BLOCK_SIZE];
  9800. int8_t L[IQ1M_BLOCK_SIZE];
  9801. float pairs[2*IQ1M_BLOCK_SIZE];
  9802. uint16_t index[IQ1M_BLOCK_SIZE/8];
  9803. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  9804. int64_t nblock = n_per_row/QK_K;
  9805. char * qrow = (char *)dst;
  9806. for (int64_t row = 0; row < nrow; ++row) {
  9807. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  9808. src += n_per_row;
  9809. qrow += nblock*sizeof(block_iq1_m);
  9810. }
  9811. return nrow * nblock * sizeof(block_iq1_m);
  9812. }
  9813. // ============================ 4-bit non-linear quants
  9814. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9815. if (x <= val[0]) return 0;
  9816. if (x >= val[n-1]) return n-1;
  9817. int ml = 0, mu = n-1;
  9818. while (mu-ml > 1) {
  9819. int mav = (ml+mu)/2;
  9820. if (x < val[mav]) mu = mav; else ml = mav;
  9821. }
  9822. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9823. }
  9824. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9825. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9826. float * scales, float * weight, uint8_t * L,
  9827. const int8_t * values,
  9828. const float * quant_weights,
  9829. const int ntry) {
  9830. float sigma2 = 0;
  9831. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9832. sigma2 *= 2.f/super_block_size;
  9833. memset(q4, 0, super_block_size/2);
  9834. dh[0] = GGML_FP32_TO_FP16(0.f);
  9835. float max_scale = 0, amax_scale = 0;
  9836. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9837. const float * xb = x + ib*block_size;
  9838. uint8_t * Lb = L + ib*block_size;
  9839. if (quant_weights) {
  9840. const float * qw = quant_weights + ib*block_size;
  9841. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9842. } else {
  9843. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9844. }
  9845. float amax = 0, max = 0;
  9846. for (int j = 0; j < block_size; ++j) {
  9847. float ax = fabsf(xb[j]);
  9848. if (ax > amax) {
  9849. amax = ax; max = xb[j];
  9850. }
  9851. }
  9852. if (!amax) {
  9853. scales[ib] = 0;
  9854. continue;
  9855. }
  9856. float d = ntry > 0 ? -max/values[0] : max/values[0];
  9857. float id = 1/d;
  9858. float sumqx = 0, sumq2 = 0;
  9859. for (int j = 0; j < block_size; ++j) {
  9860. float al = id*xb[j];
  9861. int l = best_index_int8(16, values, al);
  9862. Lb[j] = l;
  9863. float q = values[l];
  9864. float w = weight[j];
  9865. sumqx += w*q*xb[j];
  9866. sumq2 += w*q*q;
  9867. }
  9868. d = sumqx/sumq2;
  9869. float best = d*sumqx;
  9870. for (int itry = -ntry; itry <= ntry; ++itry) {
  9871. id = (itry + values[0])/max;
  9872. sumqx = sumq2 = 0;
  9873. for (int j = 0; j < block_size; ++j) {
  9874. float al = id*xb[j];
  9875. int l = best_index_int8(16, values, al);
  9876. float q = values[l];
  9877. float w = weight[j];
  9878. sumqx += w*q*xb[j];
  9879. sumq2 += w*q*q;
  9880. }
  9881. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9882. d = sumqx/sumq2; best = d * sumqx;
  9883. }
  9884. }
  9885. scales[ib] = d;
  9886. float abs_d = fabsf(d);
  9887. if (abs_d > amax_scale) {
  9888. amax_scale = abs_d; max_scale = d;
  9889. }
  9890. }
  9891. if (super_block_size/block_size > 1) {
  9892. int nb = super_block_size/block_size;
  9893. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9894. float d = -max_scale/32;
  9895. dh[0] = GGML_FP32_TO_FP16(d);
  9896. float id = d ? 1/d : 0.f;
  9897. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9898. int l = nearest_int(id*scales[ib]);
  9899. l = MAX(-32, MIN(31, l));
  9900. float dl = d * l;
  9901. float idl = dl ? 1/dl : 0.f;
  9902. uint8_t * Lb = L + ib*block_size;
  9903. const float * xb = x + ib*block_size;
  9904. for (int j = 0; j < block_size; ++j) {
  9905. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9906. }
  9907. l += 32;
  9908. uint8_t l_l = l & 0xf;
  9909. uint8_t l_h = l >> 4;
  9910. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9911. else scales_l[ib/2] |= (l_l << 4);
  9912. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9913. }
  9914. } else {
  9915. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9916. if (ntry > 0) {
  9917. float id = scales[0] ? 1/scales[0] : 0;
  9918. for (int j = 0; j < super_block_size; ++j) {
  9919. L[j] = best_index_int8(16, values, id*x[j]);
  9920. }
  9921. }
  9922. }
  9923. for (int i = 0; i < super_block_size/32; ++i) {
  9924. for (int j = 0; j < 16; ++j) {
  9925. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9926. }
  9927. }
  9928. }
  9929. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9930. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9931. int64_t nblock = n_per_row/QK4_NL;
  9932. char * qrow = (char *)dst;
  9933. uint8_t L[QK4_NL];
  9934. float weight[QK4_NL];
  9935. uint16_t unused_h;
  9936. uint8_t * unused_l = NULL;
  9937. float scale;
  9938. for (int64_t row = 0; row < nrow; ++row) {
  9939. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9940. for (int ibl = 0; ibl < nblock; ++ibl) {
  9941. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9942. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9943. &scale, weight, L, kvalues_iq4nl, qw, 7);
  9944. }
  9945. src += n_per_row;
  9946. qrow += nblock*sizeof(block_iq4_nl);
  9947. }
  9948. return nrow * nblock * sizeof(block_iq4_nl);
  9949. }
  9950. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int64_t k) {
  9951. GGML_ASSERT(k%QK4_NL == 0);
  9952. int64_t nblock = k/QK4_NL;
  9953. uint8_t L[QK4_NL];
  9954. float weight[QK4_NL];
  9955. uint16_t unused_h;
  9956. uint8_t * unused_l = NULL;
  9957. float scale;
  9958. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  9959. for (int ibl = 0; ibl < nblock; ++ibl) {
  9960. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9961. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  9962. }
  9963. }
  9964. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int64_t k) {
  9965. assert(k % QK4_NL == 0);
  9966. quantize_row_iq4_nl(x, y, k);
  9967. }
  9968. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9969. #if QK_K == 64
  9970. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9971. #else
  9972. GGML_ASSERT(n_per_row%QK_K == 0);
  9973. int64_t nblock = n_per_row/QK_K;
  9974. char * qrow = (char *)dst;
  9975. uint8_t L[QK_K];
  9976. float weight[32];
  9977. float scales[QK_K/32];
  9978. for (int64_t row = 0; row < nrow; ++row) {
  9979. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9980. for (int ibl = 0; ibl < nblock; ++ibl) {
  9981. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9982. 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,
  9983. scales, weight, L, kvalues_iq4nl, qw, 7);
  9984. }
  9985. src += n_per_row;
  9986. qrow += nblock*sizeof(block_iq4_xs);
  9987. }
  9988. return nrow * nblock * sizeof(block_iq4_xs);
  9989. #endif
  9990. }
  9991. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int64_t k) {
  9992. assert(k % QK_K == 0);
  9993. block_iq4_xs * restrict y = vy;
  9994. quantize_row_iq4_xs_reference(x, y, k);
  9995. }
  9996. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int64_t k) {
  9997. assert(k % QK_K == 0);
  9998. quantize_iq4_xs(x, y, 1, k, NULL);
  9999. }
  10000. // =============================== 2.5625 bpw
  10001. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  10002. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10003. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10004. const int * kmap_q2xs = iq2_data[gindex].map;
  10005. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10006. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10007. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10008. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10009. GGML_ASSERT(n%QK_K == 0);
  10010. const int kMaxQ = 3;
  10011. const int64_t nbl = n/QK_K;
  10012. block_iq2_s * y = vy;
  10013. float scales[QK_K/16];
  10014. float weight[16];
  10015. float xval[16];
  10016. int8_t L[16];
  10017. int8_t Laux[16];
  10018. float waux[16];
  10019. bool is_on_grid[2];
  10020. bool is_on_grid_aux[2];
  10021. uint8_t block_signs[2];
  10022. for (int ibl = 0; ibl < nbl; ++ibl) {
  10023. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10024. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10025. float max_scale = 0;
  10026. const float * xbl = x + QK_K*ibl;
  10027. float sumx2 = 0;
  10028. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10029. float sigma2 = 2*sumx2/QK_K;
  10030. for (int ib = 0; ib < QK_K/16; ++ib) {
  10031. const float * xb = xbl + 16*ib;
  10032. if (quant_weights) {
  10033. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10034. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10035. } else {
  10036. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10037. }
  10038. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10039. for (int k = 0; k < 2; ++k) {
  10040. uint8_t s = 0;
  10041. for (int i = 0; i < 8; ++i) {
  10042. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10043. else {
  10044. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10045. }
  10046. }
  10047. block_signs[k] = s;
  10048. }
  10049. float max = xval[0];
  10050. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10051. if (!max) {
  10052. scales[ib] = 0;
  10053. continue;
  10054. }
  10055. float best = 0;
  10056. float scale = max/(2*kMaxQ-1);
  10057. is_on_grid[0] = is_on_grid[1] = true;
  10058. for (int is = -9; is <= 9; ++is) {
  10059. float id = (2*kMaxQ-1+is*0.1f)/max;
  10060. float this_scale = 1/id;
  10061. for (int k = 0; k < 2; ++k) {
  10062. for (int i = 0; i < 8; ++i) {
  10063. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10064. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10065. }
  10066. uint16_t u = 0;
  10067. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10068. int grid_index = kmap_q2xs[u];
  10069. is_on_grid_aux[k] = true;
  10070. if (grid_index < 0) {
  10071. is_on_grid_aux[k] = false;
  10072. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10073. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10074. }
  10075. }
  10076. float sumqx = 0, sumq2 = 0;
  10077. for (int i = 0; i < 16; ++i) {
  10078. float w = weight[i];
  10079. float q = 2*Laux[i] + 1;
  10080. sumqx += w*xval[i]*q;
  10081. sumq2 += w*q*q;
  10082. }
  10083. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10084. scale = sumqx/sumq2; best = scale*sumqx;
  10085. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10086. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10087. }
  10088. }
  10089. int n_not_ongrid = 0;
  10090. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10091. if (n_not_ongrid > 0 && scale > 0) {
  10092. float id = 1/scale;
  10093. for (int k = 0; k < 2; ++k) {
  10094. if (is_on_grid[k]) continue;
  10095. uint16_t u = 0;
  10096. for (int i = 0; i < 8; ++i) {
  10097. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10098. l = MAX(0, MIN(kMaxQ-1, l));
  10099. u |= (l << 2*i);
  10100. L[8*k + i] = l;
  10101. }
  10102. int grid_index = kmap_q2xs[u];
  10103. if (grid_index < 0) {
  10104. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10105. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10106. }
  10107. }
  10108. float sumqx = 0, sumq2 = 0;
  10109. for (int i = 0; i < 16; ++i) {
  10110. float w = weight[i];
  10111. float q = 2*L[i] + 1;
  10112. sumqx += w*xval[i]*q;
  10113. sumq2 += w*q*q;
  10114. }
  10115. if (sumq2 > 0) scale = sumqx/sumq2;
  10116. }
  10117. if (scale < 0) {
  10118. scale = -scale;
  10119. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10120. }
  10121. for (int k = 0; k < 2; ++k) {
  10122. uint16_t u = 0;
  10123. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10124. int grid_index = kmap_q2xs[u];
  10125. if (grid_index < 0) {
  10126. printf("Oops: found point %u not on grid:", u);
  10127. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10128. printf("\n");
  10129. GGML_ASSERT(false);
  10130. }
  10131. const int i8 = 2*ib + k;
  10132. y[ibl].qs[i8] = grid_index & 255;
  10133. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10134. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10135. }
  10136. GGML_ASSERT(scale >= 0);
  10137. scales[ib] = scale;
  10138. max_scale = MAX(max_scale, scale);
  10139. }
  10140. if (!max_scale) {
  10141. continue;
  10142. }
  10143. float d = max_scale/31;
  10144. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10145. float id = 1/d;
  10146. for (int ib = 0; ib < QK_K/16; ++ib) {
  10147. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10148. l = MAX(0, MIN(15, l));
  10149. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10150. else y[ibl].scales[ib/2] |= (l << 4);
  10151. }
  10152. }
  10153. }
  10154. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10155. GGML_ASSERT(n_per_row%QK_K == 0);
  10156. int64_t nblock = n_per_row/QK_K;
  10157. char * qrow = (char *)dst;
  10158. for (int64_t row = 0; row < nrow; ++row) {
  10159. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10160. src += n_per_row;
  10161. qrow += nblock*sizeof(block_iq2_s);
  10162. }
  10163. return nrow * nblock * sizeof(block_iq2_s);
  10164. }
  10165. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int64_t k) {
  10166. assert(k % QK_K == 0);
  10167. quantize_iq2_s(x, y, 1, k, NULL);
  10168. }
  10169. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int64_t k) {
  10170. assert(k % QK_K == 0);
  10171. block_iq2_s * restrict y = vy;
  10172. quantize_row_iq2_s_reference(x, y, k);
  10173. }
  10174. static bool validate_float(float f, size_t i) {
  10175. if (isinf(f)) {
  10176. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  10177. return false;
  10178. }
  10179. if (isnan(f)) {
  10180. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  10181. return false;
  10182. }
  10183. return true;
  10184. }
  10185. static bool isinf_fp16(ggml_fp16_t f) {
  10186. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) == 0;
  10187. }
  10188. static bool isnan_fp16(ggml_fp16_t f) {
  10189. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) != 0;
  10190. }
  10191. static bool validate_fp16(ggml_fp16_t f, size_t i) {
  10192. if (isinf_fp16(f)) {
  10193. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  10194. return false;
  10195. }
  10196. if (isnan_fp16(f)) {
  10197. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  10198. return false;
  10199. }
  10200. return true;
  10201. }
  10202. #define VALIDATE_ROW_DATA_D_F16_IMPL(type, data, nb) \
  10203. const type * q = (const type *) (data); \
  10204. for (size_t i = 0; i < (nb); ++i) { \
  10205. if (!validate_fp16(q[i].d, i)) { \
  10206. return false; \
  10207. } \
  10208. }
  10209. #define VALIDATE_ROW_DATA_DM_F16_IMPL(type, data, nb, d, m) \
  10210. const type * q = (const type *) (data); \
  10211. for (size_t i = 0; i < (nb); ++i) { \
  10212. if (!validate_fp16(q[i].d, i) || !validate_fp16(q[i].m, i)) { \
  10213. return false; \
  10214. } \
  10215. }
  10216. bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes) {
  10217. if (type < 0 || type >= GGML_TYPE_COUNT) {
  10218. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  10219. return false;
  10220. }
  10221. if (nbytes % ggml_type_size(type) != 0) {
  10222. fprintf(stderr, "%s: invalid size %zu for type %d\n", __func__, nbytes, type);
  10223. return false;
  10224. }
  10225. const size_t nb = nbytes/ggml_type_size(type);
  10226. switch (type) {
  10227. case GGML_TYPE_BF16:
  10228. {
  10229. int nans = 0;
  10230. int infs = 0;
  10231. const unsigned short * f = (const unsigned short *) data;
  10232. for (size_t i = 0; i < nb; ++i) {
  10233. nans += (f[i] & 0x7fff) > 0x7f80;
  10234. infs += (f[i] & 0x7fff) == 0x7f80;
  10235. }
  10236. if (nans) {
  10237. fprintf(stderr, "%s: found %d NaNs in row of %zu BF16 values\n", __func__, nans, nb);
  10238. return false;
  10239. }
  10240. if (infs) {
  10241. fprintf(stderr, "%s: found %d infinities in row of %zu BF16 values\n", __func__, infs, nb);
  10242. return false;
  10243. }
  10244. } break;
  10245. case GGML_TYPE_F16:
  10246. {
  10247. const ggml_fp16_t * f = (const ggml_fp16_t *) data;
  10248. size_t i = 0;
  10249. #if defined(__AVX2__)
  10250. for (; i + 15 < nb; i += 16) {
  10251. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  10252. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi16(0x7c00));
  10253. __m256i cmp = _mm256_cmpeq_epi16(vexp, _mm256_set1_epi16(0x7c00));
  10254. int mask = _mm256_movemask_epi8(cmp);
  10255. if (mask) {
  10256. for (size_t j = 0; j < 16; ++j) {
  10257. if (!validate_fp16(f[i + j], i + j)) {
  10258. return false;
  10259. }
  10260. }
  10261. GGML_UNREACHABLE();
  10262. }
  10263. }
  10264. #elif defined(__ARM_NEON)
  10265. for (; i + 7 < nb; i += 8) {
  10266. uint16x8_t v = vld1q_u16(f + i);
  10267. uint16x8_t vexp = vandq_u16(v, vdupq_n_u16(0x7c00));
  10268. uint16x8_t cmp = vceqq_u16(vexp, vdupq_n_u16(0x7c00));
  10269. uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(cmp, 4)), 0);
  10270. if (mask) {
  10271. for (size_t j = 0; j < 8; ++j) {
  10272. if (!validate_fp16(f[i + j], i + j)) {
  10273. return false;
  10274. }
  10275. }
  10276. GGML_UNREACHABLE();
  10277. }
  10278. }
  10279. #endif
  10280. for (; i < nb; ++i) {
  10281. if (!validate_fp16(f[i], i)) {
  10282. return false;
  10283. }
  10284. }
  10285. } break;
  10286. case GGML_TYPE_F32:
  10287. {
  10288. const float * f = (const float *) data;
  10289. size_t i = 0;
  10290. #if defined(__AVX2__)
  10291. for (; i + 7 < nb; i += 8) {
  10292. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  10293. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi32(0x7f800000));
  10294. __m256i cmp = _mm256_cmpeq_epi32(vexp, _mm256_set1_epi32(0x7f800000));
  10295. int mask = _mm256_movemask_epi8(cmp);
  10296. if (mask) {
  10297. for (size_t j = 0; j < 8; ++j) {
  10298. if (!validate_float(f[i + j], i + j)) {
  10299. return false;
  10300. }
  10301. }
  10302. GGML_UNREACHABLE();
  10303. }
  10304. }
  10305. #elif defined(__ARM_NEON)
  10306. for (; i + 3 < nb; i += 4) {
  10307. uint32x4_t v = vld1q_u32((const uint32_t *)f + i);
  10308. uint32x4_t vexp = vandq_u32(v, vdupq_n_u32(0x7f800000));
  10309. uint32x4_t cmp = vceqq_u32(vexp, vdupq_n_u32(0x7f800000));
  10310. uint64_t mask = vget_lane_u64(vreinterpret_u64_u16(vshrn_n_u32(cmp, 8)), 0);
  10311. if (mask) {
  10312. for (size_t j = 0; j < 4; ++j) {
  10313. if (!validate_float(f[i + j], i + j)) {
  10314. return false;
  10315. }
  10316. }
  10317. GGML_UNREACHABLE();
  10318. }
  10319. }
  10320. #endif
  10321. for (; i < nb; ++i) {
  10322. if (!validate_float(f[i], i)) {
  10323. return false;
  10324. }
  10325. }
  10326. } break;
  10327. case GGML_TYPE_F64:
  10328. {
  10329. const double * f = (const double *) data;
  10330. for (size_t i = 0; i < nb; ++i) {
  10331. if (!validate_float(f[i], i)) {
  10332. return false;
  10333. }
  10334. }
  10335. } break;
  10336. case GGML_TYPE_Q4_0:
  10337. {
  10338. VALIDATE_ROW_DATA_D_F16_IMPL(block_q4_0, data, nb);
  10339. } break;
  10340. case GGML_TYPE_Q4_1:
  10341. {
  10342. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_1, data, nb, d, m);
  10343. } break;
  10344. case GGML_TYPE_Q5_0:
  10345. {
  10346. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_0, data, nb);
  10347. } break;
  10348. case GGML_TYPE_Q5_1:
  10349. {
  10350. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_1, data, nb, d, m);
  10351. } break;
  10352. case GGML_TYPE_Q8_0:
  10353. {
  10354. VALIDATE_ROW_DATA_D_F16_IMPL(block_q8_0, data, nb);
  10355. } break;
  10356. case GGML_TYPE_Q2_K:
  10357. {
  10358. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q2_K, data, nb, d, dmin);
  10359. } break;
  10360. case GGML_TYPE_Q3_K:
  10361. {
  10362. VALIDATE_ROW_DATA_D_F16_IMPL(block_q3_K, data, nb);
  10363. } break;
  10364. case GGML_TYPE_Q4_K:
  10365. {
  10366. #ifdef GGML_QKK_64
  10367. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d[0], d[1]);
  10368. #else
  10369. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d, dmin);
  10370. #endif
  10371. } break;
  10372. case GGML_TYPE_Q5_K:
  10373. {
  10374. #ifdef GGML_QKK_64
  10375. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_K, data, nb);
  10376. #else
  10377. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_K, data, nb, d, dmin);
  10378. #endif
  10379. } break;
  10380. case GGML_TYPE_Q6_K:
  10381. {
  10382. VALIDATE_ROW_DATA_D_F16_IMPL(block_q6_K, data, nb);
  10383. } break;
  10384. case GGML_TYPE_Q8_K:
  10385. {
  10386. const block_q8_K * q = (const block_q8_K *) data;
  10387. for (size_t i = 0; i < nb; ++i) {
  10388. if (!validate_float(q[i].d, i)) {
  10389. return false;
  10390. }
  10391. }
  10392. } break;
  10393. case GGML_TYPE_IQ1_S:
  10394. {
  10395. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq1_s, data, nb);
  10396. } break;
  10397. case GGML_TYPE_IQ1_M:
  10398. {
  10399. const block_iq1_m * q = (const block_iq1_m *) data;
  10400. for (size_t i = 0; i < nb; ++i) {
  10401. #if QK_K == 64
  10402. if (!validate_fp16(q[i].d, i)) {
  10403. return false;
  10404. }
  10405. #else
  10406. iq1m_scale_t scale;
  10407. const uint16_t * sc = (const uint16_t *)q[i].scales;
  10408. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  10409. if (!validate_fp16(scale.f16, i)) {
  10410. return false;
  10411. }
  10412. #endif
  10413. }
  10414. } break;
  10415. case GGML_TYPE_IQ2_XXS:
  10416. {
  10417. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xxs, data, nb);
  10418. } break;
  10419. case GGML_TYPE_IQ2_XS:
  10420. {
  10421. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xs, data, nb);
  10422. } break;
  10423. case GGML_TYPE_IQ2_S:
  10424. {
  10425. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_s, data, nb);
  10426. } break;
  10427. case GGML_TYPE_IQ3_XXS:
  10428. {
  10429. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_xxs, data, nb);
  10430. } break;
  10431. case GGML_TYPE_IQ3_S:
  10432. {
  10433. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_s, data, nb);
  10434. } break;
  10435. case GGML_TYPE_IQ4_XS:
  10436. #if QK_K != 64
  10437. {
  10438. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_xs, data, nb);
  10439. } break;
  10440. #endif
  10441. // with QK_K == 64, iq4_xs is iq4_nl
  10442. case GGML_TYPE_IQ4_NL:
  10443. {
  10444. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_nl, data, nb);
  10445. } break;
  10446. case GGML_TYPE_I8:
  10447. case GGML_TYPE_I16:
  10448. case GGML_TYPE_I32:
  10449. case GGML_TYPE_I64:
  10450. // nothing to validate
  10451. break;
  10452. default:
  10453. {
  10454. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  10455. return false;
  10456. }
  10457. }
  10458. return true;
  10459. }