ggml-quants.c 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693
  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. #if defined(_MSC_VER)
  14. // disable "possible loss of data" to avoid warnings for hundreds of casts
  15. // we should just be careful :)
  16. #pragma warning(disable: 4244 4267)
  17. #endif
  18. #define UNUSED GGML_UNUSED
  19. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  20. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  21. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  22. // multiply int8_t, add results pairwise twice
  23. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  24. // Get absolute values of x vectors
  25. const __m128i ax = _mm_sign_epi8(x, x);
  26. // Sign the values of the y vectors
  27. const __m128i sy = _mm_sign_epi8(y, x);
  28. // Perform multiplication and create 16-bit values
  29. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  30. const __m128i ones = _mm_set1_epi16(1);
  31. return _mm_madd_epi16(ones, dot);
  32. }
  33. #if __AVX__ || __AVX2__ || __AVX512F__
  34. // horizontally add 8 floats
  35. static inline float hsum_float_8(const __m256 x) {
  36. __m128 res = _mm256_extractf128_ps(x, 1);
  37. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  38. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  39. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  40. return _mm_cvtss_f32(res);
  41. }
  42. // horizontally add 8 int32_t
  43. static inline int hsum_i32_8(const __m256i a) {
  44. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  45. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  46. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  47. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  48. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  49. }
  50. // horizontally add 4 int32_t
  51. static inline int hsum_i32_4(const __m128i a) {
  52. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  53. const __m128i sum64 = _mm_add_epi32(hi64, a);
  54. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  55. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  56. }
  57. #if defined(__AVX2__) || defined(__AVX512F__)
  58. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  59. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  60. uint32_t x32;
  61. memcpy(&x32, x, sizeof(uint32_t));
  62. const __m256i shuf_mask = _mm256_set_epi64x(
  63. 0x0303030303030303, 0x0202020202020202,
  64. 0x0101010101010101, 0x0000000000000000);
  65. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  66. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  67. bytes = _mm256_or_si256(bytes, bit_mask);
  68. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  69. }
  70. // Unpack 32 4-bit fields into 32 bytes
  71. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  72. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  73. {
  74. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  75. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  76. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  77. return _mm256_and_si256(lowMask, bytes);
  78. }
  79. // add int16_t pairwise and return as float vector
  80. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  81. const __m256i ones = _mm256_set1_epi16(1);
  82. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  83. return _mm256_cvtepi32_ps(summed_pairs);
  84. }
  85. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  86. #if defined(__AVXVNNI__) || (defined(__AVX512VNNI__) && defined(__AVX512VL__))
  87. const __m256i zero = _mm256_setzero_si256();
  88. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  89. return _mm256_cvtepi32_ps(summed_pairs);
  90. #else
  91. // Perform multiplication and create 16-bit values
  92. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  93. return sum_i16_pairs_float(dot);
  94. #endif
  95. }
  96. // multiply int8_t, add results pairwise twice and return as float vector
  97. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  98. #if __AVXVNNIINT8__
  99. const __m256i zero = _mm256_setzero_si256();
  100. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  101. return _mm256_cvtepi32_ps(summed_pairs);
  102. #else
  103. // Get absolute values of x vectors
  104. const __m256i ax = _mm256_sign_epi8(x, x);
  105. // Sign the values of the y vectors
  106. const __m256i sy = _mm256_sign_epi8(y, x);
  107. return mul_sum_us8_pairs_float(ax, sy);
  108. #endif
  109. }
  110. static inline __m128i packNibbles( __m256i bytes )
  111. {
  112. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  113. #if __AVX512F__
  114. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  115. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  116. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  117. #else
  118. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  119. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  120. __m256i low = _mm256_and_si256( lowByte, bytes );
  121. high = _mm256_srli_epi16( high, 4 );
  122. bytes = _mm256_or_si256( low, high );
  123. // Compress uint16_t lanes into bytes
  124. __m128i r0 = _mm256_castsi256_si128( bytes );
  125. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  126. return _mm_packus_epi16( r0, r1 );
  127. #endif
  128. }
  129. #elif defined(__AVX__)
  130. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  131. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  132. uint32_t x32;
  133. memcpy(&x32, x, sizeof(uint32_t));
  134. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  135. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  136. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  137. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  138. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  139. bytesl = _mm_or_si128(bytesl, bit_mask);
  140. bytesh = _mm_or_si128(bytesh, bit_mask);
  141. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  142. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  143. return MM256_SET_M128I(bytesh, bytesl);
  144. }
  145. // Unpack 32 4-bit fields into 32 bytes
  146. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  147. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  148. {
  149. // Load 16 bytes from memory
  150. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  151. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  152. const __m128i lowMask = _mm_set1_epi8(0xF);
  153. tmpl = _mm_and_si128(lowMask, tmpl);
  154. tmph = _mm_and_si128(lowMask, tmph);
  155. return MM256_SET_M128I(tmph, tmpl);
  156. }
  157. // add int16_t pairwise and return as float vector
  158. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  159. const __m128i ones = _mm_set1_epi16(1);
  160. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  161. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  162. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  163. return _mm256_cvtepi32_ps(summed_pairs);
  164. }
  165. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  166. const __m128i axl = _mm256_castsi256_si128(ax);
  167. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  168. const __m128i syl = _mm256_castsi256_si128(sy);
  169. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  170. // Perform multiplication and create 16-bit values
  171. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  172. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  173. return sum_i16_pairs_float(doth, dotl);
  174. }
  175. // multiply int8_t, add results pairwise twice and return as float vector
  176. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  177. const __m128i xl = _mm256_castsi256_si128(x);
  178. const __m128i xh = _mm256_extractf128_si256(x, 1);
  179. const __m128i yl = _mm256_castsi256_si128(y);
  180. const __m128i yh = _mm256_extractf128_si256(y, 1);
  181. // Get absolute values of x vectors
  182. const __m128i axl = _mm_sign_epi8(xl, xl);
  183. const __m128i axh = _mm_sign_epi8(xh, xh);
  184. // Sign the values of the y vectors
  185. const __m128i syl = _mm_sign_epi8(yl, xl);
  186. const __m128i syh = _mm_sign_epi8(yh, xh);
  187. // Perform multiplication and create 16-bit values
  188. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  189. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  190. return sum_i16_pairs_float(doth, dotl);
  191. }
  192. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  193. {
  194. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  195. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  196. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  197. __m128i low = _mm_and_si128( lowByte, bytes1 );
  198. high = _mm_srli_epi16( high, 4 );
  199. bytes1 = _mm_or_si128( low, high );
  200. high = _mm_andnot_si128( lowByte, bytes2 );
  201. low = _mm_and_si128( lowByte, bytes2 );
  202. high = _mm_srli_epi16( high, 4 );
  203. bytes2 = _mm_or_si128( low, high );
  204. return _mm_packus_epi16( bytes1, bytes2);
  205. }
  206. #endif
  207. #elif defined(__SSSE3__)
  208. // horizontally add 4x4 floats
  209. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  210. __m128 res_0 =_mm_hadd_ps(a, b);
  211. __m128 res_1 =_mm_hadd_ps(c, d);
  212. __m128 res =_mm_hadd_ps(res_0, res_1);
  213. res =_mm_hadd_ps(res, res);
  214. res =_mm_hadd_ps(res, res);
  215. return _mm_cvtss_f32(res);
  216. }
  217. #endif // __AVX__ || __AVX2__ || __AVX512F__
  218. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  219. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  220. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  221. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  222. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  223. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  224. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  225. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  226. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  227. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  228. // precomputed tables for expanding 8bits to 8 bytes:
  229. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  230. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  231. #endif
  232. // reference implementation for deterministic creation of model files
  233. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int64_t k) {
  234. static const int qk = QK4_0;
  235. assert(k % qk == 0);
  236. const int nb = k / qk;
  237. for (int i = 0; i < nb; i++) {
  238. float amax = 0.0f; // absolute max
  239. float max = 0.0f;
  240. for (int j = 0; j < qk; j++) {
  241. const float v = x[i*qk + j];
  242. if (amax < fabsf(v)) {
  243. amax = fabsf(v);
  244. max = v;
  245. }
  246. }
  247. const float d = max / -8;
  248. const float id = d ? 1.0f/d : 0.0f;
  249. y[i].d = GGML_FP32_TO_FP16(d);
  250. for (int j = 0; j < qk/2; ++j) {
  251. const float x0 = x[i*qk + 0 + j]*id;
  252. const float x1 = x[i*qk + qk/2 + j]*id;
  253. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  254. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  255. y[i].qs[j] = xi0;
  256. y[i].qs[j] |= xi1 << 4;
  257. }
  258. }
  259. }
  260. void quantize_row_q4_0(const float * restrict x, void * restrict y, int64_t k) {
  261. quantize_row_q4_0_reference(x, y, k);
  262. }
  263. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int64_t k) {
  264. const int qk = QK4_1;
  265. assert(k % qk == 0);
  266. const int nb = k / qk;
  267. for (int i = 0; i < nb; i++) {
  268. float min = FLT_MAX;
  269. float max = -FLT_MAX;
  270. for (int j = 0; j < qk; j++) {
  271. const float v = x[i*qk + j];
  272. if (v < min) min = v;
  273. if (v > max) max = v;
  274. }
  275. const float d = (max - min) / ((1 << 4) - 1);
  276. const float id = d ? 1.0f/d : 0.0f;
  277. y[i].d = GGML_FP32_TO_FP16(d);
  278. y[i].m = GGML_FP32_TO_FP16(min);
  279. for (int j = 0; j < qk/2; ++j) {
  280. const float x0 = (x[i*qk + 0 + j] - min)*id;
  281. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  282. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  283. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  284. y[i].qs[j] = xi0;
  285. y[i].qs[j] |= xi1 << 4;
  286. }
  287. }
  288. }
  289. void quantize_row_q4_1(const float * restrict x, void * restrict y, int64_t k) {
  290. quantize_row_q4_1_reference(x, y, k);
  291. }
  292. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int64_t k) {
  293. static const int qk = QK5_0;
  294. assert(k % qk == 0);
  295. const int nb = k / qk;
  296. for (int i = 0; i < nb; i++) {
  297. float amax = 0.0f; // absolute max
  298. float max = 0.0f;
  299. for (int j = 0; j < qk; j++) {
  300. const float v = x[i*qk + j];
  301. if (amax < fabsf(v)) {
  302. amax = fabsf(v);
  303. max = v;
  304. }
  305. }
  306. const float d = max / -16;
  307. const float id = d ? 1.0f/d : 0.0f;
  308. y[i].d = GGML_FP32_TO_FP16(d);
  309. uint32_t qh = 0;
  310. for (int j = 0; j < qk/2; ++j) {
  311. const float x0 = x[i*qk + 0 + j]*id;
  312. const float x1 = x[i*qk + qk/2 + j]*id;
  313. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  314. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  315. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  316. // get the 5-th bit and store it in qh at the right position
  317. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  318. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  319. }
  320. memcpy(&y[i].qh, &qh, sizeof(qh));
  321. }
  322. }
  323. void quantize_row_q5_0(const float * restrict x, void * restrict y, int64_t k) {
  324. quantize_row_q5_0_reference(x, y, k);
  325. }
  326. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int64_t k) {
  327. const int qk = QK5_1;
  328. assert(k % qk == 0);
  329. const int nb = k / qk;
  330. for (int i = 0; i < nb; i++) {
  331. float min = FLT_MAX;
  332. float max = -FLT_MAX;
  333. for (int j = 0; j < qk; j++) {
  334. const float v = x[i*qk + j];
  335. if (v < min) min = v;
  336. if (v > max) max = v;
  337. }
  338. const float d = (max - min) / ((1 << 5) - 1);
  339. const float id = d ? 1.0f/d : 0.0f;
  340. y[i].d = GGML_FP32_TO_FP16(d);
  341. y[i].m = GGML_FP32_TO_FP16(min);
  342. uint32_t qh = 0;
  343. for (int j = 0; j < qk/2; ++j) {
  344. const float x0 = (x[i*qk + 0 + j] - min)*id;
  345. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  346. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  347. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  348. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  349. // get the 5-th bit and store it in qh at the right position
  350. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  351. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  352. }
  353. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  354. }
  355. }
  356. void quantize_row_q5_1(const float * restrict x, void * restrict y, int64_t k) {
  357. quantize_row_q5_1_reference(x, y, k);
  358. }
  359. // reference implementation for deterministic creation of model files
  360. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int64_t k) {
  361. assert(k % QK8_0 == 0);
  362. const int nb = k / QK8_0;
  363. for (int i = 0; i < nb; i++) {
  364. float amax = 0.0f; // absolute max
  365. for (int j = 0; j < QK8_0; j++) {
  366. const float v = x[i*QK8_0 + j];
  367. amax = MAX(amax, fabsf(v));
  368. }
  369. const float d = amax / ((1 << 7) - 1);
  370. const float id = d ? 1.0f/d : 0.0f;
  371. y[i].d = GGML_FP32_TO_FP16(d);
  372. for (int j = 0; j < QK8_0; ++j) {
  373. const float x0 = x[i*QK8_0 + j]*id;
  374. y[i].qs[j] = roundf(x0);
  375. }
  376. }
  377. }
  378. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int64_t k) {
  379. assert(QK8_0 == 32);
  380. assert(k % QK8_0 == 0);
  381. const int nb = k / QK8_0;
  382. block_q8_0 * restrict y = vy;
  383. #if defined(__ARM_NEON)
  384. for (int i = 0; i < nb; i++) {
  385. float32x4_t srcv [8];
  386. float32x4_t asrcv[8];
  387. float32x4_t amaxv[8];
  388. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  389. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  390. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  391. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  392. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  393. const float amax = vmaxvq_f32(amaxv[0]);
  394. const float d = amax / ((1 << 7) - 1);
  395. const float id = d ? 1.0f/d : 0.0f;
  396. y[i].d = GGML_FP32_TO_FP16(d);
  397. for (int j = 0; j < 8; j++) {
  398. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  399. const int32x4_t vi = vcvtnq_s32_f32(v);
  400. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  401. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  402. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  403. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  404. }
  405. }
  406. #elif defined(__wasm_simd128__)
  407. for (int i = 0; i < nb; i++) {
  408. v128_t srcv [8];
  409. v128_t asrcv[8];
  410. v128_t amaxv[8];
  411. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  412. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  413. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  414. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  415. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  416. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  417. wasm_f32x4_extract_lane(amaxv[0], 1)),
  418. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  419. wasm_f32x4_extract_lane(amaxv[0], 3)));
  420. const float d = amax / ((1 << 7) - 1);
  421. const float id = d ? 1.0f/d : 0.0f;
  422. y[i].d = GGML_FP32_TO_FP16(d);
  423. for (int j = 0; j < 8; j++) {
  424. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  425. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  426. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  427. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  428. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  429. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  430. }
  431. }
  432. #elif defined(__AVX2__) || defined(__AVX__)
  433. for (int i = 0; i < nb; i++) {
  434. // Load elements into 4 AVX vectors
  435. __m256 v0 = _mm256_loadu_ps( x );
  436. __m256 v1 = _mm256_loadu_ps( x + 8 );
  437. __m256 v2 = _mm256_loadu_ps( x + 16 );
  438. __m256 v3 = _mm256_loadu_ps( x + 24 );
  439. x += 32;
  440. // Compute max(abs(e)) for the block
  441. const __m256 signBit = _mm256_set1_ps( -0.0f );
  442. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  443. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  444. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  445. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  446. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  447. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  448. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  449. const float maxScalar = _mm_cvtss_f32( max4 );
  450. // Quantize these floats
  451. const float d = maxScalar / 127.f;
  452. y[i].d = GGML_FP32_TO_FP16(d);
  453. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  454. const __m256 mul = _mm256_set1_ps( id );
  455. // Apply the multiplier
  456. v0 = _mm256_mul_ps( v0, mul );
  457. v1 = _mm256_mul_ps( v1, mul );
  458. v2 = _mm256_mul_ps( v2, mul );
  459. v3 = _mm256_mul_ps( v3, mul );
  460. // Round to nearest integer
  461. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  462. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  463. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  464. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  465. // Convert floats to integers
  466. __m256i i0 = _mm256_cvtps_epi32( v0 );
  467. __m256i i1 = _mm256_cvtps_epi32( v1 );
  468. __m256i i2 = _mm256_cvtps_epi32( v2 );
  469. __m256i i3 = _mm256_cvtps_epi32( v3 );
  470. #if defined(__AVX2__)
  471. // Convert int32 to int16
  472. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  473. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  474. // Convert int16 to int8
  475. 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
  476. // We got our precious signed bytes, but the order is now wrong
  477. // These AVX2 pack instructions process 16-byte pieces independently
  478. // The following instruction is fixing the order
  479. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  480. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  481. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  482. #else
  483. // Since we don't have in AVX some necessary functions,
  484. // we split the registers in half and call AVX2 analogs from SSE
  485. __m128i ni0 = _mm256_castsi256_si128( i0 );
  486. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  487. __m128i ni2 = _mm256_castsi256_si128( i1 );
  488. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  489. __m128i ni4 = _mm256_castsi256_si128( i2 );
  490. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  491. __m128i ni6 = _mm256_castsi256_si128( i3 );
  492. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  493. // Convert int32 to int16
  494. ni0 = _mm_packs_epi32( ni0, ni1 );
  495. ni2 = _mm_packs_epi32( ni2, ni3 );
  496. ni4 = _mm_packs_epi32( ni4, ni5 );
  497. ni6 = _mm_packs_epi32( ni6, ni7 );
  498. // Convert int16 to int8
  499. ni0 = _mm_packs_epi16( ni0, ni2 );
  500. ni4 = _mm_packs_epi16( ni4, ni6 );
  501. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  502. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  503. #endif
  504. }
  505. #elif defined(__riscv_v_intrinsic)
  506. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  507. for (int i = 0; i < nb; i++) {
  508. // load elements
  509. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  510. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  511. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  512. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  513. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  514. const float d = amax / ((1 << 7) - 1);
  515. const float id = d ? 1.0f/d : 0.0f;
  516. y[i].d = GGML_FP32_TO_FP16(d);
  517. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  518. // convert to integer
  519. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  520. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  521. // store result
  522. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  523. }
  524. #else
  525. GGML_UNUSED(nb);
  526. // scalar
  527. quantize_row_q8_0_reference(x, y, k);
  528. #endif
  529. }
  530. // reference implementation for deterministic creation of model files
  531. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int64_t k) {
  532. assert(QK8_1 == 32);
  533. assert(k % QK8_1 == 0);
  534. const int nb = k / QK8_1;
  535. for (int i = 0; i < nb; i++) {
  536. float amax = 0.0f; // absolute max
  537. for (int j = 0; j < QK8_1; j++) {
  538. const float v = x[i*QK8_1 + j];
  539. amax = MAX(amax, fabsf(v));
  540. }
  541. const float d = amax / ((1 << 7) - 1);
  542. const float id = d ? 1.0f/d : 0.0f;
  543. y[i].d = GGML_FP32_TO_FP16(d);
  544. int sum = 0;
  545. for (int j = 0; j < QK8_1/2; ++j) {
  546. const float v0 = x[i*QK8_1 + j]*id;
  547. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  548. y[i].qs[ j] = roundf(v0);
  549. y[i].qs[QK8_1/2 + j] = roundf(v1);
  550. sum += y[i].qs[ j];
  551. sum += y[i].qs[QK8_1/2 + j];
  552. }
  553. y[i].s = GGML_FP32_TO_FP16(sum*d);
  554. }
  555. }
  556. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int64_t k) {
  557. assert(k % QK8_1 == 0);
  558. const int nb = k / QK8_1;
  559. block_q8_1 * restrict y = vy;
  560. #if defined(__ARM_NEON)
  561. for (int i = 0; i < nb; i++) {
  562. float32x4_t srcv [8];
  563. float32x4_t asrcv[8];
  564. float32x4_t amaxv[8];
  565. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  566. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  567. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  568. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  569. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  570. const float amax = vmaxvq_f32(amaxv[0]);
  571. const float d = amax / ((1 << 7) - 1);
  572. const float id = d ? 1.0f/d : 0.0f;
  573. y[i].d = GGML_FP32_TO_FP16(d);
  574. int32x4_t accv = vdupq_n_s32(0);
  575. for (int j = 0; j < 8; j++) {
  576. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  577. const int32x4_t vi = vcvtnq_s32_f32(v);
  578. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  579. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  580. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  581. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  582. accv = vaddq_s32(accv, vi);
  583. }
  584. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  585. }
  586. #elif defined(__wasm_simd128__)
  587. for (int i = 0; i < nb; i++) {
  588. v128_t srcv [8];
  589. v128_t asrcv[8];
  590. v128_t amaxv[8];
  591. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  592. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  593. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  594. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  595. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  596. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  597. wasm_f32x4_extract_lane(amaxv[0], 1)),
  598. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  599. wasm_f32x4_extract_lane(amaxv[0], 3)));
  600. const float d = amax / ((1 << 7) - 1);
  601. const float id = d ? 1.0f/d : 0.0f;
  602. y[i].d = GGML_FP32_TO_FP16(d);
  603. v128_t accv = wasm_i32x4_splat(0);
  604. for (int j = 0; j < 8; j++) {
  605. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  606. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  607. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  608. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  609. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  610. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  611. accv = wasm_i32x4_add(accv, vi);
  612. }
  613. y[i].s = GGML_FP32_TO_FP16(
  614. d * (wasm_i32x4_extract_lane(accv, 0) +
  615. wasm_i32x4_extract_lane(accv, 1) +
  616. wasm_i32x4_extract_lane(accv, 2) +
  617. wasm_i32x4_extract_lane(accv, 3)));
  618. }
  619. #elif defined(__AVX2__) || defined(__AVX__)
  620. for (int i = 0; i < nb; i++) {
  621. // Load elements into 4 AVX vectors
  622. __m256 v0 = _mm256_loadu_ps( x );
  623. __m256 v1 = _mm256_loadu_ps( x + 8 );
  624. __m256 v2 = _mm256_loadu_ps( x + 16 );
  625. __m256 v3 = _mm256_loadu_ps( x + 24 );
  626. x += 32;
  627. // Compute max(abs(e)) for the block
  628. const __m256 signBit = _mm256_set1_ps( -0.0f );
  629. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  630. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  631. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  632. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  633. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  634. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  635. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  636. const float maxScalar = _mm_cvtss_f32( max4 );
  637. // Quantize these floats
  638. const float d = maxScalar / 127.f;
  639. y[i].d = GGML_FP32_TO_FP16(d);
  640. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  641. const __m256 mul = _mm256_set1_ps( id );
  642. // Apply the multiplier
  643. v0 = _mm256_mul_ps( v0, mul );
  644. v1 = _mm256_mul_ps( v1, mul );
  645. v2 = _mm256_mul_ps( v2, mul );
  646. v3 = _mm256_mul_ps( v3, mul );
  647. // Round to nearest integer
  648. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  649. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  650. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  651. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  652. // Convert floats to integers
  653. __m256i i0 = _mm256_cvtps_epi32( v0 );
  654. __m256i i1 = _mm256_cvtps_epi32( v1 );
  655. __m256i i2 = _mm256_cvtps_epi32( v2 );
  656. __m256i i3 = _mm256_cvtps_epi32( v3 );
  657. #if defined(__AVX2__)
  658. // Compute the sum of the quants and set y[i].s
  659. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  660. // Convert int32 to int16
  661. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  662. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  663. // Convert int16 to int8
  664. 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
  665. // We got our precious signed bytes, but the order is now wrong
  666. // These AVX2 pack instructions process 16-byte pieces independently
  667. // The following instruction is fixing the order
  668. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  669. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  670. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  671. #else
  672. // Since we don't have in AVX some necessary functions,
  673. // we split the registers in half and call AVX2 analogs from SSE
  674. __m128i ni0 = _mm256_castsi256_si128( i0 );
  675. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  676. __m128i ni2 = _mm256_castsi256_si128( i1 );
  677. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  678. __m128i ni4 = _mm256_castsi256_si128( i2 );
  679. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  680. __m128i ni6 = _mm256_castsi256_si128( i3 );
  681. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  682. // Compute the sum of the quants and set y[i].s
  683. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  684. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  685. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  686. // Convert int32 to int16
  687. ni0 = _mm_packs_epi32( ni0, ni1 );
  688. ni2 = _mm_packs_epi32( ni2, ni3 );
  689. ni4 = _mm_packs_epi32( ni4, ni5 );
  690. ni6 = _mm_packs_epi32( ni6, ni7 );
  691. // Convert int16 to int8
  692. ni0 = _mm_packs_epi16( ni0, ni2 );
  693. ni4 = _mm_packs_epi16( ni4, ni6 );
  694. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  695. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  696. #endif
  697. }
  698. #elif defined(__riscv_v_intrinsic)
  699. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  700. for (int i = 0; i < nb; i++) {
  701. // load elements
  702. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  703. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  704. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  705. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  706. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  707. const float d = amax / ((1 << 7) - 1);
  708. const float id = d ? 1.0f/d : 0.0f;
  709. y[i].d = GGML_FP32_TO_FP16(d);
  710. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  711. // convert to integer
  712. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  713. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  714. // store result
  715. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  716. // compute sum for y[i].s
  717. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  718. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  719. // set y[i].s
  720. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  721. y[i].s = GGML_FP32_TO_FP16(sum*d);
  722. }
  723. #else
  724. GGML_UNUSED(nb);
  725. // scalar
  726. quantize_row_q8_1_reference(x, y, k);
  727. #endif
  728. }
  729. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int64_t k) {
  730. static const int qk = QK4_0;
  731. assert(k % qk == 0);
  732. const int nb = k / qk;
  733. for (int i = 0; i < nb; i++) {
  734. const float d = GGML_FP16_TO_FP32(x[i].d);
  735. for (int j = 0; j < qk/2; ++j) {
  736. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  737. const int x1 = (x[i].qs[j] >> 4) - 8;
  738. y[i*qk + j + 0 ] = x0*d;
  739. y[i*qk + j + qk/2] = x1*d;
  740. }
  741. }
  742. }
  743. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int64_t k) {
  744. static const int qk = QK4_1;
  745. assert(k % qk == 0);
  746. const int nb = k / qk;
  747. for (int i = 0; i < nb; i++) {
  748. const float d = GGML_FP16_TO_FP32(x[i].d);
  749. const float m = GGML_FP16_TO_FP32(x[i].m);
  750. for (int j = 0; j < qk/2; ++j) {
  751. const int x0 = (x[i].qs[j] & 0x0F);
  752. const int x1 = (x[i].qs[j] >> 4);
  753. y[i*qk + j + 0 ] = x0*d + m;
  754. y[i*qk + j + qk/2] = x1*d + m;
  755. }
  756. }
  757. }
  758. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int64_t k) {
  759. static const int qk = QK5_0;
  760. assert(k % qk == 0);
  761. const int nb = k / qk;
  762. for (int i = 0; i < nb; i++) {
  763. const float d = GGML_FP16_TO_FP32(x[i].d);
  764. uint32_t qh;
  765. memcpy(&qh, x[i].qh, sizeof(qh));
  766. for (int j = 0; j < qk/2; ++j) {
  767. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  768. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  769. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  770. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  771. y[i*qk + j + 0 ] = x0*d;
  772. y[i*qk + j + qk/2] = x1*d;
  773. }
  774. }
  775. }
  776. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int64_t k) {
  777. static const int qk = QK5_1;
  778. assert(k % qk == 0);
  779. const int nb = k / qk;
  780. for (int i = 0; i < nb; i++) {
  781. const float d = GGML_FP16_TO_FP32(x[i].d);
  782. const float m = GGML_FP16_TO_FP32(x[i].m);
  783. uint32_t qh;
  784. memcpy(&qh, x[i].qh, sizeof(qh));
  785. for (int j = 0; j < qk/2; ++j) {
  786. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  787. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  788. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  789. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  790. y[i*qk + j + 0 ] = x0*d + m;
  791. y[i*qk + j + qk/2] = x1*d + m;
  792. }
  793. }
  794. }
  795. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int64_t k) {
  796. static const int qk = QK8_0;
  797. assert(k % qk == 0);
  798. const int nb = k / qk;
  799. for (int i = 0; i < nb; i++) {
  800. const float d = GGML_FP16_TO_FP32(x[i].d);
  801. for (int j = 0; j < qk; ++j) {
  802. y[i*qk + j] = x[i].qs[j]*d;
  803. }
  804. }
  805. }
  806. //
  807. // 2-6 bit quantization in super-blocks
  808. //
  809. //
  810. // ===================== Helper functions
  811. //
  812. static inline int nearest_int(float fval) {
  813. assert(fval <= 4194303.f);
  814. float val = fval + 12582912.f;
  815. int i; memcpy(&i, &val, sizeof(int));
  816. return (i & 0x007fffff) - 0x00400000;
  817. }
  818. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  819. const float * restrict qw) {
  820. float max = 0;
  821. float amax = 0;
  822. for (int i = 0; i < n; ++i) {
  823. float ax = fabsf(x[i]);
  824. if (ax > amax) { amax = ax; max = x[i]; }
  825. }
  826. if (amax < 1e-30f) { // all zero
  827. for (int i = 0; i < n; ++i) {
  828. L[i] = 0;
  829. }
  830. return 0.f;
  831. }
  832. float iscale = -nmax / max;
  833. if (rmse_type == 0) {
  834. for (int i = 0; i < n; ++i) {
  835. int l = nearest_int(iscale * x[i]);
  836. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  837. }
  838. return 1/iscale;
  839. }
  840. bool return_early = false;
  841. if (rmse_type < 0) {
  842. rmse_type = -rmse_type;
  843. return_early = true;
  844. }
  845. float sumlx = 0;
  846. float suml2 = 0;
  847. #ifdef HAVE_BUGGY_APPLE_LINKER
  848. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  849. for (volatile int i = 0; i < n; ++i) {
  850. #else
  851. for (int i = 0; i < n; ++i) {
  852. #endif
  853. int l = nearest_int(iscale * x[i]);
  854. l = MAX(-nmax, MIN(nmax-1, l));
  855. L[i] = l + nmax;
  856. 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]));
  857. sumlx += w*x[i]*l;
  858. suml2 += w*l*l;
  859. }
  860. float scale = sumlx/suml2;
  861. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  862. float best = scale * sumlx;
  863. for (int is = -9; is <= 9; ++is) {
  864. if (is == 0) {
  865. continue;
  866. }
  867. iscale = -(nmax + 0.1f*is) / max;
  868. sumlx = suml2 = 0;
  869. for (int i = 0; i < n; ++i) {
  870. int l = nearest_int(iscale * x[i]);
  871. l = MAX(-nmax, MIN(nmax-1, l));
  872. 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]));
  873. sumlx += w*x[i]*l;
  874. suml2 += w*l*l;
  875. }
  876. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  877. for (int i = 0; i < n; ++i) {
  878. int l = nearest_int(iscale * x[i]);
  879. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  880. }
  881. scale = sumlx/suml2; best = scale*sumlx;
  882. }
  883. }
  884. return scale;
  885. }
  886. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  887. float max = 0;
  888. float amax = 0;
  889. for (int i = 0; i < n; ++i) {
  890. float ax = fabsf(x[i]);
  891. if (ax > amax) { amax = ax; max = x[i]; }
  892. }
  893. if (!amax) { // all zero
  894. for (int i = 0; i < n; ++i) { L[i] = 0; }
  895. return 0.f;
  896. }
  897. float iscale = -nmax / max;
  898. if (do_rmse) {
  899. float sumlx = 0;
  900. float suml2 = 0;
  901. for (int i = 0; i < n; ++i) {
  902. int l = nearest_int(iscale * x[i]);
  903. l = MAX(-nmax, MIN(nmax-1, l));
  904. L[i] = l;
  905. float w = x[i]*x[i];
  906. sumlx += w*x[i]*l;
  907. suml2 += w*l*l;
  908. }
  909. for (int itry = 0; itry < 5; ++itry) {
  910. int n_changed = 0;
  911. for (int i = 0; i < n; ++i) {
  912. float w = x[i]*x[i];
  913. float slx = sumlx - w*x[i]*L[i];
  914. if (slx > 0) {
  915. float sl2 = suml2 - w*L[i]*L[i];
  916. int new_l = nearest_int(x[i] * sl2 / slx);
  917. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  918. if (new_l != L[i]) {
  919. slx += w*x[i]*new_l;
  920. sl2 += w*new_l*new_l;
  921. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  922. L[i] = new_l; sumlx = slx; suml2 = sl2;
  923. ++n_changed;
  924. }
  925. }
  926. }
  927. }
  928. if (!n_changed) {
  929. break;
  930. }
  931. }
  932. for (int i = 0; i < n; ++i) {
  933. L[i] += nmax;
  934. }
  935. return sumlx / suml2;
  936. }
  937. for (int i = 0; i < n; ++i) {
  938. int l = nearest_int(iscale * x[i]);
  939. l = MAX(-nmax, MIN(nmax-1, l));
  940. L[i] = l + nmax;
  941. }
  942. return 1/iscale;
  943. }
  944. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  945. int ntry, float alpha) {
  946. float min = x[0];
  947. float max = x[0];
  948. for (int i = 1; i < n; ++i) {
  949. if (x[i] < min) min = x[i];
  950. if (x[i] > max) max = x[i];
  951. }
  952. if (max == min) {
  953. for (int i = 0; i < n; ++i) L[i] = 0;
  954. *the_min = 0;
  955. return 0.f;
  956. }
  957. if (min > 0) min = 0;
  958. float iscale = nmax/(max - min);
  959. float scale = 1/iscale;
  960. for (int itry = 0; itry < ntry; ++itry) {
  961. float sumlx = 0; int suml2 = 0;
  962. bool did_change = false;
  963. for (int i = 0; i < n; ++i) {
  964. int l = nearest_int(iscale*(x[i] - min));
  965. l = MAX(0, MIN(nmax, l));
  966. if (l != L[i]) {
  967. L[i] = l;
  968. did_change = true;
  969. }
  970. sumlx += (x[i] - min)*l;
  971. suml2 += l*l;
  972. }
  973. scale = sumlx/suml2;
  974. float sum = 0;
  975. for (int i = 0; i < n; ++i) {
  976. sum += x[i] - scale*L[i];
  977. }
  978. min = alpha*min + (1 - alpha)*sum/n;
  979. if (min > 0) min = 0;
  980. iscale = 1/scale;
  981. if (!did_change) break;
  982. }
  983. *the_min = -min;
  984. return scale;
  985. }
  986. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  987. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  988. float rmin, float rdelta, int nstep, bool use_mad) {
  989. float min = x[0];
  990. float max = x[0];
  991. float sum_w = weights[0];
  992. float sum_x = sum_w * x[0];
  993. #ifdef HAVE_BUGGY_APPLE_LINKER
  994. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  995. for (volatile int i = 1; i < n; ++i) {
  996. #else
  997. for (int i = 1; i < n; ++i) {
  998. #endif
  999. if (x[i] < min) min = x[i];
  1000. if (x[i] > max) max = x[i];
  1001. float w = weights[i];
  1002. sum_w += w;
  1003. sum_x += w * x[i];
  1004. }
  1005. if (min > 0) min = 0;
  1006. if (max == min) {
  1007. for (int i = 0; i < n; ++i) L[i] = 0;
  1008. *the_min = -min;
  1009. return 0.f;
  1010. }
  1011. float iscale = nmax/(max - min);
  1012. float scale = 1/iscale;
  1013. float best_mad = 0;
  1014. for (int i = 0; i < n; ++i) {
  1015. int l = nearest_int(iscale*(x[i] - min));
  1016. L[i] = MAX(0, MIN(nmax, l));
  1017. float diff = scale * L[i] + min - x[i];
  1018. diff = use_mad ? fabsf(diff) : diff * diff;
  1019. float w = weights[i];
  1020. best_mad += w * diff;
  1021. }
  1022. if (nstep < 1) {
  1023. *the_min = -min;
  1024. return scale;
  1025. }
  1026. for (int is = 0; is <= nstep; ++is) {
  1027. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1028. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1029. for (int i = 0; i < n; ++i) {
  1030. int l = nearest_int(iscale*(x[i] - min));
  1031. l = MAX(0, MIN(nmax, l));
  1032. Laux[i] = l;
  1033. float w = weights[i];
  1034. sum_l += w*l;
  1035. sum_l2 += w*l*l;
  1036. sum_xl += w*l*x[i];
  1037. }
  1038. float D = sum_w * sum_l2 - sum_l * sum_l;
  1039. if (D > 0) {
  1040. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1041. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1042. if (this_min > 0) {
  1043. this_min = 0;
  1044. this_scale = sum_xl / sum_l2;
  1045. }
  1046. float mad = 0;
  1047. for (int i = 0; i < n; ++i) {
  1048. float diff = this_scale * Laux[i] + this_min - x[i];
  1049. diff = use_mad ? fabsf(diff) : diff * diff;
  1050. float w = weights[i];
  1051. mad += w * diff;
  1052. }
  1053. if (mad < best_mad) {
  1054. for (int i = 0; i < n; ++i) {
  1055. L[i] = Laux[i];
  1056. }
  1057. best_mad = mad;
  1058. scale = this_scale;
  1059. min = this_min;
  1060. }
  1061. }
  1062. }
  1063. *the_min = -min;
  1064. return scale;
  1065. }
  1066. #if QK_K == 256
  1067. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1068. if (j < 4) {
  1069. *d = q[j] & 63; *m = q[j + 4] & 63;
  1070. } else {
  1071. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1072. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1073. }
  1074. }
  1075. #endif
  1076. //========================- 2-bit (de)-quantization
  1077. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int64_t k) {
  1078. assert(k % QK_K == 0);
  1079. const int nb = k / QK_K;
  1080. uint8_t L[QK_K];
  1081. uint8_t Laux[16];
  1082. float weights[16];
  1083. float mins[QK_K/16];
  1084. float scales[QK_K/16];
  1085. const float q4scale = 15.f;
  1086. for (int i = 0; i < nb; i++) {
  1087. float max_scale = 0; // as we are deducting the min, scales are always positive
  1088. float max_min = 0;
  1089. for (int j = 0; j < QK_K/16; ++j) {
  1090. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1091. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1092. float scale = scales[j];
  1093. if (scale > max_scale) {
  1094. max_scale = scale;
  1095. }
  1096. float min = mins[j];
  1097. if (min > max_min) {
  1098. max_min = min;
  1099. }
  1100. }
  1101. if (max_scale > 0) {
  1102. float iscale = q4scale/max_scale;
  1103. for (int j = 0; j < QK_K/16; ++j) {
  1104. int l = nearest_int(iscale*scales[j]);
  1105. y[i].scales[j] = l;
  1106. }
  1107. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1108. } else {
  1109. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1110. y[i].d = GGML_FP32_TO_FP16(0.f);
  1111. }
  1112. if (max_min > 0) {
  1113. float iscale = q4scale/max_min;
  1114. for (int j = 0; j < QK_K/16; ++j) {
  1115. int l = nearest_int(iscale*mins[j]);
  1116. y[i].scales[j] |= (l << 4);
  1117. }
  1118. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1119. } else {
  1120. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1121. }
  1122. for (int j = 0; j < QK_K/16; ++j) {
  1123. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1124. if (!d) continue;
  1125. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1126. for (int ii = 0; ii < 16; ++ii) {
  1127. int l = nearest_int((x[16*j + ii] + dm)/d);
  1128. l = MAX(0, MIN(3, l));
  1129. L[16*j + ii] = l;
  1130. }
  1131. }
  1132. #if QK_K == 256
  1133. for (int j = 0; j < QK_K; j += 128) {
  1134. for (int l = 0; l < 32; ++l) {
  1135. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1136. }
  1137. }
  1138. #else
  1139. for (int l = 0; l < 16; ++l) {
  1140. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1141. }
  1142. #endif
  1143. x += QK_K;
  1144. }
  1145. }
  1146. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int64_t k) {
  1147. assert(k % QK_K == 0);
  1148. const int nb = k / QK_K;
  1149. for (int i = 0; i < nb; i++) {
  1150. const float d = GGML_FP16_TO_FP32(x[i].d);
  1151. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1152. const uint8_t * q = x[i].qs;
  1153. #if QK_K == 256
  1154. int is = 0;
  1155. float dl, ml;
  1156. for (int n = 0; n < QK_K; n += 128) {
  1157. int shift = 0;
  1158. for (int j = 0; j < 4; ++j) {
  1159. uint8_t sc = x[i].scales[is++];
  1160. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1161. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1162. sc = x[i].scales[is++];
  1163. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1164. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1165. shift += 2;
  1166. }
  1167. q += 32;
  1168. }
  1169. #else
  1170. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1171. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1172. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1173. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1174. for (int l = 0; l < 16; ++l) {
  1175. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1176. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1177. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1178. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1179. }
  1180. y += QK_K;
  1181. #endif
  1182. }
  1183. }
  1184. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int64_t k) {
  1185. quantize_row_q2_K_reference(x, vy, k);
  1186. }
  1187. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1188. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1189. float rmin, float rdelta, int nstep, bool use_mad) {
  1190. float min = x[0];
  1191. float max = x[0];
  1192. float sum_w = weights ? weights[0] : x[0]*x[0];
  1193. float sum_x = sum_w * x[0];
  1194. #ifdef HAVE_BUGGY_APPLE_LINKER
  1195. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1196. for (volatile int i = 1; i < n; ++i) {
  1197. #else
  1198. for (int i = 1; i < n; ++i) {
  1199. #endif
  1200. if (x[i] < min) min = x[i];
  1201. if (x[i] > max) max = x[i];
  1202. float w = weights ? weights[i] : x[i]*x[i];
  1203. sum_w += w;
  1204. sum_x += w * x[i];
  1205. }
  1206. if (min > 0) {
  1207. min = 0;
  1208. }
  1209. if (max <= min) {
  1210. memset(L, 0, n);
  1211. *the_min = -min;
  1212. return 0.f;
  1213. }
  1214. float iscale = nmax/(max - min);
  1215. float scale = 1/iscale;
  1216. float best_mad = 0;
  1217. for (int i = 0; i < n; ++i) {
  1218. int l = nearest_int(iscale*(x[i] - min));
  1219. L[i] = MAX(0, MIN(nmax, l));
  1220. float diff = scale * L[i] + min - x[i];
  1221. diff = use_mad ? fabsf(diff) : diff*diff;
  1222. float w = weights ? weights[i] : x[i]*x[i];
  1223. best_mad += w * diff;
  1224. }
  1225. if (nstep < 1) {
  1226. *the_min = -min;
  1227. return scale;
  1228. }
  1229. for (int is = 0; is <= nstep; ++is) {
  1230. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1231. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1232. for (int i = 0; i < n; ++i) {
  1233. int l = nearest_int(iscale*(x[i] - min));
  1234. l = MAX(0, MIN(nmax, l));
  1235. Laux[i] = l;
  1236. float w = weights ? weights[i] : x[i]*x[i];
  1237. sum_l += w*l;
  1238. sum_l2 += w*l*l;
  1239. sum_xl += w*l*x[i];
  1240. }
  1241. float D = sum_w * sum_l2 - sum_l * sum_l;
  1242. if (D > 0) {
  1243. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1244. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1245. if (this_min > 0) {
  1246. this_min = 0;
  1247. this_scale = sum_xl / sum_l2;
  1248. }
  1249. float mad = 0;
  1250. for (int i = 0; i < n; ++i) {
  1251. float diff = this_scale * Laux[i] + this_min - x[i];
  1252. diff = use_mad ? fabsf(diff) : diff*diff;
  1253. float w = weights ? weights[i] : x[i]*x[i];
  1254. mad += w * diff;
  1255. }
  1256. if (mad < best_mad) {
  1257. for (int i = 0; i < n; ++i) {
  1258. L[i] = Laux[i];
  1259. }
  1260. best_mad = mad;
  1261. scale = this_scale;
  1262. min = this_min;
  1263. }
  1264. }
  1265. }
  1266. *the_min = -min;
  1267. return scale;
  1268. }
  1269. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1270. float max = 0;
  1271. for (int i = 0; i < n; ++i) {
  1272. max = MAX(max, x[i]);
  1273. }
  1274. if (!max) { // all zero
  1275. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1276. return 0.f;
  1277. }
  1278. float iscale = nmax / max;
  1279. for (int i = 0; i < n; ++i) {
  1280. L[i] = nearest_int(iscale * x[i]);
  1281. }
  1282. float scale = 1/iscale;
  1283. float best_mse = 0;
  1284. for (int i = 0; i < n; ++i) {
  1285. float diff = x[i] - scale*L[i];
  1286. float w = quant_weights[i];
  1287. best_mse += w*diff*diff;
  1288. }
  1289. for (int is = -4; is <= 4; ++is) {
  1290. if (is == 0) continue;
  1291. float iscale_is = (0.1f*is + nmax)/max;
  1292. float scale_is = 1/iscale_is;
  1293. float mse = 0;
  1294. for (int i = 0; i < n; ++i) {
  1295. int l = nearest_int(iscale_is*x[i]);
  1296. l = MIN(nmax, l);
  1297. float diff = x[i] - scale_is*l;
  1298. float w = quant_weights[i];
  1299. mse += w*diff*diff;
  1300. }
  1301. if (mse < best_mse) {
  1302. best_mse = mse;
  1303. iscale = iscale_is;
  1304. }
  1305. }
  1306. float sumlx = 0;
  1307. float suml2 = 0;
  1308. for (int i = 0; i < n; ++i) {
  1309. int l = nearest_int(iscale * x[i]);
  1310. l = MIN(nmax, l);
  1311. L[i] = l;
  1312. float w = quant_weights[i];
  1313. sumlx += w*x[i]*l;
  1314. suml2 += w*l*l;
  1315. }
  1316. for (int itry = 0; itry < 5; ++itry) {
  1317. int n_changed = 0;
  1318. for (int i = 0; i < n; ++i) {
  1319. float w = quant_weights[i];
  1320. float slx = sumlx - w*x[i]*L[i];
  1321. float sl2 = suml2 - w*L[i]*L[i];
  1322. if (slx > 0 && sl2 > 0) {
  1323. int new_l = nearest_int(x[i] * sl2 / slx);
  1324. new_l = MIN(nmax, new_l);
  1325. if (new_l != L[i]) {
  1326. slx += w*x[i]*new_l;
  1327. sl2 += w*new_l*new_l;
  1328. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1329. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1330. ++n_changed;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. if (!n_changed) {
  1336. break;
  1337. }
  1338. }
  1339. return sumlx / suml2;
  1340. }
  1341. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1342. GGML_ASSERT(quant_weights);
  1343. assert(k % QK_K == 0);
  1344. const int nb = k / QK_K;
  1345. const bool requantize = true;
  1346. uint8_t L[QK_K];
  1347. uint8_t Laux[16];
  1348. float mins[QK_K/16];
  1349. float scales[QK_K/16];
  1350. float sw[QK_K/16];
  1351. float weight[16];
  1352. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1353. for (int i = 0; i < nb; i++) {
  1354. memset(sw, 0, QK_K/16*sizeof(float));
  1355. float sumx2 = 0;
  1356. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1357. float sigma2 = sumx2/QK_K;
  1358. for (int j = 0; j < QK_K/16; ++j) {
  1359. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1360. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1361. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1362. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1363. }
  1364. float dm, mm;
  1365. #if QK_K == 64
  1366. float max_scale = 0, max_min = 0;
  1367. for (int j = 0; j < QK_K/16; ++j) {
  1368. max_scale = MAX(max_scale, scales[j]);
  1369. max_min = MAX(max_min, mins[j]);
  1370. }
  1371. dm = max_scale/15;
  1372. mm = max_min/15;
  1373. if (max_scale) {
  1374. float id = 1/dm;
  1375. for (int j = 0; j < QK_K/16; ++j) {
  1376. int l = nearest_int(id*scales[j]);
  1377. Ls[j] = MAX(0, MIN(15, l));
  1378. }
  1379. } else {
  1380. memset(Ls, 0, QK_K/16);
  1381. }
  1382. if (max_min) {
  1383. float id = 1/mm;
  1384. for (int j = 0; j < QK_K/16; ++j) {
  1385. int l = nearest_int(id*mins[j]);
  1386. Lm[j] = MAX(0, MIN(15, l));
  1387. }
  1388. } else {
  1389. memset(Lm, 0, QK_K/16);
  1390. }
  1391. #else
  1392. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1393. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1394. #endif
  1395. y[i].d = GGML_FP32_TO_FP16(dm);
  1396. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1397. dm = GGML_FP16_TO_FP32(y[i].d);
  1398. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1399. for (int j = 0; j < QK_K/16; ++j) {
  1400. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1401. }
  1402. if (requantize) {
  1403. for (int j = 0; j < QK_K/16; ++j) {
  1404. const float d = dm * (y[i].scales[j] & 0xF);
  1405. if (!d) continue;
  1406. const float m = mm * (y[i].scales[j] >> 4);
  1407. for (int ii = 0; ii < 16; ++ii) {
  1408. int l = nearest_int((x[16*j + ii] + m)/d);
  1409. l = MAX(0, MIN(3, l));
  1410. L[16*j + ii] = l;
  1411. }
  1412. }
  1413. }
  1414. #if QK_K == 256
  1415. for (int j = 0; j < QK_K; j += 128) {
  1416. for (int l = 0; l < 32; ++l) {
  1417. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1418. }
  1419. }
  1420. #else
  1421. for (int l = 0; l < 16; ++l) {
  1422. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1423. }
  1424. #endif
  1425. x += QK_K;
  1426. }
  1427. }
  1428. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1429. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1430. if (!quant_weights) {
  1431. quantize_row_q2_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1432. }
  1433. else {
  1434. char * qrow = (char *)dst;
  1435. for (int64_t row = 0; row < nrow; ++row) {
  1436. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1437. src += n_per_row;
  1438. qrow += row_size;
  1439. }
  1440. }
  1441. return nrow * row_size;
  1442. }
  1443. //========================= 3-bit (de)-quantization
  1444. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int64_t k) {
  1445. assert(k % QK_K == 0);
  1446. const int nb = k / QK_K;
  1447. int8_t L[QK_K];
  1448. float scales[QK_K / 16];
  1449. for (int i = 0; i < nb; i++) {
  1450. float max_scale = 0;
  1451. float amax = 0;
  1452. for (int j = 0; j < QK_K/16; ++j) {
  1453. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1454. float scale = fabsf(scales[j]);
  1455. if (scale > amax) {
  1456. amax = scale; max_scale = scales[j];
  1457. }
  1458. }
  1459. #if QK_K == 256
  1460. memset(y[i].scales, 0, 12);
  1461. if (max_scale) {
  1462. float iscale = -32.f/max_scale;
  1463. for (int j = 0; j < QK_K/16; ++j) {
  1464. int8_t l = nearest_int(iscale*scales[j]);
  1465. l = MAX(-32, MIN(31, l)) + 32;
  1466. if (j < 8) {
  1467. y[i].scales[j] = l & 0xF;
  1468. } else {
  1469. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1470. }
  1471. l >>= 4;
  1472. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1473. }
  1474. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1475. } else {
  1476. y[i].d = GGML_FP32_TO_FP16(0.f);
  1477. }
  1478. int8_t sc;
  1479. for (int j = 0; j < QK_K/16; ++j) {
  1480. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1481. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1482. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1483. if (!d) {
  1484. continue;
  1485. }
  1486. for (int ii = 0; ii < 16; ++ii) {
  1487. int l = nearest_int(x[16*j + ii]/d);
  1488. l = MAX(-4, MIN(3, l));
  1489. L[16*j + ii] = l + 4;
  1490. }
  1491. }
  1492. #else
  1493. if (max_scale) {
  1494. float iscale = -8.f/max_scale;
  1495. for (int j = 0; j < QK_K/16; j+=2) {
  1496. int l1 = nearest_int(iscale*scales[j]);
  1497. l1 = 8 + MAX(-8, MIN(7, l1));
  1498. int l2 = nearest_int(iscale*scales[j+1]);
  1499. l2 = 8 + MAX(-8, MIN(7, l2));
  1500. y[i].scales[j/2] = l1 | (l2 << 4);
  1501. }
  1502. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1503. } else {
  1504. for (int j = 0; j < QK_K/16; j+=2) {
  1505. y[i].scales[j/2] = 0;
  1506. }
  1507. y[i].d = GGML_FP32_TO_FP16(0.f);
  1508. }
  1509. for (int j = 0; j < QK_K/16; ++j) {
  1510. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1511. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1512. if (!d) {
  1513. continue;
  1514. }
  1515. for (int ii = 0; ii < 16; ++ii) {
  1516. int l = nearest_int(x[16*j + ii]/d);
  1517. l = MAX(-4, MIN(3, l));
  1518. L[16*j + ii] = l + 4;
  1519. }
  1520. }
  1521. #endif
  1522. memset(y[i].hmask, 0, QK_K/8);
  1523. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1524. int m = 0;
  1525. uint8_t hm = 1;
  1526. for (int j = 0; j < QK_K; ++j) {
  1527. if (L[j] > 3) {
  1528. y[i].hmask[m] |= hm;
  1529. L[j] -= 4;
  1530. }
  1531. if (++m == QK_K/8) {
  1532. m = 0; hm <<= 1;
  1533. }
  1534. }
  1535. #if QK_K == 256
  1536. for (int j = 0; j < QK_K; j += 128) {
  1537. for (int l = 0; l < 32; ++l) {
  1538. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1539. }
  1540. }
  1541. #else
  1542. for (int l = 0; l < 16; ++l) {
  1543. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1544. }
  1545. #endif
  1546. x += QK_K;
  1547. }
  1548. }
  1549. #if QK_K == 256
  1550. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1551. assert(k % QK_K == 0);
  1552. const int nb = k / QK_K;
  1553. const uint32_t kmask1 = 0x03030303;
  1554. const uint32_t kmask2 = 0x0f0f0f0f;
  1555. uint32_t aux[4];
  1556. const int8_t * scales = (const int8_t*)aux;
  1557. for (int i = 0; i < nb; i++) {
  1558. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1559. const uint8_t * restrict q = x[i].qs;
  1560. const uint8_t * restrict hm = x[i].hmask;
  1561. uint8_t m = 1;
  1562. memcpy(aux, x[i].scales, 12);
  1563. uint32_t tmp = aux[2];
  1564. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1565. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1566. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1567. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1568. int is = 0;
  1569. float dl;
  1570. for (int n = 0; n < QK_K; n += 128) {
  1571. int shift = 0;
  1572. for (int j = 0; j < 4; ++j) {
  1573. dl = d_all * (scales[is++] - 32);
  1574. for (int l = 0; l < 16; ++l) {
  1575. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1576. }
  1577. dl = d_all * (scales[is++] - 32);
  1578. for (int l = 0; l < 16; ++l) {
  1579. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1580. }
  1581. shift += 2;
  1582. m <<= 1;
  1583. }
  1584. q += 32;
  1585. }
  1586. }
  1587. }
  1588. #else
  1589. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1590. assert(k % QK_K == 0);
  1591. assert(QK_K == 64);
  1592. const int nb = k / QK_K;
  1593. for (int i = 0; i < nb; i++) {
  1594. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1595. const uint8_t * restrict q = x[i].qs;
  1596. const uint8_t * restrict hm = x[i].hmask;
  1597. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1598. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1599. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1600. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1601. for (int l=0; l<8; ++l) {
  1602. uint8_t h = hm[l];
  1603. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1604. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1605. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1606. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1607. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1608. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1609. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1610. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1611. }
  1612. y += QK_K;
  1613. }
  1614. }
  1615. #endif
  1616. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int64_t k) {
  1617. quantize_row_q3_K_reference(x, vy, k);
  1618. }
  1619. 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) {
  1620. #if QK_K != 256
  1621. (void)quant_weights;
  1622. quantize_row_q3_K_reference(x, y, n_per_row);
  1623. #else
  1624. assert(n_per_row % QK_K == 0);
  1625. const int nb = n_per_row / QK_K;
  1626. int8_t L[QK_K];
  1627. float scales[QK_K / 16];
  1628. float weight[16];
  1629. float sw[QK_K / 16];
  1630. int8_t Ls[QK_K / 16];
  1631. for (int i = 0; i < nb; i++) {
  1632. float sumx2 = 0;
  1633. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1634. float sigma2 = 2*sumx2/QK_K;
  1635. for (int j = 0; j < QK_K/16; ++j) {
  1636. if (quant_weights) {
  1637. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1638. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1639. } else {
  1640. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1641. }
  1642. float sumw = 0;
  1643. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1644. sw[j] = sumw;
  1645. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1646. }
  1647. memset(y[i].scales, 0, 12);
  1648. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1649. for (int j = 0; j < QK_K/16; ++j) {
  1650. int l = Ls[j];
  1651. if (j < 8) {
  1652. y[i].scales[j] = l & 0xF;
  1653. } else {
  1654. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1655. }
  1656. l >>= 4;
  1657. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1658. }
  1659. y[i].d = GGML_FP32_TO_FP16(d_block);
  1660. int8_t sc;
  1661. for (int j = 0; j < QK_K/16; ++j) {
  1662. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1663. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1664. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1665. if (!d) {
  1666. continue;
  1667. }
  1668. for (int ii = 0; ii < 16; ++ii) {
  1669. int l = nearest_int(x[16*j + ii]/d);
  1670. l = MAX(-4, MIN(3, l));
  1671. L[16*j + ii] = l + 4;
  1672. }
  1673. }
  1674. memset(y[i].hmask, 0, QK_K/8);
  1675. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1676. int m = 0;
  1677. uint8_t hm = 1;
  1678. for (int j = 0; j < QK_K; ++j) {
  1679. if (L[j] > 3) {
  1680. y[i].hmask[m] |= hm;
  1681. L[j] -= 4;
  1682. }
  1683. if (++m == QK_K/8) {
  1684. m = 0; hm <<= 1;
  1685. }
  1686. }
  1687. for (int j = 0; j < QK_K; j += 128) {
  1688. for (int l = 0; l < 32; ++l) {
  1689. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1690. }
  1691. }
  1692. x += QK_K;
  1693. }
  1694. #endif
  1695. }
  1696. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1697. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1698. if (!quant_weights) {
  1699. quantize_row_q3_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1700. }
  1701. else {
  1702. char * qrow = (char *)dst;
  1703. for (int64_t row = 0; row < nrow; ++row) {
  1704. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1705. src += n_per_row;
  1706. qrow += row_size;
  1707. }
  1708. }
  1709. return nrow * row_size;
  1710. }
  1711. // ====================== 4-bit (de)-quantization
  1712. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int64_t k) {
  1713. assert(k % QK_K == 0);
  1714. const int nb = k / QK_K;
  1715. uint8_t L[QK_K];
  1716. uint8_t Laux[32];
  1717. float weights[32];
  1718. float mins[QK_K/32];
  1719. float scales[QK_K/32];
  1720. for (int i = 0; i < nb; i++) {
  1721. float max_scale = 0; // as we are deducting the min, scales are always positive
  1722. float max_min = 0;
  1723. for (int j = 0; j < QK_K/32; ++j) {
  1724. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1725. float sum_x2 = 0;
  1726. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1727. float av_x = sqrtf(sum_x2/32);
  1728. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1729. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1730. float scale = scales[j];
  1731. if (scale > max_scale) {
  1732. max_scale = scale;
  1733. }
  1734. float min = mins[j];
  1735. if (min > max_min) {
  1736. max_min = min;
  1737. }
  1738. }
  1739. #if QK_K == 256
  1740. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1741. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1742. for (int j = 0; j < QK_K/32; ++j) {
  1743. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1744. uint8_t lm = nearest_int(inv_min*mins[j]);
  1745. ls = MIN(63, ls);
  1746. lm = MIN(63, lm);
  1747. if (j < 4) {
  1748. y[i].scales[j] = ls;
  1749. y[i].scales[j+4] = lm;
  1750. } else {
  1751. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1752. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1753. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1754. }
  1755. }
  1756. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1757. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1758. uint8_t sc, m;
  1759. for (int j = 0; j < QK_K/32; ++j) {
  1760. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1761. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1762. if (!d) continue;
  1763. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1764. for (int ii = 0; ii < 32; ++ii) {
  1765. int l = nearest_int((x[32*j + ii] + dm)/d);
  1766. l = MAX(0, MIN(15, l));
  1767. L[32*j + ii] = l;
  1768. }
  1769. }
  1770. #else
  1771. const float s_factor = 15.f;
  1772. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1773. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1774. int d1 = nearest_int(inv_scale*scales[0]);
  1775. int m1 = nearest_int(inv_min*mins[0]);
  1776. int d2 = nearest_int(inv_scale*scales[1]);
  1777. int m2 = nearest_int(inv_min*mins[1]);
  1778. y[i].scales[0] = d1 | (m1 << 4);
  1779. y[i].scales[1] = d2 | (m2 << 4);
  1780. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1781. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1782. float sumlx = 0;
  1783. int suml2 = 0;
  1784. for (int j = 0; j < QK_K/32; ++j) {
  1785. const uint8_t sd = y[i].scales[j] & 0xF;
  1786. const uint8_t sm = y[i].scales[j] >> 4;
  1787. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1788. if (!d) continue;
  1789. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1790. for (int ii = 0; ii < 32; ++ii) {
  1791. int l = nearest_int((x[32*j + ii] + m)/d);
  1792. l = MAX(0, MIN(15, l));
  1793. L[32*j + ii] = l;
  1794. sumlx += (x[32*j + ii] + m)*l*sd;
  1795. suml2 += l*l*sd*sd;
  1796. }
  1797. }
  1798. if (suml2) {
  1799. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1800. }
  1801. #endif
  1802. uint8_t * q = y[i].qs;
  1803. for (int j = 0; j < QK_K; j += 64) {
  1804. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1805. q += 32;
  1806. }
  1807. x += QK_K;
  1808. }
  1809. }
  1810. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int64_t k) {
  1811. assert(k % QK_K == 0);
  1812. const int nb = k / QK_K;
  1813. for (int i = 0; i < nb; i++) {
  1814. const uint8_t * q = x[i].qs;
  1815. #if QK_K == 256
  1816. const float d = GGML_FP16_TO_FP32(x[i].d);
  1817. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1818. int is = 0;
  1819. uint8_t sc, m;
  1820. for (int j = 0; j < QK_K; j += 64) {
  1821. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1822. const float d1 = d * sc; const float m1 = min * m;
  1823. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1824. const float d2 = d * sc; const float m2 = min * m;
  1825. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1826. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1827. q += 32; is += 2;
  1828. }
  1829. #else
  1830. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1831. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1832. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1833. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1834. for (int l = 0; l < 32; ++l) {
  1835. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1836. y[l+32] = d2 * (q[l] >> 4) - m2;
  1837. }
  1838. y += QK_K;
  1839. #endif
  1840. }
  1841. }
  1842. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int64_t k) {
  1843. assert(k % QK_K == 0);
  1844. block_q4_K * restrict y = vy;
  1845. quantize_row_q4_K_reference(x, y, k);
  1846. }
  1847. 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) {
  1848. #if QK_K != 256
  1849. (void)quant_weights;
  1850. quantize_row_q4_K_reference(x, y, n_per_row);
  1851. #else
  1852. assert(n_per_row % QK_K == 0);
  1853. const int64_t nb = n_per_row / QK_K;
  1854. uint8_t L[QK_K];
  1855. uint8_t Laux[32];
  1856. uint8_t Ls[QK_K/32];
  1857. uint8_t Lm[QK_K/32];
  1858. float weights[32];
  1859. float sw[QK_K/32];
  1860. float mins[QK_K/32];
  1861. float scales[QK_K/32];
  1862. for (int i = 0; i < nb; i++) {
  1863. float sum_x2 = 0;
  1864. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1865. float sigma2 = 2*sum_x2/QK_K;
  1866. float av_x = sqrtf(sigma2);
  1867. for (int j = 0; j < QK_K/32; ++j) {
  1868. if (quant_weights) {
  1869. const float * qw = quant_weights + QK_K*i + 32*j;
  1870. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1871. } else {
  1872. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1873. }
  1874. float sumw = 0;
  1875. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1876. sw[j] = sumw;
  1877. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1878. }
  1879. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1880. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1881. for (int j = 0; j < QK_K/32; ++j) {
  1882. uint8_t ls = Ls[j];
  1883. uint8_t lm = Lm[j];
  1884. if (j < 4) {
  1885. y[i].scales[j] = ls;
  1886. y[i].scales[j+4] = lm;
  1887. } else {
  1888. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1889. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1890. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1891. }
  1892. }
  1893. y[i].d = GGML_FP32_TO_FP16(d_block);
  1894. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1895. uint8_t sc, m;
  1896. for (int j = 0; j < QK_K/32; ++j) {
  1897. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1898. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1899. if (!d) continue;
  1900. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1901. for (int ii = 0; ii < 32; ++ii) {
  1902. int l = nearest_int((x[32*j + ii] + dm)/d);
  1903. l = MAX(0, MIN(15, l));
  1904. L[32*j + ii] = l;
  1905. }
  1906. }
  1907. uint8_t * q = y[i].qs;
  1908. for (int j = 0; j < QK_K; j += 64) {
  1909. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1910. q += 32;
  1911. }
  1912. x += QK_K;
  1913. }
  1914. #endif
  1915. }
  1916. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1917. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  1918. if (!quant_weights) {
  1919. quantize_row_q4_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1920. }
  1921. else {
  1922. char * qrow = (char *)dst;
  1923. for (int64_t row = 0; row < nrow; ++row) {
  1924. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  1925. src += n_per_row;
  1926. qrow += row_size;
  1927. }
  1928. }
  1929. return nrow * row_size;
  1930. }
  1931. // ====================== 5-bit (de)-quantization
  1932. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int64_t k) {
  1933. assert(k % QK_K == 0);
  1934. const int64_t nb = k / QK_K;
  1935. #if QK_K == 256
  1936. uint8_t L[QK_K];
  1937. float mins[QK_K/32];
  1938. float scales[QK_K/32];
  1939. float weights[32];
  1940. uint8_t Laux[32];
  1941. #else
  1942. int8_t L[QK_K];
  1943. float scales[QK_K/16];
  1944. #endif
  1945. for (int i = 0; i < nb; i++) {
  1946. #if QK_K == 256
  1947. float max_scale = 0; // as we are deducting the min, scales are always positive
  1948. float max_min = 0;
  1949. for (int j = 0; j < QK_K/32; ++j) {
  1950. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1951. float sum_x2 = 0;
  1952. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1953. float av_x = sqrtf(sum_x2/32);
  1954. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1955. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1956. float scale = scales[j];
  1957. if (scale > max_scale) {
  1958. max_scale = scale;
  1959. }
  1960. float min = mins[j];
  1961. if (min > max_min) {
  1962. max_min = min;
  1963. }
  1964. }
  1965. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1966. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1967. for (int j = 0; j < QK_K/32; ++j) {
  1968. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1969. uint8_t lm = nearest_int(inv_min*mins[j]);
  1970. ls = MIN(63, ls);
  1971. lm = MIN(63, lm);
  1972. if (j < 4) {
  1973. y[i].scales[j] = ls;
  1974. y[i].scales[j+4] = lm;
  1975. } else {
  1976. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1977. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1978. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1979. }
  1980. }
  1981. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1982. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1983. uint8_t sc, m;
  1984. for (int j = 0; j < QK_K/32; ++j) {
  1985. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1986. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1987. if (!d) continue;
  1988. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1989. for (int ii = 0; ii < 32; ++ii) {
  1990. int l = nearest_int((x[32*j + ii] + dm)/d);
  1991. l = MAX(0, MIN(31, l));
  1992. L[32*j + ii] = l;
  1993. }
  1994. }
  1995. uint8_t * restrict qh = y[i].qh;
  1996. uint8_t * restrict ql = y[i].qs;
  1997. memset(qh, 0, QK_K/8);
  1998. uint8_t m1 = 1, m2 = 2;
  1999. for (int n = 0; n < QK_K; n += 64) {
  2000. for (int j = 0; j < 32; ++j) {
  2001. int l1 = L[n + j];
  2002. if (l1 > 15) {
  2003. l1 -= 16; qh[j] |= m1;
  2004. }
  2005. int l2 = L[n + j + 32];
  2006. if (l2 > 15) {
  2007. l2 -= 16; qh[j] |= m2;
  2008. }
  2009. ql[j] = l1 | (l2 << 4);
  2010. }
  2011. m1 <<= 2; m2 <<= 2;
  2012. ql += 32;
  2013. }
  2014. #else
  2015. float max_scale = 0, amax = 0;
  2016. for (int j = 0; j < QK_K/16; ++j) {
  2017. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2018. float abs_scale = fabsf(scales[j]);
  2019. if (abs_scale > amax) {
  2020. amax = abs_scale;
  2021. max_scale = scales[j];
  2022. }
  2023. }
  2024. float iscale = -128.f/max_scale;
  2025. for (int j = 0; j < QK_K/16; ++j) {
  2026. int l = nearest_int(iscale*scales[j]);
  2027. y[i].scales[j] = MAX(-128, MIN(127, l));
  2028. }
  2029. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2030. for (int j = 0; j < QK_K/16; ++j) {
  2031. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2032. if (!d) continue;
  2033. for (int ii = 0; ii < 16; ++ii) {
  2034. int l = nearest_int(x[16*j + ii]/d);
  2035. l = MAX(-16, MIN(15, l));
  2036. L[16*j + ii] = l + 16;
  2037. }
  2038. }
  2039. uint8_t * restrict qh = y[i].qh;
  2040. uint8_t * restrict ql = y[i].qs;
  2041. memset(qh, 0, QK_K/8);
  2042. for (int j = 0; j < 32; ++j) {
  2043. int jm = j%8;
  2044. int is = j/8;
  2045. int l1 = L[j];
  2046. if (l1 > 15) {
  2047. l1 -= 16; qh[jm] |= (1 << is);
  2048. }
  2049. int l2 = L[j + 32];
  2050. if (l2 > 15) {
  2051. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2052. }
  2053. ql[j] = l1 | (l2 << 4);
  2054. }
  2055. #endif
  2056. x += QK_K;
  2057. }
  2058. }
  2059. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int64_t k) {
  2060. assert(k % QK_K == 0);
  2061. const int64_t nb = k / QK_K;
  2062. for (int i = 0; i < nb; i++) {
  2063. const uint8_t * ql = x[i].qs;
  2064. const uint8_t * qh = x[i].qh;
  2065. #if QK_K == 256
  2066. const float d = GGML_FP16_TO_FP32(x[i].d);
  2067. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2068. int is = 0;
  2069. uint8_t sc, m;
  2070. uint8_t u1 = 1, u2 = 2;
  2071. for (int j = 0; j < QK_K; j += 64) {
  2072. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2073. const float d1 = d * sc; const float m1 = min * m;
  2074. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2075. const float d2 = d * sc; const float m2 = min * m;
  2076. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2077. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2078. ql += 32; is += 2;
  2079. u1 <<= 2; u2 <<= 2;
  2080. }
  2081. #else
  2082. float d = GGML_FP16_TO_FP32(x[i].d);
  2083. const int8_t * restrict s = x[i].scales;
  2084. for (int l = 0; l < 8; ++l) {
  2085. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2086. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2087. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2088. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2089. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2090. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2091. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2092. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2093. }
  2094. y += QK_K;
  2095. #endif
  2096. }
  2097. }
  2098. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int64_t k) {
  2099. assert(k % QK_K == 0);
  2100. block_q5_K * restrict y = vy;
  2101. quantize_row_q5_K_reference(x, y, k);
  2102. }
  2103. 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) {
  2104. #if QK_K != 256
  2105. (void)quant_weights;
  2106. quantize_row_q5_K_reference(x, y, n_per_row);
  2107. #else
  2108. assert(n_per_row % QK_K == 0);
  2109. const int64_t nb = n_per_row / QK_K;
  2110. uint8_t L[QK_K];
  2111. uint8_t Laux[32];
  2112. uint8_t Ls[QK_K/32];
  2113. uint8_t Lm[QK_K/32];
  2114. float mins[QK_K/32];
  2115. float scales[QK_K/32];
  2116. float sw[QK_K/32];
  2117. float weights[32];
  2118. for (int i = 0; i < nb; i++) {
  2119. float sum_x2 = 0;
  2120. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2121. float sigma2 = 2*sum_x2/QK_K;
  2122. float av_x = sqrtf(sigma2);
  2123. for (int j = 0; j < QK_K/32; ++j) {
  2124. if (quant_weights) {
  2125. const float * qw = quant_weights + QK_K*i + 32*j;
  2126. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2127. } else {
  2128. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2129. }
  2130. float sumw = 0;
  2131. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2132. sw[j] = sumw;
  2133. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2134. }
  2135. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2136. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2137. for (int j = 0; j < QK_K/32; ++j) {
  2138. uint8_t ls = Ls[j];
  2139. uint8_t lm = Lm[j];
  2140. ls = MIN(63, ls);
  2141. lm = MIN(63, lm);
  2142. if (j < 4) {
  2143. y[i].scales[j] = ls;
  2144. y[i].scales[j+4] = lm;
  2145. } else {
  2146. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2147. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2148. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2149. }
  2150. }
  2151. y[i].d = GGML_FP32_TO_FP16(d_block);
  2152. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2153. uint8_t sc, m;
  2154. for (int j = 0; j < QK_K/32; ++j) {
  2155. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2156. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2157. if (!d) continue;
  2158. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2159. for (int ii = 0; ii < 32; ++ii) {
  2160. int l = nearest_int((x[32*j + ii] + dm)/d);
  2161. l = MAX(0, MIN(31, l));
  2162. L[32*j + ii] = l;
  2163. }
  2164. }
  2165. uint8_t * restrict qh = y[i].qh;
  2166. uint8_t * restrict ql = y[i].qs;
  2167. memset(qh, 0, QK_K/8);
  2168. uint8_t m1 = 1, m2 = 2;
  2169. for (int n = 0; n < QK_K; n += 64) {
  2170. for (int j = 0; j < 32; ++j) {
  2171. int l1 = L[n + j];
  2172. if (l1 > 15) {
  2173. l1 -= 16; qh[j] |= m1;
  2174. }
  2175. int l2 = L[n + j + 32];
  2176. if (l2 > 15) {
  2177. l2 -= 16; qh[j] |= m2;
  2178. }
  2179. ql[j] = l1 | (l2 << 4);
  2180. }
  2181. m1 <<= 2; m2 <<= 2;
  2182. ql += 32;
  2183. }
  2184. x += QK_K;
  2185. }
  2186. #endif
  2187. }
  2188. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2189. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2190. if (!quant_weights) {
  2191. quantize_row_q5_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2192. }
  2193. else {
  2194. char * qrow = (char *)dst;
  2195. for (int64_t row = 0; row < nrow; ++row) {
  2196. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2197. src += n_per_row;
  2198. qrow += row_size;
  2199. }
  2200. }
  2201. return nrow * row_size;
  2202. }
  2203. // ====================== 6-bit (de)-quantization
  2204. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int64_t k) {
  2205. assert(k % QK_K == 0);
  2206. const int64_t nb = k / QK_K;
  2207. int8_t L[QK_K];
  2208. float scales[QK_K/16];
  2209. for (int i = 0; i < nb; i++) {
  2210. float max_scale = 0;
  2211. float max_abs_scale = 0;
  2212. for (int ib = 0; ib < QK_K/16; ++ib) {
  2213. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2214. scales[ib] = scale;
  2215. const float abs_scale = fabsf(scale);
  2216. if (abs_scale > max_abs_scale) {
  2217. max_abs_scale = abs_scale;
  2218. max_scale = scale;
  2219. }
  2220. }
  2221. if (!max_abs_scale) {
  2222. memset(&y[i], 0, sizeof(block_q6_K));
  2223. y[i].d = GGML_FP32_TO_FP16(0.f);
  2224. x += QK_K;
  2225. continue;
  2226. }
  2227. float iscale = -128.f/max_scale;
  2228. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2229. for (int ib = 0; ib < QK_K/16; ++ib) {
  2230. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2231. }
  2232. for (int j = 0; j < QK_K/16; ++j) {
  2233. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2234. if (!d) {
  2235. continue;
  2236. }
  2237. for (int ii = 0; ii < 16; ++ii) {
  2238. int l = nearest_int(x[16*j + ii]/d);
  2239. l = MAX(-32, MIN(31, l));
  2240. L[16*j + ii] = l + 32;
  2241. }
  2242. }
  2243. uint8_t * restrict ql = y[i].ql;
  2244. uint8_t * restrict qh = y[i].qh;
  2245. #if QK_K == 256
  2246. for (int j = 0; j < QK_K; j += 128) {
  2247. for (int l = 0; l < 32; ++l) {
  2248. const uint8_t q1 = L[j + l + 0] & 0xF;
  2249. const uint8_t q2 = L[j + l + 32] & 0xF;
  2250. const uint8_t q3 = L[j + l + 64] & 0xF;
  2251. const uint8_t q4 = L[j + l + 96] & 0xF;
  2252. ql[l+ 0] = q1 | (q3 << 4);
  2253. ql[l+32] = q2 | (q4 << 4);
  2254. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2255. }
  2256. ql += 64;
  2257. qh += 32;
  2258. }
  2259. #else
  2260. for (int l = 0; l < 32; ++l) {
  2261. const uint8_t q1 = L[l + 0] & 0xF;
  2262. const uint8_t q2 = L[l + 32] & 0xF;
  2263. ql[l] = q1 | (q2 << 4);
  2264. }
  2265. for (int l = 0; l < 16; ++l) {
  2266. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2267. }
  2268. #endif
  2269. x += QK_K;
  2270. }
  2271. }
  2272. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int64_t k) {
  2273. assert(k % QK_K == 0);
  2274. const int64_t nb = k / QK_K;
  2275. for (int i = 0; i < nb; i++) {
  2276. const float d = GGML_FP16_TO_FP32(x[i].d);
  2277. const uint8_t * restrict ql = x[i].ql;
  2278. const uint8_t * restrict qh = x[i].qh;
  2279. const int8_t * restrict sc = x[i].scales;
  2280. #if QK_K == 256
  2281. for (int n = 0; n < QK_K; n += 128) {
  2282. for (int l = 0; l < 32; ++l) {
  2283. int is = l/16;
  2284. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2285. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2286. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2287. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2288. y[l + 0] = d * sc[is + 0] * q1;
  2289. y[l + 32] = d * sc[is + 2] * q2;
  2290. y[l + 64] = d * sc[is + 4] * q3;
  2291. y[l + 96] = d * sc[is + 6] * q4;
  2292. }
  2293. y += 128;
  2294. ql += 64;
  2295. qh += 32;
  2296. sc += 8;
  2297. }
  2298. #else
  2299. for (int l = 0; l < 16; ++l) {
  2300. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2301. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2302. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2303. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2304. y[l+ 0] = d * sc[0] * q1;
  2305. y[l+16] = d * sc[1] * q2;
  2306. y[l+32] = d * sc[2] * q3;
  2307. y[l+48] = d * sc[3] * q4;
  2308. }
  2309. y += 64;
  2310. #endif
  2311. }
  2312. }
  2313. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int64_t k) {
  2314. assert(k % QK_K == 0);
  2315. block_q6_K * restrict y = vy;
  2316. quantize_row_q6_K_reference(x, y, k);
  2317. }
  2318. 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) {
  2319. #if QK_K != 256
  2320. (void)quant_weights;
  2321. quantize_row_q6_K_reference(x, y, n_per_row);
  2322. #else
  2323. assert(n_per_row % QK_K == 0);
  2324. const int64_t nb = n_per_row / QK_K;
  2325. int8_t L[QK_K];
  2326. float scales[QK_K/16];
  2327. //float weights[16];
  2328. for (int i = 0; i < nb; i++) {
  2329. //float sum_x2 = 0;
  2330. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2331. //float sigma2 = sum_x2/QK_K;
  2332. float max_scale = 0;
  2333. float max_abs_scale = 0;
  2334. for (int ib = 0; ib < QK_K/16; ++ib) {
  2335. float scale;
  2336. if (quant_weights) {
  2337. const float * qw = quant_weights + QK_K*i + 16*ib;
  2338. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2339. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2340. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2341. } else {
  2342. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2343. }
  2344. scales[ib] = scale;
  2345. const float abs_scale = fabsf(scale);
  2346. if (abs_scale > max_abs_scale) {
  2347. max_abs_scale = abs_scale;
  2348. max_scale = scale;
  2349. }
  2350. }
  2351. if (!max_abs_scale) {
  2352. memset(&y[i], 0, sizeof(block_q6_K));
  2353. y[i].d = GGML_FP32_TO_FP16(0.f);
  2354. x += QK_K;
  2355. continue;
  2356. }
  2357. float iscale = -128.f/max_scale;
  2358. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2359. for (int ib = 0; ib < QK_K/16; ++ib) {
  2360. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2361. }
  2362. for (int j = 0; j < QK_K/16; ++j) {
  2363. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2364. if (!d) {
  2365. continue;
  2366. }
  2367. for (int ii = 0; ii < 16; ++ii) {
  2368. int l = nearest_int(x[16*j + ii]/d);
  2369. l = MAX(-32, MIN(31, l));
  2370. L[16*j + ii] = l + 32;
  2371. }
  2372. }
  2373. uint8_t * restrict ql = y[i].ql;
  2374. uint8_t * restrict qh = y[i].qh;
  2375. for (int j = 0; j < QK_K; j += 128) {
  2376. for (int l = 0; l < 32; ++l) {
  2377. const uint8_t q1 = L[j + l + 0] & 0xF;
  2378. const uint8_t q2 = L[j + l + 32] & 0xF;
  2379. const uint8_t q3 = L[j + l + 64] & 0xF;
  2380. const uint8_t q4 = L[j + l + 96] & 0xF;
  2381. ql[l+ 0] = q1 | (q3 << 4);
  2382. ql[l+32] = q2 | (q4 << 4);
  2383. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2384. }
  2385. ql += 64;
  2386. qh += 32;
  2387. }
  2388. x += QK_K;
  2389. }
  2390. #endif
  2391. }
  2392. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2393. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2394. if (!quant_weights) {
  2395. quantize_row_q6_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2396. }
  2397. else {
  2398. char * qrow = (char *)dst;
  2399. for (int64_t row = 0; row < nrow; ++row) {
  2400. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2401. src += n_per_row;
  2402. qrow += row_size;
  2403. }
  2404. }
  2405. return nrow * row_size;
  2406. }
  2407. 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) {
  2408. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2409. if (!quant_weights) {
  2410. quantize_row_q4_0_reference(x, y, n_per_row);
  2411. return;
  2412. }
  2413. float weight[QK4_0];
  2414. int8_t L[QK4_0];
  2415. float sum_x2 = 0;
  2416. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2417. float sigma2 = sum_x2/n_per_row;
  2418. const int64_t nb = n_per_row/QK4_0;
  2419. for (int ib = 0; ib < nb; ++ib) {
  2420. const float * xb = x + QK4_0 * ib;
  2421. const float * qw = quant_weights + QK4_0 * ib;
  2422. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2423. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2424. y[ib].d = GGML_FP32_TO_FP16(d);
  2425. for (int j = 0; j < 16; ++j) {
  2426. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2427. }
  2428. }
  2429. }
  2430. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2431. if (!quant_weights) {
  2432. quantize_row_q4_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2433. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2434. }
  2435. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2436. char * qrow = (char *)dst;
  2437. for (int64_t row = 0; row < nrow; ++row) {
  2438. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2439. src += n_per_row;
  2440. qrow += row_size;
  2441. }
  2442. return nrow * row_size;
  2443. }
  2444. 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) {
  2445. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2446. if (!quant_weights) {
  2447. quantize_row_q4_1_reference(x, y, n_per_row);
  2448. return;
  2449. }
  2450. float weight[QK4_1];
  2451. uint8_t L[QK4_1], Laux[QK4_1];
  2452. float sum_x2 = 0;
  2453. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2454. float sigma2 = sum_x2/n_per_row;
  2455. const int64_t nb = n_per_row/QK4_1;
  2456. for (int ib = 0; ib < nb; ++ib) {
  2457. const float * xb = x + QK4_1 * ib;
  2458. const float * qw = quant_weights + QK4_1 * ib;
  2459. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2460. float min;
  2461. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2462. y[ib].d = GGML_FP32_TO_FP16(d);
  2463. y[ib].m = GGML_FP32_TO_FP16(-min);
  2464. for (int j = 0; j < 16; ++j) {
  2465. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2466. }
  2467. }
  2468. }
  2469. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2470. if (!quant_weights) {
  2471. quantize_row_q4_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2472. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2473. }
  2474. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2475. char * qrow = (char *)dst;
  2476. for (int64_t row = 0; row < nrow; ++row) {
  2477. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2478. src += n_per_row;
  2479. qrow += row_size;
  2480. }
  2481. return nrow * row_size;
  2482. }
  2483. 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) {
  2484. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2485. if (!quant_weights) {
  2486. quantize_row_q5_0_reference(x, y, n_per_row);
  2487. return;
  2488. }
  2489. float weight[QK5_0];
  2490. int8_t L[QK5_0];
  2491. float sum_x2 = 0;
  2492. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2493. float sigma2 = sum_x2/n_per_row;
  2494. const int64_t nb = n_per_row/QK5_0;
  2495. for (int ib = 0; ib < nb; ++ib) {
  2496. const float * xb = x + QK5_0 * ib;
  2497. const float * qw = quant_weights + QK5_0 * ib;
  2498. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2499. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2500. y[ib].d = GGML_FP32_TO_FP16(d);
  2501. uint32_t qh = 0;
  2502. for (int j = 0; j < 16; ++j) {
  2503. const uint8_t xi0 = L[j];
  2504. const uint8_t xi1 = L[j+16];
  2505. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2506. // get the 5-th bit and store it in qh at the right position
  2507. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2508. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2509. }
  2510. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2511. }
  2512. }
  2513. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2514. if (!quant_weights) {
  2515. quantize_row_q5_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2516. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2517. }
  2518. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2519. char * qrow = (char *)dst;
  2520. for (int64_t row = 0; row < nrow; ++row) {
  2521. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2522. src += n_per_row;
  2523. qrow += row_size;
  2524. }
  2525. return nrow * row_size;
  2526. }
  2527. 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) {
  2528. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2529. if (!quant_weights) {
  2530. quantize_row_q5_1_reference(x, y, n_per_row);
  2531. return;
  2532. }
  2533. float weight[QK5_1];
  2534. uint8_t L[QK5_1], Laux[QK5_1];
  2535. float sum_x2 = 0;
  2536. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2537. float sigma2 = sum_x2/n_per_row;
  2538. const int64_t nb = n_per_row/QK5_1;
  2539. for (int ib = 0; ib < nb; ++ib) {
  2540. const float * xb = x + QK5_1 * ib;
  2541. const float * qw = quant_weights + QK5_1 * ib;
  2542. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2543. float min;
  2544. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2545. y[ib].d = GGML_FP32_TO_FP16(d);
  2546. y[ib].m = GGML_FP32_TO_FP16(-min);
  2547. uint32_t qh = 0;
  2548. for (int j = 0; j < 16; ++j) {
  2549. const uint8_t xi0 = L[j];
  2550. const uint8_t xi1 = L[j+16];
  2551. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2552. // get the 5-th bit and store it in qh at the right position
  2553. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2554. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2555. }
  2556. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2557. }
  2558. }
  2559. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2560. if (!quant_weights) {
  2561. quantize_row_q5_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2562. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2563. }
  2564. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2565. char * qrow = (char *)dst;
  2566. for (int64_t row = 0; row < nrow; ++row) {
  2567. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2568. src += n_per_row;
  2569. qrow += row_size;
  2570. }
  2571. return nrow * row_size;
  2572. }
  2573. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2574. (void)quant_weights; // not used
  2575. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2576. quantize_row_q8_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2577. return nrow * row_size;
  2578. }
  2579. // ====================== "True" 2-bit (de)-quantization
  2580. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int64_t k) {
  2581. assert(k % QK_K == 0);
  2582. const int64_t nb = k / QK_K;
  2583. uint32_t aux32[2];
  2584. const uint8_t * aux8 = (const uint8_t *)aux32;
  2585. for (int i = 0; i < nb; i++) {
  2586. const float d = GGML_FP16_TO_FP32(x[i].d);
  2587. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2588. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2589. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2590. for (int l = 0; l < 4; ++l) {
  2591. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2592. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2593. for (int j = 0; j < 8; ++j) {
  2594. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2595. }
  2596. y += 8;
  2597. }
  2598. }
  2599. }
  2600. }
  2601. // ====================== 2.3125 bpw (de)-quantization
  2602. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int64_t k) {
  2603. assert(k % QK_K == 0);
  2604. const int64_t nb = k / QK_K;
  2605. float db[2];
  2606. for (int i = 0; i < nb; i++) {
  2607. const float d = GGML_FP16_TO_FP32(x[i].d);
  2608. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2609. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2610. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2611. for (int l = 0; l < 4; ++l) {
  2612. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2613. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2614. for (int j = 0; j < 8; ++j) {
  2615. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2616. }
  2617. y += 8;
  2618. }
  2619. }
  2620. }
  2621. }
  2622. // ====================== 2.5625 bpw (de)-quantization
  2623. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int64_t k) {
  2624. assert(k % QK_K == 0);
  2625. const int64_t nb = k / QK_K;
  2626. float db[2];
  2627. for (int i = 0; i < nb; i++) {
  2628. const float d = GGML_FP16_TO_FP32(x[i].d);
  2629. const uint8_t * qs = x[i].qs;
  2630. const uint8_t * qh = x[i].qh;
  2631. const uint8_t * signs = qs + QK_K/8;
  2632. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2633. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2634. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2635. for (int l = 0; l < 4; ++l) {
  2636. const float dl = db[l/2];
  2637. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2638. for (int j = 0; j < 8; ++j) {
  2639. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2640. }
  2641. y += 8;
  2642. }
  2643. qs += 4;
  2644. signs += 4;
  2645. }
  2646. }
  2647. }
  2648. // ====================== 3.0625 bpw (de)-quantization
  2649. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int64_t k) {
  2650. assert(k % QK_K == 0);
  2651. const int64_t nb = k / QK_K;
  2652. uint32_t aux32;
  2653. for (int i = 0; i < nb; i++) {
  2654. const float d = GGML_FP16_TO_FP32(x[i].d);
  2655. const uint8_t * qs = x[i].qs;
  2656. const uint8_t * scales_and_signs = qs + QK_K/4;
  2657. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2658. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2659. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2660. for (int l = 0; l < 4; ++l) {
  2661. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2662. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2663. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2664. for (int j = 0; j < 4; ++j) {
  2665. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2666. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2667. }
  2668. y += 8;
  2669. }
  2670. qs += 8;
  2671. }
  2672. }
  2673. }
  2674. // ====================== 3.3125 bpw (de)-quantization
  2675. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int64_t k) {
  2676. assert(k % QK_K == 0);
  2677. const int64_t nb = k / QK_K;
  2678. for (int i = 0; i < nb; i++) {
  2679. const float d = GGML_FP16_TO_FP32(x[i].d);
  2680. const uint8_t * qs = x[i].qs;
  2681. const uint8_t * qh = x[i].qh;
  2682. const uint8_t * signs = x[i].signs;
  2683. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2684. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2685. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2686. for (int l = 0; l < 4; ++l) {
  2687. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2688. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2689. for (int j = 0; j < 4; ++j) {
  2690. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2691. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2692. }
  2693. y += 8;
  2694. }
  2695. qs += 8;
  2696. signs += 4;
  2697. for (int l = 0; l < 4; ++l) {
  2698. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2699. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2700. for (int j = 0; j < 4; ++j) {
  2701. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2702. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2703. }
  2704. y += 8;
  2705. }
  2706. qh += 2;
  2707. qs += 8;
  2708. signs += 4;
  2709. }
  2710. }
  2711. }
  2712. // ====================== 1.5625 bpw (de)-quantization
  2713. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int64_t k) {
  2714. assert(k % QK_K == 0);
  2715. const int64_t nb = k / QK_K;
  2716. for (int i = 0; i < nb; i++) {
  2717. const float d = GGML_FP16_TO_FP32(x[i].d);
  2718. const uint8_t * qs = x[i].qs;
  2719. const uint16_t * qh = x[i].qh;
  2720. for (int ib = 0; ib < QK_K/32; ++ib) {
  2721. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2722. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2723. for (int l = 0; l < 4; ++l) {
  2724. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2725. for (int j = 0; j < 8; ++j) {
  2726. y[j] = dl * (grid[j] + delta);
  2727. }
  2728. y += 8;
  2729. }
  2730. qs += 4;
  2731. }
  2732. }
  2733. }
  2734. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int64_t k) {
  2735. assert(k % QK_K == 0);
  2736. const int64_t nb = k / QK_K;
  2737. float delta[4];
  2738. uint16_t idx[4];
  2739. #if QK_K != 64
  2740. iq1m_scale_t scale;
  2741. #endif
  2742. for (int i = 0; i < nb; i++) {
  2743. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2744. #if QK_K == 64
  2745. const float d = GGML_FP16_TO_FP32(x[i].d);
  2746. #else
  2747. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2748. const float d = GGML_FP16_TO_FP32(scale.f16);
  2749. #endif
  2750. const uint8_t * qs = x[i].qs;
  2751. const uint8_t * qh = x[i].qh;
  2752. for (int ib = 0; ib < QK_K/32; ++ib) {
  2753. #if QK_K == 64
  2754. const float dl1 = d * (2*((sc[ib/2] >> (8*(ib%2)+0)) & 0xf) + 1);
  2755. const float dl2 = d * (2*((sc[ib/2] >> (8*(ib%2)+4)) & 0xf) + 1);
  2756. #else
  2757. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2758. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2759. #endif
  2760. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2761. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2762. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2763. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2764. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2765. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2766. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2767. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2768. for (int l = 0; l < 2; ++l) {
  2769. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2770. for (int j = 0; j < 8; ++j) {
  2771. y[j] = dl1 * (grid[j] + delta[l]);
  2772. }
  2773. y += 8;
  2774. }
  2775. for (int l = 2; l < 4; ++l) {
  2776. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2777. for (int j = 0; j < 8; ++j) {
  2778. y[j] = dl2 * (grid[j] + delta[l]);
  2779. }
  2780. y += 8;
  2781. }
  2782. qs += 4;
  2783. qh += 2;
  2784. }
  2785. }
  2786. }
  2787. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2788. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int64_t k) {
  2789. assert(k % QK4_NL == 0);
  2790. const int64_t nb = k / QK4_NL;
  2791. for (int i = 0; i < nb; i++) {
  2792. const uint8_t * qs = x[i].qs;
  2793. const float d = GGML_FP16_TO_FP32(x[i].d);
  2794. for (int j = 0; j < QK4_NL/2; ++j) {
  2795. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2796. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2797. }
  2798. y += QK4_NL;
  2799. qs += QK4_NL/2;
  2800. }
  2801. }
  2802. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int64_t k) {
  2803. assert(k % QK_K == 0);
  2804. #if QK_K == 64
  2805. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2806. #else
  2807. const int64_t nb = k / QK_K;
  2808. for (int i = 0; i < nb; i++) {
  2809. const uint8_t * qs = x[i].qs;
  2810. const float d = GGML_FP16_TO_FP32(x[i].d);
  2811. for (int ib = 0; ib < QK_K/32; ++ib) {
  2812. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2813. const float dl = d * (ls - 32);
  2814. for (int j = 0; j < 16; ++j) {
  2815. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2816. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2817. }
  2818. y += 32;
  2819. qs += 16;
  2820. }
  2821. }
  2822. #endif
  2823. }
  2824. //===================================== Q8_K ==============================================
  2825. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int64_t k) {
  2826. assert(k % QK_K == 0);
  2827. const int64_t nb = k / QK_K;
  2828. for (int i = 0; i < nb; i++) {
  2829. float max = 0;
  2830. float amax = 0;
  2831. for (int j = 0; j < QK_K; ++j) {
  2832. float ax = fabsf(x[j]);
  2833. if (ax > amax) {
  2834. amax = ax; max = x[j];
  2835. }
  2836. }
  2837. if (!amax) {
  2838. y[i].d = 0;
  2839. memset(y[i].qs, 0, QK_K);
  2840. x += QK_K;
  2841. continue;
  2842. }
  2843. //const float iscale = -128.f/max;
  2844. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2845. const float iscale = -127.f/max;
  2846. for (int j = 0; j < QK_K; ++j) {
  2847. int v = nearest_int(iscale*x[j]);
  2848. y[i].qs[j] = MIN(127, v);
  2849. }
  2850. for (int j = 0; j < QK_K/16; ++j) {
  2851. int sum = 0;
  2852. for (int ii = 0; ii < 16; ++ii) {
  2853. sum += y[i].qs[j*16 + ii];
  2854. }
  2855. y[i].bsums[j] = sum;
  2856. }
  2857. y[i].d = 1/iscale;
  2858. x += QK_K;
  2859. }
  2860. }
  2861. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int64_t k) {
  2862. assert(k % QK_K == 0);
  2863. const int64_t nb = k / QK_K;
  2864. for (int i = 0; i < nb; i++) {
  2865. for (int j = 0; j < QK_K; ++j) {
  2866. *y++ = x[i].d * x[i].qs[j];
  2867. }
  2868. }
  2869. }
  2870. void quantize_row_q8_K(const float * restrict x, void * restrict y, int64_t k) {
  2871. quantize_row_q8_K_reference(x, y, k);
  2872. }
  2873. //===================================== Dot ptoducts =================================
  2874. //
  2875. // Helper functions
  2876. //
  2877. #if __AVX__ || __AVX2__ || __AVX512F__
  2878. // shuffles to pick the required scales in dot products
  2879. static inline __m256i get_scale_shuffle_q3k(int i) {
  2880. static const uint8_t k_shuffle[128] = {
  2881. 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,
  2882. 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,
  2883. 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,
  2884. 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,
  2885. };
  2886. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2887. }
  2888. static inline __m256i get_scale_shuffle_k4(int i) {
  2889. static const uint8_t k_shuffle[256] = {
  2890. 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,
  2891. 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,
  2892. 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,
  2893. 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,
  2894. 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,
  2895. 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,
  2896. 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,
  2897. 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
  2898. };
  2899. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2900. }
  2901. static inline __m128i get_scale_shuffle(int i) {
  2902. static const uint8_t k_shuffle[128] = {
  2903. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2904. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2905. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2906. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2907. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2908. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2909. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2910. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2911. };
  2912. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2913. }
  2914. #endif
  2915. 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) {
  2916. const int qk = QK8_0;
  2917. const int nb = n / qk;
  2918. assert(n % qk == 0);
  2919. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2920. assert((nrc == 2) || (nrc == 1));
  2921. #else
  2922. assert(nrc == 1);
  2923. #endif
  2924. UNUSED(nrc);
  2925. UNUSED(bx);
  2926. UNUSED(by);
  2927. UNUSED(bs);
  2928. const block_q4_0 * restrict x = vx;
  2929. const block_q8_0 * restrict y = vy;
  2930. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2931. if (nrc == 2) {
  2932. const block_q4_0 * restrict vx0 = vx;
  2933. const block_q4_0 * restrict vx1 = vx + bx;
  2934. const block_q8_0 * restrict vy0 = vy;
  2935. const block_q8_0 * restrict vy1 = vy + by;
  2936. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2937. for (int i = 0; i < nb; i++) {
  2938. const block_q4_0 * restrict b_x0 = &vx0[i];
  2939. const block_q4_0 * restrict b_x1 = &vx1[i];
  2940. const block_q8_0 * restrict b_y0 = &vy0[i];
  2941. const block_q8_0 * restrict b_y1 = &vy1[i];
  2942. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2943. const int8x16_t s8b = vdupq_n_s8(0x8);
  2944. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  2945. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  2946. // 4-bit -> 8-bit
  2947. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2948. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2949. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2950. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2951. // sub 8
  2952. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  2953. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  2954. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  2955. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  2956. // load y
  2957. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  2958. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  2959. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  2960. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  2961. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  2962. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  2963. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  2964. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  2965. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2966. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2967. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2968. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2969. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2970. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2971. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2972. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2973. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  2974. l1, r1)), l2, r2)), l3, r3))), scale);
  2975. }
  2976. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  2977. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  2978. vst1_f32(s, vget_low_f32(sumv2));
  2979. vst1_f32(s + bs, vget_high_f32(sumv2));
  2980. return;
  2981. }
  2982. #endif
  2983. #if defined(__ARM_NEON)
  2984. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2985. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2986. assert(nb % 2 == 0); // TODO: handle odd nb
  2987. for (int i = 0; i < nb; i += 2) {
  2988. const block_q4_0 * restrict x0 = &x[i + 0];
  2989. const block_q4_0 * restrict x1 = &x[i + 1];
  2990. const block_q8_0 * restrict y0 = &y[i + 0];
  2991. const block_q8_0 * restrict y1 = &y[i + 1];
  2992. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2993. const int8x16_t s8b = vdupq_n_s8(0x8);
  2994. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2995. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2996. // 4-bit -> 8-bit
  2997. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2998. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2999. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3000. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3001. // sub 8
  3002. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3003. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3004. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3005. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3006. // load y
  3007. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3008. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3009. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3010. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3011. // dot product into int32x4_t
  3012. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3013. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3014. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3015. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3016. }
  3017. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3018. #elif defined(__AVX2__)
  3019. // Initialize accumulator with zeros
  3020. __m256 acc = _mm256_setzero_ps();
  3021. // Main loop
  3022. for (int i = 0; i < nb; ++i) {
  3023. /* Compute combined scale for the block */
  3024. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3025. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3026. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3027. const __m256i off = _mm256_set1_epi8( 8 );
  3028. qx = _mm256_sub_epi8( qx, off );
  3029. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3030. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3031. /* Multiply q with scale and accumulate */
  3032. acc = _mm256_fmadd_ps( d, q, acc );
  3033. }
  3034. *s = hsum_float_8(acc);
  3035. #elif defined(__AVX__)
  3036. // Initialize accumulator with zeros
  3037. __m256 acc = _mm256_setzero_ps();
  3038. // Main loop
  3039. for (int i = 0; i < nb; ++i) {
  3040. // Compute combined scale for the block
  3041. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3042. const __m128i lowMask = _mm_set1_epi8(0xF);
  3043. const __m128i off = _mm_set1_epi8(8);
  3044. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3045. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3046. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3047. bx_0 = _mm_sub_epi8(bx_0, off);
  3048. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3049. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3050. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3051. bx_0 = _mm_sub_epi8(bx_0, off);
  3052. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3053. // Convert int32_t to float
  3054. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3055. // Apply the scale, and accumulate
  3056. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3057. }
  3058. *s = hsum_float_8(acc);
  3059. #elif defined(__SSSE3__)
  3060. // set constants
  3061. const __m128i lowMask = _mm_set1_epi8(0xF);
  3062. const __m128i off = _mm_set1_epi8(8);
  3063. // Initialize accumulator with zeros
  3064. __m128 acc_0 = _mm_setzero_ps();
  3065. __m128 acc_1 = _mm_setzero_ps();
  3066. __m128 acc_2 = _mm_setzero_ps();
  3067. __m128 acc_3 = _mm_setzero_ps();
  3068. // First round without accumulation
  3069. {
  3070. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3071. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3072. // Compute combined scale for the block 0 and 1
  3073. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3074. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3075. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3076. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3077. bx_0 = _mm_sub_epi8(bx_0, off);
  3078. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3079. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3080. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3081. bx_1 = _mm_sub_epi8(bx_1, off);
  3082. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3083. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3084. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3085. // Compute combined scale for the block 2 and 3
  3086. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3087. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3088. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3089. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3090. bx_2 = _mm_sub_epi8(bx_2, off);
  3091. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3092. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3093. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3094. bx_3 = _mm_sub_epi8(bx_3, off);
  3095. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3096. // Convert int32_t to float
  3097. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3098. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3099. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3100. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3101. // Apply the scale
  3102. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3103. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3104. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3105. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3106. }
  3107. assert(nb % 2 == 0); // TODO: handle odd nb
  3108. // Main loop
  3109. for (int i = 2; i < nb; i+=2) {
  3110. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3111. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3112. // Compute combined scale for the block 0 and 1
  3113. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3114. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3115. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3116. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3117. bx_0 = _mm_sub_epi8(bx_0, off);
  3118. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3119. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3120. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3121. bx_1 = _mm_sub_epi8(bx_1, off);
  3122. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3123. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3124. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3125. // Compute combined scale for the block 2 and 3
  3126. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3127. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3128. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3129. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3130. bx_2 = _mm_sub_epi8(bx_2, off);
  3131. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3132. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3133. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3134. bx_3 = _mm_sub_epi8(bx_3, off);
  3135. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3136. // Convert int32_t to float
  3137. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3138. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3139. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3140. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3141. // Apply the scale
  3142. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3143. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3144. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3145. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3146. // Acummulate
  3147. acc_0 = _mm_add_ps(p0_d, acc_0);
  3148. acc_1 = _mm_add_ps(p1_d, acc_1);
  3149. acc_2 = _mm_add_ps(p2_d, acc_2);
  3150. acc_3 = _mm_add_ps(p3_d, acc_3);
  3151. }
  3152. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3153. #elif defined(__riscv_v_intrinsic)
  3154. float sumf = 0.0;
  3155. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3156. for (int i = 0; i < nb; i++) {
  3157. // load elements
  3158. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3159. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3160. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3161. // mask and store lower part of x, and then upper part
  3162. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3163. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3164. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3165. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3166. // subtract offset
  3167. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3168. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3169. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3170. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3171. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3172. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3173. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3174. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3175. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3176. }
  3177. *s = sumf;
  3178. #else
  3179. // scalar
  3180. float sumf = 0.0;
  3181. for (int i = 0; i < nb; i++) {
  3182. int sumi = 0;
  3183. for (int j = 0; j < qk/2; ++j) {
  3184. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3185. const int v1 = (x[i].qs[j] >> 4) - 8;
  3186. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3187. }
  3188. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3189. }
  3190. *s = sumf;
  3191. #endif
  3192. }
  3193. 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) {
  3194. const int qk = QK8_1;
  3195. const int nb = n / qk;
  3196. assert(n % qk == 0);
  3197. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3198. assert((nrc == 2) || (nrc == 1));
  3199. #else
  3200. assert(nrc == 1);
  3201. #endif
  3202. UNUSED(nrc);
  3203. UNUSED(bx);
  3204. UNUSED(by);
  3205. UNUSED(bs);
  3206. const block_q4_1 * restrict x = vx;
  3207. const block_q8_1 * restrict y = vy;
  3208. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3209. if (nrc == 2) {
  3210. const block_q4_1 * restrict vx0 = vx;
  3211. const block_q4_1 * restrict vx1 = vx + bx;
  3212. const block_q8_1 * restrict vy0 = vy;
  3213. const block_q8_1 * restrict vy1 = vy + by;
  3214. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3215. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3216. for (int i = 0; i < nb; i++) {
  3217. const block_q4_1 * restrict b_x0 = &vx0[i];
  3218. const block_q4_1 * restrict b_x1 = &vx1[i];
  3219. const block_q8_1 * restrict b_y0 = &vy0[i];
  3220. const block_q8_1 * restrict b_y1 = &vy1[i];
  3221. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3222. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3223. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3224. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3225. summs0 += summs_t;
  3226. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3227. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3228. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3229. // 4-bit -> 8-bit
  3230. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3231. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3232. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3233. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3234. // load y
  3235. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3236. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3237. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3238. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3239. // mmla into int32x4_t
  3240. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3241. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3242. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3243. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3244. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3245. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3246. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3247. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3248. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3249. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3250. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3251. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3252. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3253. l1, r1)), l2, r2)), l3, r3))), scale);
  3254. }
  3255. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3256. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3257. sumv2 = sumv2 + summs0;
  3258. vst1_f32(s, vget_low_f32(sumv2));
  3259. vst1_f32(s + bs, vget_high_f32(sumv2));
  3260. return;
  3261. }
  3262. #endif
  3263. // TODO: add WASM SIMD
  3264. #if defined(__ARM_NEON)
  3265. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3266. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3267. float summs = 0;
  3268. assert(nb % 2 == 0); // TODO: handle odd nb
  3269. for (int i = 0; i < nb; i += 2) {
  3270. const block_q4_1 * restrict x0 = &x[i + 0];
  3271. const block_q4_1 * restrict x1 = &x[i + 1];
  3272. const block_q8_1 * restrict y0 = &y[i + 0];
  3273. const block_q8_1 * restrict y1 = &y[i + 1];
  3274. 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);
  3275. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3276. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3277. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3278. // 4-bit -> 8-bit
  3279. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3280. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3281. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3282. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3283. // load y
  3284. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3285. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3286. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3287. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3288. // dot product into int32x4_t
  3289. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3290. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3291. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3292. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3293. }
  3294. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3295. #elif defined(__AVX2__) || defined(__AVX__)
  3296. // Initialize accumulator with zeros
  3297. __m256 acc = _mm256_setzero_ps();
  3298. float summs = 0;
  3299. // Main loop
  3300. for (int i = 0; i < nb; ++i) {
  3301. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3302. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3303. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3304. const __m256 d0v = _mm256_set1_ps( d0 );
  3305. const __m256 d1v = _mm256_set1_ps( d1 );
  3306. // Compute combined scales
  3307. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3308. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3309. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3310. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3311. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3312. // Accumulate d0*d1*x*y
  3313. #if defined(__AVX2__)
  3314. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3315. #else
  3316. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3317. #endif
  3318. }
  3319. *s = hsum_float_8(acc) + summs;
  3320. #elif defined(__riscv_v_intrinsic)
  3321. float sumf = 0.0;
  3322. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3323. for (int i = 0; i < nb; i++) {
  3324. // load elements
  3325. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3326. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3327. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3328. // mask and store lower part of x, and then upper part
  3329. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3330. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3331. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3332. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3333. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3334. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3335. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3336. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3337. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3338. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3339. 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);
  3340. }
  3341. *s = sumf;
  3342. #else
  3343. // scalar
  3344. float sumf = 0.0;
  3345. for (int i = 0; i < nb; i++) {
  3346. int sumi = 0;
  3347. for (int j = 0; j < qk/2; ++j) {
  3348. const int v0 = (x[i].qs[j] & 0x0F);
  3349. const int v1 = (x[i].qs[j] >> 4);
  3350. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3351. }
  3352. 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);
  3353. }
  3354. *s = sumf;
  3355. #endif
  3356. }
  3357. 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) {
  3358. const int qk = QK8_0;
  3359. const int nb = n / qk;
  3360. assert(n % qk == 0);
  3361. assert(qk == QK5_0);
  3362. assert(nrc == 1);
  3363. UNUSED(nrc);
  3364. UNUSED(bx);
  3365. UNUSED(by);
  3366. UNUSED(bs);
  3367. const block_q5_0 * restrict x = vx;
  3368. const block_q8_0 * restrict y = vy;
  3369. #if defined(__ARM_NEON)
  3370. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3371. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3372. uint32_t qh0;
  3373. uint32_t qh1;
  3374. uint64_t tmp0[4];
  3375. uint64_t tmp1[4];
  3376. assert(nb % 2 == 0); // TODO: handle odd nb
  3377. for (int i = 0; i < nb; i += 2) {
  3378. const block_q5_0 * restrict x0 = &x[i];
  3379. const block_q5_0 * restrict x1 = &x[i + 1];
  3380. const block_q8_0 * restrict y0 = &y[i];
  3381. const block_q8_0 * restrict y1 = &y[i + 1];
  3382. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3383. // extract the 5th bit via lookup table ((!b) << 4)
  3384. memcpy(&qh0, x0->qh, sizeof(qh0));
  3385. memcpy(&qh1, x1->qh, sizeof(qh1));
  3386. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3387. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3388. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3389. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3390. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3391. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3392. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3393. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3394. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3395. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3396. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3397. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3398. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3399. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3400. // 4-bit -> 8-bit
  3401. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3402. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3403. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3404. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3405. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3406. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3407. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3408. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3409. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3410. // load y
  3411. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3412. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3413. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3414. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3415. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3416. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3417. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3418. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3419. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3420. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3421. }
  3422. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3423. #elif defined(__wasm_simd128__)
  3424. v128_t sumv = wasm_f32x4_splat(0.0f);
  3425. uint32_t qh;
  3426. uint64_t tmp[4];
  3427. // TODO: check if unrolling this is better
  3428. for (int i = 0; i < nb; ++i) {
  3429. const block_q5_0 * restrict x0 = &x[i];
  3430. const block_q8_0 * restrict y0 = &y[i];
  3431. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3432. // extract the 5th bit
  3433. memcpy(&qh, x0->qh, sizeof(qh));
  3434. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3435. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3436. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3437. tmp[3] = table_b2b_1[(qh >> 24) ];
  3438. const v128_t qhl = wasm_v128_load(tmp + 0);
  3439. const v128_t qhh = wasm_v128_load(tmp + 2);
  3440. const v128_t v0 = wasm_v128_load(x0->qs);
  3441. // 4-bit -> 8-bit
  3442. const v128_t v0l = wasm_v128_and (v0, m4b);
  3443. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3444. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3445. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3446. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3447. // load y
  3448. const v128_t v1l = wasm_v128_load(y0->qs);
  3449. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3450. // int8x16 -> int16x8
  3451. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3452. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3453. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3454. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3455. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3456. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3457. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3458. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3459. // dot product
  3460. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3461. wasm_i32x4_add(
  3462. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3463. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3464. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3465. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3466. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3467. }
  3468. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3469. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3470. #elif defined(__AVX2__)
  3471. // Initialize accumulator with zeros
  3472. __m256 acc = _mm256_setzero_ps();
  3473. // Main loop
  3474. for (int i = 0; i < nb; i++) {
  3475. /* Compute combined scale for the block */
  3476. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3477. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3478. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3479. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3480. qx = _mm256_or_si256(qx, bxhi);
  3481. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3482. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3483. /* Multiply q with scale and accumulate */
  3484. acc = _mm256_fmadd_ps(d, q, acc);
  3485. }
  3486. *s = hsum_float_8(acc);
  3487. #elif defined(__AVX__)
  3488. // Initialize accumulator with zeros
  3489. __m256 acc = _mm256_setzero_ps();
  3490. __m128i mask = _mm_set1_epi8((char)0xF0);
  3491. // Main loop
  3492. for (int i = 0; i < nb; i++) {
  3493. /* Compute combined scale for the block */
  3494. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3495. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3496. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3497. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3498. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3499. bxhil = _mm_andnot_si128(bxhil, mask);
  3500. bxhih = _mm_andnot_si128(bxhih, mask);
  3501. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3502. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3503. bxl = _mm_or_si128(bxl, bxhil);
  3504. bxh = _mm_or_si128(bxh, bxhih);
  3505. bx_0 = MM256_SET_M128I(bxh, bxl);
  3506. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3507. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3508. /* Multiply q with scale and accumulate */
  3509. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3510. }
  3511. *s = hsum_float_8(acc);
  3512. #elif defined(__riscv_v_intrinsic)
  3513. float sumf = 0.0;
  3514. uint32_t qh;
  3515. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3516. // These temporary registers are for masking and shift operations
  3517. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3518. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3519. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3520. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3521. for (int i = 0; i < nb; i++) {
  3522. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3523. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3524. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3525. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3526. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3527. // ((qh & (1u << (j + 16))) >> (j + 12));
  3528. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3529. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3530. // narrowing
  3531. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3532. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3533. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3534. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3535. // load
  3536. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3537. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3538. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3539. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3540. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3541. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3542. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3543. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3544. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3545. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3546. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3547. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3548. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3549. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3550. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3551. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3552. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3553. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3554. }
  3555. *s = sumf;
  3556. #else
  3557. // scalar
  3558. float sumf = 0.0;
  3559. for (int i = 0; i < nb; i++) {
  3560. uint32_t qh;
  3561. memcpy(&qh, x[i].qh, sizeof(qh));
  3562. int sumi = 0;
  3563. for (int j = 0; j < qk/2; ++j) {
  3564. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3565. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3566. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3567. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3568. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3569. }
  3570. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3571. }
  3572. *s = sumf;
  3573. #endif
  3574. }
  3575. 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) {
  3576. const int qk = QK8_1;
  3577. const int nb = n / qk;
  3578. assert(n % qk == 0);
  3579. assert(qk == QK5_1);
  3580. assert(nrc == 1);
  3581. UNUSED(nrc);
  3582. UNUSED(bx);
  3583. UNUSED(by);
  3584. UNUSED(bs);
  3585. const block_q5_1 * restrict x = vx;
  3586. const block_q8_1 * restrict y = vy;
  3587. #if defined(__ARM_NEON)
  3588. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3589. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3590. float summs0 = 0.0f;
  3591. float summs1 = 0.0f;
  3592. uint32_t qh0;
  3593. uint32_t qh1;
  3594. uint64_t tmp0[4];
  3595. uint64_t tmp1[4];
  3596. assert(nb % 2 == 0); // TODO: handle odd nb
  3597. for (int i = 0; i < nb; i += 2) {
  3598. const block_q5_1 * restrict x0 = &x[i];
  3599. const block_q5_1 * restrict x1 = &x[i + 1];
  3600. const block_q8_1 * restrict y0 = &y[i];
  3601. const block_q8_1 * restrict y1 = &y[i + 1];
  3602. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3603. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3604. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3605. // extract the 5th bit via lookup table ((b) << 4)
  3606. memcpy(&qh0, x0->qh, sizeof(qh0));
  3607. memcpy(&qh1, x1->qh, sizeof(qh1));
  3608. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3609. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3610. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3611. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3612. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3613. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3614. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3615. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3616. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3617. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3618. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3619. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3620. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3621. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3622. // 4-bit -> 8-bit
  3623. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3624. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3625. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3626. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3627. // add high bit
  3628. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3629. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3630. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3631. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3632. // load y
  3633. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3634. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3635. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3636. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3637. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3638. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3639. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3640. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3641. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3642. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3643. }
  3644. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3645. #elif defined(__wasm_simd128__)
  3646. v128_t sumv = wasm_f32x4_splat(0.0f);
  3647. float summs = 0.0f;
  3648. uint32_t qh;
  3649. uint64_t tmp[4];
  3650. // TODO: check if unrolling this is better
  3651. for (int i = 0; i < nb; ++i) {
  3652. const block_q5_1 * restrict x0 = &x[i];
  3653. const block_q8_1 * restrict y0 = &y[i];
  3654. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3655. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3656. // extract the 5th bit
  3657. memcpy(&qh, x0->qh, sizeof(qh));
  3658. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3659. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3660. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3661. tmp[3] = table_b2b_0[(qh >> 24) ];
  3662. const v128_t qhl = wasm_v128_load(tmp + 0);
  3663. const v128_t qhh = wasm_v128_load(tmp + 2);
  3664. const v128_t v0 = wasm_v128_load(x0->qs);
  3665. // 4-bit -> 8-bit
  3666. const v128_t v0l = wasm_v128_and (v0, m4b);
  3667. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3668. // add high bit
  3669. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3670. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3671. // load y
  3672. const v128_t v1l = wasm_v128_load(y0->qs);
  3673. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3674. // int8x16 -> int16x8
  3675. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3676. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3677. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3678. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3679. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3680. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3681. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3682. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3683. // dot product
  3684. sumv = wasm_f32x4_add(sumv,
  3685. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3686. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3687. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3688. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3689. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3690. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3691. }
  3692. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3693. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3694. #elif defined(__AVX2__)
  3695. // Initialize accumulator with zeros
  3696. __m256 acc = _mm256_setzero_ps();
  3697. float summs = 0.0f;
  3698. // Main loop
  3699. for (int i = 0; i < nb; i++) {
  3700. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3701. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3702. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3703. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3704. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3705. qx = _mm256_or_si256(qx, bxhi);
  3706. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3707. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3708. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3709. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3710. }
  3711. *s = hsum_float_8(acc) + summs;
  3712. #elif defined(__AVX__)
  3713. // Initialize accumulator with zeros
  3714. __m256 acc = _mm256_setzero_ps();
  3715. __m128i mask = _mm_set1_epi8(0x10);
  3716. float summs = 0.0f;
  3717. // Main loop
  3718. for (int i = 0; i < nb; i++) {
  3719. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3720. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3721. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3722. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3723. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3724. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3725. bxhil = _mm_and_si128(bxhil, mask);
  3726. bxhih = _mm_and_si128(bxhih, mask);
  3727. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3728. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3729. bxl = _mm_or_si128(bxl, bxhil);
  3730. bxh = _mm_or_si128(bxh, bxhih);
  3731. bx_0 = MM256_SET_M128I(bxh, bxl);
  3732. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3733. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3734. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3735. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3736. }
  3737. *s = hsum_float_8(acc) + summs;
  3738. #elif defined(__riscv_v_intrinsic)
  3739. float sumf = 0.0;
  3740. uint32_t qh;
  3741. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3742. // temporary registers for shift operations
  3743. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3744. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3745. for (int i = 0; i < nb; i++) {
  3746. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3747. // load qh
  3748. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3749. // ((qh >> (j + 0)) << 4) & 0x10;
  3750. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3751. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3752. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3753. // ((qh >> (j + 12)) ) & 0x10;
  3754. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3755. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3756. // narrowing
  3757. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3758. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3759. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3760. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3761. // load
  3762. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3763. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3764. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3765. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3766. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3767. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3768. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3769. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3770. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3771. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3772. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3773. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3774. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3775. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3776. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3777. 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);
  3778. }
  3779. *s = sumf;
  3780. #else
  3781. // scalar
  3782. float sumf = 0.0;
  3783. for (int i = 0; i < nb; i++) {
  3784. uint32_t qh;
  3785. memcpy(&qh, x[i].qh, sizeof(qh));
  3786. int sumi = 0;
  3787. for (int j = 0; j < qk/2; ++j) {
  3788. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3789. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3790. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3791. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3792. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3793. }
  3794. 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);
  3795. }
  3796. *s = sumf;
  3797. #endif
  3798. }
  3799. 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) {
  3800. const int qk = QK8_0;
  3801. const int nb = n / qk;
  3802. assert(n % qk == 0);
  3803. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3804. assert((nrc == 2) || (nrc == 1));
  3805. #else
  3806. assert(nrc == 1);
  3807. #endif
  3808. UNUSED(nrc);
  3809. UNUSED(bx);
  3810. UNUSED(by);
  3811. UNUSED(bs);
  3812. const block_q8_0 * restrict x = vx;
  3813. const block_q8_0 * restrict y = vy;
  3814. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3815. if (nrc == 2) {
  3816. const block_q8_0 * restrict vx0 = vx;
  3817. const block_q8_0 * restrict vx1 = vx + bx;
  3818. const block_q8_0 * restrict vy0 = vy;
  3819. const block_q8_0 * restrict vy1 = vy + by;
  3820. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3821. for (int i = 0; i < nb; i++) {
  3822. const block_q8_0 * restrict b_x0 = &vx0[i];
  3823. const block_q8_0 * restrict b_y0 = &vy0[i];
  3824. const block_q8_0 * restrict b_x1 = &vx1[i];
  3825. const block_q8_0 * restrict b_y1 = &vy1[i];
  3826. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3827. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3828. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3829. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3830. // load y
  3831. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3832. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3833. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3834. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3835. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3836. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3837. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3838. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3839. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3840. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3841. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3842. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3843. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3844. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3845. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3846. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3847. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3848. l1, r1)), l2, r2)), l3, r3))), scale);
  3849. }
  3850. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3851. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3852. vst1_f32(s, vget_low_f32(sumv2));
  3853. vst1_f32(s + bs, vget_high_f32(sumv2));
  3854. return;
  3855. }
  3856. #endif
  3857. #if defined(__ARM_NEON)
  3858. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3859. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3860. assert(nb % 2 == 0); // TODO: handle odd nb
  3861. for (int i = 0; i < nb; i += 2) {
  3862. const block_q8_0 * restrict x0 = &x[i + 0];
  3863. const block_q8_0 * restrict x1 = &x[i + 1];
  3864. const block_q8_0 * restrict y0 = &y[i + 0];
  3865. const block_q8_0 * restrict y1 = &y[i + 1];
  3866. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3867. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3868. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3869. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3870. // load y
  3871. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3872. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3873. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3874. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3875. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3876. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3877. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3878. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3879. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3880. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3881. }
  3882. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3883. #elif defined(__AVX2__) || defined(__AVX__)
  3884. // Initialize accumulator with zeros
  3885. __m256 acc = _mm256_setzero_ps();
  3886. // Main loop
  3887. for (int i = 0; i < nb; ++i) {
  3888. // Compute combined scale for the block
  3889. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3890. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3891. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3892. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3893. // Multiply q with scale and accumulate
  3894. #if defined(__AVX2__)
  3895. acc = _mm256_fmadd_ps( d, q, acc );
  3896. #else
  3897. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3898. #endif
  3899. }
  3900. *s = hsum_float_8(acc);
  3901. #elif defined(__riscv_v_intrinsic)
  3902. float sumf = 0.0;
  3903. size_t vl = __riscv_vsetvl_e8m1(qk);
  3904. for (int i = 0; i < nb; i++) {
  3905. // load elements
  3906. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3907. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3908. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  3909. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3910. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3911. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3912. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3913. }
  3914. *s = sumf;
  3915. #else
  3916. // scalar
  3917. float sumf = 0.0;
  3918. for (int i = 0; i < nb; i++) {
  3919. int sumi = 0;
  3920. for (int j = 0; j < qk; j++) {
  3921. sumi += x[i].qs[j]*y[i].qs[j];
  3922. }
  3923. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3924. }
  3925. *s = sumf;
  3926. #endif
  3927. }
  3928. #if QK_K == 256
  3929. 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) {
  3930. assert(nrc == 1);
  3931. UNUSED(nrc);
  3932. UNUSED(bx);
  3933. UNUSED(by);
  3934. UNUSED(bs);
  3935. const block_q2_K * restrict x = vx;
  3936. const block_q8_K * restrict y = vy;
  3937. const int nb = n / QK_K;
  3938. #ifdef __ARM_NEON
  3939. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3940. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3941. const int32x4_t vzero = vdupq_n_s32(0);
  3942. ggml_int8x16x2_t q2bytes;
  3943. uint8_t aux[16];
  3944. float sum = 0;
  3945. for (int i = 0; i < nb; ++i) {
  3946. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3947. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3948. const uint8_t * restrict q2 = x[i].qs;
  3949. const int8_t * restrict q8 = y[i].qs;
  3950. const uint8_t * restrict sc = x[i].scales;
  3951. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3952. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3953. vst1q_u8(aux, scales);
  3954. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3955. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3956. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3957. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3958. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3959. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3960. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3961. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3962. int isum = 0;
  3963. int is = 0;
  3964. // We use this macro instead of a function call because for some reason
  3965. // the code runs 2-3% slower, even if the function is declared inline
  3966. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3967. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3968. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3969. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3970. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3971. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3972. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3973. MULTIPLY_ACCUM_WITH_SCALE((index));
  3974. for (int j = 0; j < QK_K/128; ++j) {
  3975. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3976. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3977. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3978. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3979. MULTIPLY_ACCUM_WITH_SCALE(0);
  3980. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3981. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3982. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3983. is += 8;
  3984. }
  3985. sum += d * isum;
  3986. }
  3987. *s = sum;
  3988. #elif defined __AVX2__
  3989. const __m256i m3 = _mm256_set1_epi8(3);
  3990. const __m128i m4 = _mm_set1_epi8(0xF);
  3991. __m256 acc = _mm256_setzero_ps();
  3992. for (int i = 0; i < nb; ++i) {
  3993. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3994. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3995. const uint8_t * restrict q2 = x[i].qs;
  3996. const int8_t * restrict q8 = y[i].qs;
  3997. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3998. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3999. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4000. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4001. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4002. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4003. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4004. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4005. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4006. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4007. __m256i sumi = _mm256_setzero_si256();
  4008. for (int j = 0; j < QK_K/128; ++j) {
  4009. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4010. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4011. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4012. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4013. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4014. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4015. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4016. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4017. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4018. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4019. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4020. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4021. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4022. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4023. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4024. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4025. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4026. p0 = _mm256_add_epi32(p0, p1);
  4027. p2 = _mm256_add_epi32(p2, p3);
  4028. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4029. }
  4030. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4031. }
  4032. *s = hsum_float_8(acc);
  4033. #elif defined __AVX__
  4034. const __m128i m3 = _mm_set1_epi8(0x3);
  4035. const __m128i m4 = _mm_set1_epi8(0xF);
  4036. const __m128i m2 = _mm_set1_epi8(0x2);
  4037. __m256 acc = _mm256_setzero_ps();
  4038. for (int i = 0; i < nb; ++i) {
  4039. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4040. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4041. const uint8_t * restrict q2 = x[i].qs;
  4042. const int8_t * restrict q8 = y[i].qs;
  4043. // load mins and scales from block_q2_K.scales[QK_K/16]
  4044. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4045. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4046. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4047. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4048. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4049. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4050. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4051. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4052. // sumf += -dmin * summs in 32bits*8
  4053. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4054. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4055. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4056. const __m128i scales[2] = { scales_0, scales_1 };
  4057. __m128i sumi_0 = _mm_setzero_si128();
  4058. __m128i sumi_1 = _mm_setzero_si128();
  4059. for (int j = 0; j < QK_K/128; ++j) {
  4060. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4061. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4062. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4063. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4064. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4065. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4066. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4067. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4068. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4069. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4070. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4071. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4072. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4073. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4074. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4075. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4076. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4077. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4078. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4079. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4080. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4081. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4082. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4083. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4084. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4085. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4086. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4087. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4088. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4089. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4090. __m128i shuffle = _mm_set1_epi16(0x0100);
  4091. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4092. shuffle = _mm_add_epi16(shuffle, m2);
  4093. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4094. shuffle = _mm_add_epi16(shuffle, m2);
  4095. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4096. shuffle = _mm_add_epi16(shuffle, m2);
  4097. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4098. shuffle = _mm_add_epi16(shuffle, m2);
  4099. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4100. shuffle = _mm_add_epi16(shuffle, m2);
  4101. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4102. shuffle = _mm_add_epi16(shuffle, m2);
  4103. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4104. shuffle = _mm_add_epi16(shuffle, m2);
  4105. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4106. p0 = _mm_add_epi32(p0, p1);
  4107. p2 = _mm_add_epi32(p2, p3);
  4108. p4 = _mm_add_epi32(p4, p5);
  4109. p6 = _mm_add_epi32(p6, p7);
  4110. // isum in 32bits*4*2
  4111. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4112. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4113. }
  4114. // sumf += dall * isum - dmin * summs in 32bits
  4115. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4116. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4117. }
  4118. *s = hsum_float_8(acc);
  4119. #elif defined __riscv_v_intrinsic
  4120. float sumf = 0;
  4121. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4122. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4123. for (int i = 0; i < nb; ++i) {
  4124. const uint8_t * q2 = x[i].qs;
  4125. const int8_t * q8 = y[i].qs;
  4126. const uint8_t * sc = x[i].scales;
  4127. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4128. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4129. size_t vl = 16;
  4130. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4131. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4132. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4133. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4134. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4135. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4136. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4137. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4138. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4139. vl = 32;
  4140. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4141. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4142. uint8_t is=0;
  4143. int isum=0;
  4144. for (int j = 0; j < QK_K/128; ++j) {
  4145. // load Q2
  4146. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4147. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4148. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4149. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4150. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4151. // duplicate scale elements for product
  4152. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4153. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4154. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4155. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4156. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4157. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4158. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4159. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4160. // load Q8
  4161. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4162. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4163. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4164. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4165. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4166. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4167. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4168. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4169. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4170. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4171. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4172. q2+=32; q8+=128; is=8;
  4173. }
  4174. sumf += dall * isum;
  4175. }
  4176. *s = sumf;
  4177. #else
  4178. float sumf = 0;
  4179. for (int i = 0; i < nb; ++i) {
  4180. const uint8_t * q2 = x[i].qs;
  4181. const int8_t * q8 = y[i].qs;
  4182. const uint8_t * sc = x[i].scales;
  4183. int summs = 0;
  4184. for (int j = 0; j < 16; ++j) {
  4185. summs += y[i].bsums[j] * (sc[j] >> 4);
  4186. }
  4187. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4188. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4189. int isum = 0;
  4190. int is = 0;
  4191. int d;
  4192. for (int k = 0; k < QK_K/128; ++k) {
  4193. int shift = 0;
  4194. for (int j = 0; j < 4; ++j) {
  4195. d = sc[is++] & 0xF;
  4196. int isuml = 0;
  4197. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4198. isum += d * isuml;
  4199. d = sc[is++] & 0xF;
  4200. isuml = 0;
  4201. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4202. isum += d * isuml;
  4203. shift += 2;
  4204. q8 += 32;
  4205. }
  4206. q2 += 32;
  4207. }
  4208. sumf += dall * isum - dmin * summs;
  4209. }
  4210. *s = sumf;
  4211. #endif
  4212. }
  4213. #else
  4214. 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) {
  4215. assert(nrc == 1);
  4216. UNUSED(nrc);
  4217. UNUSED(bx);
  4218. UNUSED(by);
  4219. UNUSED(bs);
  4220. const block_q2_K * restrict x = vx;
  4221. const block_q8_K * restrict y = vy;
  4222. const int nb = n / QK_K;
  4223. #ifdef __ARM_NEON
  4224. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4225. const int32x4_t vzero = vdupq_n_s32(0);
  4226. ggml_int8x16x4_t q2bytes;
  4227. uint32_t aux32[2];
  4228. const uint8_t * scales = (const uint8_t *)aux32;
  4229. float sum = 0;
  4230. for (int i = 0; i < nb; ++i) {
  4231. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4232. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4233. const uint8_t * restrict q2 = x[i].qs;
  4234. const int8_t * restrict q8 = y[i].qs;
  4235. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4236. aux32[0] = sc[0] & 0x0f0f0f0f;
  4237. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4238. 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]);
  4239. int isum1 = 0, isum2 = 0;
  4240. const uint8x16_t q2bits = vld1q_u8(q2);
  4241. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4242. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4243. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4244. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4245. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4246. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4247. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4248. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4249. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4250. sum += d * (isum1 + isum2);
  4251. }
  4252. *s = sum;
  4253. #elif defined __AVX2__
  4254. const __m256i m3 = _mm256_set1_epi8(3);
  4255. __m256 acc = _mm256_setzero_ps();
  4256. uint32_t ud, um;
  4257. const uint8_t * restrict db = (const uint8_t *)&ud;
  4258. const uint8_t * restrict mb = (const uint8_t *)&um;
  4259. float summs = 0;
  4260. // TODO: optimize this
  4261. for (int i = 0; i < nb; ++i) {
  4262. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4263. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4264. const uint8_t * restrict q2 = x[i].qs;
  4265. const int8_t * restrict q8 = y[i].qs;
  4266. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4267. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4268. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4269. 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];
  4270. summs += dmin * smin;
  4271. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4272. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4273. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4274. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4275. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4276. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4277. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4278. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4279. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4280. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4281. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4282. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4283. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4284. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4285. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4286. }
  4287. *s = hsum_float_8(acc) + summs;
  4288. #elif defined __AVX__
  4289. const __m128i m3 = _mm_set1_epi8(3);
  4290. __m256 acc = _mm256_setzero_ps();
  4291. uint32_t ud, um;
  4292. const uint8_t * restrict db = (const uint8_t *)&ud;
  4293. const uint8_t * restrict mb = (const uint8_t *)&um;
  4294. float summs = 0;
  4295. // TODO: optimize this
  4296. for (int i = 0; i < nb; ++i) {
  4297. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4298. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4299. const uint8_t * restrict q2 = x[i].qs;
  4300. const int8_t * restrict q8 = y[i].qs;
  4301. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4302. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4303. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4304. 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];
  4305. summs += dmin * smin;
  4306. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4307. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4308. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4309. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4310. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4311. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4312. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4313. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4314. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4315. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4316. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4317. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4318. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4319. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4320. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4321. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4322. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4323. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4324. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4325. }
  4326. *s = hsum_float_8(acc) + summs;
  4327. #elif defined __riscv_v_intrinsic
  4328. uint32_t aux32[2];
  4329. const uint8_t * scales = (const uint8_t *)aux32;
  4330. float sumf = 0;
  4331. for (int i = 0; i < nb; ++i) {
  4332. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4333. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4334. const uint8_t * restrict q2 = x[i].qs;
  4335. const int8_t * restrict q8 = y[i].qs;
  4336. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4337. aux32[0] = sc[0] & 0x0f0f0f0f;
  4338. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4339. 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]);
  4340. int isum1 = 0;
  4341. int isum2 = 0;
  4342. size_t vl = 16;
  4343. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4344. // load Q2
  4345. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4346. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4347. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4348. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4349. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4350. // load Q8, and take product with Q2
  4351. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4352. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4353. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4354. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4355. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4356. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4357. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4358. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4359. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4360. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4361. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4362. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4363. sumf += d * (isum1 + isum2);
  4364. }
  4365. *s = sumf;
  4366. #else
  4367. float sumf = 0;
  4368. int isum[QK_K/16];
  4369. for (int i = 0; i < nb; ++i) {
  4370. const uint8_t * q2 = x[i].qs;
  4371. const int8_t * q8 = y[i].qs;
  4372. const uint8_t * sc = x[i].scales;
  4373. int summs = 0;
  4374. for (int j = 0; j < QK_K/16; ++j) {
  4375. summs += y[i].bsums[j] * (sc[j] >> 4);
  4376. }
  4377. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4378. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4379. memset(isum, 0, (QK_K/16)*sizeof(int));
  4380. for (int l = 0; l < 16; ++l) {
  4381. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4382. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4383. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4384. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4385. }
  4386. for (int l = 0; l < QK_K/16; ++l) {
  4387. isum[l] *= (sc[l] & 0xF);
  4388. }
  4389. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4390. }
  4391. *s = sumf;
  4392. #endif
  4393. }
  4394. #endif
  4395. #if QK_K == 256
  4396. 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) {
  4397. assert(n % QK_K == 0);
  4398. assert(nrc == 1);
  4399. UNUSED(nrc);
  4400. UNUSED(bx);
  4401. UNUSED(by);
  4402. UNUSED(bs);
  4403. const uint32_t kmask1 = 0x03030303;
  4404. const uint32_t kmask2 = 0x0f0f0f0f;
  4405. const block_q3_K * restrict x = vx;
  4406. const block_q8_K * restrict y = vy;
  4407. const int nb = n / QK_K;
  4408. #ifdef __ARM_NEON
  4409. uint32_t aux[3];
  4410. uint32_t utmp[4];
  4411. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4412. const int32x4_t vzero = vdupq_n_s32(0);
  4413. const uint8x16_t m0 = vdupq_n_u8(1);
  4414. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4415. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4416. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4417. const int8_t m32 = 32;
  4418. ggml_int8x16x4_t q3bytes;
  4419. float sum = 0;
  4420. for (int i = 0; i < nb; ++i) {
  4421. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4422. const uint8_t * restrict q3 = x[i].qs;
  4423. const uint8_t * restrict qh = x[i].hmask;
  4424. const int8_t * restrict q8 = y[i].qs;
  4425. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4426. ggml_uint8x16x4_t q3h;
  4427. int32_t isum = 0;
  4428. // Set up scales
  4429. memcpy(aux, x[i].scales, 12);
  4430. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4431. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4432. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4433. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4434. int8_t * scale = (int8_t *)utmp;
  4435. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4436. for (int j = 0; j < QK_K/128; ++j) {
  4437. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4438. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4439. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4440. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4441. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4442. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4443. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4444. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4445. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4446. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4447. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4448. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4449. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4450. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4451. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4452. scale += 4;
  4453. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4454. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4455. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4456. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4457. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4458. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4459. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4460. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4461. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4462. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4463. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4464. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4465. scale += 4;
  4466. if (j == 0) {
  4467. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4468. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4469. }
  4470. }
  4471. sum += d * isum;
  4472. }
  4473. *s = sum;
  4474. #elif defined __AVX2__
  4475. const __m256i m3 = _mm256_set1_epi8(3);
  4476. const __m256i mone = _mm256_set1_epi8(1);
  4477. const __m128i m32 = _mm_set1_epi8(32);
  4478. __m256 acc = _mm256_setzero_ps();
  4479. uint32_t aux[3];
  4480. for (int i = 0; i < nb; ++i) {
  4481. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4482. const uint8_t * restrict q3 = x[i].qs;
  4483. const int8_t * restrict q8 = y[i].qs;
  4484. // Set up scales
  4485. memcpy(aux, x[i].scales, 12);
  4486. __m128i scales128 = _mm_set_epi32(
  4487. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4488. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4489. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4490. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4491. scales128 = _mm_sub_epi8(scales128, m32);
  4492. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4493. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4494. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4495. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4496. // high bit
  4497. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4498. // integer accumulator
  4499. __m256i sumi = _mm256_setzero_si256();
  4500. int bit = 0;
  4501. int is = 0;
  4502. for (int j = 0; j < QK_K/128; ++j) {
  4503. // load low 2 bits
  4504. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4505. // prepare low and high bits
  4506. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4507. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4508. ++bit;
  4509. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4510. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4511. ++bit;
  4512. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4513. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4514. ++bit;
  4515. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4516. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4517. ++bit;
  4518. // load Q8 quants
  4519. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4520. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4521. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4522. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4523. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4524. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4525. // and 2 if the high bit was set)
  4526. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4527. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4528. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4529. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4530. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4531. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4532. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4533. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4534. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4535. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4536. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4537. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4538. // multiply with scales
  4539. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4540. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4541. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4542. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4543. // accumulate
  4544. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4545. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4546. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4547. }
  4548. // multiply with block scale and accumulate
  4549. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4550. }
  4551. *s = hsum_float_8(acc);
  4552. #elif defined __AVX__
  4553. const __m128i m3 = _mm_set1_epi8(3);
  4554. const __m128i mone = _mm_set1_epi8(1);
  4555. const __m128i m32 = _mm_set1_epi8(32);
  4556. const __m128i m2 = _mm_set1_epi8(2);
  4557. __m256 acc = _mm256_setzero_ps();
  4558. const uint32_t *aux;
  4559. for (int i = 0; i < nb; ++i) {
  4560. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4561. const uint8_t * restrict q3 = x[i].qs;
  4562. const int8_t * restrict q8 = y[i].qs;
  4563. // Set up scales
  4564. aux = (const uint32_t *)x[i].scales;
  4565. __m128i scales128 = _mm_set_epi32(
  4566. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4567. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4568. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4569. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4570. scales128 = _mm_sub_epi8(scales128, m32);
  4571. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4572. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4573. const __m128i scales[2] = { scales_0, scales_1 };
  4574. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4575. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4576. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4577. // integer accumulator
  4578. __m128i sumi_0 = _mm_setzero_si128();
  4579. __m128i sumi_1 = _mm_setzero_si128();
  4580. for (int j = 0; j < QK_K/128; ++j) {
  4581. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4582. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4583. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4584. // prepare low and high bits
  4585. const int bit = j << 2;
  4586. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4587. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4588. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4589. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4590. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4591. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4592. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4593. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4594. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4595. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4596. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4597. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4598. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4599. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4600. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4601. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4602. // load Q8 quants from block_q8_K.qs[QK_K]
  4603. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4604. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4605. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4606. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4607. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4608. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4609. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4610. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4611. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4612. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4613. // and 2 if the high bit was set)
  4614. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4615. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4616. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4617. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4618. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4619. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4620. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4621. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4622. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4623. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4624. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4625. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4626. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4627. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4628. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4629. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4630. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4631. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4632. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4633. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4634. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4635. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4636. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4637. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4638. // multiply with scales
  4639. __m128i shuffle = _mm_set1_epi16(0x0100);
  4640. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4641. shuffle = _mm_add_epi16(shuffle, m2);
  4642. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4643. shuffle = _mm_add_epi16(shuffle, m2);
  4644. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4645. shuffle = _mm_add_epi16(shuffle, m2);
  4646. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4647. shuffle = _mm_add_epi16(shuffle, m2);
  4648. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4649. shuffle = _mm_add_epi16(shuffle, m2);
  4650. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4651. shuffle = _mm_add_epi16(shuffle, m2);
  4652. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4653. shuffle = _mm_add_epi16(shuffle, m2);
  4654. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4655. // accumulate
  4656. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4657. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4658. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4659. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4660. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4661. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4662. }
  4663. // multiply with block scale and accumulate
  4664. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4665. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4666. }
  4667. *s = hsum_float_8(acc);
  4668. #elif defined __riscv_v_intrinsic
  4669. uint32_t aux[3];
  4670. uint32_t utmp[4];
  4671. float sumf = 0;
  4672. for (int i = 0; i < nb; ++i) {
  4673. const uint8_t * restrict q3 = x[i].qs;
  4674. const uint8_t * restrict qh = x[i].hmask;
  4675. const int8_t * restrict q8 = y[i].qs;
  4676. memcpy(aux, x[i].scales, 12);
  4677. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4678. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4679. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4680. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4681. int8_t * scale = (int8_t *)utmp;
  4682. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4683. size_t vl = 32;
  4684. uint8_t m = 1;
  4685. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4686. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4687. int sum_t = 0;
  4688. for (int j = 0; j < QK_K; j += 128) {
  4689. vl = 32;
  4690. // load Q3
  4691. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4692. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4693. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4694. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4695. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4696. // compute mask for subtraction
  4697. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4698. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4699. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4700. m <<= 1;
  4701. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4702. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4703. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4704. m <<= 1;
  4705. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4706. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4707. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4708. m <<= 1;
  4709. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4710. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4711. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4712. m <<= 1;
  4713. // load Q8 and take product with Q3
  4714. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4715. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4716. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4717. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4718. vl = 16;
  4719. // retrieve lane to multiply with scale
  4720. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4721. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4722. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4723. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4724. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4725. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4726. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4727. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4728. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4729. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4730. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4731. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4732. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4733. q3 += 32; q8 += 128; scale += 8;
  4734. }
  4735. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4736. sumf += d*sum_t;
  4737. }
  4738. *s = sumf;
  4739. #else
  4740. // scalar version
  4741. // This function is written like this so the compiler can manage to vectorize most of it
  4742. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4743. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4744. // The ideal situation would be if we could just write the code once, and the compiler would
  4745. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4746. // write vectorized versions for AVX, ARM_NEON, etc.
  4747. int8_t aux8[QK_K];
  4748. int16_t aux16[8];
  4749. float sums [8];
  4750. int32_t aux32[8];
  4751. memset(sums, 0, 8*sizeof(float));
  4752. uint32_t auxs[4];
  4753. const int8_t * scales = (const int8_t*)auxs;
  4754. float sumf = 0;
  4755. for (int i = 0; i < nb; ++i) {
  4756. const uint8_t * restrict q3 = x[i].qs;
  4757. const uint8_t * restrict hm = x[i].hmask;
  4758. const int8_t * restrict q8 = y[i].qs;
  4759. memset(aux32, 0, 8*sizeof(int32_t));
  4760. int8_t * restrict a = aux8;
  4761. uint8_t m = 1;
  4762. for (int j = 0; j < QK_K; j += 128) {
  4763. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4764. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4765. a += 32; m <<= 1;
  4766. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4767. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4768. a += 32; m <<= 1;
  4769. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4770. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4771. a += 32; m <<= 1;
  4772. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4773. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4774. a += 32; m <<= 1;
  4775. q3 += 32;
  4776. }
  4777. a = aux8;
  4778. memcpy(auxs, x[i].scales, 12);
  4779. uint32_t tmp = auxs[2];
  4780. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4781. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4782. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4783. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4784. for (int j = 0; j < QK_K/16; ++j) {
  4785. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4786. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4787. q8 += 8; a += 8;
  4788. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4789. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4790. q8 += 8; a += 8;
  4791. }
  4792. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4793. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4794. }
  4795. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4796. *s = sumf;
  4797. #endif
  4798. }
  4799. #else
  4800. 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) {
  4801. assert(n % QK_K == 0);
  4802. assert(nrc == 1);
  4803. UNUSED(nrc);
  4804. UNUSED(bx);
  4805. UNUSED(by);
  4806. UNUSED(bs);
  4807. const block_q3_K * restrict x = vx;
  4808. const block_q8_K * restrict y = vy;
  4809. const int nb = n / QK_K;
  4810. #ifdef __ARM_NEON
  4811. const int32x4_t vzero = vdupq_n_s32(0);
  4812. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4813. const uint8x16_t mh = vdupq_n_u8(4);
  4814. ggml_int8x16x4_t q3bytes;
  4815. uint16_t aux16[2];
  4816. int8_t * scales = (int8_t *)aux16;
  4817. float sum = 0;
  4818. for (int i = 0; i < nb; ++i) {
  4819. ggml_uint8x16x4_t q3h;
  4820. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4821. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4822. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4823. const uint16_t a = *(const uint16_t *)x[i].scales;
  4824. aux16[0] = a & 0x0f0f;
  4825. aux16[1] = (a >> 4) & 0x0f0f;
  4826. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4827. 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]);
  4828. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4829. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4830. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4831. q3h.val[1] = vandq_u8(mh, htmp);
  4832. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4833. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4834. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4835. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4836. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4837. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4838. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4839. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4840. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4841. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4842. sum += d * isum;
  4843. }
  4844. *s = sum;
  4845. #elif defined __AVX2__
  4846. const __m256i m3 = _mm256_set1_epi8(3);
  4847. const __m256i m1 = _mm256_set1_epi8(1);
  4848. __m256 acc = _mm256_setzero_ps();
  4849. uint64_t aux64;
  4850. uint16_t aux16[2];
  4851. const int8_t * aux8 = (const int8_t *)aux16;
  4852. for (int i = 0; i < nb; ++i) {
  4853. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4854. const uint8_t * restrict q3 = x[i].qs;
  4855. const int8_t * restrict q8 = y[i].qs;
  4856. const uint16_t a = *(const uint16_t *)x[i].scales;
  4857. aux16[0] = a & 0x0f0f;
  4858. aux16[1] = (a >> 4) & 0x0f0f;
  4859. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4860. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4861. memcpy(&aux64, x[i].hmask, 8);
  4862. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4863. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4864. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4865. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4866. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4867. // load low 2 bits
  4868. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4869. // prepare low and high bits
  4870. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4871. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4872. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4873. // load Q8 quants
  4874. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4875. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4876. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4877. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4878. // and 2 if the high bit was set)
  4879. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4880. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4881. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4882. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4883. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4884. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4885. // multiply with scales
  4886. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4887. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4888. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4889. // multiply with block scale and accumulate
  4890. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4891. }
  4892. *s = hsum_float_8(acc);
  4893. #elif defined __AVX__
  4894. const __m128i m3 = _mm_set1_epi8(3);
  4895. const __m128i m1 = _mm_set1_epi8(1);
  4896. __m256 acc = _mm256_setzero_ps();
  4897. uint64_t aux64;
  4898. uint16_t aux16[2];
  4899. const int8_t * aux8 = (const int8_t *)aux16;
  4900. for (int i = 0; i < nb; ++i) {
  4901. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4902. const uint8_t * restrict q3 = x[i].qs;
  4903. const int8_t * restrict q8 = y[i].qs;
  4904. const uint16_t a = *(const uint16_t *)x[i].scales;
  4905. aux16[0] = a & 0x0f0f;
  4906. aux16[1] = (a >> 4) & 0x0f0f;
  4907. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4908. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4909. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4910. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4911. memcpy(&aux64, x[i].hmask, 8);
  4912. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4913. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4914. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4915. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4916. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4917. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4918. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4919. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4920. // load low 2 bits
  4921. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4922. // prepare low and high bits
  4923. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4924. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4925. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4926. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4927. // load Q8 quants
  4928. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4929. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4930. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4931. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4932. // and 2 if the high bit was set)
  4933. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4934. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4935. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4936. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4937. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4938. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4939. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4940. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4941. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4942. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4943. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4944. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4945. // multiply with scales
  4946. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4947. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4948. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4949. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4950. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4951. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4952. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4953. // multiply with block scale and accumulate
  4954. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4955. }
  4956. *s = hsum_float_8(acc);
  4957. #elif defined __riscv_v_intrinsic
  4958. uint16_t aux16[2];
  4959. int8_t * scales = (int8_t *)aux16;
  4960. float sumf = 0;
  4961. for (int i = 0; i < nb; ++i) {
  4962. const uint8_t * restrict q3 = x[i].qs;
  4963. const int8_t * restrict q8 = y[i].qs;
  4964. const uint16_t a = *(const uint16_t *)x[i].scales;
  4965. aux16[0] = a & 0x0f0f;
  4966. aux16[1] = (a >> 4) & 0x0f0f;
  4967. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4968. 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]);
  4969. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4970. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4971. // load qh
  4972. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4973. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4974. size_t vl = 16;
  4975. // extend and combine both qh_x1 and qh_x2
  4976. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4977. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4978. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4979. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4980. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4981. // load Q3
  4982. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4983. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4984. 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);
  4985. 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);
  4986. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4987. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4988. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4989. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4990. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4991. // load Q8 and take product with Q3
  4992. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4993. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4994. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4995. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4996. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4997. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4998. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4999. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5000. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5001. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5002. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5003. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5004. sumf += d * isum;
  5005. }
  5006. *s = sumf;
  5007. #else
  5008. int8_t aux8[QK_K];
  5009. int16_t aux16[8];
  5010. float sums [8];
  5011. int32_t aux32[8];
  5012. int32_t scales[4];
  5013. memset(sums, 0, 8*sizeof(float));
  5014. float sumf = 0;
  5015. for (int i = 0; i < nb; ++i) {
  5016. const uint8_t * restrict q3 = x[i].qs;
  5017. const uint8_t * restrict hm = x[i].hmask;
  5018. const int8_t * restrict q8 = y[i].qs;
  5019. int8_t * restrict a = aux8;
  5020. for (int l = 0; l < 8; ++l) {
  5021. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5022. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5023. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5024. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5025. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5026. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5027. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5028. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5029. }
  5030. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5031. scales[1] = (x[i].scales[0] >> 4) - 8;
  5032. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5033. scales[3] = (x[i].scales[1] >> 4) - 8;
  5034. memset(aux32, 0, 8*sizeof(int32_t));
  5035. for (int j = 0; j < QK_K/16; ++j) {
  5036. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5037. q8 += 8; a += 8;
  5038. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5039. q8 += 8; a += 8;
  5040. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5041. }
  5042. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5043. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5044. }
  5045. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5046. *s = sumf;
  5047. #endif
  5048. }
  5049. #endif
  5050. #if QK_K == 256
  5051. 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) {
  5052. assert(n % QK_K == 0);
  5053. assert(nrc == 1);
  5054. UNUSED(nrc);
  5055. UNUSED(bx);
  5056. UNUSED(by);
  5057. UNUSED(bs);
  5058. const block_q4_K * restrict x = vx;
  5059. const block_q8_K * restrict y = vy;
  5060. const int nb = n / QK_K;
  5061. static const uint32_t kmask1 = 0x3f3f3f3f;
  5062. static const uint32_t kmask2 = 0x0f0f0f0f;
  5063. static const uint32_t kmask3 = 0x03030303;
  5064. uint32_t utmp[4];
  5065. #ifdef __ARM_NEON
  5066. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5067. const int32x4_t mzero = vdupq_n_s32(0);
  5068. ggml_int8x16x2_t q4bytes;
  5069. ggml_int8x16x2_t q8bytes;
  5070. float sumf = 0;
  5071. for (int i = 0; i < nb; ++i) {
  5072. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5073. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5074. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5075. memcpy(utmp, x[i].scales, 12);
  5076. uint32x2_t mins8 = { 0 };
  5077. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5078. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5079. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5080. utmp[0] &= kmask1;
  5081. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5082. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5083. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5084. sumf -= dmin * vaddvq_s32(prod);
  5085. const uint8_t * scales = (const uint8_t *)utmp;
  5086. const uint8_t * restrict q4 = x[i].qs;
  5087. const int8_t * restrict q8 = y[i].qs;
  5088. int32_t sumi1 = 0;
  5089. int32_t sumi2 = 0;
  5090. for (int j = 0; j < QK_K/64; ++j) {
  5091. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5092. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5093. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5094. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5095. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5096. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5097. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5098. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5099. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5100. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5101. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5102. }
  5103. sumf += d * (sumi1 + sumi2);
  5104. }
  5105. *s = sumf;
  5106. #elif defined __AVX2__
  5107. const __m256i m4 = _mm256_set1_epi8(0xF);
  5108. __m256 acc = _mm256_setzero_ps();
  5109. __m128 acc_m = _mm_setzero_ps();
  5110. for (int i = 0; i < nb; ++i) {
  5111. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5112. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5113. memcpy(utmp, x[i].scales, 12);
  5114. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5115. const uint32_t uaux = utmp[1] & kmask1;
  5116. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5117. utmp[2] = uaux;
  5118. utmp[0] &= kmask1;
  5119. const uint8_t * restrict q4 = x[i].qs;
  5120. const int8_t * restrict q8 = y[i].qs;
  5121. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5122. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5123. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5124. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5125. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5126. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5127. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5128. __m256i sumi = _mm256_setzero_si256();
  5129. for (int j = 0; j < QK_K/64; ++j) {
  5130. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5131. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5132. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5133. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5134. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5135. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5136. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5137. p16l = _mm256_madd_epi16(scale_l, p16l);
  5138. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5139. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5140. p16h = _mm256_madd_epi16(scale_h, p16h);
  5141. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5142. sumi = _mm256_add_epi32(sumi, sumj);
  5143. }
  5144. __m256 vd = _mm256_set1_ps(d);
  5145. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5146. }
  5147. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5148. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5149. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5150. #elif defined __AVX__
  5151. const __m128i m4 = _mm_set1_epi8(0xF);
  5152. const __m128i m2 = _mm_set1_epi8(0x2);
  5153. __m256 acc = _mm256_setzero_ps();
  5154. __m128 acc_m = _mm_setzero_ps();
  5155. for (int i = 0; i < nb; ++i) {
  5156. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5157. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5158. const uint8_t * restrict q4 = x[i].qs;
  5159. const int8_t * restrict q8 = y[i].qs;
  5160. memcpy(utmp, x[i].scales, 12);
  5161. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5162. const uint32_t uaux = utmp[1] & kmask1;
  5163. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5164. utmp[2] = uaux;
  5165. utmp[0] &= kmask1;
  5166. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5167. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5168. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5169. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5170. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5171. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5172. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5173. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5174. __m128i sumi_0 = _mm_setzero_si128();
  5175. __m128i sumi_1 = _mm_setzero_si128();
  5176. __m128i shuffle = _mm_set1_epi16(0x0100);
  5177. for (int j = 0; j < QK_K/64; ++j) {
  5178. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5179. shuffle = _mm_add_epi16(shuffle, m2);
  5180. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5181. shuffle = _mm_add_epi16(shuffle, m2);
  5182. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5183. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5184. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5185. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5186. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5187. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5188. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5189. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5190. p16l = _mm_madd_epi16(scale_l, p16l);
  5191. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5192. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5193. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5194. p16l = _mm_madd_epi16(scale_l, p16l);
  5195. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5196. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5197. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5198. p16h = _mm_madd_epi16(scale_h, p16h);
  5199. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5200. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5201. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5202. p16h = _mm_madd_epi16(scale_h, p16h);
  5203. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5204. }
  5205. __m256 vd = _mm256_set1_ps(d);
  5206. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5207. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5208. }
  5209. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5210. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5211. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5212. #elif defined __riscv_v_intrinsic
  5213. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5214. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5215. float sumf = 0;
  5216. for (int i = 0; i < nb; ++i) {
  5217. size_t vl = 8;
  5218. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5219. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5220. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5221. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5222. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5223. memcpy(utmp, x[i].scales, 12);
  5224. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5225. const uint32_t uaux = utmp[1] & kmask1;
  5226. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5227. utmp[2] = uaux;
  5228. utmp[0] &= kmask1;
  5229. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5230. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5231. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5232. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5233. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5234. const uint8_t * restrict q4 = x[i].qs;
  5235. const int8_t * restrict q8 = y[i].qs;
  5236. vl = 32;
  5237. int32_t sum_1 = 0;
  5238. int32_t sum_2 = 0;
  5239. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5240. for (int j = 0; j < QK_K/64; ++j) {
  5241. // load Q4
  5242. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5243. // load Q8 and multiply it with lower Q4 nibble
  5244. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5245. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5246. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5247. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5248. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5249. // load Q8 and multiply it with upper Q4 nibble
  5250. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5251. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5252. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5253. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5254. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5255. q4 += 32; q8 += 64;
  5256. }
  5257. sumf += d*(sum_1 + sum_2);
  5258. }
  5259. *s = sumf;
  5260. #else
  5261. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5262. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5263. int8_t aux8[QK_K];
  5264. int16_t aux16[8];
  5265. float sums [8];
  5266. int32_t aux32[8];
  5267. memset(sums, 0, 8*sizeof(float));
  5268. float sumf = 0;
  5269. for (int i = 0; i < nb; ++i) {
  5270. const uint8_t * restrict q4 = x[i].qs;
  5271. const int8_t * restrict q8 = y[i].qs;
  5272. memset(aux32, 0, 8*sizeof(int32_t));
  5273. int8_t * restrict a = aux8;
  5274. for (int j = 0; j < QK_K/64; ++j) {
  5275. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5276. a += 32;
  5277. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5278. a += 32; q4 += 32;
  5279. }
  5280. memcpy(utmp, x[i].scales, 12);
  5281. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5282. const uint32_t uaux = utmp[1] & kmask1;
  5283. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5284. utmp[2] = uaux;
  5285. utmp[0] &= kmask1;
  5286. int sumi = 0;
  5287. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5288. a = aux8;
  5289. int is = 0;
  5290. for (int j = 0; j < QK_K/32; ++j) {
  5291. int32_t scale = scales[is++];
  5292. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5293. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5294. q8 += 8; a += 8;
  5295. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5296. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5297. q8 += 8; a += 8;
  5298. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5299. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5300. q8 += 8; a += 8;
  5301. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5302. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5303. q8 += 8; a += 8;
  5304. }
  5305. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5306. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5307. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5308. sumf -= dmin * sumi;
  5309. }
  5310. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5311. *s = sumf;
  5312. #endif
  5313. }
  5314. #else
  5315. 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) {
  5316. assert(n % QK_K == 0);
  5317. assert(nrc == 1);
  5318. UNUSED(nrc);
  5319. UNUSED(bx);
  5320. UNUSED(by);
  5321. UNUSED(bs);
  5322. const block_q4_K * restrict x = vx;
  5323. const block_q8_K * restrict y = vy;
  5324. const int nb = n / QK_K;
  5325. #ifdef __ARM_NEON
  5326. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5327. const int32x4_t mzero = vdupq_n_s32(0);
  5328. float sumf = 0;
  5329. ggml_int8x16x2_t q4bytes;
  5330. ggml_int8x16x4_t q8bytes;
  5331. float sum_mins = 0.f;
  5332. uint16_t aux16[2];
  5333. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5334. for (int i = 0; i < nb; ++i) {
  5335. const uint8_t * restrict q4 = x[i].qs;
  5336. const int8_t * restrict q8 = y[i].qs;
  5337. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5338. aux16[0] = a[0] & 0x0f0f;
  5339. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5340. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5341. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5342. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5343. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5344. q8bytes = ggml_vld1q_s8_x4(q8);
  5345. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5346. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5347. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5348. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5349. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5350. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5351. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5352. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5353. sumf += d * (sumi1 + sumi2);
  5354. }
  5355. *s = sumf - sum_mins;
  5356. #elif defined __AVX2__
  5357. const __m256i m4 = _mm256_set1_epi8(0xF);
  5358. __m256 acc = _mm256_setzero_ps();
  5359. float summs = 0;
  5360. uint16_t aux16[2];
  5361. const uint8_t * scales = (const uint8_t *)aux16;
  5362. for (int i = 0; i < nb; ++i) {
  5363. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5364. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5365. const __m256 vd = _mm256_set1_ps(d);
  5366. const uint16_t * a = (const uint16_t *)x[i].scales;
  5367. aux16[0] = a[0] & 0x0f0f;
  5368. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5369. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5370. const uint8_t * restrict q4 = x[i].qs;
  5371. const int8_t * restrict q8 = y[i].qs;
  5372. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5373. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5374. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5375. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5376. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5377. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5378. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5379. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5380. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5381. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5382. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5383. }
  5384. *s = hsum_float_8(acc) - summs;
  5385. #elif defined __AVX__
  5386. const __m128i m4 = _mm_set1_epi8(0xF);
  5387. __m256 acc = _mm256_setzero_ps();
  5388. float summs = 0;
  5389. uint16_t aux16[2];
  5390. const uint8_t * scales = (const uint8_t *)aux16;
  5391. for (int i = 0; i < nb; ++i) {
  5392. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5393. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5394. const __m256 vd = _mm256_set1_ps(d);
  5395. const uint16_t * a = (const uint16_t *)x[i].scales;
  5396. aux16[0] = a[0] & 0x0f0f;
  5397. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5398. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5399. const uint8_t * restrict q4 = x[i].qs;
  5400. const int8_t * restrict q8 = y[i].qs;
  5401. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5402. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5403. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5404. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5405. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5406. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5407. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5408. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5409. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5410. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5411. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5412. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5413. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5414. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5415. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5416. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5417. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5418. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5419. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5420. }
  5421. *s = hsum_float_8(acc) - summs;
  5422. #elif defined __riscv_v_intrinsic
  5423. uint16_t s16[2];
  5424. const uint8_t * restrict scales = (const uint8_t *)s16;
  5425. float sumf = 0;
  5426. for (int i = 0; i < nb; ++i) {
  5427. const uint8_t * restrict q4 = x[i].qs;
  5428. const int8_t * restrict q8 = y[i].qs;
  5429. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5430. s16[0] = b[0] & 0x0f0f;
  5431. s16[1] = (b[0] >> 4) & 0x0f0f;
  5432. 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]));
  5433. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5434. size_t vl = 32;
  5435. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5436. // load Q4
  5437. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5438. // load Q8 and multiply it with lower Q4 nibble
  5439. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5440. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5441. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5442. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5443. // load Q8 and multiply it with upper Q4 nibble
  5444. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5445. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5446. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5447. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5448. }
  5449. *s = sumf;
  5450. #else
  5451. uint8_t aux8[QK_K];
  5452. int16_t aux16[16];
  5453. float sums [8];
  5454. memset(sums, 0, 8*sizeof(float));
  5455. uint16_t s16[2];
  5456. const uint8_t * restrict scales = (const uint8_t *)s16;
  5457. float sumf = 0;
  5458. for (int i = 0; i < nb; ++i) {
  5459. const uint8_t * restrict q4 = x[i].qs;
  5460. const int8_t * restrict q8 = y[i].qs;
  5461. uint8_t * restrict a = aux8;
  5462. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5463. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5464. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5465. s16[0] = b[0] & 0x0f0f;
  5466. s16[1] = (b[0] >> 4) & 0x0f0f;
  5467. 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]));
  5468. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5469. for (int j = 0; j < QK_K/32; ++j) {
  5470. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5471. q8 += 16; a += 16;
  5472. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5473. q8 += 16; a += 16;
  5474. const float dl = d * scales[j];
  5475. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5476. }
  5477. }
  5478. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5479. *s = sumf;
  5480. #endif
  5481. }
  5482. #endif
  5483. #if QK_K == 256
  5484. 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) {
  5485. assert(n % QK_K == 0);
  5486. assert(nrc == 1);
  5487. UNUSED(nrc);
  5488. UNUSED(bx);
  5489. UNUSED(by);
  5490. UNUSED(bs);
  5491. const block_q5_K * restrict x = vx;
  5492. const block_q8_K * restrict y = vy;
  5493. const int nb = n / QK_K;
  5494. static const uint32_t kmask1 = 0x3f3f3f3f;
  5495. static const uint32_t kmask2 = 0x0f0f0f0f;
  5496. static const uint32_t kmask3 = 0x03030303;
  5497. uint32_t utmp[4];
  5498. #ifdef __ARM_NEON
  5499. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5500. const uint8x16_t mone = vdupq_n_u8(1);
  5501. const uint8x16_t mtwo = vdupq_n_u8(2);
  5502. const int32x4_t mzero = vdupq_n_s32(0);
  5503. ggml_int8x16x4_t q5bytes;
  5504. float sumf = 0;
  5505. for (int i = 0; i < nb; ++i) {
  5506. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5507. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5508. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5509. memcpy(utmp, x[i].scales, 12);
  5510. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5511. const uint32_t uaux = utmp[1] & kmask1;
  5512. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5513. utmp[2] = uaux;
  5514. utmp[0] &= kmask1;
  5515. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5516. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5517. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5518. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5519. int32_t sumi_mins = vaddvq_s32(prod);
  5520. const uint8_t * scales = (const uint8_t *)utmp;
  5521. const uint8_t * restrict q5 = x[i].qs;
  5522. const uint8_t * restrict qh = x[i].qh;
  5523. const int8_t * restrict q8 = y[i].qs;
  5524. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5525. ggml_uint8x16x4_t q5h;
  5526. int32_t sumi = 0;
  5527. for (int j = 0; j < QK_K/64; ++j) {
  5528. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5529. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5530. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5531. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5532. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5533. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5534. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5535. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5536. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5537. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5538. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5539. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5540. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5541. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5542. }
  5543. sumf += d * sumi - dmin * sumi_mins;
  5544. }
  5545. *s = sumf;
  5546. #elif defined __AVX2__
  5547. const __m256i m4 = _mm256_set1_epi8(0xF);
  5548. const __m128i mzero = _mm_setzero_si128();
  5549. const __m256i mone = _mm256_set1_epi8(1);
  5550. __m256 acc = _mm256_setzero_ps();
  5551. float summs = 0.f;
  5552. for (int i = 0; i < nb; ++i) {
  5553. const uint8_t * restrict q5 = x[i].qs;
  5554. const int8_t * restrict q8 = y[i].qs;
  5555. #if QK_K == 256
  5556. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5557. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5558. memcpy(utmp, x[i].scales, 12);
  5559. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5560. const uint32_t uaux = utmp[1] & kmask1;
  5561. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5562. utmp[2] = uaux;
  5563. utmp[0] &= kmask1;
  5564. #else
  5565. // TODO
  5566. const float d = 0, dmin = 0;
  5567. #endif
  5568. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5569. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5570. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5571. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5572. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5573. summs += dmin * _mm_extract_epi32(hsum, 0);
  5574. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5575. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5576. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5577. __m256i hmask = mone;
  5578. __m256i sumi = _mm256_setzero_si256();
  5579. int bit = 0;
  5580. for (int j = 0; j < QK_K/64; ++j) {
  5581. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5582. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5583. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5584. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5585. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5586. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5587. hmask = _mm256_slli_epi16(hmask, 1);
  5588. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5589. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5590. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5591. hmask = _mm256_slli_epi16(hmask, 1);
  5592. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5593. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5594. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5595. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5596. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5597. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5598. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5599. }
  5600. __m256 vd = _mm256_set1_ps(d);
  5601. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5602. }
  5603. *s = hsum_float_8(acc) + summs;
  5604. #elif defined __AVX__
  5605. const __m128i m4 = _mm_set1_epi8(0xF);
  5606. const __m128i mzero = _mm_setzero_si128();
  5607. const __m128i mone = _mm_set1_epi8(1);
  5608. const __m128i m2 = _mm_set1_epi8(2);
  5609. __m256 acc = _mm256_setzero_ps();
  5610. float summs = 0.f;
  5611. for (int i = 0; i < nb; ++i) {
  5612. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5613. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5614. const uint8_t * restrict q5 = x[i].qs;
  5615. const int8_t * restrict q8 = y[i].qs;
  5616. memcpy(utmp, x[i].scales, 12);
  5617. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5618. const uint32_t uaux = utmp[1] & kmask1;
  5619. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5620. utmp[2] = uaux;
  5621. utmp[0] &= kmask1;
  5622. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5623. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5624. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5625. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5626. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5627. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5628. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5629. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5630. summs += dmin * _mm_extract_epi32(hsum, 0);
  5631. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5632. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5633. __m128i hmask = mone;
  5634. __m128i sumi_0 = _mm_setzero_si128();
  5635. __m128i sumi_1 = _mm_setzero_si128();
  5636. int bit = 0;
  5637. __m128i shuffle = _mm_set1_epi16(0x0100);
  5638. for (int j = 0; j < QK_K/64; ++j) {
  5639. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5640. shuffle = _mm_add_epi16(shuffle, m2);
  5641. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5642. shuffle = _mm_add_epi16(shuffle, m2);
  5643. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5644. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5645. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5646. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5647. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5648. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5649. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5650. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5651. hmask = _mm_slli_epi16(hmask, 1);
  5652. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5653. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5654. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5655. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5656. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5657. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5658. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5659. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5660. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5661. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5662. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5663. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5664. hmask = _mm_slli_epi16(hmask, 1);
  5665. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5666. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5667. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5668. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5669. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5670. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5671. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5672. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5673. }
  5674. __m256 vd = _mm256_set1_ps(d);
  5675. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5676. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5677. }
  5678. *s = hsum_float_8(acc) + summs;
  5679. #elif defined __riscv_v_intrinsic
  5680. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5681. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5682. float sumf = 0;
  5683. float sums = 0.0;
  5684. size_t vl;
  5685. for (int i = 0; i < nb; ++i) {
  5686. vl = 8;
  5687. const uint8_t * restrict q5 = x[i].qs;
  5688. const uint8_t * restrict hm = x[i].qh;
  5689. const int8_t * restrict q8 = y[i].qs;
  5690. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5691. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5692. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5693. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5694. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5695. memcpy(utmp, x[i].scales, 12);
  5696. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5697. const uint32_t uaux = utmp[1] & kmask1;
  5698. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5699. utmp[2] = uaux;
  5700. utmp[0] &= kmask1;
  5701. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5702. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5703. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5704. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5705. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5706. vl = 32;
  5707. int32_t aux32 = 0;
  5708. int is = 0;
  5709. uint8_t m = 1;
  5710. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5711. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5712. for (int j = 0; j < QK_K/64; ++j) {
  5713. // load Q5 and Q8
  5714. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5715. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5716. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5717. // compute mask for addition
  5718. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5719. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5720. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5721. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5722. m <<= 1;
  5723. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5724. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5725. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5726. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5727. m <<= 1;
  5728. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5729. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5730. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5731. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5732. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5733. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5734. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5735. q5 += 32; q8 += 64;
  5736. }
  5737. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5738. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5739. }
  5740. *s = sumf+sums;
  5741. #else
  5742. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5743. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5744. int8_t aux8[QK_K];
  5745. int16_t aux16[8];
  5746. float sums [8];
  5747. int32_t aux32[8];
  5748. memset(sums, 0, 8*sizeof(float));
  5749. float sumf = 0;
  5750. for (int i = 0; i < nb; ++i) {
  5751. const uint8_t * restrict q4 = x[i].qs;
  5752. const uint8_t * restrict hm = x[i].qh;
  5753. const int8_t * restrict q8 = y[i].qs;
  5754. memset(aux32, 0, 8*sizeof(int32_t));
  5755. int8_t * restrict a = aux8;
  5756. uint8_t m = 1;
  5757. for (int j = 0; j < QK_K/64; ++j) {
  5758. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5759. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5760. a += 32; m <<= 1;
  5761. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5762. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5763. a += 32; m <<= 1;
  5764. q4 += 32;
  5765. }
  5766. memcpy(utmp, x[i].scales, 12);
  5767. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5768. const uint32_t uaux = utmp[1] & kmask1;
  5769. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5770. utmp[2] = uaux;
  5771. utmp[0] &= kmask1;
  5772. int sumi = 0;
  5773. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5774. a = aux8;
  5775. int is = 0;
  5776. for (int j = 0; j < QK_K/32; ++j) {
  5777. int32_t scale = scales[is++];
  5778. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5779. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5780. q8 += 8; a += 8;
  5781. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5782. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5783. q8 += 8; a += 8;
  5784. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5785. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5786. q8 += 8; a += 8;
  5787. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5788. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5789. q8 += 8; a += 8;
  5790. }
  5791. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5792. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5793. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5794. sumf -= dmin * sumi;
  5795. }
  5796. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5797. *s = sumf;
  5798. #endif
  5799. }
  5800. #else
  5801. 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) {
  5802. assert(n % QK_K == 0);
  5803. assert(nrc == 1);
  5804. UNUSED(nrc);
  5805. UNUSED(bx);
  5806. UNUSED(by);
  5807. UNUSED(bs);
  5808. const block_q5_K * restrict x = vx;
  5809. const block_q8_K * restrict y = vy;
  5810. const int nb = n / QK_K;
  5811. #ifdef __ARM_NEON
  5812. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5813. const uint8x16_t mh = vdupq_n_u8(16);
  5814. const int32x4_t mzero = vdupq_n_s32(0);
  5815. ggml_int8x16x4_t q5bytes;
  5816. ggml_uint8x16x4_t q5h;
  5817. float sumf = 0;
  5818. for (int i = 0; i < nb; ++i) {
  5819. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5820. const int8_t * sc = x[i].scales;
  5821. const uint8_t * restrict q5 = x[i].qs;
  5822. const uint8_t * restrict qh = x[i].qh;
  5823. const int8_t * restrict q8 = y[i].qs;
  5824. const uint8x8_t qhbits = vld1_u8(qh);
  5825. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5826. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5827. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5828. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5829. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5830. q5h.val[2] = vbicq_u8(mh, htmp);
  5831. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5832. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5833. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5834. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5835. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5836. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5837. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5838. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5839. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5840. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5841. }
  5842. *s = sumf;
  5843. #elif defined __AVX2__
  5844. const __m256i m4 = _mm256_set1_epi8(0xF);
  5845. const __m256i mone = _mm256_set1_epi8(1);
  5846. __m256 acc = _mm256_setzero_ps();
  5847. for (int i = 0; i < nb; ++i) {
  5848. const uint8_t * restrict q5 = x[i].qs;
  5849. const int8_t * restrict q8 = y[i].qs;
  5850. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5851. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5852. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5853. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5854. int64_t aux64;
  5855. memcpy(&aux64, x[i].qh, 8);
  5856. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5857. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5858. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5859. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5860. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5861. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5862. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5863. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5864. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5865. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5866. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5867. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5868. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5869. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5870. }
  5871. *s = hsum_float_8(acc);
  5872. #elif defined __AVX__
  5873. const __m128i m4 = _mm_set1_epi8(0xF);
  5874. const __m128i mone = _mm_set1_epi8(1);
  5875. __m256 acc = _mm256_setzero_ps();
  5876. for (int i = 0; i < nb; ++i) {
  5877. const uint8_t * restrict q5 = x[i].qs;
  5878. const int8_t * restrict q8 = y[i].qs;
  5879. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5880. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5881. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5882. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5883. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5884. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5885. int64_t aux64;
  5886. memcpy(&aux64, x[i].qh, 8);
  5887. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5888. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5889. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5890. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5891. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5892. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5893. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5894. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5895. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5896. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5897. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5898. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5899. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5900. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5901. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5902. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5903. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5904. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5905. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5906. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5907. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5908. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5909. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5910. }
  5911. *s = hsum_float_8(acc);
  5912. #elif defined __riscv_v_intrinsic
  5913. float sumf = 0;
  5914. for (int i = 0; i < nb; ++i) {
  5915. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5916. const int8_t * sc = x[i].scales;
  5917. const uint8_t * restrict q5 = x[i].qs;
  5918. const uint8_t * restrict qh = x[i].qh;
  5919. const int8_t * restrict q8 = y[i].qs;
  5920. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5921. // load qh
  5922. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5923. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5924. size_t vl = 16;
  5925. // combine both qh_1 and qh_2
  5926. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5927. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5928. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5929. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5930. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5931. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5932. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5933. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5934. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5935. // load q5
  5936. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5937. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5938. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5939. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5940. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5941. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5942. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5943. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5944. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5945. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5946. // load Q8 and multiply it with Q5
  5947. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5948. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5949. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5950. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5951. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5952. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5953. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5954. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5955. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5956. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5957. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5958. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5959. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5960. }
  5961. *s = sumf;
  5962. #else
  5963. int8_t aux8[QK_K];
  5964. int16_t aux16[16];
  5965. float sums [8];
  5966. memset(sums, 0, 8*sizeof(float));
  5967. float sumf = 0;
  5968. for (int i = 0; i < nb; ++i) {
  5969. const uint8_t * restrict q4 = x[i].qs;
  5970. const uint8_t * restrict hm = x[i].qh;
  5971. const int8_t * restrict q8 = y[i].qs;
  5972. int8_t * restrict a = aux8;
  5973. for (int l = 0; l < 32; ++l) {
  5974. a[l+ 0] = q4[l] & 0xF;
  5975. a[l+32] = q4[l] >> 4;
  5976. }
  5977. for (int is = 0; is < 8; ++is) {
  5978. uint8_t m = 1 << is;
  5979. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5980. }
  5981. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5982. const int8_t * restrict sc = x[i].scales;
  5983. for (int j = 0; j < QK_K/16; ++j) {
  5984. const float dl = d * sc[j];
  5985. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5986. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5987. q8 += 16; a += 16;
  5988. }
  5989. }
  5990. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5991. *s = sumf;
  5992. #endif
  5993. }
  5994. #endif
  5995. #if QK_K == 256
  5996. 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) {
  5997. assert(n % QK_K == 0);
  5998. assert(nrc == 1);
  5999. UNUSED(nrc);
  6000. UNUSED(bx);
  6001. UNUSED(by);
  6002. UNUSED(bs);
  6003. const block_q6_K * restrict x = vx;
  6004. const block_q8_K * restrict y = vy;
  6005. const int nb = n / QK_K;
  6006. #ifdef __ARM_NEON
  6007. float sum = 0;
  6008. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6009. const int32x4_t vzero = vdupq_n_s32(0);
  6010. //const int8x16_t m32s = vdupq_n_s8(32);
  6011. const uint8x16_t mone = vdupq_n_u8(3);
  6012. ggml_int8x16x4_t q6bytes;
  6013. ggml_uint8x16x4_t q6h;
  6014. for (int i = 0; i < nb; ++i) {
  6015. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6016. const uint8_t * restrict q6 = x[i].ql;
  6017. const uint8_t * restrict qh = x[i].qh;
  6018. const int8_t * restrict q8 = y[i].qs;
  6019. const int8_t * restrict scale = x[i].scales;
  6020. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6021. const int8x16_t scales = vld1q_s8(scale);
  6022. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6023. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6024. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6025. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6026. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6027. int32_t isum_mins = vaddvq_s32(prod);
  6028. int32_t isum = 0;
  6029. for (int j = 0; j < QK_K/128; ++j) {
  6030. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6031. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6032. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6033. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6034. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6035. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6036. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6037. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6038. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6039. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6040. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6041. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6042. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6043. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6044. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6045. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6046. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6047. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6048. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6049. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6050. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6051. scale += 4;
  6052. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6053. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6054. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6055. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6056. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6057. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6058. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6059. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6060. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6061. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6062. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6063. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6064. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6065. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6066. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6067. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6068. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6069. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6070. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6071. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6072. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6073. scale += 4;
  6074. }
  6075. //sum += isum * d_all * y[i].d;
  6076. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6077. }
  6078. *s = sum;
  6079. #elif defined __AVX2__
  6080. const __m256i m4 = _mm256_set1_epi8(0xF);
  6081. const __m256i m2 = _mm256_set1_epi8(3);
  6082. const __m256i m32s = _mm256_set1_epi8(32);
  6083. __m256 acc = _mm256_setzero_ps();
  6084. for (int i = 0; i < nb; ++i) {
  6085. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6086. const uint8_t * restrict q4 = x[i].ql;
  6087. const uint8_t * restrict qh = x[i].qh;
  6088. const int8_t * restrict q8 = y[i].qs;
  6089. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6090. __m256i sumi = _mm256_setzero_si256();
  6091. int is = 0;
  6092. for (int j = 0; j < QK_K/128; ++j) {
  6093. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6094. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6095. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6096. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6097. is += 4;
  6098. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6099. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6100. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6101. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6102. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6103. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6104. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6105. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6106. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6107. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6108. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6109. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6110. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6111. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6112. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6113. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6114. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6115. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6116. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6117. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6118. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6119. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6120. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6121. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6122. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6123. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6124. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6125. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6126. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6127. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6128. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6129. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6130. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6131. }
  6132. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6133. }
  6134. *s = hsum_float_8(acc);
  6135. #elif defined __AVX__
  6136. const __m128i m4 = _mm_set1_epi8(0xF);
  6137. const __m128i m3 = _mm_set1_epi8(3);
  6138. const __m128i m32s = _mm_set1_epi8(32);
  6139. const __m128i m2 = _mm_set1_epi8(2);
  6140. __m256 acc = _mm256_setzero_ps();
  6141. for (int i = 0; i < nb; ++i) {
  6142. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6143. const uint8_t * restrict q4 = x[i].ql;
  6144. const uint8_t * restrict qh = x[i].qh;
  6145. const int8_t * restrict q8 = y[i].qs;
  6146. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6147. __m128i sumi_0 = _mm_setzero_si128();
  6148. __m128i sumi_1 = _mm_setzero_si128();
  6149. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6150. for (int j = 0; j < QK_K/128; ++j) {
  6151. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6152. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6153. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6154. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6155. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6156. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6157. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6158. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6159. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6160. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6161. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6162. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6163. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6164. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6165. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6166. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6167. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6168. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6169. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6170. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6171. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6172. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6173. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6174. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6175. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6176. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6177. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6178. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6179. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6180. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6181. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6182. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6183. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6184. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6185. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6186. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6187. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6188. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6189. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6190. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6191. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6192. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6193. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6194. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6195. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6196. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6197. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6198. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6199. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6200. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6201. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6202. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6203. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6204. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6205. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6206. shuffle = _mm_add_epi8(shuffle, m2);
  6207. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6208. shuffle = _mm_add_epi8(shuffle, m2);
  6209. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6210. shuffle = _mm_add_epi8(shuffle, m2);
  6211. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6212. shuffle = _mm_add_epi8(shuffle, m2);
  6213. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6214. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6215. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6216. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6217. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6218. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6219. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6220. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6221. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6222. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6223. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6224. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6225. }
  6226. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6227. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6228. }
  6229. *s = hsum_float_8(acc);
  6230. #elif defined __riscv_v_intrinsic
  6231. float sumf = 0;
  6232. for (int i = 0; i < nb; ++i) {
  6233. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6234. const uint8_t * restrict q6 = x[i].ql;
  6235. const uint8_t * restrict qh = x[i].qh;
  6236. const int8_t * restrict q8 = y[i].qs;
  6237. const int8_t * restrict scale = x[i].scales;
  6238. size_t vl;
  6239. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6240. int sum_t = 0;
  6241. int is = 0;
  6242. for (int j = 0; j < QK_K/128; ++j) {
  6243. vl = 32;
  6244. // load qh
  6245. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6246. // load Q6
  6247. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6248. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6249. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6250. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6251. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6252. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6253. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6254. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6255. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6256. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6257. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6258. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6259. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6260. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6261. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6262. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6263. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6264. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6265. // load Q8 and take product
  6266. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6267. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6268. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6269. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6270. vl = 16;
  6271. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6272. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6273. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6274. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6275. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6276. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6277. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6278. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6279. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6280. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6281. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6282. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6283. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6284. q6 += 64; qh += 32; q8 += 128; is=8;
  6285. }
  6286. sumf += d * sum_t;
  6287. }
  6288. *s = sumf;
  6289. #else
  6290. int8_t aux8[QK_K];
  6291. int16_t aux16[8];
  6292. float sums [8];
  6293. int32_t aux32[8];
  6294. memset(sums, 0, 8*sizeof(float));
  6295. float sumf = 0;
  6296. for (int i = 0; i < nb; ++i) {
  6297. const uint8_t * restrict q4 = x[i].ql;
  6298. const uint8_t * restrict qh = x[i].qh;
  6299. const int8_t * restrict q8 = y[i].qs;
  6300. memset(aux32, 0, 8*sizeof(int32_t));
  6301. int8_t * restrict a = aux8;
  6302. for (int j = 0; j < QK_K; j += 128) {
  6303. for (int l = 0; l < 32; ++l) {
  6304. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6305. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6306. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6307. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6308. }
  6309. a += 128;
  6310. q4 += 64;
  6311. qh += 32;
  6312. }
  6313. a = aux8;
  6314. int is = 0;
  6315. for (int j = 0; j < QK_K/16; ++j) {
  6316. int scale = x[i].scales[is++];
  6317. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6318. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6319. q8 += 8; a += 8;
  6320. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6321. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6322. q8 += 8; a += 8;
  6323. }
  6324. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6325. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6326. }
  6327. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6328. *s = sumf;
  6329. #endif
  6330. }
  6331. #else
  6332. 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) {
  6333. assert(n % QK_K == 0);
  6334. assert(nrc == 1);
  6335. UNUSED(nrc);
  6336. UNUSED(bx);
  6337. UNUSED(by);
  6338. UNUSED(bs);
  6339. const block_q6_K * restrict x = vx;
  6340. const block_q8_K * restrict y = vy;
  6341. const int nb = n / QK_K;
  6342. #ifdef __ARM_NEON
  6343. float sum = 0;
  6344. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6345. const int8x16_t m32s = vdupq_n_s8(32);
  6346. const int32x4_t vzero = vdupq_n_s32(0);
  6347. const uint8x16_t mone = vdupq_n_u8(3);
  6348. ggml_int8x16x4_t q6bytes;
  6349. ggml_uint8x16x4_t q6h;
  6350. for (int i = 0; i < nb; ++i) {
  6351. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6352. const uint8_t * restrict q6 = x[i].ql;
  6353. const uint8_t * restrict qh = x[i].qh;
  6354. const int8_t * restrict q8 = y[i].qs;
  6355. const int8_t * restrict scale = x[i].scales;
  6356. int32_t isum = 0;
  6357. uint8x16_t qhbits = vld1q_u8(qh);
  6358. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6359. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6360. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6361. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6362. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6363. shifted = vshrq_n_u8(qhbits, 4);
  6364. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6365. shifted = vshrq_n_u8(qhbits, 6);
  6366. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6367. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6368. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6369. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6370. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6371. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6372. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6373. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6374. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6375. sum += isum * d_all * y[i].d;
  6376. }
  6377. *s = sum;
  6378. #elif defined __AVX2__
  6379. const __m256i m4 = _mm256_set1_epi8(0xF);
  6380. const __m256i m2 = _mm256_set1_epi8(3);
  6381. const __m256i m32s = _mm256_set1_epi8(32);
  6382. __m256 acc = _mm256_setzero_ps();
  6383. for (int i = 0; i < nb; ++i) {
  6384. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6385. const uint8_t * restrict q4 = x[i].ql;
  6386. const uint8_t * restrict qh = x[i].qh;
  6387. const int8_t * restrict q8 = y[i].qs;
  6388. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6389. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6390. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6391. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6392. __m256i sumi = _mm256_setzero_si256();
  6393. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6394. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6395. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6396. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6397. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6398. 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);
  6399. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6400. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6401. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6402. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6403. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6404. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6405. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6406. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6407. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6408. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6409. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6410. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6411. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6412. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6413. }
  6414. *s = hsum_float_8(acc);
  6415. #elif defined __AVX__
  6416. const __m128i m4 = _mm_set1_epi8(0xF);
  6417. const __m128i m2 = _mm_set1_epi8(3);
  6418. const __m128i m32s = _mm_set1_epi8(32);
  6419. __m256 acc = _mm256_setzero_ps();
  6420. for (int i = 0; i < nb; ++i) {
  6421. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6422. const uint8_t * restrict q4 = x[i].ql;
  6423. const uint8_t * restrict qh = x[i].qh;
  6424. const int8_t * restrict q8 = y[i].qs;
  6425. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6426. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6427. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6428. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6429. __m128i sumi_0 = _mm_setzero_si128();
  6430. __m128i sumi_1 = _mm_setzero_si128();
  6431. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6432. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6433. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6434. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6435. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6436. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6437. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6438. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6439. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6440. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6441. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6442. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6443. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6444. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6445. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6446. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6447. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6448. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6449. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6450. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6451. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6452. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6453. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6454. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6455. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6456. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6457. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6458. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6459. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6460. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6461. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6462. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6463. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6464. }
  6465. *s = hsum_float_8(acc);
  6466. #elif defined __riscv_v_intrinsic
  6467. float sumf = 0;
  6468. for (int i = 0; i < nb; ++i) {
  6469. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6470. const uint8_t * restrict q6 = x[i].ql;
  6471. const uint8_t * restrict qh = x[i].qh;
  6472. const int8_t * restrict q8 = y[i].qs;
  6473. const int8_t * restrict scale = x[i].scales;
  6474. int32_t isum = 0;
  6475. size_t vl = 16;
  6476. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6477. // load Q6
  6478. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6479. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6480. // load qh
  6481. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6482. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6483. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6484. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6485. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6486. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6487. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6488. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6489. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6490. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6491. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6492. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6493. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6494. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6495. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6496. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6497. // load Q8 and take product
  6498. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6499. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6500. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6501. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6502. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6503. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6504. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6505. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6506. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6507. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6508. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6509. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6510. sumf += isum * d_all * y[i].d;
  6511. }
  6512. *s = sumf;
  6513. #else
  6514. int8_t aux8[QK_K];
  6515. int16_t aux16[8];
  6516. float sums [8];
  6517. int32_t aux32[8];
  6518. memset(sums, 0, 8*sizeof(float));
  6519. float sumf = 0;
  6520. for (int i = 0; i < nb; ++i) {
  6521. const uint8_t * restrict q4 = x[i].ql;
  6522. const uint8_t * restrict qh = x[i].qh;
  6523. const int8_t * restrict q8 = y[i].qs;
  6524. memset(aux32, 0, 8*sizeof(int32_t));
  6525. int8_t * restrict a = aux8;
  6526. for (int l = 0; l < 16; ++l) {
  6527. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6528. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6529. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6530. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6531. }
  6532. int is = 0;
  6533. for (int j = 0; j < QK_K/16; ++j) {
  6534. int scale = x[i].scales[is++];
  6535. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6536. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6537. q8 += 8; a += 8;
  6538. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6539. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6540. q8 += 8; a += 8;
  6541. }
  6542. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6543. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6544. }
  6545. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6546. *s = sumf;
  6547. #endif
  6548. }
  6549. #endif
  6550. #if defined (__AVX2__) || defined (__ARM_NEON)
  6551. static const int8_t keven_signs_q2xs[1024] = {
  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. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6580. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6581. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6582. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6583. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6584. };
  6585. #endif
  6586. 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) {
  6587. assert(n % QK_K == 0);
  6588. assert(nrc == 1);
  6589. UNUSED(nrc);
  6590. UNUSED(bx);
  6591. UNUSED(by);
  6592. UNUSED(bs);
  6593. const block_iq2_xxs * restrict x = vx;
  6594. const block_q8_K * restrict y = vy;
  6595. const int nb = n / QK_K;
  6596. #if defined(__ARM_NEON)
  6597. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6598. uint32_t aux32[4];
  6599. const uint8_t * aux8 = (const uint8_t *)aux32;
  6600. ggml_int8x16x4_t q2u;
  6601. ggml_int8x16x4_t q2s;
  6602. ggml_int8x16x4_t q8b;
  6603. float sumf = 0;
  6604. for (int i = 0; i < nb; ++i) {
  6605. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6606. const uint16_t * restrict q2 = x[i].qs;
  6607. const int8_t * restrict q8 = y[i].qs;
  6608. float sumf1 = 0, sumf2 = 0;
  6609. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6610. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6611. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6612. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6613. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6614. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6615. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6616. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6617. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6618. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6619. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6620. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6621. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6622. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6623. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6624. 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]);
  6625. 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]);
  6626. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6627. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6628. }
  6629. sumf += d*(sumf1 + sumf2);
  6630. }
  6631. *s = 0.25f * sumf;
  6632. #elif defined(__AVX2__)
  6633. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6634. uint32_t aux32[4];
  6635. const uint8_t * aux8 = (const uint8_t *)aux32;
  6636. __m256 accumf = _mm256_setzero_ps();
  6637. for (int i = 0; i < nb; ++i) {
  6638. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6639. const uint16_t * restrict q2 = x[i].qs;
  6640. const int8_t * restrict q8 = y[i].qs;
  6641. __m256i sumi1 = _mm256_setzero_si256();
  6642. __m256i sumi2 = _mm256_setzero_si256();
  6643. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6644. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6645. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6646. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6647. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6648. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6649. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6650. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6651. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6652. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6653. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6654. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6655. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6656. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6657. const uint16_t ls1 = aux32[1] >> 28;
  6658. const uint16_t ls2 = aux32[3] >> 28;
  6659. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6660. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6661. sumi1 = _mm256_add_epi32(sumi1, p1);
  6662. sumi2 = _mm256_add_epi32(sumi2, p2);
  6663. }
  6664. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6665. }
  6666. *s = 0.125f * hsum_float_8(accumf);
  6667. #else
  6668. uint32_t aux32[2];
  6669. const uint8_t * aux8 = (const uint8_t *)aux32;
  6670. float sumf = 0.f;
  6671. for (int i = 0; i < nb; ++i) {
  6672. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6673. const uint16_t * restrict q2 = x[i].qs;
  6674. const int8_t * restrict q8 = y[i].qs;
  6675. int32_t bsum = 0;
  6676. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6677. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6678. q2 += 4;
  6679. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6680. int32_t sumi = 0;
  6681. for (int l = 0; l < 4; ++l) {
  6682. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6683. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6684. for (int j = 0; j < 8; ++j) {
  6685. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6686. }
  6687. q8 += 8;
  6688. }
  6689. bsum += sumi * ls;
  6690. }
  6691. sumf += d * bsum;
  6692. }
  6693. *s = 0.125f * sumf;
  6694. #endif
  6695. }
  6696. 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) {
  6697. assert(n % QK_K == 0);
  6698. assert(nrc == 1);
  6699. UNUSED(nrc);
  6700. UNUSED(bx);
  6701. UNUSED(by);
  6702. UNUSED(bs);
  6703. const block_iq2_xs * restrict x = vx;
  6704. const block_q8_K * restrict y = vy;
  6705. const int nb = n / QK_K;
  6706. #if defined(__ARM_NEON)
  6707. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6708. ggml_int8x16x4_t q2u;
  6709. ggml_int8x16x4_t q2s;
  6710. ggml_int8x16x4_t q8b;
  6711. int32x4x4_t scales32;
  6712. float sumf = 0;
  6713. for (int i = 0; i < nb; ++i) {
  6714. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6715. const uint16_t * restrict q2 = x[i].qs;
  6716. const int8_t * restrict q8 = y[i].qs;
  6717. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6718. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6719. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6720. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6721. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6722. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6723. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6724. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6725. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6726. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6727. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6728. int32x4_t sumi = vdupq_n_s32(0);
  6729. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6730. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6731. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6732. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6733. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6734. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6735. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6736. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6737. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6738. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6739. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6740. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6741. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6742. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6743. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6744. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6745. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6746. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6747. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6748. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6749. q2 += 8;
  6750. }
  6751. sumf += d*vaddvq_s32(sumi);
  6752. }
  6753. *s = 0.125f * sumf;
  6754. #elif defined(__AVX2__)
  6755. const __m256i mone = _mm256_set1_epi8(1);
  6756. static const char block_sign_shuffle_mask_1[32] = {
  6757. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6758. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6759. };
  6760. static const char block_sign_shuffle_mask_2[32] = {
  6761. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6762. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6763. };
  6764. static const uint8_t bit_selector_mask_bytes[32] = {
  6765. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6766. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6767. };
  6768. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6769. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6770. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6771. #if QK_K == 64
  6772. static const uint8_t k_bit_helper[16] = {
  6773. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6774. };
  6775. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6776. const __m128i m511 = _mm_set1_epi16(511);
  6777. typedef union {
  6778. __m128i vec_index;
  6779. uint16_t index[8];
  6780. } index_t;
  6781. index_t idx;
  6782. __m256 accumf = _mm256_setzero_ps();
  6783. for (int i = 0; i < nb; ++i) {
  6784. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6785. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6786. idx.vec_index = _mm_and_si128(q2_data, m511);
  6787. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6788. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6789. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6790. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6791. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6792. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6793. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6794. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6795. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6796. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6797. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6798. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6799. __m256i signs;
  6800. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6801. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6802. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6803. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6804. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6805. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6806. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6807. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6808. 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));
  6809. 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));
  6810. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6811. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6812. }
  6813. *s = 0.125f * hsum_float_8(accumf);
  6814. #else
  6815. static const uint8_t k_bit_helper[32] = {
  6816. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6817. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6818. };
  6819. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6820. const __m256i m511 = _mm256_set1_epi16(511);
  6821. const __m128i m4 = _mm_set1_epi8(0xf);
  6822. const __m128i m1 = _mm_set1_epi8(1);
  6823. uint64_t aux64;
  6824. // somewhat hacky, but gives a significant boost in performance
  6825. __m256i aux_gindex;
  6826. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6827. __m256 accumf = _mm256_setzero_ps();
  6828. for (int i = 0; i < nb; ++i) {
  6829. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6830. const uint16_t * restrict q2 = x[i].qs;
  6831. const int8_t * restrict q8 = y[i].qs;
  6832. memcpy(&aux64, x[i].scales, 8);
  6833. __m128i stmp = _mm_set1_epi64x(aux64);
  6834. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6835. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6836. __m256i sumi1 = _mm256_setzero_si256();
  6837. __m256i sumi2 = _mm256_setzero_si256();
  6838. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6839. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6840. aux_gindex = _mm256_and_si256(q2_data, m511);
  6841. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6842. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6843. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6844. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6845. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6846. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6847. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6848. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6849. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6850. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6851. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6852. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6853. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6854. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6855. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6856. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6857. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6858. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6859. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6860. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  6861. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  6862. __m256i signs;
  6863. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6864. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6865. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6866. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6867. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6868. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6869. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6870. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6871. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6872. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6873. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6874. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6875. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6876. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6877. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6878. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6879. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6880. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6881. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6882. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6883. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6884. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6885. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6886. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  6887. }
  6888. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6889. }
  6890. *s = 0.125f * hsum_float_8(accumf);
  6891. #endif
  6892. #else
  6893. float sumf = 0.f;
  6894. for (int i = 0; i < nb; ++i) {
  6895. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6896. const uint16_t * restrict q2 = x[i].qs;
  6897. const uint8_t * restrict sc = x[i].scales;
  6898. const int8_t * restrict q8 = y[i].qs;
  6899. int32_t bsum = 0;
  6900. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6901. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6902. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6903. int32_t sumi = 0;
  6904. for (int l = 0; l < 2; ++l) {
  6905. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6906. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6907. for (int j = 0; j < 8; ++j) {
  6908. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6909. }
  6910. q8 += 8;
  6911. }
  6912. bsum += sumi * ls1;
  6913. sumi = 0;
  6914. for (int l = 2; l < 4; ++l) {
  6915. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6916. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6917. for (int j = 0; j < 8; ++j) {
  6918. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6919. }
  6920. q8 += 8;
  6921. }
  6922. bsum += sumi * ls2;
  6923. q2 += 4;
  6924. }
  6925. sumf += d * bsum;
  6926. }
  6927. *s = 0.125f * sumf;
  6928. #endif
  6929. }
  6930. 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) {
  6931. assert(n % QK_K == 0);
  6932. assert(nrc == 1);
  6933. UNUSED(nrc);
  6934. UNUSED(bx);
  6935. UNUSED(by);
  6936. UNUSED(bs);
  6937. const block_iq2_s * restrict x = vx;
  6938. const block_q8_K * restrict y = vy;
  6939. const int nb = n / QK_K;
  6940. #if defined(__ARM_NEON)
  6941. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6942. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6943. };
  6944. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  6945. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  6946. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  6947. const uint8x16_t m1 = vdupq_n_u8(1);
  6948. const int32x4_t vzero = vdupq_n_s32(0);
  6949. uint8x16x2_t vs;
  6950. ggml_int8x16x4_t q2s;
  6951. ggml_int8x16x4_t q8b;
  6952. float sumf = 0;
  6953. for (int i = 0; i < nb; ++i) {
  6954. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6955. const uint8_t * restrict qs = x[i].qs;
  6956. const uint8_t * restrict qh = x[i].qh;
  6957. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  6958. const int8_t * restrict q8 = y[i].qs;
  6959. int sumi1 = 0, sumi2 = 0;
  6960. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6961. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6962. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  6963. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  6964. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  6965. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  6966. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  6967. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  6968. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  6969. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  6970. qs += 8;
  6971. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  6972. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6973. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6974. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6975. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6976. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  6977. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  6978. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  6979. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6980. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6981. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6982. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6983. signs += 4;
  6984. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  6985. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  6986. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  6987. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  6988. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  6989. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  6990. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  6991. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  6992. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  6993. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  6994. }
  6995. sumf += d*(sumi1 + sumi2);
  6996. }
  6997. *s = 0.125f * sumf;
  6998. #elif defined(__AVX2__)
  6999. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7000. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7001. };
  7002. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7003. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7004. };
  7005. const __m128i m4 = _mm_set1_epi8(0xf);
  7006. const __m128i m1 = _mm_set1_epi8(1);
  7007. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7008. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7009. uint64_t aux64;
  7010. __m256 accumf = _mm256_setzero_ps();
  7011. for (int i = 0; i < nb; ++i) {
  7012. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7013. const uint8_t * restrict qs = x[i].qs;
  7014. const uint8_t * restrict qh = x[i].qh;
  7015. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7016. const int8_t * restrict q8 = y[i].qs;
  7017. memcpy(&aux64, x[i].scales, 8);
  7018. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7019. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7020. __m256i sumi1 = _mm256_setzero_si256();
  7021. __m256i sumi2 = _mm256_setzero_si256();
  7022. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7023. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7024. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7025. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7026. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7027. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7028. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7029. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7030. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7031. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7032. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7033. qs += 8;
  7034. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7035. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7036. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7037. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7038. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7039. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7040. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7041. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7042. signs += 4;
  7043. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7044. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7045. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7046. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7047. sumi1 = _mm256_add_epi32(sumi1, p1);
  7048. sumi2 = _mm256_add_epi32(sumi2, p2);
  7049. }
  7050. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7051. }
  7052. *s = 0.125f * hsum_float_8(accumf);
  7053. #else
  7054. float sumf = 0;
  7055. for (int i = 0; i < nb; i++) {
  7056. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7057. const int8_t * q8 = y[i].qs;
  7058. const uint8_t * qs = x[i].qs;
  7059. const uint8_t * qh = x[i].qh;
  7060. const uint8_t * signs = qs + QK_K/8;
  7061. int bsum = 0;
  7062. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7063. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7064. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7065. int sumi1 = 0, sumi2 = 0;
  7066. for (int l = 0; l < 2; ++l) {
  7067. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7068. for (int j = 0; j < 8; ++j) {
  7069. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7070. }
  7071. q8 += 8;
  7072. }
  7073. for (int l = 2; l < 4; ++l) {
  7074. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7075. for (int j = 0; j < 8; ++j) {
  7076. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7077. }
  7078. q8 += 8;
  7079. }
  7080. bsum += ls1 * sumi1 + ls2 * sumi2;
  7081. qs += 4;
  7082. signs += 4;
  7083. }
  7084. sumf += d * bsum;
  7085. }
  7086. *s = 0.125f * sumf;
  7087. #endif
  7088. }
  7089. 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) {
  7090. assert(n % QK_K == 0);
  7091. assert(nrc == 1);
  7092. UNUSED(nrc);
  7093. UNUSED(bx);
  7094. UNUSED(by);
  7095. UNUSED(bs);
  7096. const block_iq3_xxs * restrict x = vx;
  7097. const block_q8_K * restrict y = vy;
  7098. const int nb = n / QK_K;
  7099. #if defined(__ARM_NEON)
  7100. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7101. uint32_t aux32[2];
  7102. ggml_int8x16x4_t q3s;
  7103. ggml_int8x16x4_t q8b;
  7104. float sumf = 0;
  7105. for (int i = 0; i < nb; ++i) {
  7106. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7107. const uint8_t * restrict q3 = x[i].qs;
  7108. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7109. const int8_t * restrict q8 = y[i].qs;
  7110. float sumf1 = 0, sumf2 = 0;
  7111. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7112. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7113. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7114. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7115. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7116. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7117. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7118. q3 += 16;
  7119. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7120. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7121. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7122. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7123. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7124. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7125. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7126. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7127. 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]);
  7128. 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]);
  7129. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7130. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7131. }
  7132. sumf += d*(sumf1 + sumf2);
  7133. }
  7134. *s = 0.5f * sumf;
  7135. #elif defined(__AVX2__)
  7136. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7137. uint32_t aux32[2];
  7138. __m256 accumf = _mm256_setzero_ps();
  7139. for (int i = 0; i < nb; ++i) {
  7140. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7141. const uint8_t * restrict q3 = x[i].qs;
  7142. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7143. const int8_t * restrict q8 = y[i].qs;
  7144. __m256i sumi1 = _mm256_setzero_si256();
  7145. __m256i sumi2 = _mm256_setzero_si256();
  7146. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7147. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7148. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7149. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7150. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7151. q3 += 8;
  7152. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7153. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7154. q3 += 8;
  7155. memcpy(aux32, gas, 8); gas += 8;
  7156. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7157. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7158. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7159. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7160. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7161. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7162. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7163. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7164. const uint16_t ls1 = aux32[0] >> 28;
  7165. const uint16_t ls2 = aux32[1] >> 28;
  7166. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7167. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7168. sumi1 = _mm256_add_epi32(sumi1, p1);
  7169. sumi2 = _mm256_add_epi32(sumi2, p2);
  7170. }
  7171. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7172. }
  7173. *s = 0.25f * hsum_float_8(accumf);
  7174. #else
  7175. uint32_t aux32;
  7176. float sumf = 0.f;
  7177. for (int i = 0; i < nb; ++i) {
  7178. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7179. const uint8_t * restrict q3 = x[i].qs;
  7180. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7181. const int8_t * restrict q8 = y[i].qs;
  7182. int32_t bsum = 0;
  7183. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7184. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7185. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7186. int32_t sumi = 0;
  7187. for (int l = 0; l < 4; ++l) {
  7188. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7189. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7190. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7191. for (int j = 0; j < 4; ++j) {
  7192. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7193. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7194. }
  7195. q8 += 8;
  7196. }
  7197. q3 += 8;
  7198. bsum += sumi * ls;
  7199. }
  7200. sumf += d * bsum;
  7201. }
  7202. *s = 0.25f * sumf;
  7203. #endif
  7204. }
  7205. 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) {
  7206. assert(n % QK_K == 0);
  7207. assert(nrc == 1);
  7208. UNUSED(nrc);
  7209. UNUSED(bx);
  7210. UNUSED(by);
  7211. UNUSED(bs);
  7212. const block_iq3_s * restrict x = vx;
  7213. const block_q8_K * restrict y = vy;
  7214. const int nb = n / QK_K;
  7215. #if defined(__ARM_NEON)
  7216. typedef union {
  7217. uint16x8_t vec_index;
  7218. uint16_t index[8];
  7219. } vec_index_t;
  7220. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7221. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7222. };
  7223. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7224. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7225. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7226. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7227. const int16x8_t hshift = vld1q_s16(k_shift);
  7228. const uint16x8_t m256 = vdupq_n_u16(256);
  7229. const uint8x16_t m1 = vdupq_n_u8(1);
  7230. uint8x16x2_t vs;
  7231. ggml_int8x16x4_t q3s;
  7232. ggml_int8x16x4_t q8b;
  7233. vec_index_t idx;
  7234. #if QK_K == 256
  7235. uint32_t scales32[2];
  7236. const uint8_t * scales8 = (const uint8_t *)scales32;
  7237. #endif
  7238. float sumf = 0;
  7239. for (int i = 0; i < nb; ++i) {
  7240. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7241. const uint8_t * restrict qs = x[i].qs;
  7242. const uint8_t * restrict qh = x[i].qh;
  7243. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7244. const int8_t * restrict q8 = y[i].qs;
  7245. #if QK_K == 256
  7246. memcpy(scales32, x[i].scales, 4);
  7247. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7248. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7249. #endif
  7250. int sumi1 = 0, sumi2 = 0;
  7251. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7252. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7253. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7254. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7255. const uint32x4_t aux32x4_0 = 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_1 = 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. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7260. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7261. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7262. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7263. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7264. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7265. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7266. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7267. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7268. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7269. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7270. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7271. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7272. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7273. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7274. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7275. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7276. signs += 4;
  7277. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7278. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7279. 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]);
  7280. 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]);
  7281. #if QK_K == 256
  7282. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7283. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7284. #else
  7285. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7286. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7287. #endif
  7288. }
  7289. sumf += d*(sumi1 + sumi2);
  7290. }
  7291. *s = sumf;
  7292. #elif defined(__AVX2__)
  7293. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7294. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7295. };
  7296. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7297. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7298. };
  7299. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7300. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7301. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7302. const __m256i idx_mask = _mm256_set1_epi32(256);
  7303. typedef union {
  7304. __m256i vec[2];
  7305. uint32_t index[16];
  7306. } index_t;
  7307. index_t idx;
  7308. __m256 accumf = _mm256_setzero_ps();
  7309. for (int i = 0; i < nb; ++i) {
  7310. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7311. const uint8_t * restrict qs = x[i].qs;
  7312. const uint8_t * restrict qh = x[i].qh;
  7313. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7314. const int8_t * restrict q8 = y[i].qs;
  7315. __m256i sumi1 = _mm256_setzero_si256();
  7316. __m256i sumi2 = _mm256_setzero_si256();
  7317. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7318. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7319. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7320. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7321. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7322. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7323. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7324. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7325. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7326. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7327. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7328. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7329. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7330. const __m256i q2_1 = _mm256_set_epi32(
  7331. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7332. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7333. );
  7334. const __m256i q2_2 = _mm256_set_epi32(
  7335. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7336. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7337. );
  7338. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7339. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7340. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7341. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7342. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7343. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7344. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7345. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7346. signs += 4;
  7347. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7348. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7349. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7350. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7351. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7352. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7353. sumi1 = _mm256_add_epi32(sumi1, p1);
  7354. sumi2 = _mm256_add_epi32(sumi2, p2);
  7355. }
  7356. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7357. }
  7358. *s = hsum_float_8(accumf);
  7359. #else
  7360. float sumf = 0.f;
  7361. for (int i = 0; i < nb; ++i) {
  7362. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7363. const uint8_t * restrict qs = x[i].qs;
  7364. const uint8_t * restrict qh = x[i].qh;
  7365. const uint8_t * restrict signs = x[i].signs;
  7366. const int8_t * restrict q8 = y[i].qs;
  7367. int32_t bsum = 0;
  7368. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7369. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7370. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7371. int32_t sumi = 0;
  7372. for (int l = 0; l < 4; ++l) {
  7373. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7374. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7375. for (int j = 0; j < 4; ++j) {
  7376. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7377. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7378. }
  7379. q8 += 8;
  7380. }
  7381. qs += 8;
  7382. signs += 4;
  7383. bsum += sumi * ls1;
  7384. sumi = 0;
  7385. for (int l = 0; l < 4; ++l) {
  7386. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7387. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7388. for (int j = 0; j < 4; ++j) {
  7389. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7390. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7391. }
  7392. q8 += 8;
  7393. }
  7394. qs += 8;
  7395. signs += 4;
  7396. bsum += sumi * ls2;
  7397. }
  7398. sumf += d * bsum;
  7399. }
  7400. *s = sumf;
  7401. #endif
  7402. }
  7403. #ifdef __AVX2__
  7404. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7405. const __m256i ax = _mm256_sign_epi8(x, x);
  7406. const __m256i sy = _mm256_sign_epi8(y, x);
  7407. return _mm256_maddubs_epi16(ax, sy);
  7408. }
  7409. #endif
  7410. 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) {
  7411. assert(n % QK_K == 0);
  7412. assert(nrc == 1);
  7413. UNUSED(nrc);
  7414. UNUSED(bx);
  7415. UNUSED(by);
  7416. UNUSED(bs);
  7417. const block_iq1_s * restrict x = vx;
  7418. const block_q8_K * restrict y = vy;
  7419. const int nb = n / QK_K;
  7420. #if defined __ARM_NEON
  7421. ggml_int8x16x4_t q1b;
  7422. ggml_int8x16x4_t q8b;
  7423. float sumf = 0;
  7424. for (int i = 0; i < nb; ++i) {
  7425. const int8_t * q8 = y[i].qs;
  7426. const uint8_t * qs = x[i].qs;
  7427. const uint16_t * qh = x[i].qh;
  7428. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7429. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7430. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7431. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7432. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7433. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7434. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7435. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7436. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7437. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7438. qs += 8;
  7439. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7440. 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]);
  7441. 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]);
  7442. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7443. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7444. sumi1 += vaddvq_s32(p1) * ls1;
  7445. sumi2 += vaddvq_s32(p2) * ls2;
  7446. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7447. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7448. }
  7449. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7450. }
  7451. *s = sumf;
  7452. #elif defined __AVX2__
  7453. __m256 accum = _mm256_setzero_ps();
  7454. float accum1 = 0;
  7455. for (int i = 0; i < nb; ++i) {
  7456. const int8_t * q8 = y[i].qs;
  7457. const uint8_t * qs = x[i].qs;
  7458. const uint16_t * qh = x[i].qh;
  7459. __m256i sumi = _mm256_setzero_si256();
  7460. int sumi1 = 0;
  7461. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7462. 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)],
  7463. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7464. 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)],
  7465. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7466. qs += 8;
  7467. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7468. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7469. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7470. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7471. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7472. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7473. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7474. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7475. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7476. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7477. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7478. }
  7479. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7480. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7481. accum1 += d * sumi1;
  7482. }
  7483. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7484. #else
  7485. float sumf = 0;
  7486. for (int i = 0; i < nb; i++) {
  7487. const int8_t * q8 = y[i].qs;
  7488. const uint8_t * qs = x[i].qs;
  7489. const uint16_t * qh = x[i].qh;
  7490. int sumi = 0, sumi1 = 0;
  7491. for (int ib = 0; ib < QK_K/32; ++ib) {
  7492. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7493. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7494. int lsum = 0;
  7495. for (int l = 0; l < 4; ++l) {
  7496. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7497. for (int j = 0; j < 8; ++j) {
  7498. lsum += q8[j] * grid[j];
  7499. }
  7500. q8 += 8;
  7501. }
  7502. sumi += ls * lsum;
  7503. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7504. qs += 4;
  7505. }
  7506. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7507. }
  7508. *s = sumf;
  7509. #endif
  7510. }
  7511. 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) {
  7512. assert(n % QK_K == 0);
  7513. assert(nrc == 1);
  7514. UNUSED(nrc);
  7515. UNUSED(bx);
  7516. UNUSED(by);
  7517. UNUSED(bs);
  7518. const block_iq1_m * restrict x = vx;
  7519. const block_q8_K * restrict y = vy;
  7520. const int nb = n / QK_K;
  7521. #if QK_K != 64
  7522. iq1m_scale_t scale;
  7523. #endif
  7524. #if defined __ARM_NEON
  7525. #if QK_K == 64
  7526. const int32x4_t mask = vdupq_n_s32(0xf);
  7527. #else
  7528. const int32x4_t mask = vdupq_n_s32(0x7);
  7529. #endif
  7530. const int32x4_t mone = vdupq_n_s32(1);
  7531. const int32x4_t mzero = vdupq_n_s32(0);
  7532. ggml_int8x16x4_t deltas;
  7533. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7534. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7535. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7536. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7537. ggml_int8x16x4_t q1b;
  7538. ggml_int8x16x4_t q8b;
  7539. uint32_t aux32;
  7540. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7541. float sumf = 0;
  7542. for (int i = 0; i < nb; ++i) {
  7543. const int8_t * q8 = y[i].qs;
  7544. const uint8_t * qs = x[i].qs;
  7545. const uint8_t * qh = x[i].qh;
  7546. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7547. #if QK_K != 64
  7548. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7549. #endif
  7550. int32x4_t sumi1 = mzero;
  7551. int32x4_t sumi2 = mzero;
  7552. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7553. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7554. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7555. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7556. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7557. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7558. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7559. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7560. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7561. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7562. 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]));
  7563. 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]));
  7564. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7565. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7566. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7567. 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]));
  7568. 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]));
  7569. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7570. #if QK_K == 64
  7571. int32x4_t scales_4 = ggml_vld1q_u32(sc[0] >> 0, sc[0] >> 4, sc[0] >> 8, sc[0] >> 12);
  7572. #else
  7573. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7574. #endif
  7575. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7576. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7577. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7578. qs += 8; qh += 4;
  7579. }
  7580. #if QK_K == 64
  7581. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7582. #else
  7583. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7584. #endif
  7585. }
  7586. *s = sumf;
  7587. #elif defined __AVX2__
  7588. #if QK_K == 64
  7589. const __m256i mask = _mm256_set1_epi16(0xf);
  7590. #else
  7591. const __m256i mask = _mm256_set1_epi16(0x7);
  7592. #endif
  7593. const __m256i mone = _mm256_set1_epi16(1);
  7594. __m256 accum1 = _mm256_setzero_ps();
  7595. __m256 accum2 = _mm256_setzero_ps();
  7596. for (int i = 0; i < nb; ++i) {
  7597. const int8_t * q8 = y[i].qs;
  7598. const uint8_t * qs = x[i].qs;
  7599. const uint8_t * qh = x[i].qh;
  7600. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7601. #if QK_K != 64
  7602. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7603. #endif
  7604. __m256i sumi1 = _mm256_setzero_si256();
  7605. __m256i sumi2 = _mm256_setzero_si256();
  7606. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7607. const __m256i q1b_1 = _mm256_set_epi64x(
  7608. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7609. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7610. );
  7611. const __m256i q1b_2 = _mm256_set_epi64x(
  7612. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7613. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7614. );
  7615. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7616. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7617. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7618. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7619. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7620. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7621. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7622. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7623. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7624. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7625. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7626. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7627. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7628. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7629. #if QK_K == 64
  7630. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 4), _mm_set1_epi16(sc[0] >> 0));
  7631. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 12), _mm_set1_epi16(sc[0] >> 8));
  7632. #else
  7633. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7634. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7635. #endif
  7636. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7637. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7638. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7639. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7640. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7641. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7642. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7643. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7644. qs += 8; qh += 4;
  7645. }
  7646. #if QK_K == 64
  7647. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d));
  7648. #else
  7649. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7650. #endif
  7651. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7652. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7653. }
  7654. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7655. #else
  7656. int sum1[2], sum2[2], delta[4];
  7657. float sumf = 0;
  7658. for (int i = 0; i < nb; i++) {
  7659. const int8_t * q8 = y[i].qs;
  7660. const uint8_t * qs = x[i].qs;
  7661. const uint8_t * qh = x[i].qh;
  7662. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7663. #if QK_K != 64
  7664. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7665. #endif
  7666. int sumi1 = 0, sumi2 = 0;
  7667. for (int ib = 0; ib < QK_K/32; ++ib) {
  7668. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7669. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7670. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7671. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7672. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7673. for (int l = 0; l < 4; ++l) {
  7674. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7675. int lsum1 = 0, lsum2 = 0;
  7676. for (int j = 0; j < 8; ++j) {
  7677. lsum1 += q8[j] * grid[j];
  7678. lsum2 += q8[j];
  7679. }
  7680. q8 += 8;
  7681. sum1[l/2] += lsum1;
  7682. sum2[l/2] += lsum2*delta[l];
  7683. }
  7684. #if QK_K == 64
  7685. const int ls1 = 2*((sc[0] >> (8*(ib%2)+0)) & 0xf) + 1;
  7686. const int ls2 = 2*((sc[0] >> (8*(ib%2)+4)) & 0xf) + 1;
  7687. #else
  7688. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7689. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7690. #endif
  7691. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7692. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7693. qs += 4;
  7694. qh += 2;
  7695. }
  7696. #if QK_K == 64
  7697. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7698. #else
  7699. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7700. #endif
  7701. }
  7702. *s = sumf;
  7703. #endif
  7704. }
  7705. 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) {
  7706. assert(nrc == 1);
  7707. UNUSED(nrc);
  7708. UNUSED(bx);
  7709. UNUSED(by);
  7710. UNUSED(bs);
  7711. assert(n % QK4_NL == 0);
  7712. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7713. const block_iq4_nl * restrict x = vx;
  7714. const block_q8_0 * restrict y = vy;
  7715. const int nb = n / QK4_NL;
  7716. #if defined __ARM_NEON
  7717. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7718. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7719. uint8x16x2_t q4bits;
  7720. int8x16x4_t q4b;
  7721. int8x16x4_t q8b;
  7722. int32x4_t prod_1, prod_2;
  7723. float sumf = 0;
  7724. for (int ib = 0; ib < nb; ib += 2) {
  7725. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7726. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7727. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7728. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7729. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7730. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7731. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7732. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7733. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7734. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7735. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7736. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7737. sumf +=
  7738. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7739. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7740. }
  7741. *s = sumf;
  7742. #elif defined __AVX2__
  7743. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7744. const __m128i m4b = _mm_set1_epi8(0x0f);
  7745. const __m256i mone = _mm256_set1_epi16(1);
  7746. __m256 accum1 = _mm256_setzero_ps();
  7747. __m256 accum2 = _mm256_setzero_ps();
  7748. for (int ib = 0; ib < nb; ib += 2) {
  7749. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7750. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7751. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7752. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7753. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7754. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7755. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7756. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7757. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7758. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7759. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7760. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7761. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7762. _mm256_cvtepi32_ps(p_1), accum1);
  7763. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7764. _mm256_cvtepi32_ps(p_2), accum2);
  7765. y += 2;
  7766. x += 2;
  7767. }
  7768. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7769. #else
  7770. float sumf = 0;
  7771. for (int ib = 0; ib < nb; ++ib) {
  7772. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7773. int sumi1 = 0, sumi2 = 0;
  7774. for (int j = 0; j < QK4_NL/2; ++j) {
  7775. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7776. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7777. }
  7778. sumf += d * (sumi1 + sumi2);
  7779. }
  7780. *s = sumf;
  7781. #endif
  7782. }
  7783. 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) {
  7784. assert(nrc == 1);
  7785. UNUSED(nrc);
  7786. UNUSED(bx);
  7787. UNUSED(by);
  7788. UNUSED(bs);
  7789. assert(n % QK_K == 0);
  7790. #if QK_K == 64
  7791. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7792. #else
  7793. const block_iq4_xs * restrict x = vx;
  7794. const block_q8_K * restrict y = vy;
  7795. const int nb = n / QK_K;
  7796. #if defined __ARM_NEON
  7797. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7798. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7799. ggml_uint8x16x2_t q4bits;
  7800. ggml_int8x16x4_t q4b;
  7801. ggml_int8x16x4_t q8b;
  7802. int32x4_t prod_1, prod_2;
  7803. float sumf = 0;
  7804. for (int ibl = 0; ibl < nb; ++ibl) {
  7805. const int8_t * q8 = y[ibl].qs;
  7806. const uint8_t * q4 = x[ibl].qs;
  7807. uint16_t h = x[ibl].scales_h;
  7808. int sumi1 = 0, sumi2 = 0;
  7809. for (int ib = 0; ib < QK_K/64; ++ib) {
  7810. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7811. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7812. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7813. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7814. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7815. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7816. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7817. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7818. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7819. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7820. h >>= 4;
  7821. sumi1 += vaddvq_s32(prod_1) * ls1;
  7822. sumi2 += vaddvq_s32(prod_2) * ls2;
  7823. }
  7824. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7825. }
  7826. *s = sumf;
  7827. #elif defined __AVX2__
  7828. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7829. const __m128i m4b = _mm_set1_epi8(0x0f);
  7830. __m256 accum = _mm256_setzero_ps();
  7831. for (int ibl = 0; ibl < nb; ++ibl) {
  7832. const uint8_t * qs = x[ibl].qs;
  7833. const int8_t * q8 = y[ibl].qs;
  7834. uint16_t sh = x[ibl].scales_h;
  7835. __m256i sumi1 = _mm256_setzero_si256();
  7836. __m256i sumi2 = _mm256_setzero_si256();
  7837. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7838. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7839. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7840. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7841. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7842. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7843. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7844. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7845. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7846. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7847. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7848. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7849. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7850. sh >>= 4;
  7851. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7852. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7853. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7854. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7855. }
  7856. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7857. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7858. }
  7859. *s = hsum_float_8(accum);
  7860. #else
  7861. float sumf = 0;
  7862. for (int ibl = 0; ibl < nb; ++ibl) {
  7863. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7864. uint16_t h = x[ibl].scales_h;
  7865. const uint8_t * qs = x[ibl].qs;
  7866. const int8_t * q8 = y[ibl].qs;
  7867. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7868. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7869. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7870. h >>= 4;
  7871. const float d1 = d4d8*(ls1 - 32);
  7872. const float d2 = d4d8*(ls2 - 32);
  7873. int sumi1 = 0, sumi2 = 0;
  7874. for (int j = 0; j < 16; ++j) {
  7875. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7876. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7877. }
  7878. sumf += d1 * (sumi1 + sumi2);
  7879. qs += 16;
  7880. q8 += 32;
  7881. sumi1 = sumi2 = 0;
  7882. for (int j = 0; j < 16; ++j) {
  7883. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7884. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7885. }
  7886. sumf += d2 * (sumi1 + sumi2);
  7887. qs += 16;
  7888. q8 += 32;
  7889. }
  7890. }
  7891. *s = sumf;
  7892. #endif
  7893. #endif
  7894. }
  7895. // ================================ IQ2 quantization =============================================
  7896. typedef struct {
  7897. uint64_t * grid;
  7898. int * map;
  7899. uint16_t * neighbours;
  7900. } iq2_entry_t;
  7901. static iq2_entry_t iq2_data[4] = {
  7902. {NULL, NULL, NULL},
  7903. {NULL, NULL, NULL},
  7904. {NULL, NULL, NULL},
  7905. {NULL, NULL, NULL},
  7906. };
  7907. static inline int iq2_data_index(enum ggml_type type) {
  7908. 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);
  7909. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7910. type == GGML_TYPE_IQ2_XS ? 1 :
  7911. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  7912. }
  7913. static inline int iq2_grid_size(enum ggml_type type) {
  7914. 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);
  7915. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7916. type == GGML_TYPE_IQ2_XS ? 512 :
  7917. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  7918. }
  7919. static int iq2_compare_func(const void * left, const void * right) {
  7920. const int * l = (const int *)left;
  7921. const int * r = (const int *)right;
  7922. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7923. }
  7924. void iq2xs_init_impl(enum ggml_type type) {
  7925. const int gindex = iq2_data_index(type);
  7926. const int grid_size = iq2_grid_size(type);
  7927. if (iq2_data[gindex].grid) {
  7928. return;
  7929. }
  7930. static const uint16_t kgrid_2bit_256[256] = {
  7931. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7932. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7933. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7934. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7935. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7936. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7937. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7938. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7939. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7940. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7941. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7942. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7943. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7944. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7945. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7946. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7947. };
  7948. static const uint16_t kgrid_2bit_512[512] = {
  7949. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7950. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7951. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7952. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7953. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7954. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7955. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7956. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7957. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7958. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7959. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7960. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7961. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7962. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7963. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7964. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7965. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7966. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7967. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7968. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7969. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7970. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7971. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7972. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7973. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7974. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7975. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7976. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7977. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7978. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7979. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7980. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7981. };
  7982. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7983. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7984. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7985. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7986. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7987. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7988. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7989. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7990. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7991. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7992. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7993. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7994. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7995. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7996. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7997. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7998. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7999. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  8000. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  8001. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  8002. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  8003. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  8004. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8005. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8006. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8007. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8008. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8009. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8010. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8011. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8012. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8013. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8014. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8015. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8016. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8017. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8018. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8019. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8020. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8021. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8022. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8023. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8024. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8025. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8026. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8027. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8028. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8029. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8030. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8031. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8032. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8033. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8034. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8035. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8036. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8037. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8038. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8039. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8040. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8041. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8042. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8043. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8044. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8045. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8046. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8047. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8048. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8049. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8050. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8051. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8052. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8053. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8054. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8055. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8056. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8057. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8058. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8059. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8060. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8061. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8062. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8063. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8064. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8065. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8066. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8067. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8068. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8069. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8070. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8071. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8072. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8073. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8074. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8075. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8076. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8077. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8078. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8079. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8080. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8081. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8082. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8083. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8084. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8085. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8086. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8087. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8088. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8089. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8090. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8091. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8092. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8093. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8094. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8095. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8096. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8097. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8098. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8099. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8100. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8101. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8102. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8103. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8104. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8105. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8106. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8107. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8108. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8109. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8110. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8111. };
  8112. static const uint16_t kgrid_2bit_1024[1024] = {
  8113. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8114. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8115. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8116. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8117. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8118. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8119. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8120. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8121. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8122. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8123. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8124. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8125. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8126. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8127. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8128. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8129. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8130. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8131. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8132. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8133. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8134. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8135. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8136. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8137. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8138. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8139. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8140. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8141. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8142. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8143. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8144. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8145. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8146. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8147. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8148. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8149. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8150. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8151. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8152. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8153. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8154. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8155. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8156. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8157. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8158. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8159. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8160. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8161. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8162. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8163. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8164. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8165. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8166. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8167. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8168. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8169. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8170. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8171. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8172. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8173. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8174. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8175. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8176. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8177. };
  8178. const int kmap_size = 43692;
  8179. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8180. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8181. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8182. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8183. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8184. uint64_t * kgrid_q2xs;
  8185. int * kmap_q2xs;
  8186. uint16_t * kneighbors_q2xs;
  8187. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8188. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8189. for (int k = 0; k < grid_size; ++k) {
  8190. int8_t * pos = (int8_t *)(the_grid + k);
  8191. for (int i = 0; i < 8; ++i) {
  8192. int l = (kgrid[k] >> 2*i) & 0x3;
  8193. pos[i] = 2*l + 1;
  8194. }
  8195. }
  8196. kgrid_q2xs = the_grid;
  8197. iq2_data[gindex].grid = the_grid;
  8198. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8199. iq2_data[gindex].map = kmap_q2xs;
  8200. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8201. uint64_t aux64;
  8202. uint8_t * aux8 = (uint8_t *)&aux64;
  8203. for (int i = 0; i < grid_size; ++i) {
  8204. aux64 = kgrid_q2xs[i];
  8205. uint16_t index = 0;
  8206. for (int k=0; k<8; ++k) {
  8207. uint16_t q = (aux8[k] - 1)/2;
  8208. index |= (q << 2*k);
  8209. }
  8210. kmap_q2xs[index] = i;
  8211. }
  8212. int8_t pos[8];
  8213. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8214. int num_neighbors = 0, num_not_in_map = 0;
  8215. for (int i = 0; i < kmap_size; ++i) {
  8216. if (kmap_q2xs[i] >= 0) continue;
  8217. ++num_not_in_map;
  8218. for (int k = 0; k < 8; ++k) {
  8219. int l = (i >> 2*k) & 0x3;
  8220. pos[k] = 2*l + 1;
  8221. }
  8222. for (int j = 0; j < grid_size; ++j) {
  8223. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8224. int d2 = 0;
  8225. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8226. dist2[2*j+0] = d2;
  8227. dist2[2*j+1] = j;
  8228. }
  8229. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8230. int n = 0; int d2 = dist2[0];
  8231. int nhave = 1;
  8232. for (int j = 0; j < grid_size; ++j) {
  8233. if (dist2[2*j] > d2) {
  8234. if (nhave == nwant) break;
  8235. d2 = dist2[2*j];
  8236. ++nhave;
  8237. }
  8238. ++n;
  8239. }
  8240. num_neighbors += n;
  8241. }
  8242. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8243. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8244. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8245. int counter = 0;
  8246. for (int i = 0; i < kmap_size; ++i) {
  8247. if (kmap_q2xs[i] >= 0) continue;
  8248. for (int k = 0; k < 8; ++k) {
  8249. int l = (i >> 2*k) & 0x3;
  8250. pos[k] = 2*l + 1;
  8251. }
  8252. for (int j = 0; j < grid_size; ++j) {
  8253. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8254. int d2 = 0;
  8255. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8256. dist2[2*j+0] = d2;
  8257. dist2[2*j+1] = j;
  8258. }
  8259. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8260. kmap_q2xs[i] = -(counter + 1);
  8261. int d2 = dist2[0];
  8262. uint16_t * start = &kneighbors_q2xs[counter++];
  8263. int n = 0, nhave = 1;
  8264. for (int j = 0; j < grid_size; ++j) {
  8265. if (dist2[2*j] > d2) {
  8266. if (nhave == nwant) break;
  8267. d2 = dist2[2*j];
  8268. ++nhave;
  8269. }
  8270. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8271. ++n;
  8272. }
  8273. *start = n;
  8274. }
  8275. free(dist2);
  8276. }
  8277. void iq2xs_free_impl(enum ggml_type type) {
  8278. 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);
  8279. const int gindex = iq2_data_index(type);
  8280. if (iq2_data[gindex].grid) {
  8281. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8282. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8283. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8284. }
  8285. }
  8286. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8287. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8288. int num_neighbors = neighbours[0];
  8289. GGML_ASSERT(num_neighbors > 0);
  8290. float best_d2 = FLT_MAX;
  8291. int grid_index = -1;
  8292. for (int j = 1; j <= num_neighbors; ++j) {
  8293. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8294. float d2 = 0;
  8295. for (int i = 0; i < 8; ++i) {
  8296. float q = pg[i];
  8297. float diff = scale*q - xval[i];
  8298. d2 += weight[i]*diff*diff;
  8299. }
  8300. if (d2 < best_d2) {
  8301. best_d2 = d2; grid_index = neighbours[j];
  8302. }
  8303. }
  8304. GGML_ASSERT(grid_index >= 0);
  8305. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8306. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8307. return grid_index;
  8308. }
  8309. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8310. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8311. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8312. const int * kmap_q2xs = iq2_data[gindex].map;
  8313. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8314. GGML_ASSERT(quant_weights && "missing quantization weights");
  8315. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8316. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8317. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8318. GGML_ASSERT(n%QK_K == 0);
  8319. const int kMaxQ = 3;
  8320. const int64_t nbl = n/QK_K;
  8321. block_iq2_xxs * y = vy;
  8322. float scales[QK_K/32];
  8323. float weight[32];
  8324. float xval[32];
  8325. int8_t L[32];
  8326. int8_t Laux[32];
  8327. float waux[32];
  8328. uint8_t block_signs[4];
  8329. uint32_t q2[2*(QK_K/32)];
  8330. for (int ibl = 0; ibl < nbl; ++ibl) {
  8331. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8332. memset(q2, 0, QK_K/4);
  8333. float max_scale = 0;
  8334. const float * xbl = x + QK_K*ibl;
  8335. float sumx2 = 0;
  8336. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8337. float sigma2 = sumx2/QK_K;
  8338. for (int ib = 0; ib < QK_K/32; ++ib) {
  8339. const float * xb = xbl + 32*ib;
  8340. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8341. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8342. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8343. for (int k = 0; k < 4; ++k) {
  8344. int nflip = 0;
  8345. uint8_t s = 0;
  8346. for (int i = 0; i < 8; ++i) {
  8347. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8348. else {
  8349. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8350. }
  8351. }
  8352. if (nflip%2) {
  8353. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8354. for (int i = 1; i < 8; ++i) {
  8355. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8356. if (ax < min) {
  8357. min = ax; imin = i;
  8358. }
  8359. }
  8360. xval[8*k+imin] = -xval[8*k+imin];
  8361. s ^= (1 << imin);
  8362. }
  8363. block_signs[k] = s & 127;
  8364. }
  8365. float max = xval[0];
  8366. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8367. if (!max) {
  8368. scales[ib] = 0;
  8369. memset(L, 0, 32);
  8370. continue;
  8371. }
  8372. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8373. float eff_max = scale*kMaxQ;
  8374. float best = 0;
  8375. for (int is = -6; is <= 6; ++is) {
  8376. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8377. float this_scale = 1/id;
  8378. for (int k = 0; k < 4; ++k) {
  8379. for (int i = 0; i < 8; ++i) {
  8380. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8381. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8382. }
  8383. uint16_t u = 0;
  8384. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8385. int grid_index = kmap_q2xs[u];
  8386. if (grid_index < 0) {
  8387. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8388. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8389. }
  8390. }
  8391. float sumqx = 0, sumq2 = 0;
  8392. for (int i = 0; i < 32; ++i) {
  8393. float w = weight[i];
  8394. float q = 2*Laux[i] + 1;
  8395. sumqx += w*xval[i]*q;
  8396. sumq2 += w*q*q;
  8397. }
  8398. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8399. scale = sumqx/sumq2; best = scale*sumqx;
  8400. memcpy(L, Laux, 32);
  8401. }
  8402. }
  8403. if (scale > 0) {
  8404. float id = 1/scale;
  8405. for (int k = 0; k < 4; ++k) {
  8406. uint16_t u = 0;
  8407. for (int i = 0; i < 8; ++i) {
  8408. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8409. l = MAX(0, MIN(kMaxQ-1, l));
  8410. u |= (l << 2*i);
  8411. }
  8412. int grid_index = kmap_q2xs[u];
  8413. if (grid_index < 0) {
  8414. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8415. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8416. }
  8417. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8418. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8419. }
  8420. float sumqx = 0, sumq2 = 0;
  8421. for (int i = 0; i < 32; ++i) {
  8422. float w = weight[i];
  8423. float q = 2*L[i] + 1;
  8424. sumqx += w*xval[i]*q;
  8425. sumq2 += w*q*q;
  8426. }
  8427. if (sumq2 > 0) scale = sumqx/sumq2;
  8428. }
  8429. if (scale < 0) {
  8430. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8431. // and correspondingly flip quant signs.
  8432. scale = -scale;
  8433. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8434. }
  8435. for (int k = 0; k < 4; ++k) {
  8436. uint16_t u = 0;
  8437. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8438. int grid_index = kmap_q2xs[u];
  8439. if (grid_index < 0) {
  8440. printf("Oops: found point %u not on grid:", u);
  8441. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8442. printf("\n");
  8443. GGML_ASSERT(false);
  8444. }
  8445. q2[2*ib+0] |= (grid_index << 8*k);
  8446. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8447. }
  8448. GGML_ASSERT(scale >= 0);
  8449. scales[ib] = scale;
  8450. max_scale = MAX(max_scale, scale);
  8451. }
  8452. if (!max_scale) {
  8453. memset(y[ibl].qs, 0, QK_K/4);
  8454. continue;
  8455. }
  8456. float d = max_scale/31;
  8457. y[ibl].d = GGML_FP32_TO_FP16(d);
  8458. float id = 1/d;
  8459. for (int ib = 0; ib < QK_K/32; ++ib) {
  8460. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8461. l = MAX(0, MIN(15, l));
  8462. q2[2*ib+1] |= ((uint32_t)l << 28);
  8463. }
  8464. memcpy(y[ibl].qs, q2, QK_K/4);
  8465. }
  8466. }
  8467. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8468. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8469. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8470. const int * kmap_q2xs = iq2_data[gindex].map;
  8471. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8472. GGML_ASSERT(quant_weights && "missing quantization weights");
  8473. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8474. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8475. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8476. GGML_ASSERT(n%QK_K == 0);
  8477. const int kMaxQ = 3;
  8478. const int64_t nbl = n/QK_K;
  8479. block_iq2_xs * y = vy;
  8480. float scales[QK_K/16];
  8481. float weight[16];
  8482. float xval[16];
  8483. int8_t L[16];
  8484. int8_t Laux[16];
  8485. float waux[16];
  8486. bool is_on_grid[2];
  8487. bool is_on_grid_aux[2];
  8488. uint8_t block_signs[2];
  8489. uint16_t q2[2*(QK_K/16)];
  8490. for (int ibl = 0; ibl < nbl; ++ibl) {
  8491. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8492. memset(q2, 0, QK_K/4);
  8493. memset(y[ibl].scales, 0, QK_K/32);
  8494. float max_scale = 0;
  8495. const float * xbl = x + QK_K*ibl;
  8496. float sumx2 = 0;
  8497. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8498. float sigma2 = sumx2/QK_K;
  8499. for (int ib = 0; ib < QK_K/16; ++ib) {
  8500. const float * xb = xbl + 16*ib;
  8501. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8502. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8503. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8504. for (int k = 0; k < 2; ++k) {
  8505. int nflip = 0;
  8506. uint8_t s = 0;
  8507. for (int i = 0; i < 8; ++i) {
  8508. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8509. else {
  8510. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8511. }
  8512. }
  8513. if (nflip%2) {
  8514. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8515. for (int i = 1; i < 8; ++i) {
  8516. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8517. if (ax < min) {
  8518. min = ax; imin = i;
  8519. }
  8520. }
  8521. xval[8*k+imin] = -xval[8*k+imin];
  8522. s ^= (1 << imin);
  8523. }
  8524. block_signs[k] = s & 127;
  8525. }
  8526. float max = xval[0];
  8527. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8528. if (!max) {
  8529. scales[ib] = 0;
  8530. memset(L, 0, 16);
  8531. continue;
  8532. }
  8533. float best = 0;
  8534. float scale = max/(2*kMaxQ-1);
  8535. is_on_grid[0] = is_on_grid[1] = true;
  8536. for (int is = -9; is <= 9; ++is) {
  8537. float id = (2*kMaxQ-1+is*0.1f)/max;
  8538. float this_scale = 1/id;
  8539. for (int k = 0; k < 2; ++k) {
  8540. for (int i = 0; i < 8; ++i) {
  8541. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8542. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8543. }
  8544. uint16_t u = 0;
  8545. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8546. int grid_index = kmap_q2xs[u];
  8547. is_on_grid_aux[k] = true;
  8548. if (grid_index < 0) {
  8549. is_on_grid_aux[k] = false;
  8550. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8551. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8552. }
  8553. }
  8554. float sumqx = 0, sumq2 = 0;
  8555. for (int i = 0; i < 16; ++i) {
  8556. float w = weight[i];
  8557. float q = 2*Laux[i] + 1;
  8558. sumqx += w*xval[i]*q;
  8559. sumq2 += w*q*q;
  8560. }
  8561. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8562. scale = sumqx/sumq2; best = scale*sumqx;
  8563. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8564. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8565. }
  8566. }
  8567. int n_not_ongrid = 0;
  8568. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8569. if (n_not_ongrid > 0 && scale > 0) {
  8570. float id = 1/scale;
  8571. for (int k = 0; k < 2; ++k) {
  8572. if (is_on_grid[k]) continue;
  8573. uint16_t u = 0;
  8574. for (int i = 0; i < 8; ++i) {
  8575. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8576. l = MAX(0, MIN(kMaxQ-1, l));
  8577. u |= (l << 2*i);
  8578. L[8*k + i] = l;
  8579. }
  8580. int grid_index = kmap_q2xs[u];
  8581. if (grid_index < 0) {
  8582. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8583. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8584. }
  8585. }
  8586. float sumqx = 0, sumq2 = 0;
  8587. for (int i = 0; i < 16; ++i) {
  8588. float w = weight[i];
  8589. float q = 2*L[i] + 1;
  8590. sumqx += w*xval[i]*q;
  8591. sumq2 += w*q*q;
  8592. }
  8593. if (sumq2 > 0) scale = sumqx/sumq2;
  8594. }
  8595. if (scale < 0) {
  8596. scale = -scale;
  8597. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8598. }
  8599. for (int k = 0; k < 2; ++k) {
  8600. uint16_t u = 0;
  8601. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8602. int grid_index = kmap_q2xs[u];
  8603. if (grid_index < 0) {
  8604. printf("Oops: found point %u not on grid:", u);
  8605. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8606. printf("\n");
  8607. GGML_ASSERT(false);
  8608. }
  8609. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8610. }
  8611. GGML_ASSERT(scale >= 0);
  8612. scales[ib] = scale;
  8613. max_scale = MAX(max_scale, scale);
  8614. }
  8615. if (!max_scale) {
  8616. memset(y[ibl].qs, 0, QK_K/4);
  8617. continue;
  8618. }
  8619. float d = max_scale/31;
  8620. y[ibl].d = GGML_FP32_TO_FP16(d);
  8621. float id = 1/d;
  8622. for (int ib = 0; ib < QK_K/16; ++ib) {
  8623. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8624. l = MAX(0, MIN(15, l));
  8625. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8626. else y[ibl].scales[ib/2] |= (l << 4);
  8627. }
  8628. memcpy(y[ibl].qs, q2, QK_K/4);
  8629. }
  8630. }
  8631. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8632. GGML_ASSERT(n_per_row%QK_K == 0);
  8633. int64_t nblock = n_per_row/QK_K;
  8634. char * qrow = (char *)dst;
  8635. for (int64_t row = 0; row < nrow; ++row) {
  8636. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8637. src += n_per_row;
  8638. qrow += nblock*sizeof(block_iq2_xxs);
  8639. }
  8640. return nrow * nblock * sizeof(block_iq2_xxs);
  8641. }
  8642. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8643. GGML_ASSERT(n_per_row%QK_K == 0);
  8644. int64_t nblock = n_per_row/QK_K;
  8645. char * qrow = (char *)dst;
  8646. for (int64_t row = 0; row < nrow; ++row) {
  8647. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8648. src += n_per_row;
  8649. qrow += nblock*sizeof(block_iq2_xs);
  8650. }
  8651. return nrow * nblock * sizeof(block_iq2_xs);
  8652. }
  8653. //
  8654. // ============================================= 3-bit using D4 lattice
  8655. //
  8656. typedef struct {
  8657. uint32_t * grid;
  8658. int * map;
  8659. uint16_t * neighbours;
  8660. } iq3_entry_t;
  8661. static iq3_entry_t iq3_data[2] = {
  8662. {NULL, NULL, NULL},
  8663. {NULL, NULL, NULL},
  8664. };
  8665. static inline int iq3_data_index(int grid_size) {
  8666. (void)grid_size;
  8667. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8668. return grid_size == 256 ? 0 : 1;
  8669. }
  8670. static int iq3_compare_func(const void * left, const void * right) {
  8671. const int * l = (const int *)left;
  8672. const int * r = (const int *)right;
  8673. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8674. }
  8675. void iq3xs_init_impl(int grid_size) {
  8676. const int gindex = iq3_data_index(grid_size);
  8677. if (iq3_data[gindex].grid) {
  8678. return;
  8679. }
  8680. static const uint16_t kgrid_256[256] = {
  8681. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8682. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8683. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8684. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8685. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8686. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8687. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8688. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8689. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8690. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8691. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8692. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8693. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8694. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8695. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8696. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8697. };
  8698. static const uint16_t kgrid_512[512] = {
  8699. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8700. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8701. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8702. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8703. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8704. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8705. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8706. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8707. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8708. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8709. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8710. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8711. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8712. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8713. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8714. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8715. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8716. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8717. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8718. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8719. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8720. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8721. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8722. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8723. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8724. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8725. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8726. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8727. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8728. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8729. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8730. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8731. };
  8732. const int kmap_size = 4096;
  8733. const int nwant = grid_size == 256 ? 2 : 3;
  8734. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8735. uint32_t * kgrid_q3xs;
  8736. int * kmap_q3xs;
  8737. uint16_t * kneighbors_q3xs;
  8738. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8739. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8740. for (int k = 0; k < grid_size; ++k) {
  8741. int8_t * pos = (int8_t *)(the_grid + k);
  8742. for (int i = 0; i < 4; ++i) {
  8743. int l = (kgrid[k] >> 3*i) & 0x7;
  8744. pos[i] = 2*l + 1;
  8745. }
  8746. }
  8747. kgrid_q3xs = the_grid;
  8748. iq3_data[gindex].grid = the_grid;
  8749. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8750. iq3_data[gindex].map = kmap_q3xs;
  8751. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8752. uint32_t aux32;
  8753. uint8_t * aux8 = (uint8_t *)&aux32;
  8754. for (int i = 0; i < grid_size; ++i) {
  8755. aux32 = kgrid_q3xs[i];
  8756. uint16_t index = 0;
  8757. for (int k=0; k<4; ++k) {
  8758. uint16_t q = (aux8[k] - 1)/2;
  8759. index |= (q << 3*k);
  8760. }
  8761. kmap_q3xs[index] = i;
  8762. }
  8763. int8_t pos[4];
  8764. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8765. int num_neighbors = 0, num_not_in_map = 0;
  8766. for (int i = 0; i < kmap_size; ++i) {
  8767. if (kmap_q3xs[i] >= 0) continue;
  8768. ++num_not_in_map;
  8769. for (int k = 0; k < 4; ++k) {
  8770. int l = (i >> 3*k) & 0x7;
  8771. pos[k] = 2*l + 1;
  8772. }
  8773. for (int j = 0; j < grid_size; ++j) {
  8774. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8775. int d2 = 0;
  8776. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8777. dist2[2*j+0] = d2;
  8778. dist2[2*j+1] = j;
  8779. }
  8780. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8781. int n = 0; int d2 = dist2[0];
  8782. int nhave = 1;
  8783. for (int j = 0; j < grid_size; ++j) {
  8784. if (dist2[2*j] > d2) {
  8785. if (nhave == nwant) break;
  8786. d2 = dist2[2*j];
  8787. ++nhave;
  8788. }
  8789. ++n;
  8790. }
  8791. num_neighbors += n;
  8792. }
  8793. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8794. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8795. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8796. int counter = 0;
  8797. for (int i = 0; i < kmap_size; ++i) {
  8798. if (kmap_q3xs[i] >= 0) continue;
  8799. for (int k = 0; k < 4; ++k) {
  8800. int l = (i >> 3*k) & 0x7;
  8801. pos[k] = 2*l + 1;
  8802. }
  8803. for (int j = 0; j < grid_size; ++j) {
  8804. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8805. int d2 = 0;
  8806. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8807. dist2[2*j+0] = d2;
  8808. dist2[2*j+1] = j;
  8809. }
  8810. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8811. kmap_q3xs[i] = -(counter + 1);
  8812. int d2 = dist2[0];
  8813. uint16_t * start = &kneighbors_q3xs[counter++];
  8814. int n = 0, nhave = 1;
  8815. for (int j = 0; j < grid_size; ++j) {
  8816. if (dist2[2*j] > d2) {
  8817. if (nhave == nwant) break;
  8818. d2 = dist2[2*j];
  8819. ++nhave;
  8820. }
  8821. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8822. ++n;
  8823. }
  8824. *start = n;
  8825. }
  8826. free(dist2);
  8827. }
  8828. void iq3xs_free_impl(int grid_size) {
  8829. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8830. const int gindex = iq3_data_index(grid_size);
  8831. if (iq3_data[gindex].grid) {
  8832. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8833. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8834. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8835. }
  8836. }
  8837. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8838. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8839. int num_neighbors = neighbours[0];
  8840. GGML_ASSERT(num_neighbors > 0);
  8841. float best_d2 = FLT_MAX;
  8842. int grid_index = -1;
  8843. for (int j = 1; j <= num_neighbors; ++j) {
  8844. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8845. float d2 = 0;
  8846. for (int i = 0; i < 4; ++i) {
  8847. float q = pg[i];
  8848. float diff = scale*q - xval[i];
  8849. d2 += weight[i]*diff*diff;
  8850. }
  8851. if (d2 < best_d2) {
  8852. best_d2 = d2; grid_index = neighbours[j];
  8853. }
  8854. }
  8855. GGML_ASSERT(grid_index >= 0);
  8856. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8857. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8858. return grid_index;
  8859. }
  8860. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int64_t n,
  8861. const float * restrict quant_weights) {
  8862. const int gindex = iq3_data_index(grid_size);
  8863. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8864. const int * kmap_q3xs = iq3_data[gindex].map;
  8865. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8866. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8867. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8868. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8869. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8870. GGML_ASSERT(n%QK_K == 0);
  8871. const int kMaxQ = 8;
  8872. const int64_t nbl = n/QK_K;
  8873. ggml_fp16_t * dh;
  8874. uint8_t * qs;
  8875. int block_size;
  8876. if (grid_size == 256) {
  8877. block_iq3_xxs * y = vy;
  8878. dh = &y->d;
  8879. qs = y->qs;
  8880. block_size = sizeof(block_iq3_xxs);
  8881. } else {
  8882. block_iq3_s * y = vy;
  8883. dh = &y->d;
  8884. qs = y->qs;
  8885. block_size = sizeof(block_iq3_s);
  8886. }
  8887. int quant_size = block_size - sizeof(ggml_fp16_t);
  8888. float scales[QK_K/32];
  8889. float weight[32];
  8890. float xval[32];
  8891. int8_t L[32];
  8892. int8_t Laux[32];
  8893. float waux[32];
  8894. bool is_on_grid[8];
  8895. bool is_on_grid_aux[8];
  8896. uint8_t block_signs[8];
  8897. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8898. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8899. uint8_t * qh = q3 + 3*(QK_K/8);
  8900. for (int ibl = 0; ibl < nbl; ++ibl) {
  8901. dh[0] = GGML_FP32_TO_FP16(0.f);
  8902. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8903. float max_scale = 0;
  8904. const float * xbl = x + QK_K*ibl;
  8905. float sumx2 = 0;
  8906. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8907. float sigma2 = 2*sumx2/QK_K;
  8908. for (int ib = 0; ib < QK_K/32; ++ib) {
  8909. const float * xb = xbl + 32*ib;
  8910. if (quant_weights) {
  8911. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8912. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8913. } else {
  8914. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8915. }
  8916. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8917. for (int k = 0; k < 4; ++k) {
  8918. int nflip = 0;
  8919. uint8_t s = 0;
  8920. for (int i = 0; i < 8; ++i) {
  8921. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8922. else {
  8923. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8924. }
  8925. }
  8926. if (nflip%2) {
  8927. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8928. for (int i = 1; i < 8; ++i) {
  8929. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8930. if (ax < min) {
  8931. min = ax; imin = i;
  8932. }
  8933. }
  8934. xval[8*k+imin] = -xval[8*k+imin];
  8935. s ^= (1 << imin);
  8936. }
  8937. block_signs[k] = s & 127;
  8938. }
  8939. float max = xval[0];
  8940. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8941. if (!max) {
  8942. scales[ib] = 0;
  8943. memset(L, 0, 32);
  8944. continue;
  8945. }
  8946. float best = 0;
  8947. float scale = max/(2*kMaxQ-1);
  8948. for (int is = -15; is <= 15; ++is) {
  8949. float id = (2*kMaxQ-1+is*0.2f)/max;
  8950. float this_scale = 1/id;
  8951. for (int k = 0; k < 8; ++k) {
  8952. for (int i = 0; i < 4; ++i) {
  8953. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8954. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8955. }
  8956. uint16_t u = 0;
  8957. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8958. int grid_index = kmap_q3xs[u];
  8959. is_on_grid_aux[k] = true;
  8960. if (grid_index < 0) {
  8961. is_on_grid_aux[k] = false;
  8962. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8963. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8964. }
  8965. }
  8966. float sumqx = 0, sumq2 = 0;
  8967. for (int i = 0; i < 32; ++i) {
  8968. float w = weight[i];
  8969. float q = 2*Laux[i] + 1;
  8970. sumqx += w*xval[i]*q;
  8971. sumq2 += w*q*q;
  8972. }
  8973. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8974. scale = sumqx/sumq2; best = scale*sumqx;
  8975. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8976. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8977. }
  8978. }
  8979. int n_not_ongrid = 0;
  8980. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8981. if (n_not_ongrid > 0 && scale > 0) {
  8982. float id = 1/scale;
  8983. for (int k = 0; k < 8; ++k) {
  8984. if (is_on_grid[k]) continue;
  8985. uint16_t u = 0;
  8986. for (int i = 0; i < 4; ++i) {
  8987. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8988. l = MAX(0, MIN(kMaxQ-1, l));
  8989. u |= (l << 3*i);
  8990. }
  8991. int grid_index = kmap_q3xs[u];
  8992. if (grid_index < 0) {
  8993. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8994. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8995. }
  8996. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8997. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8998. }
  8999. float sumqx = 0, sumq2 = 0;
  9000. for (int i = 0; i < 32; ++i) {
  9001. float w = weight[i];
  9002. float q = 2*L[i] + 1;
  9003. sumqx += w*xval[i]*q;
  9004. sumq2 += w*q*q;
  9005. }
  9006. if (sumq2 > 0) scale = sumqx/sumq2;
  9007. }
  9008. if (scale < 0) {
  9009. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9010. // and correspondingly flip quant signs.
  9011. scale = -scale;
  9012. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9013. }
  9014. for (int k = 0; k < 8; ++k) {
  9015. uint16_t u = 0;
  9016. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9017. int grid_index = kmap_q3xs[u];
  9018. if (grid_index < 0) {
  9019. printf("Oops: found point %u not on grid:", u);
  9020. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9021. printf("\n");
  9022. GGML_ASSERT(false);
  9023. }
  9024. if (grid_size == 256) {
  9025. q3[8*ib+k] = grid_index;
  9026. } else {
  9027. q3[8*ib+k] = grid_index & 255;
  9028. qh[ib] |= ((grid_index >> 8) << k);
  9029. }
  9030. }
  9031. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9032. GGML_ASSERT(scale >= 0);
  9033. scales[ib] = scale;
  9034. max_scale = MAX(max_scale, scale);
  9035. }
  9036. if (!max_scale) {
  9037. memset(qs, 0, quant_size);
  9038. dh += block_size/sizeof(ggml_fp16_t);
  9039. qs += block_size;
  9040. continue;
  9041. }
  9042. float d = max_scale/31;
  9043. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9044. float id = 1/d;
  9045. for (int ib = 0; ib < QK_K/32; ++ib) {
  9046. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9047. l = MAX(0, MIN(15, l));
  9048. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9049. }
  9050. memcpy(qs, q3, quant_size);
  9051. dh += block_size/sizeof(ggml_fp16_t);
  9052. qs += block_size;
  9053. }
  9054. }
  9055. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9056. GGML_ASSERT(n_per_row%QK_K == 0);
  9057. int64_t nblock = n_per_row/QK_K;
  9058. char * qrow = (char *)dst;
  9059. for (int64_t row = 0; row < nrow; ++row) {
  9060. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9061. src += n_per_row;
  9062. qrow += nblock*sizeof(block_iq3_xxs);
  9063. }
  9064. return nrow * nblock * sizeof(block_iq3_xxs);
  9065. }
  9066. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int64_t k) {
  9067. assert(k % QK_K == 0);
  9068. block_iq3_xxs * restrict y = vy;
  9069. quantize_row_iq3_xxs_reference(x, y, k);
  9070. }
  9071. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int64_t k) {
  9072. assert(k % QK_K == 0);
  9073. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9074. }
  9075. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9076. const float * restrict quant_weights,
  9077. float * scales,
  9078. float * weight,
  9079. float * xval,
  9080. int8_t * L,
  9081. int8_t * Laux,
  9082. float * waux,
  9083. bool * is_on_grid,
  9084. bool * is_on_grid_aux,
  9085. uint8_t * block_signs) {
  9086. const int gindex = iq3_data_index(512);
  9087. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9088. const int * kmap_q3xs = iq3_data[gindex].map;
  9089. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9090. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9091. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9092. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9093. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9094. GGML_ASSERT(n%QK_K == 0);
  9095. const int kMaxQ = 8;
  9096. const int64_t nbl = n/QK_K;
  9097. block_iq3_s * y = vy;
  9098. const int bs4 = block_size/4;
  9099. const int bs8 = block_size/8;
  9100. for (int ibl = 0; ibl < nbl; ++ibl) {
  9101. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9102. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9103. uint8_t * qs = y[ibl].qs;
  9104. uint8_t * qh = y[ibl].qh;
  9105. uint8_t * signs = y[ibl].signs;
  9106. float max_scale = 0;
  9107. const float * xbl = x + QK_K*ibl;
  9108. float sumx2 = 0;
  9109. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9110. float sigma2 = 2*sumx2/QK_K;
  9111. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9112. const float * xb = xbl + block_size*ib;
  9113. if (quant_weights) {
  9114. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9115. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9116. } else {
  9117. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9118. }
  9119. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9120. for (int k = 0; k < bs8; ++k) {
  9121. uint8_t s = 0;
  9122. for (int i = 0; i < 8; ++i) {
  9123. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9124. else {
  9125. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9126. }
  9127. }
  9128. block_signs[k] = s;
  9129. }
  9130. float max = xval[0];
  9131. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9132. if (!max) {
  9133. scales[ib] = 0;
  9134. continue;
  9135. }
  9136. float best = 0;
  9137. float scale = max/(2*kMaxQ-1);
  9138. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9139. for (int is = -9; is <= 9; ++is) {
  9140. float id = (2*kMaxQ-1+is*0.2f)/max;
  9141. float this_scale = 1/id;
  9142. for (int k = 0; k < bs4; ++k) {
  9143. for (int i = 0; i < 4; ++i) {
  9144. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9145. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9146. }
  9147. uint16_t u = 0;
  9148. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9149. int grid_index = kmap_q3xs[u];
  9150. is_on_grid_aux[k] = true;
  9151. if (grid_index < 0) {
  9152. is_on_grid_aux[k] = false;
  9153. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9154. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9155. }
  9156. }
  9157. float sumqx = 0, sumq2 = 0;
  9158. for (int i = 0; i < block_size; ++i) {
  9159. float w = weight[i];
  9160. float q = 2*Laux[i] + 1;
  9161. sumqx += w*xval[i]*q;
  9162. sumq2 += w*q*q;
  9163. }
  9164. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9165. scale = sumqx/sumq2; best = scale*sumqx;
  9166. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9167. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9168. }
  9169. }
  9170. int n_not_ongrid = 0;
  9171. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9172. if (n_not_ongrid > 0 && scale > 0) {
  9173. float id = 1/scale;
  9174. for (int k = 0; k < bs4; ++k) {
  9175. //if (is_on_grid[k]) continue;
  9176. uint16_t u = 0;
  9177. for (int i = 0; i < 4; ++i) {
  9178. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9179. l = MAX(0, MIN(kMaxQ-1, l));
  9180. u |= (l << 3*i);
  9181. }
  9182. int grid_index = kmap_q3xs[u];
  9183. if (grid_index < 0) {
  9184. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9185. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9186. }
  9187. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9188. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9189. }
  9190. float sumqx = 0, sumq2 = 0;
  9191. for (int i = 0; i < block_size; ++i) {
  9192. float w = weight[i];
  9193. float q = 2*L[i] + 1;
  9194. sumqx += w*xval[i]*q;
  9195. sumq2 += w*q*q;
  9196. }
  9197. if (sumq2 > 0) scale = sumqx/sumq2;
  9198. }
  9199. if (scale < 0) {
  9200. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9201. // and correspondingly flip quant signs.
  9202. scale = -scale;
  9203. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9204. }
  9205. for (int k = 0; k < bs4; ++k) {
  9206. uint16_t u = 0;
  9207. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9208. int grid_index = kmap_q3xs[u];
  9209. if (grid_index < 0) {
  9210. printf("Oops: found point %u not on grid:", u);
  9211. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9212. printf("\n");
  9213. GGML_ASSERT(false);
  9214. }
  9215. qs[k] = grid_index & 255;
  9216. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9217. }
  9218. qs += bs4;
  9219. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9220. signs += bs8;
  9221. GGML_ASSERT(scale >= 0);
  9222. scales[ib] = scale;
  9223. max_scale = MAX(max_scale, scale);
  9224. }
  9225. if (!max_scale) {
  9226. continue;
  9227. }
  9228. float d = max_scale/31;
  9229. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9230. float id = 1/d;
  9231. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9232. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9233. l1 = MAX(0, MIN(15, l1));
  9234. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9235. l2 = MAX(0, MIN(15, l2));
  9236. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9237. }
  9238. }
  9239. }
  9240. #define IQ3S_BLOCK_SIZE 32
  9241. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9242. GGML_ASSERT(n_per_row%QK_K == 0);
  9243. int64_t nblock = n_per_row/QK_K;
  9244. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9245. float weight[IQ3S_BLOCK_SIZE];
  9246. float xval[IQ3S_BLOCK_SIZE];
  9247. int8_t L[IQ3S_BLOCK_SIZE];
  9248. int8_t Laux[IQ3S_BLOCK_SIZE];
  9249. float waux[IQ3S_BLOCK_SIZE];
  9250. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9251. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9252. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9253. char * qrow = (char *)dst;
  9254. for (int64_t row = 0; row < nrow; ++row) {
  9255. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9256. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9257. src += n_per_row;
  9258. qrow += nblock*sizeof(block_iq3_s);
  9259. }
  9260. return nrow * nblock * sizeof(block_iq3_s);
  9261. }
  9262. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int64_t k) {
  9263. assert(k % QK_K == 0);
  9264. block_iq3_s * restrict y = vy;
  9265. quantize_row_iq3_s_reference(x, y, k);
  9266. }
  9267. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int64_t k) {
  9268. assert(k % QK_K == 0);
  9269. quantize_iq3_s(x, y, 1, k, NULL);
  9270. }
  9271. // =================================== 1.5 bpw ===================================================
  9272. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9273. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9274. int num_neighbors = neighbours[0];
  9275. GGML_ASSERT(num_neighbors > 0);
  9276. float best_score = 0;
  9277. int grid_index = -1;
  9278. for (int j = 1; j <= num_neighbors; ++j) {
  9279. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9280. float sumqx = 0, sumq2 = 0;
  9281. for (int i = 0; i < 8; ++i) {
  9282. float q = (pg[i] - 3)/2;
  9283. float w = weight[i];
  9284. sumqx += w*q*xval[i];
  9285. sumq2 += w*q*q;
  9286. }
  9287. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9288. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9289. grid_index = neighbours[j];
  9290. }
  9291. }
  9292. if (grid_index < 0) {
  9293. for (int i = 0; i < ngrid; ++i) {
  9294. const int8_t * grid_i = (const int8_t *)(grid + i);
  9295. float sumqx = 0, sumq2 = 0;
  9296. for (int j = 0; j < 8; ++j) {
  9297. float w = weight[j];
  9298. float q = (grid_i[j] - 3)/2;
  9299. sumqx += w*q*xval[j];
  9300. sumq2 += w*q*q;
  9301. }
  9302. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9303. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9304. grid_index = i;
  9305. }
  9306. }
  9307. }
  9308. if (grid_index < 0) {
  9309. printf("Oops, did not find grid point\n");
  9310. printf("Have %d neighbours\n", num_neighbors);
  9311. for (int j = 1; j <= num_neighbors; ++j) {
  9312. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9313. float sumqx = 0, sumq2 = 0;
  9314. for (int i = 0; i < 8; ++i) {
  9315. float q = (pg[i] - 3)/2;
  9316. float w = weight[i];
  9317. sumqx += w*q*xval[i];
  9318. sumq2 += w*q*q;
  9319. }
  9320. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9321. }
  9322. }
  9323. GGML_ASSERT(grid_index >= 0);
  9324. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9325. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9326. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9327. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9328. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9329. return grid_index;
  9330. }
  9331. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9332. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9333. int num_neighbors = neighbours[0];
  9334. GGML_ASSERT(num_neighbors > 0);
  9335. float best_score = FLT_MAX;
  9336. int grid_index = -1;
  9337. for (int j = 1; j <= num_neighbors; ++j) {
  9338. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9339. float d2 = 0;
  9340. for (int i = 0; i < 8; ++i) {
  9341. float q = xg[(pg[i] - 1)/2];
  9342. float w = weight[i];
  9343. float diff = scale*q - xval[i];
  9344. d2 += w*diff*diff;
  9345. }
  9346. if (d2 < best_score) {
  9347. best_score = d2;
  9348. grid_index = neighbours[j];
  9349. }
  9350. }
  9351. if (grid_index < 0) {
  9352. for (int i = 0; i < ngrid; ++i) {
  9353. const int8_t * grid_i = (const int8_t *)(grid + i);
  9354. float d2 = 0;
  9355. for (int j = 0; j < 8; ++j) {
  9356. float w = weight[j];
  9357. float q = xg[(grid_i[j] - 1)/2];
  9358. float diff = scale*q - xval[i];
  9359. d2 += w*diff*diff;
  9360. }
  9361. if (d2 < best_score) {
  9362. best_score = d2;
  9363. grid_index = i;
  9364. }
  9365. }
  9366. }
  9367. if (grid_index < 0) {
  9368. printf("Oops, did not find grid point\n");
  9369. printf("Have %d neighbours\n", num_neighbors);
  9370. for (int j = 1; j <= num_neighbors; ++j) {
  9371. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9372. float sumqx = 0, sumq2 = 0;
  9373. for (int i = 0; i < 8; ++i) {
  9374. float q = xg[(pg[i] - 1)/2];
  9375. float w = weight[i];
  9376. sumqx += w*q*xval[i];
  9377. sumq2 += w*q*q;
  9378. }
  9379. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9380. }
  9381. }
  9382. GGML_ASSERT(grid_index >= 0);
  9383. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9384. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9385. return grid_index;
  9386. }
  9387. static int iq1_sort_helper(const void * left, const void * right) {
  9388. const float * l = left;
  9389. const float * r = right;
  9390. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9391. }
  9392. #define IQ1S_BLOCK_SIZE 32
  9393. #define IQ1M_BLOCK_SIZE 16
  9394. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9395. float * scales,
  9396. float * weight,
  9397. float * sumx,
  9398. float * sumw,
  9399. float * pairs,
  9400. int8_t * L,
  9401. uint16_t * index,
  9402. int8_t * shifts) {
  9403. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9404. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9405. const int * kmap_q2xs = iq2_data[gindex].map;
  9406. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9407. GGML_ASSERT(quant_weights && "missing quantization weights");
  9408. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9409. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9410. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9411. GGML_ASSERT(n%QK_K == 0);
  9412. block_iq1_s * y = vy;
  9413. const int64_t nbl = n/QK_K;
  9414. const int block_size = IQ1S_BLOCK_SIZE;
  9415. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9416. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9417. int * idx = (int *)(pairs + 1);
  9418. for (int ibl = 0; ibl < nbl; ++ibl) {
  9419. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9420. memset(y[ibl].qs, 0, QK_K/8);
  9421. memset(y[ibl].qh, 0, QK_K/16);
  9422. float max_scale = 0;
  9423. const float * xbl = x + QK_K*ibl;
  9424. float sumx2 = 0;
  9425. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9426. float sigma2 = 2*sumx2/QK_K;
  9427. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9428. const float * xb = xbl + block_size*ib;
  9429. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9430. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9431. float max = fabsf(xb[0]);
  9432. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9433. if (!max) {
  9434. scales[ib] = 0;
  9435. memset(L, 1, block_size);
  9436. continue;
  9437. }
  9438. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9439. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9440. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9441. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9442. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9443. // for each possible and score for each split.
  9444. for (int j = 0; j < block_size; ++j) {
  9445. pairs[2*j] = xb[j];
  9446. idx[2*j] = j;
  9447. }
  9448. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9449. {
  9450. sumx[0] = sumw[0] = 0;
  9451. for (int j = 0; j < block_size; ++j) {
  9452. int i = idx[2*j];
  9453. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9454. sumw[j+1] = sumw[j] + weight[i];
  9455. }
  9456. }
  9457. float best_score = 0, scale = max;
  9458. int besti1 = -1, besti2 = -1, best_shift = 0;
  9459. for (int i1 = 0; i1 <= block_size; ++i1) {
  9460. for (int i2 = i1; i2 <= block_size; ++i2) {
  9461. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9462. 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];
  9463. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9464. scale = sumqx/sumq2; best_score = scale*sumqx;
  9465. besti1 = i1; besti2 = i2; best_shift = 1;
  9466. }
  9467. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9468. 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];
  9469. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9470. scale = sumqx/sumq2; best_score = scale*sumqx;
  9471. besti1 = i1; besti2 = i2; best_shift = -1;
  9472. }
  9473. }
  9474. }
  9475. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9476. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9477. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9478. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9479. if (scale < 0) {
  9480. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9481. scale = -scale; best_shift = -best_shift;
  9482. }
  9483. bool all_on_grid = true;
  9484. const float * xx = best_shift == 1 ? x_p : x_m;
  9485. for (int k = 0; k < block_size/8; ++k) {
  9486. uint16_t u = 0;
  9487. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9488. int grid_index = kmap_q2xs[u];
  9489. if (grid_index < 0) {
  9490. all_on_grid = false;
  9491. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9492. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9493. GGML_ASSERT(grid_index >= 0);
  9494. }
  9495. index[k] = grid_index;
  9496. }
  9497. if (!all_on_grid) {
  9498. float sumqx = 0, sumq2 = 0;
  9499. for (int k = 0; k < block_size/8; ++k) {
  9500. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9501. for (int j = 0; j < 8; ++j) {
  9502. float w = weight[8*k + j];
  9503. float q = xx[(pg[j] - 1)/2];
  9504. sumqx += w*q*xb[8*k+j];
  9505. sumq2 += w*q*q;
  9506. }
  9507. }
  9508. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9509. }
  9510. uint16_t h = 0;
  9511. for (int k = 0; k < block_size/8; ++k) {
  9512. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9513. h |= (index[k] >> 8) << 3*k;
  9514. }
  9515. y[ibl].qh[ib] = h;
  9516. GGML_ASSERT(scale >= 0);
  9517. scales[ib] = scale;
  9518. shifts[ib] = best_shift;
  9519. max_scale = MAX(max_scale, scale);
  9520. }
  9521. if (!max_scale) {
  9522. continue;
  9523. }
  9524. float d = max_scale/15;
  9525. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9526. float id = 1/d;
  9527. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9528. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9529. l = MAX(0, MIN(7, l));
  9530. if (shifts[ib] == -1) l |= 8;
  9531. y[ibl].qh[ib] |= (l << 12);
  9532. }
  9533. }
  9534. }
  9535. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9536. GGML_ASSERT(n_per_row%QK_K == 0);
  9537. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9538. float weight[IQ1S_BLOCK_SIZE];
  9539. int8_t L[IQ1S_BLOCK_SIZE];
  9540. float sumx[IQ1S_BLOCK_SIZE+1];
  9541. float sumw[IQ1S_BLOCK_SIZE+1];
  9542. float pairs[2*IQ1S_BLOCK_SIZE];
  9543. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9544. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9545. int64_t nblock = n_per_row/QK_K;
  9546. char * qrow = (char *)dst;
  9547. for (int64_t row = 0; row < nrow; ++row) {
  9548. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9549. src += n_per_row;
  9550. qrow += nblock*sizeof(block_iq1_s);
  9551. }
  9552. return nrow * nblock * sizeof(block_iq1_s);
  9553. }
  9554. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9555. float * scales,
  9556. float * weight,
  9557. float * pairs,
  9558. int8_t * L,
  9559. uint16_t * index,
  9560. int8_t * shifts) {
  9561. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9562. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9563. const int * kmap_q2xs = iq2_data[gindex].map;
  9564. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9565. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9566. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9567. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9568. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9569. GGML_ASSERT(n%QK_K == 0);
  9570. block_iq1_m * y = vy;
  9571. const int64_t nbl = n/QK_K;
  9572. const int block_size = IQ1M_BLOCK_SIZE;
  9573. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9574. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9575. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9576. int * idx = (int *)(pairs + 1);
  9577. float sumqx[4], sumq2[4];
  9578. iq1m_scale_t s;
  9579. const float * xx;
  9580. for (int ibl = 0; ibl < nbl; ++ibl) {
  9581. #if QK_K == 64
  9582. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9583. #endif
  9584. memset(y[ibl].qs, 0, QK_K/8);
  9585. memset(y[ibl].qh, 0, QK_K/16);
  9586. memset(y[ibl].scales, 0, QK_K/32);
  9587. float max_scale = 0;
  9588. const float * xbl = x + QK_K*ibl;
  9589. float sumx2 = 0;
  9590. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9591. float sigma2 = 2*sumx2/QK_K;
  9592. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9593. const float * xb = xbl + block_size*ib;
  9594. if (quant_weights) {
  9595. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9596. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9597. } else {
  9598. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9599. }
  9600. float max = fabsf(xb[0]);
  9601. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9602. if (!max) {
  9603. scales[ib] = 0;
  9604. memset(L, 1, block_size);
  9605. continue;
  9606. }
  9607. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9608. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9609. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9610. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9611. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9612. // for each possible and score for each split.
  9613. for (int j = 0; j < block_size; ++j) {
  9614. pairs[2*j] = xb[j];
  9615. idx[2*j] = j;
  9616. }
  9617. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9618. float best_score = 0, scale = max;
  9619. int besti1 = -1, besti2 = -1, best_k = -1;
  9620. // 0: +, +
  9621. // 1: +, -
  9622. // 2: -, +
  9623. // 3: -, -
  9624. for (int i1 = 0; i1 <= block_size; ++i1) {
  9625. for (int i2 = i1; i2 <= block_size; ++i2) {
  9626. memset(sumqx, 0, 4*sizeof(float));
  9627. memset(sumq2, 0, 4*sizeof(float));
  9628. for (int j = 0; j < i1; ++j) {
  9629. int i = idx[2*j];
  9630. if (i < block_size/2) {
  9631. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9632. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9633. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9634. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9635. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9636. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9637. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9638. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9639. } else {
  9640. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9641. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9642. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9643. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9644. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9645. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9646. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9647. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9648. }
  9649. }
  9650. for (int j = i1; j < i2; ++j) {
  9651. int i = idx[2*j];
  9652. if (i < block_size/2) {
  9653. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9654. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9655. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9656. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9657. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9658. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9659. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9660. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9661. } else {
  9662. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9663. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9664. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9665. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9666. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9667. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9668. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9669. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9670. }
  9671. }
  9672. for (int j = i2; j < block_size; ++j) {
  9673. int i = idx[2*j];
  9674. if (i < block_size/2) {
  9675. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9676. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9677. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9678. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9679. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9680. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9681. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9682. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9683. } else {
  9684. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9685. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9686. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9687. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9688. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9689. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9690. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9691. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9692. }
  9693. }
  9694. for (int k = 0; k < 4; ++k) {
  9695. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9696. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9697. besti1 = i1; besti2 = i2; best_k = k;
  9698. }
  9699. }
  9700. }
  9701. }
  9702. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9703. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9704. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9705. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9706. if (scale < 0) {
  9707. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9708. scale = -scale;
  9709. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9710. }
  9711. bool all_on_grid = true;
  9712. for (int k = 0; k < block_size/8; ++k) {
  9713. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9714. else xx = best_k%2 == 0 ? x_p : x_m;
  9715. uint16_t u = 0;
  9716. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9717. int grid_index = kmap_q2xs[u];
  9718. if (grid_index < 0) {
  9719. all_on_grid = false;
  9720. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9721. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9722. GGML_ASSERT(grid_index >= 0);
  9723. }
  9724. index[k] = grid_index;
  9725. }
  9726. if (!all_on_grid) {
  9727. float sumqx_f = 0, sumq2_f = 0;
  9728. for (int k = 0; k < block_size/8; ++k) {
  9729. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9730. else xx = best_k%2 == 0 ? x_p : x_m;
  9731. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9732. for (int j = 0; j < 8; ++j) {
  9733. float w = weight[8*k + j];
  9734. float q = xx[(pg[j] - 1)/2];
  9735. sumqx_f += w*q*xb[8*k+j];
  9736. sumq2_f += w*q*q;
  9737. }
  9738. }
  9739. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9740. }
  9741. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9742. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9743. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9744. GGML_ASSERT(scale >= 0);
  9745. scales[ib] = scale;
  9746. shifts[ib] = best_k;
  9747. max_scale = MAX(max_scale, scale);
  9748. }
  9749. if (!max_scale) {
  9750. continue;
  9751. }
  9752. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9753. #if QK_K == 64
  9754. float d = max_scale/31;
  9755. #else
  9756. float d = max_scale/15;
  9757. #endif
  9758. float id = 1/d;
  9759. float sumqx_f = 0, sumq2_f = 0;
  9760. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9761. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9762. #if QK_K == 64
  9763. l = MAX(0, MIN(15, l));
  9764. sc[ib/4] |= (l << 4*(ib%4));
  9765. #else
  9766. l = MAX(0, MIN(7, l));
  9767. sc[ib/4] |= (l << 3*(ib%4));
  9768. #endif
  9769. y[ibl].qh[ib] |= masks[shifts[ib]];
  9770. const float * xb = xbl + block_size*ib;
  9771. if (quant_weights) {
  9772. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9773. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9774. } else {
  9775. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9776. }
  9777. for (int k = 0; k < block_size/8; ++k) {
  9778. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9779. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9780. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  9781. for (int j = 0; j < 8; ++j) {
  9782. float w = weight[8*k + j];
  9783. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  9784. sumqx_f += w*q*xb[8*k+j];
  9785. sumq2_f += w*q*q;
  9786. }
  9787. }
  9788. }
  9789. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  9790. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  9791. #if QK_K == 64
  9792. y[ibl].d = s.f16;
  9793. #else
  9794. sc[0] |= ((s.u16 & 0x000f) << 12);
  9795. sc[1] |= ((s.u16 & 0x00f0) << 8);
  9796. sc[2] |= ((s.u16 & 0x0f00) << 4);
  9797. sc[3] |= ((s.u16 & 0xf000) << 0);
  9798. #endif
  9799. }
  9800. }
  9801. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9802. GGML_ASSERT(n_per_row%QK_K == 0);
  9803. float scales[QK_K/IQ1M_BLOCK_SIZE];
  9804. float weight[IQ1M_BLOCK_SIZE];
  9805. int8_t L[IQ1M_BLOCK_SIZE];
  9806. float pairs[2*IQ1M_BLOCK_SIZE];
  9807. uint16_t index[IQ1M_BLOCK_SIZE/8];
  9808. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  9809. int64_t nblock = n_per_row/QK_K;
  9810. char * qrow = (char *)dst;
  9811. for (int64_t row = 0; row < nrow; ++row) {
  9812. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  9813. src += n_per_row;
  9814. qrow += nblock*sizeof(block_iq1_m);
  9815. }
  9816. return nrow * nblock * sizeof(block_iq1_m);
  9817. }
  9818. // ============================ 4-bit non-linear quants
  9819. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9820. if (x <= val[0]) return 0;
  9821. if (x >= val[n-1]) return n-1;
  9822. int ml = 0, mu = n-1;
  9823. while (mu-ml > 1) {
  9824. int mav = (ml+mu)/2;
  9825. if (x < val[mav]) mu = mav; else ml = mav;
  9826. }
  9827. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9828. }
  9829. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9830. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9831. float * scales, float * weight, uint8_t * L,
  9832. const int8_t * values,
  9833. const float * quant_weights,
  9834. const int ntry) {
  9835. float sigma2 = 0;
  9836. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9837. sigma2 *= 2.f/super_block_size;
  9838. memset(q4, 0, super_block_size/2);
  9839. dh[0] = GGML_FP32_TO_FP16(0.f);
  9840. float max_scale = 0, amax_scale = 0;
  9841. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9842. const float * xb = x + ib*block_size;
  9843. uint8_t * Lb = L + ib*block_size;
  9844. if (quant_weights) {
  9845. const float * qw = quant_weights + ib*block_size;
  9846. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9847. } else {
  9848. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9849. }
  9850. float amax = 0, max = 0;
  9851. for (int j = 0; j < block_size; ++j) {
  9852. float ax = fabsf(xb[j]);
  9853. if (ax > amax) {
  9854. amax = ax; max = xb[j];
  9855. }
  9856. }
  9857. if (!amax) {
  9858. scales[ib] = 0;
  9859. continue;
  9860. }
  9861. float d = ntry > 0 ? -max/values[0] : max/values[0];
  9862. float id = 1/d;
  9863. float sumqx = 0, sumq2 = 0;
  9864. for (int j = 0; j < block_size; ++j) {
  9865. float al = id*xb[j];
  9866. int l = best_index_int8(16, values, al);
  9867. Lb[j] = l;
  9868. float q = values[l];
  9869. float w = weight[j];
  9870. sumqx += w*q*xb[j];
  9871. sumq2 += w*q*q;
  9872. }
  9873. d = sumqx/sumq2;
  9874. float best = d*sumqx;
  9875. for (int itry = -ntry; itry <= ntry; ++itry) {
  9876. id = (itry + values[0])/max;
  9877. sumqx = sumq2 = 0;
  9878. for (int j = 0; j < block_size; ++j) {
  9879. float al = id*xb[j];
  9880. int l = best_index_int8(16, values, al);
  9881. float q = values[l];
  9882. float w = weight[j];
  9883. sumqx += w*q*xb[j];
  9884. sumq2 += w*q*q;
  9885. }
  9886. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9887. d = sumqx/sumq2; best = d * sumqx;
  9888. }
  9889. }
  9890. scales[ib] = d;
  9891. float abs_d = fabsf(d);
  9892. if (abs_d > amax_scale) {
  9893. amax_scale = abs_d; max_scale = d;
  9894. }
  9895. }
  9896. if (super_block_size/block_size > 1) {
  9897. int nb = super_block_size/block_size;
  9898. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9899. float d = -max_scale/32;
  9900. dh[0] = GGML_FP32_TO_FP16(d);
  9901. float id = d ? 1/d : 0.f;
  9902. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9903. int l = nearest_int(id*scales[ib]);
  9904. l = MAX(-32, MIN(31, l));
  9905. float dl = d * l;
  9906. float idl = dl ? 1/dl : 0.f;
  9907. uint8_t * Lb = L + ib*block_size;
  9908. const float * xb = x + ib*block_size;
  9909. for (int j = 0; j < block_size; ++j) {
  9910. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9911. }
  9912. l += 32;
  9913. uint8_t l_l = l & 0xf;
  9914. uint8_t l_h = l >> 4;
  9915. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9916. else scales_l[ib/2] |= (l_l << 4);
  9917. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9918. }
  9919. } else {
  9920. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9921. if (ntry > 0) {
  9922. float id = scales[0] ? 1/scales[0] : 0;
  9923. for (int j = 0; j < super_block_size; ++j) {
  9924. L[j] = best_index_int8(16, values, id*x[j]);
  9925. }
  9926. }
  9927. }
  9928. for (int i = 0; i < super_block_size/32; ++i) {
  9929. for (int j = 0; j < 16; ++j) {
  9930. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9931. }
  9932. }
  9933. }
  9934. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9935. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9936. int64_t nblock = n_per_row/QK4_NL;
  9937. char * qrow = (char *)dst;
  9938. uint8_t L[QK4_NL];
  9939. float weight[QK4_NL];
  9940. uint16_t unused_h;
  9941. uint8_t * unused_l = NULL;
  9942. float scale;
  9943. for (int64_t row = 0; row < nrow; ++row) {
  9944. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9945. for (int ibl = 0; ibl < nblock; ++ibl) {
  9946. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9947. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9948. &scale, weight, L, kvalues_iq4nl, qw, 7);
  9949. }
  9950. src += n_per_row;
  9951. qrow += nblock*sizeof(block_iq4_nl);
  9952. }
  9953. return nrow * nblock * sizeof(block_iq4_nl);
  9954. }
  9955. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int64_t k) {
  9956. GGML_ASSERT(k%QK4_NL == 0);
  9957. int64_t nblock = k/QK4_NL;
  9958. uint8_t L[QK4_NL];
  9959. float weight[QK4_NL];
  9960. uint16_t unused_h;
  9961. uint8_t * unused_l = NULL;
  9962. float scale;
  9963. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  9964. for (int ibl = 0; ibl < nblock; ++ibl) {
  9965. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9966. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  9967. }
  9968. }
  9969. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int64_t k) {
  9970. assert(k % QK4_NL == 0);
  9971. quantize_row_iq4_nl(x, y, k);
  9972. }
  9973. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9974. #if QK_K == 64
  9975. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9976. #else
  9977. GGML_ASSERT(n_per_row%QK_K == 0);
  9978. int64_t nblock = n_per_row/QK_K;
  9979. char * qrow = (char *)dst;
  9980. uint8_t L[QK_K];
  9981. float weight[32];
  9982. float scales[QK_K/32];
  9983. for (int64_t row = 0; row < nrow; ++row) {
  9984. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9985. for (int ibl = 0; ibl < nblock; ++ibl) {
  9986. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9987. 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,
  9988. scales, weight, L, kvalues_iq4nl, qw, 7);
  9989. }
  9990. src += n_per_row;
  9991. qrow += nblock*sizeof(block_iq4_xs);
  9992. }
  9993. return nrow * nblock * sizeof(block_iq4_xs);
  9994. #endif
  9995. }
  9996. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int64_t k) {
  9997. assert(k % QK_K == 0);
  9998. block_iq4_xs * restrict y = vy;
  9999. quantize_row_iq4_xs_reference(x, y, k);
  10000. }
  10001. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int64_t k) {
  10002. assert(k % QK_K == 0);
  10003. quantize_iq4_xs(x, y, 1, k, NULL);
  10004. }
  10005. // =============================== 2.5625 bpw
  10006. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  10007. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10008. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10009. const int * kmap_q2xs = iq2_data[gindex].map;
  10010. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10011. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10012. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10013. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10014. GGML_ASSERT(n%QK_K == 0);
  10015. const int kMaxQ = 3;
  10016. const int64_t nbl = n/QK_K;
  10017. block_iq2_s * y = vy;
  10018. float scales[QK_K/16];
  10019. float weight[16];
  10020. float xval[16];
  10021. int8_t L[16];
  10022. int8_t Laux[16];
  10023. float waux[16];
  10024. bool is_on_grid[2];
  10025. bool is_on_grid_aux[2];
  10026. uint8_t block_signs[2];
  10027. for (int ibl = 0; ibl < nbl; ++ibl) {
  10028. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10029. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10030. float max_scale = 0;
  10031. const float * xbl = x + QK_K*ibl;
  10032. float sumx2 = 0;
  10033. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10034. float sigma2 = 2*sumx2/QK_K;
  10035. for (int ib = 0; ib < QK_K/16; ++ib) {
  10036. const float * xb = xbl + 16*ib;
  10037. if (quant_weights) {
  10038. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10039. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10040. } else {
  10041. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10042. }
  10043. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10044. for (int k = 0; k < 2; ++k) {
  10045. uint8_t s = 0;
  10046. for (int i = 0; i < 8; ++i) {
  10047. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10048. else {
  10049. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10050. }
  10051. }
  10052. block_signs[k] = s;
  10053. }
  10054. float max = xval[0];
  10055. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10056. if (!max) {
  10057. scales[ib] = 0;
  10058. continue;
  10059. }
  10060. float best = 0;
  10061. float scale = max/(2*kMaxQ-1);
  10062. is_on_grid[0] = is_on_grid[1] = true;
  10063. for (int is = -9; is <= 9; ++is) {
  10064. float id = (2*kMaxQ-1+is*0.1f)/max;
  10065. float this_scale = 1/id;
  10066. for (int k = 0; k < 2; ++k) {
  10067. for (int i = 0; i < 8; ++i) {
  10068. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10069. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10070. }
  10071. uint16_t u = 0;
  10072. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10073. int grid_index = kmap_q2xs[u];
  10074. is_on_grid_aux[k] = true;
  10075. if (grid_index < 0) {
  10076. is_on_grid_aux[k] = false;
  10077. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10078. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10079. }
  10080. }
  10081. float sumqx = 0, sumq2 = 0;
  10082. for (int i = 0; i < 16; ++i) {
  10083. float w = weight[i];
  10084. float q = 2*Laux[i] + 1;
  10085. sumqx += w*xval[i]*q;
  10086. sumq2 += w*q*q;
  10087. }
  10088. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10089. scale = sumqx/sumq2; best = scale*sumqx;
  10090. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10091. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10092. }
  10093. }
  10094. int n_not_ongrid = 0;
  10095. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10096. if (n_not_ongrid > 0 && scale > 0) {
  10097. float id = 1/scale;
  10098. for (int k = 0; k < 2; ++k) {
  10099. if (is_on_grid[k]) continue;
  10100. uint16_t u = 0;
  10101. for (int i = 0; i < 8; ++i) {
  10102. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10103. l = MAX(0, MIN(kMaxQ-1, l));
  10104. u |= (l << 2*i);
  10105. L[8*k + i] = l;
  10106. }
  10107. int grid_index = kmap_q2xs[u];
  10108. if (grid_index < 0) {
  10109. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10110. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10111. }
  10112. }
  10113. float sumqx = 0, sumq2 = 0;
  10114. for (int i = 0; i < 16; ++i) {
  10115. float w = weight[i];
  10116. float q = 2*L[i] + 1;
  10117. sumqx += w*xval[i]*q;
  10118. sumq2 += w*q*q;
  10119. }
  10120. if (sumq2 > 0) scale = sumqx/sumq2;
  10121. }
  10122. if (scale < 0) {
  10123. scale = -scale;
  10124. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10125. }
  10126. for (int k = 0; k < 2; ++k) {
  10127. uint16_t u = 0;
  10128. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10129. int grid_index = kmap_q2xs[u];
  10130. if (grid_index < 0) {
  10131. printf("Oops: found point %u not on grid:", u);
  10132. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10133. printf("\n");
  10134. GGML_ASSERT(false);
  10135. }
  10136. const int i8 = 2*ib + k;
  10137. y[ibl].qs[i8] = grid_index & 255;
  10138. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10139. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10140. }
  10141. GGML_ASSERT(scale >= 0);
  10142. scales[ib] = scale;
  10143. max_scale = MAX(max_scale, scale);
  10144. }
  10145. if (!max_scale) {
  10146. continue;
  10147. }
  10148. float d = max_scale/31;
  10149. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10150. float id = 1/d;
  10151. for (int ib = 0; ib < QK_K/16; ++ib) {
  10152. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10153. l = MAX(0, MIN(15, l));
  10154. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10155. else y[ibl].scales[ib/2] |= (l << 4);
  10156. }
  10157. }
  10158. }
  10159. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10160. GGML_ASSERT(n_per_row%QK_K == 0);
  10161. int64_t nblock = n_per_row/QK_K;
  10162. char * qrow = (char *)dst;
  10163. for (int64_t row = 0; row < nrow; ++row) {
  10164. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10165. src += n_per_row;
  10166. qrow += nblock*sizeof(block_iq2_s);
  10167. }
  10168. return nrow * nblock * sizeof(block_iq2_s);
  10169. }
  10170. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int64_t k) {
  10171. assert(k % QK_K == 0);
  10172. quantize_iq2_s(x, y, 1, k, NULL);
  10173. }
  10174. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int64_t k) {
  10175. assert(k % QK_K == 0);
  10176. block_iq2_s * restrict y = vy;
  10177. quantize_row_iq2_s_reference(x, y, k);
  10178. }
  10179. static bool validate_float(float f, size_t i) {
  10180. if (isinf(f)) {
  10181. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  10182. return false;
  10183. }
  10184. if (isnan(f)) {
  10185. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  10186. return false;
  10187. }
  10188. return true;
  10189. }
  10190. static bool isinf_fp16(ggml_fp16_t f) {
  10191. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) == 0;
  10192. }
  10193. static bool isnan_fp16(ggml_fp16_t f) {
  10194. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) != 0;
  10195. }
  10196. static bool validate_fp16(ggml_fp16_t f, size_t i) {
  10197. if (isinf_fp16(f)) {
  10198. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  10199. return false;
  10200. }
  10201. if (isnan_fp16(f)) {
  10202. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  10203. return false;
  10204. }
  10205. return true;
  10206. }
  10207. #define VALIDATE_ROW_DATA_D_F16_IMPL(type, data, nb) \
  10208. const type * q = (const type *) (data); \
  10209. for (size_t i = 0; i < (nb); ++i) { \
  10210. if (!validate_fp16(q[i].d, i)) { \
  10211. return false; \
  10212. } \
  10213. }
  10214. #define VALIDATE_ROW_DATA_DM_F16_IMPL(type, data, nb, d, m) \
  10215. const type * q = (const type *) (data); \
  10216. for (size_t i = 0; i < (nb); ++i) { \
  10217. if (!validate_fp16(q[i].d, i) || !validate_fp16(q[i].m, i)) { \
  10218. return false; \
  10219. } \
  10220. }
  10221. bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes) {
  10222. if (type < 0 || type >= GGML_TYPE_COUNT) {
  10223. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  10224. return false;
  10225. }
  10226. if (nbytes % ggml_type_size(type) != 0) {
  10227. fprintf(stderr, "%s: invalid size %zu for type %d\n", __func__, nbytes, type);
  10228. return false;
  10229. }
  10230. const size_t nb = nbytes/ggml_type_size(type);
  10231. switch (type) {
  10232. case GGML_TYPE_BF16:
  10233. {
  10234. int nans = 0;
  10235. int infs = 0;
  10236. const unsigned short * f = (const unsigned short *) data;
  10237. for (size_t i = 0; i < nb; ++i) {
  10238. nans += (f[i] & 0x7fff) > 0x7f80;
  10239. infs += (f[i] & 0x7fff) == 0x7f80;
  10240. }
  10241. if (nans) {
  10242. fprintf(stderr, "%s: found %d NaNs in row of %zu BF16 values\n", __func__, nans, nb);
  10243. return false;
  10244. }
  10245. if (infs) {
  10246. fprintf(stderr, "%s: found %d infinities in row of %zu BF16 values\n", __func__, infs, nb);
  10247. return false;
  10248. }
  10249. } break;
  10250. case GGML_TYPE_F16:
  10251. {
  10252. const ggml_fp16_t * f = (const ggml_fp16_t *) data;
  10253. size_t i = 0;
  10254. #if defined(__AVX2__)
  10255. for (; i + 15 < nb; i += 16) {
  10256. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  10257. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi16(0x7c00));
  10258. __m256i cmp = _mm256_cmpeq_epi16(vexp, _mm256_set1_epi16(0x7c00));
  10259. int mask = _mm256_movemask_epi8(cmp);
  10260. if (mask) {
  10261. for (size_t j = 0; j < 16; ++j) {
  10262. if (!validate_fp16(f[i + j], i + j)) {
  10263. return false;
  10264. }
  10265. }
  10266. GGML_UNREACHABLE();
  10267. }
  10268. }
  10269. #elif defined(__ARM_NEON)
  10270. for (; i + 7 < nb; i += 8) {
  10271. uint16x8_t v = vld1q_u16(f + i);
  10272. uint16x8_t vexp = vandq_u16(v, vdupq_n_u16(0x7c00));
  10273. uint16x8_t cmp = vceqq_u16(vexp, vdupq_n_u16(0x7c00));
  10274. uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(cmp, 4)), 0);
  10275. if (mask) {
  10276. for (size_t j = 0; j < 8; ++j) {
  10277. if (!validate_fp16(f[i + j], i + j)) {
  10278. return false;
  10279. }
  10280. }
  10281. GGML_UNREACHABLE();
  10282. }
  10283. }
  10284. #endif
  10285. for (; i < nb; ++i) {
  10286. if (!validate_fp16(f[i], i)) {
  10287. return false;
  10288. }
  10289. }
  10290. } break;
  10291. case GGML_TYPE_F32:
  10292. {
  10293. const float * f = (const float *) data;
  10294. size_t i = 0;
  10295. #if defined(__AVX2__)
  10296. for (; i + 7 < nb; i += 8) {
  10297. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  10298. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi32(0x7f800000));
  10299. __m256i cmp = _mm256_cmpeq_epi32(vexp, _mm256_set1_epi32(0x7f800000));
  10300. int mask = _mm256_movemask_epi8(cmp);
  10301. if (mask) {
  10302. for (size_t j = 0; j < 8; ++j) {
  10303. if (!validate_float(f[i + j], i + j)) {
  10304. return false;
  10305. }
  10306. }
  10307. GGML_UNREACHABLE();
  10308. }
  10309. }
  10310. #elif defined(__ARM_NEON)
  10311. for (; i + 3 < nb; i += 4) {
  10312. uint32x4_t v = vld1q_u32((const uint32_t *)f + i);
  10313. uint32x4_t vexp = vandq_u32(v, vdupq_n_u32(0x7f800000));
  10314. uint32x4_t cmp = vceqq_u32(vexp, vdupq_n_u32(0x7f800000));
  10315. uint64_t mask = vget_lane_u64(vreinterpret_u64_u16(vshrn_n_u32(cmp, 8)), 0);
  10316. if (mask) {
  10317. for (size_t j = 0; j < 4; ++j) {
  10318. if (!validate_float(f[i + j], i + j)) {
  10319. return false;
  10320. }
  10321. }
  10322. GGML_UNREACHABLE();
  10323. }
  10324. }
  10325. #endif
  10326. for (; i < nb; ++i) {
  10327. if (!validate_float(f[i], i)) {
  10328. return false;
  10329. }
  10330. }
  10331. } break;
  10332. case GGML_TYPE_F64:
  10333. {
  10334. const double * f = (const double *) data;
  10335. for (size_t i = 0; i < nb; ++i) {
  10336. if (!validate_float(f[i], i)) {
  10337. return false;
  10338. }
  10339. }
  10340. } break;
  10341. case GGML_TYPE_Q4_0:
  10342. {
  10343. VALIDATE_ROW_DATA_D_F16_IMPL(block_q4_0, data, nb);
  10344. } break;
  10345. case GGML_TYPE_Q4_1:
  10346. {
  10347. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_1, data, nb, d, m);
  10348. } break;
  10349. case GGML_TYPE_Q5_0:
  10350. {
  10351. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_0, data, nb);
  10352. } break;
  10353. case GGML_TYPE_Q5_1:
  10354. {
  10355. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_1, data, nb, d, m);
  10356. } break;
  10357. case GGML_TYPE_Q8_0:
  10358. {
  10359. VALIDATE_ROW_DATA_D_F16_IMPL(block_q8_0, data, nb);
  10360. } break;
  10361. case GGML_TYPE_Q2_K:
  10362. {
  10363. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q2_K, data, nb, d, dmin);
  10364. } break;
  10365. case GGML_TYPE_Q3_K:
  10366. {
  10367. VALIDATE_ROW_DATA_D_F16_IMPL(block_q3_K, data, nb);
  10368. } break;
  10369. case GGML_TYPE_Q4_K:
  10370. {
  10371. #ifdef GGML_QKK_64
  10372. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d[0], d[1]);
  10373. #else
  10374. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d, dmin);
  10375. #endif
  10376. } break;
  10377. case GGML_TYPE_Q5_K:
  10378. {
  10379. #ifdef GGML_QKK_64
  10380. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_K, data, nb);
  10381. #else
  10382. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_K, data, nb, d, dmin);
  10383. #endif
  10384. } break;
  10385. case GGML_TYPE_Q6_K:
  10386. {
  10387. VALIDATE_ROW_DATA_D_F16_IMPL(block_q6_K, data, nb);
  10388. } break;
  10389. case GGML_TYPE_Q8_K:
  10390. {
  10391. const block_q8_K * q = (const block_q8_K *) data;
  10392. for (size_t i = 0; i < nb; ++i) {
  10393. if (!validate_float(q[i].d, i)) {
  10394. return false;
  10395. }
  10396. }
  10397. } break;
  10398. case GGML_TYPE_IQ1_S:
  10399. {
  10400. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq1_s, data, nb);
  10401. } break;
  10402. case GGML_TYPE_IQ1_M:
  10403. {
  10404. const block_iq1_m * q = (const block_iq1_m *) data;
  10405. for (size_t i = 0; i < nb; ++i) {
  10406. #if QK_K == 64
  10407. if (!validate_fp16(q[i].d, i)) {
  10408. return false;
  10409. }
  10410. #else
  10411. iq1m_scale_t scale;
  10412. const uint16_t * sc = (const uint16_t *)q[i].scales;
  10413. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  10414. if (!validate_fp16(scale.f16, i)) {
  10415. return false;
  10416. }
  10417. #endif
  10418. }
  10419. } break;
  10420. case GGML_TYPE_IQ2_XXS:
  10421. {
  10422. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xxs, data, nb);
  10423. } break;
  10424. case GGML_TYPE_IQ2_XS:
  10425. {
  10426. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xs, data, nb);
  10427. } break;
  10428. case GGML_TYPE_IQ2_S:
  10429. {
  10430. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_s, data, nb);
  10431. } break;
  10432. case GGML_TYPE_IQ3_XXS:
  10433. {
  10434. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_xxs, data, nb);
  10435. } break;
  10436. case GGML_TYPE_IQ3_S:
  10437. {
  10438. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_s, data, nb);
  10439. } break;
  10440. case GGML_TYPE_IQ4_XS:
  10441. #if QK_K != 64
  10442. {
  10443. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_xs, data, nb);
  10444. } break;
  10445. #endif
  10446. // with QK_K == 64, iq4_xs is iq4_nl
  10447. case GGML_TYPE_IQ4_NL:
  10448. {
  10449. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_nl, data, nb);
  10450. } break;
  10451. case GGML_TYPE_I8:
  10452. case GGML_TYPE_I16:
  10453. case GGML_TYPE_I32:
  10454. case GGML_TYPE_I64:
  10455. // nothing to validate
  10456. break;
  10457. default:
  10458. {
  10459. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  10460. return false;
  10461. }
  10462. }
  10463. return true;
  10464. }