ggml-quants.c 339 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <float.h>
  7. #include <stdlib.h> // for qsort
  8. #include <stdio.h> // for GGML_ASSERT
  9. #ifdef __ARM_NEON
  10. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  11. //
  12. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  13. //
  14. #include <arm_neon.h>
  15. #else
  16. #ifdef __wasm_simd128__
  17. #include <wasm_simd128.h>
  18. #else
  19. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  20. #include <altivec.h>
  21. #undef bool
  22. #define bool _Bool
  23. #else
  24. #if defined(_MSC_VER) || defined(__MINGW32__)
  25. #include <intrin.h>
  26. #else
  27. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  28. #if !defined(__riscv)
  29. #include <immintrin.h>
  30. #endif
  31. #endif
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #ifdef __riscv_v_intrinsic
  37. #include <riscv_vector.h>
  38. #endif
  39. #undef MIN
  40. #undef MAX
  41. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  42. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  43. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  44. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  45. // multiply int8_t, add results pairwise twice
  46. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  47. // Get absolute values of x vectors
  48. const __m128i ax = _mm_sign_epi8(x, x);
  49. // Sign the values of the y vectors
  50. const __m128i sy = _mm_sign_epi8(y, x);
  51. // Perform multiplication and create 16-bit values
  52. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  53. const __m128i ones = _mm_set1_epi16(1);
  54. return _mm_madd_epi16(ones, dot);
  55. }
  56. #if __AVX__ || __AVX2__ || __AVX512F__
  57. // horizontally add 8 floats
  58. static inline float hsum_float_8(const __m256 x) {
  59. __m128 res = _mm256_extractf128_ps(x, 1);
  60. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  61. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  62. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  63. return _mm_cvtss_f32(res);
  64. }
  65. // horizontally add 8 int32_t
  66. static inline int hsum_i32_8(const __m256i a) {
  67. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  68. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  69. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  70. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  71. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  72. }
  73. // horizontally add 4 int32_t
  74. static inline int hsum_i32_4(const __m128i a) {
  75. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  76. const __m128i sum64 = _mm_add_epi32(hi64, a);
  77. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  78. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  79. }
  80. #if defined(__AVX2__) || defined(__AVX512F__)
  81. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  82. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  83. uint32_t x32;
  84. memcpy(&x32, x, sizeof(uint32_t));
  85. const __m256i shuf_mask = _mm256_set_epi64x(
  86. 0x0303030303030303, 0x0202020202020202,
  87. 0x0101010101010101, 0x0000000000000000);
  88. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  89. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  90. bytes = _mm256_or_si256(bytes, bit_mask);
  91. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  92. }
  93. // Unpack 32 4-bit fields into 32 bytes
  94. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  95. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  96. {
  97. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  98. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  99. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  100. return _mm256_and_si256(lowMask, bytes);
  101. }
  102. // add int16_t pairwise and return as float vector
  103. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  104. const __m256i ones = _mm256_set1_epi16(1);
  105. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  106. return _mm256_cvtepi32_ps(summed_pairs);
  107. }
  108. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  109. #if __AVXVNNI__
  110. const __m256i zero = _mm256_setzero_si256();
  111. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. #else
  114. // Perform multiplication and create 16-bit values
  115. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  116. return sum_i16_pairs_float(dot);
  117. #endif
  118. }
  119. // multiply int8_t, add results pairwise twice and return as float vector
  120. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  121. #if __AVXVNNIINT8__
  122. const __m256i zero = _mm256_setzero_si256();
  123. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  124. return _mm256_cvtepi32_ps(summed_pairs);
  125. #else
  126. // Get absolute values of x vectors
  127. const __m256i ax = _mm256_sign_epi8(x, x);
  128. // Sign the values of the y vectors
  129. const __m256i sy = _mm256_sign_epi8(y, x);
  130. return mul_sum_us8_pairs_float(ax, sy);
  131. #endif
  132. }
  133. static inline __m128i packNibbles( __m256i bytes )
  134. {
  135. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  136. #if __AVX512F__
  137. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  138. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  139. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  140. #else
  141. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  142. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  143. __m256i low = _mm256_and_si256( lowByte, bytes );
  144. high = _mm256_srli_epi16( high, 4 );
  145. bytes = _mm256_or_si256( low, high );
  146. // Compress uint16_t lanes into bytes
  147. __m128i r0 = _mm256_castsi256_si128( bytes );
  148. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  149. return _mm_packus_epi16( r0, r1 );
  150. #endif
  151. }
  152. #elif defined(__AVX__)
  153. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  154. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  155. uint32_t x32;
  156. memcpy(&x32, x, sizeof(uint32_t));
  157. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  158. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  159. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  160. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  161. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  162. bytesl = _mm_or_si128(bytesl, bit_mask);
  163. bytesh = _mm_or_si128(bytesh, bit_mask);
  164. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  165. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  166. return MM256_SET_M128I(bytesh, bytesl);
  167. }
  168. // Unpack 32 4-bit fields into 32 bytes
  169. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  170. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  171. {
  172. // Load 16 bytes from memory
  173. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  174. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  175. const __m128i lowMask = _mm_set1_epi8(0xF);
  176. tmpl = _mm_and_si128(lowMask, tmpl);
  177. tmph = _mm_and_si128(lowMask, tmph);
  178. return MM256_SET_M128I(tmph, tmpl);
  179. }
  180. // add int16_t pairwise and return as float vector
  181. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  182. const __m128i ones = _mm_set1_epi16(1);
  183. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  184. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  185. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  186. return _mm256_cvtepi32_ps(summed_pairs);
  187. }
  188. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  189. const __m128i axl = _mm256_castsi256_si128(ax);
  190. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  191. const __m128i syl = _mm256_castsi256_si128(sy);
  192. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  193. // Perform multiplication and create 16-bit values
  194. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  195. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  196. return sum_i16_pairs_float(doth, dotl);
  197. }
  198. // multiply int8_t, add results pairwise twice and return as float vector
  199. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  200. const __m128i xl = _mm256_castsi256_si128(x);
  201. const __m128i xh = _mm256_extractf128_si256(x, 1);
  202. const __m128i yl = _mm256_castsi256_si128(y);
  203. const __m128i yh = _mm256_extractf128_si256(y, 1);
  204. // Get absolute values of x vectors
  205. const __m128i axl = _mm_sign_epi8(xl, xl);
  206. const __m128i axh = _mm_sign_epi8(xh, xh);
  207. // Sign the values of the y vectors
  208. const __m128i syl = _mm_sign_epi8(yl, xl);
  209. const __m128i syh = _mm_sign_epi8(yh, xh);
  210. // Perform multiplication and create 16-bit values
  211. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  212. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  213. return sum_i16_pairs_float(doth, dotl);
  214. }
  215. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  216. {
  217. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  218. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  219. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  220. __m128i low = _mm_and_si128( lowByte, bytes1 );
  221. high = _mm_srli_epi16( high, 4 );
  222. bytes1 = _mm_or_si128( low, high );
  223. high = _mm_andnot_si128( lowByte, bytes2 );
  224. low = _mm_and_si128( lowByte, bytes2 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes2 = _mm_or_si128( low, high );
  227. return _mm_packus_epi16( bytes1, bytes2);
  228. }
  229. #endif
  230. #elif defined(__SSSE3__)
  231. // horizontally add 4x4 floats
  232. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  233. __m128 res_0 =_mm_hadd_ps(a, b);
  234. __m128 res_1 =_mm_hadd_ps(c, d);
  235. __m128 res =_mm_hadd_ps(res_0, res_1);
  236. res =_mm_hadd_ps(res, res);
  237. res =_mm_hadd_ps(res, res);
  238. return _mm_cvtss_f32(res);
  239. }
  240. #endif // __AVX__ || __AVX2__ || __AVX512F__
  241. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  242. #if defined(__ARM_NEON)
  243. #if !defined(__aarch64__)
  244. // 64-bit compatibility
  245. // vaddvq_s16
  246. // vpaddq_s16
  247. // vpaddq_s32
  248. // vaddvq_s32
  249. // vaddvq_f32
  250. // vmaxvq_f32
  251. // vcvtnq_s32_f32
  252. // vzip1_u8
  253. // vzip2_u8
  254. inline static int32_t vaddvq_s16(int16x8_t v) {
  255. return
  256. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  257. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  258. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  259. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  260. }
  261. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  262. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  263. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  264. return vcombine_s16(a0, b0);
  265. }
  266. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  267. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  268. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  269. return vcombine_s32(a0, b0);
  270. }
  271. inline static int32_t vaddvq_s32(int32x4_t v) {
  272. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  273. }
  274. inline static float vaddvq_f32(float32x4_t v) {
  275. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  276. }
  277. inline static float vmaxvq_f32(float32x4_t v) {
  278. return
  279. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  280. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  281. }
  282. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  283. int32x4_t res;
  284. res[0] = roundf(vgetq_lane_f32(v, 0));
  285. res[1] = roundf(vgetq_lane_f32(v, 1));
  286. res[2] = roundf(vgetq_lane_f32(v, 2));
  287. res[3] = roundf(vgetq_lane_f32(v, 3));
  288. return res;
  289. }
  290. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  291. uint8x8_t res;
  292. res[0] = a[0]; res[1] = b[0];
  293. res[2] = a[1]; res[3] = b[1];
  294. res[4] = a[2]; res[5] = b[2];
  295. res[6] = a[3]; res[7] = b[3];
  296. return res;
  297. }
  298. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  299. uint8x8_t res;
  300. res[0] = a[4]; res[1] = b[4];
  301. res[2] = a[5]; res[3] = b[5];
  302. res[4] = a[6]; res[5] = b[6];
  303. res[6] = a[7]; res[7] = b[7];
  304. return res;
  305. }
  306. // vld1q_s16_x2
  307. // vld1q_u8_x2
  308. // vld1q_u8_x4
  309. // vld1q_s8_x2
  310. // vld1q_s8_x4
  311. // TODO: double-check these work correctly
  312. typedef struct ggml_int16x8x2_t {
  313. int16x8_t val[2];
  314. } ggml_int16x8x2_t;
  315. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  316. ggml_int16x8x2_t res;
  317. res.val[0] = vld1q_s16(ptr + 0);
  318. res.val[1] = vld1q_s16(ptr + 8);
  319. return res;
  320. }
  321. typedef struct ggml_uint8x16x2_t {
  322. uint8x16_t val[2];
  323. } ggml_uint8x16x2_t;
  324. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  325. ggml_uint8x16x2_t res;
  326. res.val[0] = vld1q_u8(ptr + 0);
  327. res.val[1] = vld1q_u8(ptr + 16);
  328. return res;
  329. }
  330. typedef struct ggml_uint8x16x4_t {
  331. uint8x16_t val[4];
  332. } ggml_uint8x16x4_t;
  333. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  334. ggml_uint8x16x4_t res;
  335. res.val[0] = vld1q_u8(ptr + 0);
  336. res.val[1] = vld1q_u8(ptr + 16);
  337. res.val[2] = vld1q_u8(ptr + 32);
  338. res.val[3] = vld1q_u8(ptr + 48);
  339. return res;
  340. }
  341. typedef struct ggml_int8x16x2_t {
  342. int8x16_t val[2];
  343. } ggml_int8x16x2_t;
  344. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  345. ggml_int8x16x2_t res;
  346. res.val[0] = vld1q_s8(ptr + 0);
  347. res.val[1] = vld1q_s8(ptr + 16);
  348. return res;
  349. }
  350. typedef struct ggml_int8x16x4_t {
  351. int8x16_t val[4];
  352. } ggml_int8x16x4_t;
  353. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  354. ggml_int8x16x4_t res;
  355. res.val[0] = vld1q_s8(ptr + 0);
  356. res.val[1] = vld1q_s8(ptr + 16);
  357. res.val[2] = vld1q_s8(ptr + 32);
  358. res.val[3] = vld1q_s8(ptr + 48);
  359. return res;
  360. }
  361. #else
  362. #define ggml_int16x8x2_t int16x8x2_t
  363. #define ggml_uint8x16x2_t uint8x16x2_t
  364. #define ggml_uint8x16x4_t uint8x16x4_t
  365. #define ggml_int8x16x2_t int8x16x2_t
  366. #define ggml_int8x16x4_t int8x16x4_t
  367. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  368. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  369. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  370. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  371. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  372. #endif
  373. #if !defined(__ARM_FEATURE_DOTPROD)
  374. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  375. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  376. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  377. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  378. }
  379. #else
  380. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  381. #endif
  382. #endif
  383. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  384. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  385. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  386. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  387. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  388. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  389. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  390. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  391. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  392. // precomputed tables for expanding 8bits to 8 bytes:
  393. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  394. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  395. #endif
  396. // reference implementation for deterministic creation of model files
  397. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  398. static const int qk = QK4_0;
  399. assert(k % qk == 0);
  400. const int nb = k / qk;
  401. for (int i = 0; i < nb; i++) {
  402. float amax = 0.0f; // absolute max
  403. float max = 0.0f;
  404. for (int j = 0; j < qk; j++) {
  405. const float v = x[i*qk + j];
  406. if (amax < fabsf(v)) {
  407. amax = fabsf(v);
  408. max = v;
  409. }
  410. }
  411. const float d = max / -8;
  412. const float id = d ? 1.0f/d : 0.0f;
  413. y[i].d = GGML_FP32_TO_FP16(d);
  414. for (int j = 0; j < qk/2; ++j) {
  415. const float x0 = x[i*qk + 0 + j]*id;
  416. const float x1 = x[i*qk + qk/2 + j]*id;
  417. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  418. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  419. y[i].qs[j] = xi0;
  420. y[i].qs[j] |= xi1 << 4;
  421. }
  422. }
  423. }
  424. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  425. quantize_row_q4_0_reference(x, y, k);
  426. }
  427. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  428. const int qk = QK4_1;
  429. assert(k % qk == 0);
  430. const int nb = k / qk;
  431. for (int i = 0; i < nb; i++) {
  432. float min = FLT_MAX;
  433. float max = -FLT_MAX;
  434. for (int j = 0; j < qk; j++) {
  435. const float v = x[i*qk + j];
  436. if (v < min) min = v;
  437. if (v > max) max = v;
  438. }
  439. const float d = (max - min) / ((1 << 4) - 1);
  440. const float id = d ? 1.0f/d : 0.0f;
  441. y[i].d = GGML_FP32_TO_FP16(d);
  442. y[i].m = GGML_FP32_TO_FP16(min);
  443. for (int j = 0; j < qk/2; ++j) {
  444. const float x0 = (x[i*qk + 0 + j] - min)*id;
  445. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  446. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  447. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  448. y[i].qs[j] = xi0;
  449. y[i].qs[j] |= xi1 << 4;
  450. }
  451. }
  452. }
  453. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  454. quantize_row_q4_1_reference(x, y, k);
  455. }
  456. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  457. static const int qk = QK5_0;
  458. assert(k % qk == 0);
  459. const int nb = k / qk;
  460. for (int i = 0; i < nb; i++) {
  461. float amax = 0.0f; // absolute max
  462. float max = 0.0f;
  463. for (int j = 0; j < qk; j++) {
  464. const float v = x[i*qk + j];
  465. if (amax < fabsf(v)) {
  466. amax = fabsf(v);
  467. max = v;
  468. }
  469. }
  470. const float d = max / -16;
  471. const float id = d ? 1.0f/d : 0.0f;
  472. y[i].d = GGML_FP32_TO_FP16(d);
  473. uint32_t qh = 0;
  474. for (int j = 0; j < qk/2; ++j) {
  475. const float x0 = x[i*qk + 0 + j]*id;
  476. const float x1 = x[i*qk + qk/2 + j]*id;
  477. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  478. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  479. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  480. // get the 5-th bit and store it in qh at the right position
  481. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  482. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  483. }
  484. memcpy(&y[i].qh, &qh, sizeof(qh));
  485. }
  486. }
  487. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  488. quantize_row_q5_0_reference(x, y, k);
  489. }
  490. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  491. const int qk = QK5_1;
  492. assert(k % qk == 0);
  493. const int nb = k / qk;
  494. for (int i = 0; i < nb; i++) {
  495. float min = FLT_MAX;
  496. float max = -FLT_MAX;
  497. for (int j = 0; j < qk; j++) {
  498. const float v = x[i*qk + j];
  499. if (v < min) min = v;
  500. if (v > max) max = v;
  501. }
  502. const float d = (max - min) / ((1 << 5) - 1);
  503. const float id = d ? 1.0f/d : 0.0f;
  504. y[i].d = GGML_FP32_TO_FP16(d);
  505. y[i].m = GGML_FP32_TO_FP16(min);
  506. uint32_t qh = 0;
  507. for (int j = 0; j < qk/2; ++j) {
  508. const float x0 = (x[i*qk + 0 + j] - min)*id;
  509. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  510. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  511. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  512. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  513. // get the 5-th bit and store it in qh at the right position
  514. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  515. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  516. }
  517. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  518. }
  519. }
  520. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  521. quantize_row_q5_1_reference(x, y, k);
  522. }
  523. // reference implementation for deterministic creation of model files
  524. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  525. assert(k % QK8_0 == 0);
  526. const int nb = k / QK8_0;
  527. for (int i = 0; i < nb; i++) {
  528. float amax = 0.0f; // absolute max
  529. for (int j = 0; j < QK8_0; j++) {
  530. const float v = x[i*QK8_0 + j];
  531. amax = MAX(amax, fabsf(v));
  532. }
  533. const float d = amax / ((1 << 7) - 1);
  534. const float id = d ? 1.0f/d : 0.0f;
  535. y[i].d = GGML_FP32_TO_FP16(d);
  536. for (int j = 0; j < QK8_0; ++j) {
  537. const float x0 = x[i*QK8_0 + j]*id;
  538. y[i].qs[j] = roundf(x0);
  539. }
  540. }
  541. }
  542. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  543. assert(QK8_0 == 32);
  544. assert(k % QK8_0 == 0);
  545. const int nb = k / QK8_0;
  546. block_q8_0 * restrict y = vy;
  547. #if defined(__ARM_NEON)
  548. for (int i = 0; i < nb; i++) {
  549. float32x4_t srcv [8];
  550. float32x4_t asrcv[8];
  551. float32x4_t amaxv[8];
  552. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  553. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  554. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  555. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  556. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  557. const float amax = vmaxvq_f32(amaxv[0]);
  558. const float d = amax / ((1 << 7) - 1);
  559. const float id = d ? 1.0f/d : 0.0f;
  560. y[i].d = GGML_FP32_TO_FP16(d);
  561. for (int j = 0; j < 8; j++) {
  562. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  563. const int32x4_t vi = vcvtnq_s32_f32(v);
  564. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  565. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  566. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  567. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  568. }
  569. }
  570. #elif defined(__wasm_simd128__)
  571. for (int i = 0; i < nb; i++) {
  572. v128_t srcv [8];
  573. v128_t asrcv[8];
  574. v128_t amaxv[8];
  575. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  576. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  577. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  578. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  579. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  580. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  581. wasm_f32x4_extract_lane(amaxv[0], 1)),
  582. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  583. wasm_f32x4_extract_lane(amaxv[0], 3)));
  584. const float d = amax / ((1 << 7) - 1);
  585. const float id = d ? 1.0f/d : 0.0f;
  586. y[i].d = GGML_FP32_TO_FP16(d);
  587. for (int j = 0; j < 8; j++) {
  588. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  589. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  590. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  591. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  592. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  593. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  594. }
  595. }
  596. #elif defined(__AVX2__) || defined(__AVX__)
  597. for (int i = 0; i < nb; i++) {
  598. // Load elements into 4 AVX vectors
  599. __m256 v0 = _mm256_loadu_ps( x );
  600. __m256 v1 = _mm256_loadu_ps( x + 8 );
  601. __m256 v2 = _mm256_loadu_ps( x + 16 );
  602. __m256 v3 = _mm256_loadu_ps( x + 24 );
  603. x += 32;
  604. // Compute max(abs(e)) for the block
  605. const __m256 signBit = _mm256_set1_ps( -0.0f );
  606. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  607. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  608. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  609. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  610. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  611. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  612. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  613. const float maxScalar = _mm_cvtss_f32( max4 );
  614. // Quantize these floats
  615. const float d = maxScalar / 127.f;
  616. y[i].d = GGML_FP32_TO_FP16(d);
  617. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  618. const __m256 mul = _mm256_set1_ps( id );
  619. // Apply the multiplier
  620. v0 = _mm256_mul_ps( v0, mul );
  621. v1 = _mm256_mul_ps( v1, mul );
  622. v2 = _mm256_mul_ps( v2, mul );
  623. v3 = _mm256_mul_ps( v3, mul );
  624. // Round to nearest integer
  625. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  626. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  627. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  628. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  629. // Convert floats to integers
  630. __m256i i0 = _mm256_cvtps_epi32( v0 );
  631. __m256i i1 = _mm256_cvtps_epi32( v1 );
  632. __m256i i2 = _mm256_cvtps_epi32( v2 );
  633. __m256i i3 = _mm256_cvtps_epi32( v3 );
  634. #if defined(__AVX2__)
  635. // Convert int32 to int16
  636. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  637. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  638. // Convert int16 to int8
  639. 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
  640. // We got our precious signed bytes, but the order is now wrong
  641. // These AVX2 pack instructions process 16-byte pieces independently
  642. // The following instruction is fixing the order
  643. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  644. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  645. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  646. #else
  647. // Since we don't have in AVX some necessary functions,
  648. // we split the registers in half and call AVX2 analogs from SSE
  649. __m128i ni0 = _mm256_castsi256_si128( i0 );
  650. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  651. __m128i ni2 = _mm256_castsi256_si128( i1 );
  652. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  653. __m128i ni4 = _mm256_castsi256_si128( i2 );
  654. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  655. __m128i ni6 = _mm256_castsi256_si128( i3 );
  656. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  657. // Convert int32 to int16
  658. ni0 = _mm_packs_epi32( ni0, ni1 );
  659. ni2 = _mm_packs_epi32( ni2, ni3 );
  660. ni4 = _mm_packs_epi32( ni4, ni5 );
  661. ni6 = _mm_packs_epi32( ni6, ni7 );
  662. // Convert int16 to int8
  663. ni0 = _mm_packs_epi16( ni0, ni2 );
  664. ni4 = _mm_packs_epi16( ni4, ni6 );
  665. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  666. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  667. #endif
  668. }
  669. #elif defined(__riscv_v_intrinsic)
  670. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  671. for (int i = 0; i < nb; i++) {
  672. // load elements
  673. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  674. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  675. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  676. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  677. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  678. const float d = amax / ((1 << 7) - 1);
  679. const float id = d ? 1.0f/d : 0.0f;
  680. y[i].d = GGML_FP32_TO_FP16(d);
  681. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  682. // convert to integer
  683. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  684. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  685. // store result
  686. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  687. }
  688. #else
  689. GGML_UNUSED(nb);
  690. // scalar
  691. quantize_row_q8_0_reference(x, y, k);
  692. #endif
  693. }
  694. // reference implementation for deterministic creation of model files
  695. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  696. assert(QK8_1 == 32);
  697. assert(k % QK8_1 == 0);
  698. const int nb = k / QK8_1;
  699. for (int i = 0; i < nb; i++) {
  700. float amax = 0.0f; // absolute max
  701. for (int j = 0; j < QK8_1; j++) {
  702. const float v = x[i*QK8_1 + j];
  703. amax = MAX(amax, fabsf(v));
  704. }
  705. const float d = amax / ((1 << 7) - 1);
  706. const float id = d ? 1.0f/d : 0.0f;
  707. y[i].d = d;
  708. int sum = 0;
  709. for (int j = 0; j < QK8_1/2; ++j) {
  710. const float v0 = x[i*QK8_1 + j]*id;
  711. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  712. y[i].qs[ j] = roundf(v0);
  713. y[i].qs[QK8_1/2 + j] = roundf(v1);
  714. sum += y[i].qs[ j];
  715. sum += y[i].qs[QK8_1/2 + j];
  716. }
  717. y[i].s = sum*d;
  718. }
  719. }
  720. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  721. assert(k % QK8_1 == 0);
  722. const int nb = k / QK8_1;
  723. block_q8_1 * restrict y = vy;
  724. #if defined(__ARM_NEON)
  725. for (int i = 0; i < nb; i++) {
  726. float32x4_t srcv [8];
  727. float32x4_t asrcv[8];
  728. float32x4_t amaxv[8];
  729. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  730. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  731. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  732. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  733. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  734. const float amax = vmaxvq_f32(amaxv[0]);
  735. const float d = amax / ((1 << 7) - 1);
  736. const float id = d ? 1.0f/d : 0.0f;
  737. y[i].d = d;
  738. int32x4_t accv = vdupq_n_s32(0);
  739. for (int j = 0; j < 8; j++) {
  740. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  741. const int32x4_t vi = vcvtnq_s32_f32(v);
  742. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  743. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  744. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  745. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  746. accv = vaddq_s32(accv, vi);
  747. }
  748. y[i].s = d * vaddvq_s32(accv);
  749. }
  750. #elif defined(__wasm_simd128__)
  751. for (int i = 0; i < nb; i++) {
  752. v128_t srcv [8];
  753. v128_t asrcv[8];
  754. v128_t amaxv[8];
  755. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  756. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  757. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  758. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  759. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  760. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  761. wasm_f32x4_extract_lane(amaxv[0], 1)),
  762. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  763. wasm_f32x4_extract_lane(amaxv[0], 3)));
  764. const float d = amax / ((1 << 7) - 1);
  765. const float id = d ? 1.0f/d : 0.0f;
  766. y[i].d = d;
  767. v128_t accv = wasm_i32x4_splat(0);
  768. for (int j = 0; j < 8; j++) {
  769. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  770. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  771. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  772. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  773. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  774. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  775. accv = wasm_i32x4_add(accv, vi);
  776. }
  777. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  778. wasm_i32x4_extract_lane(accv, 1) +
  779. wasm_i32x4_extract_lane(accv, 2) +
  780. wasm_i32x4_extract_lane(accv, 3));
  781. }
  782. #elif defined(__AVX2__) || defined(__AVX__)
  783. for (int i = 0; i < nb; i++) {
  784. // Load elements into 4 AVX vectors
  785. __m256 v0 = _mm256_loadu_ps( x );
  786. __m256 v1 = _mm256_loadu_ps( x + 8 );
  787. __m256 v2 = _mm256_loadu_ps( x + 16 );
  788. __m256 v3 = _mm256_loadu_ps( x + 24 );
  789. x += 32;
  790. // Compute max(abs(e)) for the block
  791. const __m256 signBit = _mm256_set1_ps( -0.0f );
  792. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  793. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  794. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  795. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  796. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  797. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  798. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  799. const float maxScalar = _mm_cvtss_f32( max4 );
  800. // Quantize these floats
  801. const float d = maxScalar / 127.f;
  802. y[i].d = d;
  803. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  804. const __m256 mul = _mm256_set1_ps( id );
  805. // Apply the multiplier
  806. v0 = _mm256_mul_ps( v0, mul );
  807. v1 = _mm256_mul_ps( v1, mul );
  808. v2 = _mm256_mul_ps( v2, mul );
  809. v3 = _mm256_mul_ps( v3, mul );
  810. // Round to nearest integer
  811. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  812. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  813. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  814. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  815. // Convert floats to integers
  816. __m256i i0 = _mm256_cvtps_epi32( v0 );
  817. __m256i i1 = _mm256_cvtps_epi32( v1 );
  818. __m256i i2 = _mm256_cvtps_epi32( v2 );
  819. __m256i i3 = _mm256_cvtps_epi32( v3 );
  820. #if defined(__AVX2__)
  821. // Compute the sum of the quants and set y[i].s
  822. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  823. // Convert int32 to int16
  824. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  825. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  826. // Convert int16 to int8
  827. 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
  828. // We got our precious signed bytes, but the order is now wrong
  829. // These AVX2 pack instructions process 16-byte pieces independently
  830. // The following instruction is fixing the order
  831. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  832. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  833. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  834. #else
  835. // Since we don't have in AVX some necessary functions,
  836. // we split the registers in half and call AVX2 analogs from SSE
  837. __m128i ni0 = _mm256_castsi256_si128( i0 );
  838. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  839. __m128i ni2 = _mm256_castsi256_si128( i1 );
  840. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  841. __m128i ni4 = _mm256_castsi256_si128( i2 );
  842. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  843. __m128i ni6 = _mm256_castsi256_si128( i3 );
  844. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  845. // Compute the sum of the quants and set y[i].s
  846. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  847. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  848. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  849. // Convert int32 to int16
  850. ni0 = _mm_packs_epi32( ni0, ni1 );
  851. ni2 = _mm_packs_epi32( ni2, ni3 );
  852. ni4 = _mm_packs_epi32( ni4, ni5 );
  853. ni6 = _mm_packs_epi32( ni6, ni7 );
  854. // Convert int16 to int8
  855. ni0 = _mm_packs_epi16( ni0, ni2 );
  856. ni4 = _mm_packs_epi16( ni4, ni6 );
  857. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  858. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  859. #endif
  860. }
  861. #elif defined(__riscv_v_intrinsic)
  862. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  863. for (int i = 0; i < nb; i++) {
  864. // load elements
  865. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  866. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  867. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  868. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  869. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  870. const float d = amax / ((1 << 7) - 1);
  871. const float id = d ? 1.0f/d : 0.0f;
  872. y[i].d = d;
  873. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  874. // convert to integer
  875. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  876. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  877. // store result
  878. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  879. // compute sum for y[i].s
  880. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  881. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  882. // set y[i].s
  883. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  884. y[i].s = sum*d;
  885. }
  886. #else
  887. GGML_UNUSED(nb);
  888. // scalar
  889. quantize_row_q8_1_reference(x, y, k);
  890. #endif
  891. }
  892. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  893. static const int qk = QK4_0;
  894. assert(k % qk == 0);
  895. const int nb = k / qk;
  896. for (int i = 0; i < nb; i++) {
  897. const float d = GGML_FP16_TO_FP32(x[i].d);
  898. for (int j = 0; j < qk/2; ++j) {
  899. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  900. const int x1 = (x[i].qs[j] >> 4) - 8;
  901. y[i*qk + j + 0 ] = x0*d;
  902. y[i*qk + j + qk/2] = x1*d;
  903. }
  904. }
  905. }
  906. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  907. static const int qk = QK4_1;
  908. assert(k % qk == 0);
  909. const int nb = k / qk;
  910. for (int i = 0; i < nb; i++) {
  911. const float d = GGML_FP16_TO_FP32(x[i].d);
  912. const float m = GGML_FP16_TO_FP32(x[i].m);
  913. for (int j = 0; j < qk/2; ++j) {
  914. const int x0 = (x[i].qs[j] & 0x0F);
  915. const int x1 = (x[i].qs[j] >> 4);
  916. y[i*qk + j + 0 ] = x0*d + m;
  917. y[i*qk + j + qk/2] = x1*d + m;
  918. }
  919. }
  920. }
  921. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  922. static const int qk = QK5_0;
  923. assert(k % qk == 0);
  924. const int nb = k / qk;
  925. for (int i = 0; i < nb; i++) {
  926. const float d = GGML_FP16_TO_FP32(x[i].d);
  927. uint32_t qh;
  928. memcpy(&qh, x[i].qh, sizeof(qh));
  929. for (int j = 0; j < qk/2; ++j) {
  930. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  931. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  932. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  933. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  934. y[i*qk + j + 0 ] = x0*d;
  935. y[i*qk + j + qk/2] = x1*d;
  936. }
  937. }
  938. }
  939. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  940. static const int qk = QK5_1;
  941. assert(k % qk == 0);
  942. const int nb = k / qk;
  943. for (int i = 0; i < nb; i++) {
  944. const float d = GGML_FP16_TO_FP32(x[i].d);
  945. const float m = GGML_FP16_TO_FP32(x[i].m);
  946. uint32_t qh;
  947. memcpy(&qh, x[i].qh, sizeof(qh));
  948. for (int j = 0; j < qk/2; ++j) {
  949. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  950. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  951. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  952. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  953. y[i*qk + j + 0 ] = x0*d + m;
  954. y[i*qk + j + qk/2] = x1*d + m;
  955. }
  956. }
  957. }
  958. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  959. static const int qk = QK8_0;
  960. assert(k % qk == 0);
  961. const int nb = k / qk;
  962. for (int i = 0; i < nb; i++) {
  963. const float d = GGML_FP16_TO_FP32(x[i].d);
  964. for (int j = 0; j < qk; ++j) {
  965. y[i*qk + j] = x[i].qs[j]*d;
  966. }
  967. }
  968. }
  969. //
  970. // 2-6 bit quantization in super-blocks
  971. //
  972. //
  973. // ===================== Helper functions
  974. //
  975. static inline int nearest_int(float fval) {
  976. assert(fval <= 4194303.f);
  977. float val = fval + 12582912.f;
  978. int i; memcpy(&i, &val, sizeof(int));
  979. return (i & 0x007fffff) - 0x00400000;
  980. }
  981. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type) {
  982. float max = 0;
  983. float amax = 0;
  984. for (int i = 0; i < n; ++i) {
  985. float ax = fabsf(x[i]);
  986. if (ax > amax) { amax = ax; max = x[i]; }
  987. }
  988. if (amax < 1e-30f) { // all zero
  989. for (int i = 0; i < n; ++i) {
  990. L[i] = 0;
  991. }
  992. return 0.f;
  993. }
  994. float iscale = -nmax / max;
  995. if (rmse_type == 0) {
  996. for (int i = 0; i < n; ++i) {
  997. int l = nearest_int(iscale * x[i]);
  998. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  999. }
  1000. return 1/iscale;
  1001. }
  1002. bool return_early = false;
  1003. if (rmse_type < 0) {
  1004. rmse_type = -rmse_type;
  1005. return_early = true;
  1006. }
  1007. int weight_type = rmse_type%2;
  1008. float sumlx = 0;
  1009. float suml2 = 0;
  1010. for (int i = 0; i < n; ++i) {
  1011. int l = nearest_int(iscale * x[i]);
  1012. l = MAX(-nmax, MIN(nmax-1, l));
  1013. L[i] = l + nmax;
  1014. float w = weight_type == 1 ? x[i] * x[i] : 1;
  1015. sumlx += w*x[i]*l;
  1016. suml2 += w*l*l;
  1017. }
  1018. float scale = sumlx/suml2;
  1019. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1020. float best = scale * sumlx;
  1021. for (int is = -9; is <= 9; ++is) {
  1022. if (is == 0) {
  1023. continue;
  1024. }
  1025. iscale = -(nmax + 0.1f*is) / max;
  1026. sumlx = suml2 = 0;
  1027. for (int i = 0; i < n; ++i) {
  1028. int l = nearest_int(iscale * x[i]);
  1029. l = MAX(-nmax, MIN(nmax-1, l));
  1030. float w = weight_type == 1 ? x[i] * x[i] : 1;
  1031. sumlx += w*x[i]*l;
  1032. suml2 += w*l*l;
  1033. }
  1034. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1035. for (int i = 0; i < n; ++i) {
  1036. int l = nearest_int(iscale * x[i]);
  1037. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1038. }
  1039. scale = sumlx/suml2; best = scale*sumlx;
  1040. }
  1041. }
  1042. return scale;
  1043. }
  1044. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1045. float max = 0;
  1046. float amax = 0;
  1047. for (int i = 0; i < n; ++i) {
  1048. float ax = fabsf(x[i]);
  1049. if (ax > amax) { amax = ax; max = x[i]; }
  1050. }
  1051. if (!amax) { // all zero
  1052. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1053. return 0.f;
  1054. }
  1055. float iscale = -nmax / max;
  1056. if (do_rmse) {
  1057. float sumlx = 0;
  1058. float suml2 = 0;
  1059. for (int i = 0; i < n; ++i) {
  1060. int l = nearest_int(iscale * x[i]);
  1061. l = MAX(-nmax, MIN(nmax-1, l));
  1062. L[i] = l;
  1063. float w = x[i]*x[i];
  1064. sumlx += w*x[i]*l;
  1065. suml2 += w*l*l;
  1066. }
  1067. for (int itry = 0; itry < 5; ++itry) {
  1068. int n_changed = 0;
  1069. for (int i = 0; i < n; ++i) {
  1070. float w = x[i]*x[i];
  1071. float slx = sumlx - w*x[i]*L[i];
  1072. if (slx > 0) {
  1073. float sl2 = suml2 - w*L[i]*L[i];
  1074. int new_l = nearest_int(x[i] * sl2 / slx);
  1075. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1076. if (new_l != L[i]) {
  1077. slx += w*x[i]*new_l;
  1078. sl2 += w*new_l*new_l;
  1079. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1080. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1081. ++n_changed;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. if (!n_changed) {
  1087. break;
  1088. }
  1089. }
  1090. for (int i = 0; i < n; ++i) {
  1091. L[i] += nmax;
  1092. }
  1093. return sumlx / suml2;
  1094. }
  1095. for (int i = 0; i < n; ++i) {
  1096. int l = nearest_int(iscale * x[i]);
  1097. l = MAX(-nmax, MIN(nmax-1, l));
  1098. L[i] = l + nmax;
  1099. }
  1100. return 1/iscale;
  1101. }
  1102. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1103. int ntry, float alpha) {
  1104. float min = x[0];
  1105. float max = x[0];
  1106. for (int i = 1; i < n; ++i) {
  1107. if (x[i] < min) min = x[i];
  1108. if (x[i] > max) max = x[i];
  1109. }
  1110. if (max == min) {
  1111. for (int i = 0; i < n; ++i) L[i] = 0;
  1112. *the_min = 0;
  1113. return 0.f;
  1114. }
  1115. if (min > 0) min = 0;
  1116. float iscale = nmax/(max - min);
  1117. float scale = 1/iscale;
  1118. for (int itry = 0; itry < ntry; ++itry) {
  1119. float sumlx = 0; int suml2 = 0;
  1120. bool did_change = false;
  1121. for (int i = 0; i < n; ++i) {
  1122. int l = nearest_int(iscale*(x[i] - min));
  1123. l = MAX(0, MIN(nmax, l));
  1124. if (l != L[i]) {
  1125. L[i] = l;
  1126. did_change = true;
  1127. }
  1128. sumlx += (x[i] - min)*l;
  1129. suml2 += l*l;
  1130. }
  1131. scale = sumlx/suml2;
  1132. float sum = 0;
  1133. for (int i = 0; i < n; ++i) {
  1134. sum += x[i] - scale*L[i];
  1135. }
  1136. min = alpha*min + (1 - alpha)*sum/n;
  1137. if (min > 0) min = 0;
  1138. iscale = 1/scale;
  1139. if (!did_change) break;
  1140. }
  1141. *the_min = -min;
  1142. return scale;
  1143. }
  1144. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1145. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1146. float rmin, float rdelta, int nstep, bool use_mad) {
  1147. float min = x[0];
  1148. float max = x[0];
  1149. float sum_w = weights[0];
  1150. float sum_x = sum_w * x[0];
  1151. #ifdef HAVE_BUGGY_APPLE_LINKER
  1152. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1153. for (volatile int i = 1; i < n; ++i) {
  1154. #else
  1155. for (int i = 1; i < n; ++i) {
  1156. #endif
  1157. if (x[i] < min) min = x[i];
  1158. if (x[i] > max) max = x[i];
  1159. float w = weights[i];
  1160. sum_w += w;
  1161. sum_x += w * x[i];
  1162. }
  1163. if (min > 0) min = 0;
  1164. if (max == min) {
  1165. for (int i = 0; i < n; ++i) L[i] = 0;
  1166. *the_min = -min;
  1167. return 0.f;
  1168. }
  1169. float iscale = nmax/(max - min);
  1170. float scale = 1/iscale;
  1171. float best_mad = 0;
  1172. for (int i = 0; i < n; ++i) {
  1173. int l = nearest_int(iscale*(x[i] - min));
  1174. L[i] = MAX(0, MIN(nmax, l));
  1175. float diff = scale * L[i] + min - x[i];
  1176. diff = use_mad ? fabsf(diff) : diff * diff;
  1177. float w = weights[i];
  1178. best_mad += w * diff;
  1179. }
  1180. if (nstep < 1) {
  1181. *the_min = -min;
  1182. return scale;
  1183. }
  1184. for (int is = 0; is <= nstep; ++is) {
  1185. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1186. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1187. for (int i = 0; i < n; ++i) {
  1188. int l = nearest_int(iscale*(x[i] - min));
  1189. l = MAX(0, MIN(nmax, l));
  1190. Laux[i] = l;
  1191. float w = weights[i];
  1192. sum_l += w*l;
  1193. sum_l2 += w*l*l;
  1194. sum_xl += w*l*x[i];
  1195. }
  1196. float D = sum_w * sum_l2 - sum_l * sum_l;
  1197. if (D > 0) {
  1198. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1199. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1200. if (this_min > 0) {
  1201. this_min = 0;
  1202. this_scale = sum_xl / sum_l2;
  1203. }
  1204. float mad = 0;
  1205. for (int i = 0; i < n; ++i) {
  1206. float diff = this_scale * Laux[i] + this_min - x[i];
  1207. diff = use_mad ? fabsf(diff) : diff * diff;
  1208. float w = weights[i];
  1209. mad += w * diff;
  1210. }
  1211. if (mad < best_mad) {
  1212. for (int i = 0; i < n; ++i) {
  1213. L[i] = Laux[i];
  1214. }
  1215. best_mad = mad;
  1216. scale = this_scale;
  1217. min = this_min;
  1218. }
  1219. }
  1220. }
  1221. *the_min = -min;
  1222. return scale;
  1223. }
  1224. #if QK_K == 256
  1225. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1226. if (j < 4) {
  1227. *d = q[j] & 63; *m = q[j + 4] & 63;
  1228. } else {
  1229. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1230. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1231. }
  1232. }
  1233. #endif
  1234. //========================- 2-bit (de)-quantization
  1235. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1236. assert(k % QK_K == 0);
  1237. const int nb = k / QK_K;
  1238. uint8_t L[QK_K];
  1239. uint8_t Laux[16];
  1240. float weights[16];
  1241. float mins[QK_K/16];
  1242. float scales[QK_K/16];
  1243. const float q4scale = 15.f;
  1244. for (int i = 0; i < nb; i++) {
  1245. float max_scale = 0; // as we are deducting the min, scales are always positive
  1246. float max_min = 0;
  1247. for (int j = 0; j < QK_K/16; ++j) {
  1248. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1249. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1250. float scale = scales[j];
  1251. if (scale > max_scale) {
  1252. max_scale = scale;
  1253. }
  1254. float min = mins[j];
  1255. if (min > max_min) {
  1256. max_min = min;
  1257. }
  1258. }
  1259. if (max_scale > 0) {
  1260. float iscale = q4scale/max_scale;
  1261. for (int j = 0; j < QK_K/16; ++j) {
  1262. int l = nearest_int(iscale*scales[j]);
  1263. y[i].scales[j] = l;
  1264. }
  1265. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1266. } else {
  1267. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1268. y[i].d = GGML_FP32_TO_FP16(0.f);
  1269. }
  1270. if (max_min > 0) {
  1271. float iscale = q4scale/max_min;
  1272. for (int j = 0; j < QK_K/16; ++j) {
  1273. int l = nearest_int(iscale*mins[j]);
  1274. y[i].scales[j] |= (l << 4);
  1275. }
  1276. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1277. } else {
  1278. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1279. }
  1280. for (int j = 0; j < QK_K/16; ++j) {
  1281. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1282. if (!d) continue;
  1283. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1284. for (int ii = 0; ii < 16; ++ii) {
  1285. int l = nearest_int((x[16*j + ii] + dm)/d);
  1286. l = MAX(0, MIN(3, l));
  1287. L[16*j + ii] = l;
  1288. }
  1289. }
  1290. #if QK_K == 256
  1291. for (int j = 0; j < QK_K; j += 128) {
  1292. for (int l = 0; l < 32; ++l) {
  1293. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1294. }
  1295. }
  1296. #else
  1297. for (int l = 0; l < 16; ++l) {
  1298. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1299. }
  1300. #endif
  1301. x += QK_K;
  1302. }
  1303. }
  1304. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1305. assert(k % QK_K == 0);
  1306. const int nb = k / QK_K;
  1307. for (int i = 0; i < nb; i++) {
  1308. const float d = GGML_FP16_TO_FP32(x[i].d);
  1309. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1310. const uint8_t * q = x[i].qs;
  1311. #if QK_K == 256
  1312. int is = 0;
  1313. float dl, ml;
  1314. for (int n = 0; n < QK_K; n += 128) {
  1315. int shift = 0;
  1316. for (int j = 0; j < 4; ++j) {
  1317. uint8_t sc = x[i].scales[is++];
  1318. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1319. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1320. sc = x[i].scales[is++];
  1321. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1322. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1323. shift += 2;
  1324. }
  1325. q += 32;
  1326. }
  1327. #else
  1328. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1329. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1330. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1331. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1332. for (int l = 0; l < 16; ++l) {
  1333. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1334. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1335. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1336. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1337. }
  1338. y += QK_K;
  1339. #endif
  1340. }
  1341. }
  1342. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1343. quantize_row_q2_K_reference(x, vy, k);
  1344. }
  1345. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1346. (void)hist; // TODO: collect histograms
  1347. for (int j = 0; j < n; j += k) {
  1348. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1349. quantize_row_q2_K_reference(src + j, y, k);
  1350. }
  1351. return (n/QK_K*sizeof(block_q2_K));
  1352. }
  1353. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1354. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1355. float rmin, float rdelta, int nstep, bool use_mad) {
  1356. float min = x[0];
  1357. float max = x[0];
  1358. float sum_w = weights ? weights[0] : x[0]*x[0];
  1359. float sum_x = sum_w * x[0];
  1360. for (int i = 1; i < n; ++i) {
  1361. if (x[i] < min) min = x[i];
  1362. if (x[i] > max) max = x[i];
  1363. float w = weights ? weights[i] : x[i]*x[i];
  1364. sum_w += w;
  1365. sum_x += w * x[i];
  1366. }
  1367. if (min > 0) {
  1368. min = 0;
  1369. }
  1370. if (max <= min) {
  1371. for (int i = 0; i < n; ++i) L[i] = 0;
  1372. *the_min = -min;
  1373. return 0.f;
  1374. }
  1375. float iscale = nmax/(max - min);
  1376. float scale = 1/iscale;
  1377. float best_mad = 0;
  1378. for (int i = 0; i < n; ++i) {
  1379. int l = nearest_int(iscale*(x[i] - min));
  1380. L[i] = MAX(0, MIN(nmax, l));
  1381. float diff = scale * L[i] + min - x[i];
  1382. diff = use_mad ? fabsf(diff) : diff*diff;
  1383. float w = weights ? weights[i] : x[i]*x[i];
  1384. best_mad += w * diff;
  1385. }
  1386. if (nstep < 1) {
  1387. *the_min = -min;
  1388. return scale;
  1389. }
  1390. for (int is = 0; is <= nstep; ++is) {
  1391. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1392. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1393. for (int i = 0; i < n; ++i) {
  1394. int l = nearest_int(iscale*(x[i] - min));
  1395. l = MAX(0, MIN(nmax, l));
  1396. Laux[i] = l;
  1397. float w = weights ? weights[i] : x[i]*x[i];
  1398. sum_l += w*l;
  1399. sum_l2 += w*l*l;
  1400. sum_xl += w*l*x[i];
  1401. }
  1402. float D = sum_w * sum_l2 - sum_l * sum_l;
  1403. if (D > 0) {
  1404. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1405. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1406. if (this_min > 0) {
  1407. this_min = 0;
  1408. this_scale = sum_xl / sum_l2;
  1409. }
  1410. float mad = 0;
  1411. for (int i = 0; i < n; ++i) {
  1412. float diff = this_scale * Laux[i] + this_min - x[i];
  1413. diff = use_mad ? fabsf(diff) : diff*diff;
  1414. float w = weights ? weights[i] : x[i]*x[i];
  1415. mad += w * diff;
  1416. }
  1417. if (mad < best_mad) {
  1418. for (int i = 0; i < n; ++i) {
  1419. L[i] = Laux[i];
  1420. }
  1421. best_mad = mad;
  1422. scale = this_scale;
  1423. min = this_min;
  1424. }
  1425. }
  1426. }
  1427. *the_min = -min;
  1428. return scale;
  1429. }
  1430. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1431. float max = 0;
  1432. for (int i = 0; i < n; ++i) {
  1433. max = MAX(max, x[i]);
  1434. }
  1435. if (!max) { // all zero
  1436. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1437. return 0.f;
  1438. }
  1439. float iscale = nmax / max;
  1440. for (int i = 0; i < n; ++i) {
  1441. L[i] = nearest_int(iscale * x[i]);
  1442. }
  1443. float scale = 1/iscale;
  1444. float best_mse = 0;
  1445. for (int i = 0; i < n; ++i) {
  1446. float diff = x[i] - scale*L[i];
  1447. float w = quant_weights[i];
  1448. best_mse += w*diff*diff;
  1449. }
  1450. for (int is = -4; is <= 4; ++is) {
  1451. if (is == 0) continue;
  1452. float iscale_is = (0.1f*is + nmax)/max;
  1453. float scale_is = 1/iscale_is;
  1454. float mse = 0;
  1455. for (int i = 0; i < n; ++i) {
  1456. int l = nearest_int(iscale_is*x[i]);
  1457. l = MIN(nmax, l);
  1458. float diff = x[i] - scale_is*l;
  1459. float w = quant_weights[i];
  1460. mse += w*diff*diff;
  1461. }
  1462. if (mse < best_mse) {
  1463. best_mse = mse;
  1464. iscale = iscale_is;
  1465. }
  1466. }
  1467. float sumlx = 0;
  1468. float suml2 = 0;
  1469. for (int i = 0; i < n; ++i) {
  1470. int l = nearest_int(iscale * x[i]);
  1471. l = MIN(nmax, l);
  1472. L[i] = l;
  1473. float w = quant_weights[i];
  1474. sumlx += w*x[i]*l;
  1475. suml2 += w*l*l;
  1476. }
  1477. for (int itry = 0; itry < 5; ++itry) {
  1478. int n_changed = 0;
  1479. for (int i = 0; i < n; ++i) {
  1480. float w = quant_weights[i];
  1481. float slx = sumlx - w*x[i]*L[i];
  1482. float sl2 = suml2 - w*L[i]*L[i];
  1483. if (slx > 0 && sl2 > 0) {
  1484. int new_l = nearest_int(x[i] * sl2 / slx);
  1485. new_l = MIN(nmax, new_l);
  1486. if (new_l != L[i]) {
  1487. slx += w*x[i]*new_l;
  1488. sl2 += w*new_l*new_l;
  1489. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1490. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1491. ++n_changed;
  1492. }
  1493. }
  1494. }
  1495. }
  1496. if (!n_changed) {
  1497. break;
  1498. }
  1499. }
  1500. return sumlx / suml2;
  1501. }
  1502. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1503. GGML_ASSERT(quant_weights);
  1504. assert(k % QK_K == 0);
  1505. const int nb = k / QK_K;
  1506. const bool requantize = true;
  1507. uint8_t L[QK_K];
  1508. uint8_t Laux[16];
  1509. float mins[QK_K/16];
  1510. float scales[QK_K/16];
  1511. float sw[QK_K/16];
  1512. float weight[QK_K/16];
  1513. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1514. for (int i = 0; i < nb; i++) {
  1515. memset(sw, 0, QK_K/16*sizeof(float));
  1516. float sumx2 = 0;
  1517. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1518. float sigma2 = sumx2/QK_K;
  1519. for (int j = 0; j < QK_K/16; ++j) {
  1520. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1521. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1522. for (int l = 0; l < 16; ++l) sw[j] += weight[l];
  1523. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1524. }
  1525. float dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1526. float mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1527. y[i].d = GGML_FP32_TO_FP16(dm);
  1528. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1529. dm = GGML_FP16_TO_FP32(y[i].d);
  1530. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1531. for (int j = 0; j < QK_K/16; ++j) {
  1532. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1533. }
  1534. if (requantize) {
  1535. for (int j = 0; j < QK_K/16; ++j) {
  1536. const float d = dm * (y[i].scales[j] & 0xF);
  1537. if (!d) continue;
  1538. const float m = mm * (y[i].scales[j] >> 4);
  1539. for (int ii = 0; ii < 16; ++ii) {
  1540. int l = nearest_int((x[16*j + ii] + m)/d);
  1541. l = MAX(0, MIN(3, l));
  1542. L[16*j + ii] = l;
  1543. }
  1544. }
  1545. }
  1546. #if QK_K == 256
  1547. for (int j = 0; j < QK_K; j += 128) {
  1548. for (int l = 0; l < 32; ++l) {
  1549. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1550. }
  1551. }
  1552. #else
  1553. for (int l = 0; l < 16; ++l) {
  1554. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1555. }
  1556. #endif
  1557. x += QK_K;
  1558. }
  1559. }
  1560. size_t quantize_q2_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1561. (void)hist;
  1562. int row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1563. if (!quant_weights) {
  1564. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1565. }
  1566. else {
  1567. char * qrow = (char *)dst;
  1568. for (int row = 0; row < nrow; ++row) {
  1569. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1570. src += n_per_row;
  1571. qrow += row_size;
  1572. }
  1573. }
  1574. return nrow * row_size;
  1575. }
  1576. //========================= 3-bit (de)-quantization
  1577. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1578. assert(k % QK_K == 0);
  1579. const int nb = k / QK_K;
  1580. int8_t L[QK_K];
  1581. float scales[QK_K / 16];
  1582. for (int i = 0; i < nb; i++) {
  1583. float max_scale = 0;
  1584. float amax = 0;
  1585. for (int j = 0; j < QK_K/16; ++j) {
  1586. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1587. float scale = fabsf(scales[j]);
  1588. if (scale > amax) {
  1589. amax = scale; max_scale = scales[j];
  1590. }
  1591. }
  1592. #if QK_K == 256
  1593. memset(y[i].scales, 0, 12);
  1594. if (max_scale) {
  1595. float iscale = -32.f/max_scale;
  1596. for (int j = 0; j < QK_K/16; ++j) {
  1597. int8_t l = nearest_int(iscale*scales[j]);
  1598. l = MAX(-32, MIN(31, l)) + 32;
  1599. if (j < 8) {
  1600. y[i].scales[j] = l & 0xF;
  1601. } else {
  1602. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1603. }
  1604. l >>= 4;
  1605. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1606. }
  1607. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1608. } else {
  1609. y[i].d = GGML_FP32_TO_FP16(0.f);
  1610. }
  1611. int8_t sc;
  1612. for (int j = 0; j < QK_K/16; ++j) {
  1613. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1614. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1615. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1616. if (!d) {
  1617. continue;
  1618. }
  1619. for (int ii = 0; ii < 16; ++ii) {
  1620. int l = nearest_int(x[16*j + ii]/d);
  1621. l = MAX(-4, MIN(3, l));
  1622. L[16*j + ii] = l + 4;
  1623. }
  1624. }
  1625. #else
  1626. if (max_scale) {
  1627. float iscale = -8.f/max_scale;
  1628. for (int j = 0; j < QK_K/16; j+=2) {
  1629. int l1 = nearest_int(iscale*scales[j]);
  1630. l1 = 8 + MAX(-8, MIN(7, l1));
  1631. int l2 = nearest_int(iscale*scales[j+1]);
  1632. l2 = 8 + MAX(-8, MIN(7, l2));
  1633. y[i].scales[j/2] = l1 | (l2 << 4);
  1634. }
  1635. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1636. } else {
  1637. for (int j = 0; j < QK_K/16; j+=2) {
  1638. y[i].scales[j/2] = 0;
  1639. }
  1640. y[i].d = GGML_FP32_TO_FP16(0.f);
  1641. }
  1642. for (int j = 0; j < QK_K/16; ++j) {
  1643. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1644. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1645. if (!d) {
  1646. continue;
  1647. }
  1648. for (int ii = 0; ii < 16; ++ii) {
  1649. int l = nearest_int(x[16*j + ii]/d);
  1650. l = MAX(-4, MIN(3, l));
  1651. L[16*j + ii] = l + 4;
  1652. }
  1653. }
  1654. #endif
  1655. memset(y[i].hmask, 0, QK_K/8);
  1656. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1657. int m = 0;
  1658. uint8_t hm = 1;
  1659. for (int j = 0; j < QK_K; ++j) {
  1660. if (L[j] > 3) {
  1661. y[i].hmask[m] |= hm;
  1662. L[j] -= 4;
  1663. }
  1664. if (++m == QK_K/8) {
  1665. m = 0; hm <<= 1;
  1666. }
  1667. }
  1668. #if QK_K == 256
  1669. for (int j = 0; j < QK_K; j += 128) {
  1670. for (int l = 0; l < 32; ++l) {
  1671. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1672. }
  1673. }
  1674. #else
  1675. for (int l = 0; l < 16; ++l) {
  1676. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1677. }
  1678. #endif
  1679. x += QK_K;
  1680. }
  1681. }
  1682. #if QK_K == 256
  1683. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1684. assert(k % QK_K == 0);
  1685. const int nb = k / QK_K;
  1686. const uint32_t kmask1 = 0x03030303;
  1687. const uint32_t kmask2 = 0x0f0f0f0f;
  1688. uint32_t aux[4];
  1689. const int8_t * scales = (const int8_t*)aux;
  1690. for (int i = 0; i < nb; i++) {
  1691. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1692. const uint8_t * restrict q = x[i].qs;
  1693. const uint8_t * restrict hm = x[i].hmask;
  1694. uint8_t m = 1;
  1695. memcpy(aux, x[i].scales, 12);
  1696. uint32_t tmp = aux[2];
  1697. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1698. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1699. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1700. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1701. int is = 0;
  1702. float dl;
  1703. for (int n = 0; n < QK_K; n += 128) {
  1704. int shift = 0;
  1705. for (int j = 0; j < 4; ++j) {
  1706. dl = d_all * (scales[is++] - 32);
  1707. for (int l = 0; l < 16; ++l) {
  1708. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1709. }
  1710. dl = d_all * (scales[is++] - 32);
  1711. for (int l = 0; l < 16; ++l) {
  1712. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1713. }
  1714. shift += 2;
  1715. m <<= 1;
  1716. }
  1717. q += 32;
  1718. }
  1719. }
  1720. }
  1721. #else
  1722. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1723. assert(k % QK_K == 0);
  1724. assert(QK_K == 64);
  1725. const int nb = k / QK_K;
  1726. for (int i = 0; i < nb; i++) {
  1727. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1728. const uint8_t * restrict q = x[i].qs;
  1729. const uint8_t * restrict hm = x[i].hmask;
  1730. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1731. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1732. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1733. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1734. for (int l=0; l<8; ++l) {
  1735. uint8_t h = hm[l];
  1736. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1737. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1738. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1739. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1740. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1741. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1742. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1743. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1744. }
  1745. y += QK_K;
  1746. }
  1747. }
  1748. #endif
  1749. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1750. quantize_row_q3_K_reference(x, vy, k);
  1751. }
  1752. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1753. (void)hist; // TODO: collect histograms
  1754. for (int j = 0; j < n; j += k) {
  1755. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1756. quantize_row_q3_K_reference(src + j, y, k);
  1757. }
  1758. return (n/QK_K*sizeof(block_q3_K));
  1759. }
  1760. // ====================== 4-bit (de)-quantization
  1761. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1762. assert(k % QK_K == 0);
  1763. const int nb = k / QK_K;
  1764. uint8_t L[QK_K];
  1765. uint8_t Laux[32];
  1766. float weights[32];
  1767. float mins[QK_K/32];
  1768. float scales[QK_K/32];
  1769. for (int i = 0; i < nb; i++) {
  1770. float max_scale = 0; // as we are deducting the min, scales are always positive
  1771. float max_min = 0;
  1772. for (int j = 0; j < QK_K/32; ++j) {
  1773. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1774. float sum_x2 = 0;
  1775. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1776. float av_x = sqrtf(sum_x2/32);
  1777. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1778. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1779. float scale = scales[j];
  1780. if (scale > max_scale) {
  1781. max_scale = scale;
  1782. }
  1783. float min = mins[j];
  1784. if (min > max_min) {
  1785. max_min = min;
  1786. }
  1787. }
  1788. #if QK_K == 256
  1789. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1790. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1791. for (int j = 0; j < QK_K/32; ++j) {
  1792. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1793. uint8_t lm = nearest_int(inv_min*mins[j]);
  1794. ls = MIN(63, ls);
  1795. lm = MIN(63, lm);
  1796. if (j < 4) {
  1797. y[i].scales[j] = ls;
  1798. y[i].scales[j+4] = lm;
  1799. } else {
  1800. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1801. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1802. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1803. }
  1804. }
  1805. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1806. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1807. uint8_t sc, m;
  1808. for (int j = 0; j < QK_K/32; ++j) {
  1809. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1810. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1811. if (!d) continue;
  1812. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1813. for (int ii = 0; ii < 32; ++ii) {
  1814. int l = nearest_int((x[32*j + ii] + dm)/d);
  1815. l = MAX(0, MIN(15, l));
  1816. L[32*j + ii] = l;
  1817. }
  1818. }
  1819. #else
  1820. const float s_factor = 15.f;
  1821. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1822. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1823. int d1 = nearest_int(inv_scale*scales[0]);
  1824. int m1 = nearest_int(inv_min*mins[0]);
  1825. int d2 = nearest_int(inv_scale*scales[1]);
  1826. int m2 = nearest_int(inv_min*mins[1]);
  1827. y[i].scales[0] = d1 | (m1 << 4);
  1828. y[i].scales[1] = d2 | (m2 << 4);
  1829. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1830. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1831. float sumlx = 0;
  1832. int suml2 = 0;
  1833. for (int j = 0; j < QK_K/32; ++j) {
  1834. const uint8_t sd = y[i].scales[j] & 0xF;
  1835. const uint8_t sm = y[i].scales[j] >> 4;
  1836. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1837. if (!d) continue;
  1838. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1839. for (int ii = 0; ii < 32; ++ii) {
  1840. int l = nearest_int((x[32*j + ii] + m)/d);
  1841. l = MAX(0, MIN(15, l));
  1842. L[32*j + ii] = l;
  1843. sumlx += (x[32*j + ii] + m)*l*sd;
  1844. suml2 += l*l*sd*sd;
  1845. }
  1846. }
  1847. if (suml2) {
  1848. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1849. }
  1850. #endif
  1851. uint8_t * q = y[i].qs;
  1852. for (int j = 0; j < QK_K; j += 64) {
  1853. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1854. q += 32;
  1855. }
  1856. x += QK_K;
  1857. }
  1858. }
  1859. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1860. assert(k % QK_K == 0);
  1861. const int nb = k / QK_K;
  1862. for (int i = 0; i < nb; i++) {
  1863. const uint8_t * q = x[i].qs;
  1864. #if QK_K == 256
  1865. const float d = GGML_FP16_TO_FP32(x[i].d);
  1866. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1867. int is = 0;
  1868. uint8_t sc, m;
  1869. for (int j = 0; j < QK_K; j += 64) {
  1870. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1871. const float d1 = d * sc; const float m1 = min * m;
  1872. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1873. const float d2 = d * sc; const float m2 = min * m;
  1874. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1875. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1876. q += 32; is += 2;
  1877. }
  1878. #else
  1879. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1880. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1881. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1882. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1883. for (int l = 0; l < 32; ++l) {
  1884. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1885. y[l+32] = d2 * (q[l] >> 4) - m2;
  1886. }
  1887. y += QK_K;
  1888. #endif
  1889. }
  1890. }
  1891. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1892. assert(k % QK_K == 0);
  1893. block_q4_K * restrict y = vy;
  1894. quantize_row_q4_K_reference(x, y, k);
  1895. }
  1896. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1897. assert(k % QK_K == 0);
  1898. (void)hist; // TODO: collect histograms
  1899. for (int j = 0; j < n; j += k) {
  1900. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  1901. quantize_row_q4_K_reference(src + j, y, k);
  1902. }
  1903. return (n/QK_K*sizeof(block_q4_K));
  1904. }
  1905. // ====================== 5-bit (de)-quantization
  1906. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  1907. assert(k % QK_K == 0);
  1908. const int nb = k / QK_K;
  1909. #if QK_K == 256
  1910. uint8_t L[QK_K];
  1911. float mins[QK_K/32];
  1912. float scales[QK_K/32];
  1913. float weights[32];
  1914. uint8_t Laux[32];
  1915. #else
  1916. int8_t L[QK_K];
  1917. float scales[QK_K/16];
  1918. #endif
  1919. for (int i = 0; i < nb; i++) {
  1920. #if QK_K == 256
  1921. float max_scale = 0; // as we are deducting the min, scales are always positive
  1922. float max_min = 0;
  1923. for (int j = 0; j < QK_K/32; ++j) {
  1924. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1925. float sum_x2 = 0;
  1926. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1927. float av_x = sqrtf(sum_x2/32);
  1928. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1929. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1930. float scale = scales[j];
  1931. if (scale > max_scale) {
  1932. max_scale = scale;
  1933. }
  1934. float min = mins[j];
  1935. if (min > max_min) {
  1936. max_min = min;
  1937. }
  1938. }
  1939. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1940. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1941. for (int j = 0; j < QK_K/32; ++j) {
  1942. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1943. uint8_t lm = nearest_int(inv_min*mins[j]);
  1944. ls = MIN(63, ls);
  1945. lm = MIN(63, lm);
  1946. if (j < 4) {
  1947. y[i].scales[j] = ls;
  1948. y[i].scales[j+4] = lm;
  1949. } else {
  1950. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1951. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1952. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1953. }
  1954. }
  1955. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1956. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1957. uint8_t sc, m;
  1958. for (int j = 0; j < QK_K/32; ++j) {
  1959. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1960. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1961. if (!d) continue;
  1962. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1963. for (int ii = 0; ii < 32; ++ii) {
  1964. int l = nearest_int((x[32*j + ii] + dm)/d);
  1965. l = MAX(0, MIN(31, l));
  1966. L[32*j + ii] = l;
  1967. }
  1968. }
  1969. uint8_t * restrict qh = y[i].qh;
  1970. uint8_t * restrict ql = y[i].qs;
  1971. memset(qh, 0, QK_K/8);
  1972. uint8_t m1 = 1, m2 = 2;
  1973. for (int n = 0; n < QK_K; n += 64) {
  1974. for (int j = 0; j < 32; ++j) {
  1975. int l1 = L[n + j];
  1976. if (l1 > 15) {
  1977. l1 -= 16; qh[j] |= m1;
  1978. }
  1979. int l2 = L[n + j + 32];
  1980. if (l2 > 15) {
  1981. l2 -= 16; qh[j] |= m2;
  1982. }
  1983. ql[j] = l1 | (l2 << 4);
  1984. }
  1985. m1 <<= 2; m2 <<= 2;
  1986. ql += 32;
  1987. }
  1988. #else
  1989. float max_scale = 0, amax = 0;
  1990. for (int j = 0; j < QK_K/16; ++j) {
  1991. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1);
  1992. float abs_scale = fabsf(scales[j]);
  1993. if (abs_scale > amax) {
  1994. amax = abs_scale;
  1995. max_scale = scales[j];
  1996. }
  1997. }
  1998. float iscale = -128.f/max_scale;
  1999. for (int j = 0; j < QK_K/16; ++j) {
  2000. int l = nearest_int(iscale*scales[j]);
  2001. y[i].scales[j] = MAX(-128, MIN(127, l));
  2002. }
  2003. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2004. for (int j = 0; j < QK_K/16; ++j) {
  2005. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2006. if (!d) continue;
  2007. for (int ii = 0; ii < 16; ++ii) {
  2008. int l = nearest_int(x[16*j + ii]/d);
  2009. l = MAX(-16, MIN(15, l));
  2010. L[16*j + ii] = l + 16;
  2011. }
  2012. }
  2013. uint8_t * restrict qh = y[i].qh;
  2014. uint8_t * restrict ql = y[i].qs;
  2015. memset(qh, 0, QK_K/8);
  2016. for (int j = 0; j < 32; ++j) {
  2017. int jm = j%8;
  2018. int is = j/8;
  2019. int l1 = L[j];
  2020. if (l1 > 15) {
  2021. l1 -= 16; qh[jm] |= (1 << is);
  2022. }
  2023. int l2 = L[j + 32];
  2024. if (l2 > 15) {
  2025. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2026. }
  2027. ql[j] = l1 | (l2 << 4);
  2028. }
  2029. #endif
  2030. x += QK_K;
  2031. }
  2032. }
  2033. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2034. assert(k % QK_K == 0);
  2035. const int nb = k / QK_K;
  2036. for (int i = 0; i < nb; i++) {
  2037. const uint8_t * ql = x[i].qs;
  2038. const uint8_t * qh = x[i].qh;
  2039. #if QK_K == 256
  2040. const float d = GGML_FP16_TO_FP32(x[i].d);
  2041. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2042. int is = 0;
  2043. uint8_t sc, m;
  2044. uint8_t u1 = 1, u2 = 2;
  2045. for (int j = 0; j < QK_K; j += 64) {
  2046. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2047. const float d1 = d * sc; const float m1 = min * m;
  2048. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2049. const float d2 = d * sc; const float m2 = min * m;
  2050. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2051. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2052. ql += 32; is += 2;
  2053. u1 <<= 2; u2 <<= 2;
  2054. }
  2055. #else
  2056. float d = GGML_FP16_TO_FP32(x[i].d);
  2057. const int8_t * restrict s = x[i].scales;
  2058. for (int l = 0; l < 8; ++l) {
  2059. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2060. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2061. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2062. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2063. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2064. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2065. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2066. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2067. }
  2068. y += QK_K;
  2069. #endif
  2070. }
  2071. }
  2072. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2073. assert(k % QK_K == 0);
  2074. block_q5_K * restrict y = vy;
  2075. quantize_row_q5_K_reference(x, y, k);
  2076. }
  2077. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2078. assert(k % QK_K == 0);
  2079. (void)hist; // TODO: collect histograms
  2080. for (int j = 0; j < n; j += k) {
  2081. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  2082. quantize_row_q5_K_reference(src + j, y, k);
  2083. }
  2084. return (n/QK_K*sizeof(block_q5_K));
  2085. }
  2086. // ====================== 6-bit (de)-quantization
  2087. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2088. assert(k % QK_K == 0);
  2089. const int nb = k / QK_K;
  2090. int8_t L[QK_K];
  2091. float scales[QK_K/16];
  2092. for (int i = 0; i < nb; i++) {
  2093. float max_scale = 0;
  2094. float max_abs_scale = 0;
  2095. for (int ib = 0; ib < QK_K/16; ++ib) {
  2096. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1);
  2097. scales[ib] = scale;
  2098. const float abs_scale = fabsf(scale);
  2099. if (abs_scale > max_abs_scale) {
  2100. max_abs_scale = abs_scale;
  2101. max_scale = scale;
  2102. }
  2103. }
  2104. if (!max_abs_scale) {
  2105. memset(&y[i], 0, sizeof(block_q6_K));
  2106. y[i].d = GGML_FP32_TO_FP16(0.f);
  2107. x += QK_K;
  2108. continue;
  2109. }
  2110. float iscale = -128.f/max_scale;
  2111. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2112. for (int ib = 0; ib < QK_K/16; ++ib) {
  2113. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2114. }
  2115. for (int j = 0; j < QK_K/16; ++j) {
  2116. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2117. if (!d) {
  2118. continue;
  2119. }
  2120. for (int ii = 0; ii < 16; ++ii) {
  2121. int l = nearest_int(x[16*j + ii]/d);
  2122. l = MAX(-32, MIN(31, l));
  2123. L[16*j + ii] = l + 32;
  2124. }
  2125. }
  2126. uint8_t * restrict ql = y[i].ql;
  2127. uint8_t * restrict qh = y[i].qh;
  2128. #if QK_K == 256
  2129. for (int j = 0; j < QK_K; j += 128) {
  2130. for (int l = 0; l < 32; ++l) {
  2131. const uint8_t q1 = L[j + l + 0] & 0xF;
  2132. const uint8_t q2 = L[j + l + 32] & 0xF;
  2133. const uint8_t q3 = L[j + l + 64] & 0xF;
  2134. const uint8_t q4 = L[j + l + 96] & 0xF;
  2135. ql[l+ 0] = q1 | (q3 << 4);
  2136. ql[l+32] = q2 | (q4 << 4);
  2137. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2138. }
  2139. ql += 64;
  2140. qh += 32;
  2141. }
  2142. #else
  2143. for (int l = 0; l < 32; ++l) {
  2144. const uint8_t q1 = L[l + 0] & 0xF;
  2145. const uint8_t q2 = L[l + 32] & 0xF;
  2146. ql[l] = q1 | (q2 << 4);
  2147. }
  2148. for (int l = 0; l < 16; ++l) {
  2149. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2150. }
  2151. #endif
  2152. x += QK_K;
  2153. }
  2154. }
  2155. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2156. assert(k % QK_K == 0);
  2157. const int nb = k / QK_K;
  2158. for (int i = 0; i < nb; i++) {
  2159. const float d = GGML_FP16_TO_FP32(x[i].d);
  2160. const uint8_t * restrict ql = x[i].ql;
  2161. const uint8_t * restrict qh = x[i].qh;
  2162. const int8_t * restrict sc = x[i].scales;
  2163. #if QK_K == 256
  2164. for (int n = 0; n < QK_K; n += 128) {
  2165. for (int l = 0; l < 32; ++l) {
  2166. int is = l/16;
  2167. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2168. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2169. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2170. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2171. y[l + 0] = d * sc[is + 0] * q1;
  2172. y[l + 32] = d * sc[is + 2] * q2;
  2173. y[l + 64] = d * sc[is + 4] * q3;
  2174. y[l + 96] = d * sc[is + 6] * q4;
  2175. }
  2176. y += 128;
  2177. ql += 64;
  2178. qh += 32;
  2179. sc += 8;
  2180. }
  2181. #else
  2182. for (int l = 0; l < 16; ++l) {
  2183. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2184. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2185. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2186. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2187. y[l+ 0] = d * sc[0] * q1;
  2188. y[l+16] = d * sc[1] * q2;
  2189. y[l+32] = d * sc[2] * q3;
  2190. y[l+48] = d * sc[3] * q4;
  2191. }
  2192. y += 64;
  2193. #endif
  2194. }
  2195. }
  2196. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2197. assert(k % QK_K == 0);
  2198. block_q6_K * restrict y = vy;
  2199. quantize_row_q6_K_reference(x, y, k);
  2200. }
  2201. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  2202. assert(k % QK_K == 0);
  2203. (void)hist; // TODO: collect histograms
  2204. for (int j = 0; j < n; j += k) {
  2205. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  2206. quantize_row_q6_K_reference(src + j, y, k);
  2207. }
  2208. return (n/QK_K*sizeof(block_q6_K));
  2209. }
  2210. // ====================== "True" 2-bit (de)-quantization
  2211. static const uint64_t iq2xxs_grid[256] = {
  2212. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2213. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2214. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2215. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2216. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2217. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2218. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2219. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2220. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2221. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2222. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2223. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2224. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2225. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2226. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2227. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2228. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2229. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2230. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2231. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2232. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2233. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2234. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2235. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2236. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2237. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2238. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2239. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2240. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2241. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2242. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2243. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2244. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2245. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2246. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2247. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2248. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2249. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2250. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2251. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2252. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2253. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2254. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2255. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2256. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2257. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2258. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2259. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2260. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2261. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2262. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2263. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2264. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2265. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2266. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2267. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2268. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2269. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2270. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2271. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2272. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2273. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2274. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2275. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2276. };
  2277. static const uint64_t iq2xs_grid[512] = {
  2278. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2279. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2280. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2281. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2282. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2283. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2284. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2285. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2286. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2287. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2288. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2289. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2290. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2291. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2292. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2293. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2294. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2295. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2296. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2297. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2298. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2299. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2300. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2301. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2302. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2303. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2304. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2305. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2306. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2307. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2308. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2309. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2310. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2311. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2312. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2313. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2314. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2315. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2316. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2317. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2318. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2319. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2320. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2321. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2322. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2323. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2324. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2325. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2326. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2327. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2328. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2329. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2330. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2331. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2332. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2333. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2334. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2335. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2336. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2337. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2338. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2339. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2340. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2341. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2342. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2343. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2344. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2345. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2346. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2347. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2348. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2349. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2350. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2351. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2352. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2353. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2354. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2355. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2356. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2357. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2358. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2359. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2360. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2361. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2362. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2363. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2364. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2365. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2366. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2367. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2368. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2369. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2370. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2371. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2372. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2373. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2374. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2375. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2376. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2377. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2378. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2379. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2380. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2381. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2382. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2383. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2384. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2385. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2386. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2387. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2388. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2389. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2390. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2391. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2392. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2393. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2394. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2395. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2396. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2397. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2398. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2399. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2400. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2401. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2402. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2403. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2404. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2405. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2406. };
  2407. static const uint8_t ksigns_iq2xs[128] = {
  2408. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2409. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2410. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2411. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2412. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2413. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2414. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2415. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2416. };
  2417. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2418. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2419. assert(k % QK_K == 0);
  2420. const int nb = k / QK_K;
  2421. uint32_t aux32[2];
  2422. const uint8_t * aux8 = (const uint8_t *)aux32;
  2423. for (int i = 0; i < nb; i++) {
  2424. const float d = GGML_FP16_TO_FP32(x[i].d);
  2425. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2426. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2427. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2428. for (int l = 0; l < 4; ++l) {
  2429. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2430. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2431. for (int j = 0; j < 8; ++j) {
  2432. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2433. }
  2434. y += 8;
  2435. }
  2436. }
  2437. }
  2438. }
  2439. // ====================== 2.3125 bpw (de)-quantization
  2440. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2441. assert(k % QK_K == 0);
  2442. const int nb = k / QK_K;
  2443. float db[2];
  2444. for (int i = 0; i < nb; i++) {
  2445. const float d = GGML_FP16_TO_FP32(x[i].d);
  2446. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2447. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2448. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2449. for (int l = 0; l < 4; ++l) {
  2450. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2451. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2452. for (int j = 0; j < 8; ++j) {
  2453. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2454. }
  2455. y += 8;
  2456. }
  2457. }
  2458. }
  2459. }
  2460. //===================================== Q8_K ==============================================
  2461. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  2462. assert(k % QK_K == 0);
  2463. const int nb = k / QK_K;
  2464. for (int i = 0; i < nb; i++) {
  2465. float max = 0;
  2466. float amax = 0;
  2467. for (int j = 0; j < QK_K; ++j) {
  2468. float ax = fabsf(x[j]);
  2469. if (ax > amax) {
  2470. amax = ax; max = x[j];
  2471. }
  2472. }
  2473. if (!amax) {
  2474. y[i].d = 0;
  2475. memset(y[i].qs, 0, QK_K);
  2476. x += QK_K;
  2477. continue;
  2478. }
  2479. //const float iscale = -128.f/max;
  2480. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2481. const float iscale = -127.f/max;
  2482. for (int j = 0; j < QK_K; ++j) {
  2483. int v = nearest_int(iscale*x[j]);
  2484. y[i].qs[j] = MIN(127, v);
  2485. }
  2486. for (int j = 0; j < QK_K/16; ++j) {
  2487. int sum = 0;
  2488. for (int ii = 0; ii < 16; ++ii) {
  2489. sum += y[i].qs[j*16 + ii];
  2490. }
  2491. y[i].bsums[j] = sum;
  2492. }
  2493. y[i].d = 1/iscale;
  2494. x += QK_K;
  2495. }
  2496. }
  2497. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  2498. assert(k % QK_K == 0);
  2499. const int nb = k / QK_K;
  2500. for (int i = 0; i < nb; i++) {
  2501. for (int j = 0; j < QK_K; ++j) {
  2502. *y++ = x[i].d * x[i].qs[j];
  2503. }
  2504. }
  2505. }
  2506. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  2507. quantize_row_q8_K_reference(x, y, k);
  2508. }
  2509. //===================================== Dot ptoducts =================================
  2510. //
  2511. // Helper functions
  2512. //
  2513. #if __AVX__ || __AVX2__ || __AVX512F__
  2514. // shuffles to pick the required scales in dot products
  2515. static inline __m256i get_scale_shuffle_q3k(int i) {
  2516. static const uint8_t k_shuffle[128] = {
  2517. 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,
  2518. 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,
  2519. 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,
  2520. 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,
  2521. };
  2522. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2523. }
  2524. static inline __m256i get_scale_shuffle_k4(int i) {
  2525. static const uint8_t k_shuffle[256] = {
  2526. 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,
  2527. 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,
  2528. 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,
  2529. 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,
  2530. 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,
  2531. 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,
  2532. 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,
  2533. 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
  2534. };
  2535. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2536. }
  2537. static inline __m128i get_scale_shuffle(int i) {
  2538. static const uint8_t k_shuffle[128] = {
  2539. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2540. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2541. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2542. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2543. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2544. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2545. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2546. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2547. };
  2548. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2549. }
  2550. #endif
  2551. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2552. const int qk = QK8_0;
  2553. const int nb = n / qk;
  2554. assert(n % qk == 0);
  2555. const block_q4_0 * restrict x = vx;
  2556. const block_q8_0 * restrict y = vy;
  2557. #if defined(__ARM_NEON)
  2558. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2559. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2560. assert(nb % 2 == 0); // TODO: handle odd nb
  2561. for (int i = 0; i < nb; i += 2) {
  2562. const block_q4_0 * restrict x0 = &x[i + 0];
  2563. const block_q4_0 * restrict x1 = &x[i + 1];
  2564. const block_q8_0 * restrict y0 = &y[i + 0];
  2565. const block_q8_0 * restrict y1 = &y[i + 1];
  2566. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2567. const int8x16_t s8b = vdupq_n_s8(0x8);
  2568. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2569. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2570. // 4-bit -> 8-bit
  2571. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2572. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2573. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2574. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2575. // sub 8
  2576. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2577. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2578. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  2579. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  2580. // load y
  2581. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2582. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2583. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2584. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2585. // dot product into int32x4_t
  2586. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  2587. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  2588. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2589. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2590. }
  2591. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2592. #elif defined(__AVX2__)
  2593. // Initialize accumulator with zeros
  2594. __m256 acc = _mm256_setzero_ps();
  2595. // Main loop
  2596. for (int i = 0; i < nb; ++i) {
  2597. /* Compute combined scale for the block */
  2598. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2599. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2600. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  2601. const __m256i off = _mm256_set1_epi8( 8 );
  2602. bx = _mm256_sub_epi8( bx, off );
  2603. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2604. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2605. /* Multiply q with scale and accumulate */
  2606. acc = _mm256_fmadd_ps( d, q, acc );
  2607. }
  2608. *s = hsum_float_8(acc);
  2609. #elif defined(__AVX__)
  2610. // Initialize accumulator with zeros
  2611. __m256 acc = _mm256_setzero_ps();
  2612. // Main loop
  2613. for (int i = 0; i < nb; ++i) {
  2614. // Compute combined scale for the block
  2615. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2616. const __m128i lowMask = _mm_set1_epi8(0xF);
  2617. const __m128i off = _mm_set1_epi8(8);
  2618. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  2619. __m128i bx = _mm_and_si128(lowMask, tmp);
  2620. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  2621. bx = _mm_sub_epi8(bx, off);
  2622. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  2623. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  2624. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2625. bx = _mm_sub_epi8(bx, off);
  2626. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  2627. // Convert int32_t to float
  2628. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  2629. // Apply the scale, and accumulate
  2630. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  2631. }
  2632. *s = hsum_float_8(acc);
  2633. #elif defined(__SSSE3__)
  2634. // set constants
  2635. const __m128i lowMask = _mm_set1_epi8(0xF);
  2636. const __m128i off = _mm_set1_epi8(8);
  2637. // Initialize accumulator with zeros
  2638. __m128 acc_0 = _mm_setzero_ps();
  2639. __m128 acc_1 = _mm_setzero_ps();
  2640. __m128 acc_2 = _mm_setzero_ps();
  2641. __m128 acc_3 = _mm_setzero_ps();
  2642. // First round without accumulation
  2643. {
  2644. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  2645. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  2646. // Compute combined scale for the block 0 and 1
  2647. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  2648. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  2649. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2650. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  2651. bx_0 = _mm_sub_epi8(bx_0, off);
  2652. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2653. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2654. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  2655. bx_1 = _mm_sub_epi8(bx_1, off);
  2656. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2657. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  2658. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  2659. // Compute combined scale for the block 2 and 3
  2660. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  2661. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  2662. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2663. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  2664. bx_2 = _mm_sub_epi8(bx_2, off);
  2665. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2666. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2667. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  2668. bx_3 = _mm_sub_epi8(bx_3, off);
  2669. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2670. // Convert int32_t to float
  2671. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2672. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2673. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2674. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2675. // Apply the scale
  2676. acc_0 = _mm_mul_ps( d_0_1, p0 );
  2677. acc_1 = _mm_mul_ps( d_0_1, p1 );
  2678. acc_2 = _mm_mul_ps( d_2_3, p2 );
  2679. acc_3 = _mm_mul_ps( d_2_3, p3 );
  2680. }
  2681. assert(nb % 2 == 0); // TODO: handle odd nb
  2682. // Main loop
  2683. for (int i = 2; i < nb; i+=2) {
  2684. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  2685. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  2686. // Compute combined scale for the block 0 and 1
  2687. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2688. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  2689. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2690. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  2691. bx_0 = _mm_sub_epi8(bx_0, off);
  2692. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2693. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2694. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2695. bx_1 = _mm_sub_epi8(bx_1, off);
  2696. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2697. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  2698. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  2699. // Compute combined scale for the block 2 and 3
  2700. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  2701. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  2702. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2703. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  2704. bx_2 = _mm_sub_epi8(bx_2, off);
  2705. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2706. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2707. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  2708. bx_3 = _mm_sub_epi8(bx_3, off);
  2709. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2710. // Convert int32_t to float
  2711. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2712. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2713. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2714. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2715. // Apply the scale
  2716. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  2717. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  2718. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  2719. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  2720. // Acummulate
  2721. acc_0 = _mm_add_ps(p0_d, acc_0);
  2722. acc_1 = _mm_add_ps(p1_d, acc_1);
  2723. acc_2 = _mm_add_ps(p2_d, acc_2);
  2724. acc_3 = _mm_add_ps(p3_d, acc_3);
  2725. }
  2726. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  2727. #elif defined(__riscv_v_intrinsic)
  2728. float sumf = 0.0;
  2729. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2730. for (int i = 0; i < nb; i++) {
  2731. // load elements
  2732. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2733. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2734. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2735. // mask and store lower part of x, and then upper part
  2736. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2737. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2738. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2739. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2740. // subtract offset
  2741. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  2742. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  2743. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2744. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2745. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2746. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2747. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2748. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2749. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2750. }
  2751. *s = sumf;
  2752. #else
  2753. // scalar
  2754. float sumf = 0.0;
  2755. for (int i = 0; i < nb; i++) {
  2756. int sumi = 0;
  2757. for (int j = 0; j < qk/2; ++j) {
  2758. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  2759. const int v1 = (x[i].qs[j] >> 4) - 8;
  2760. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2761. }
  2762. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2763. }
  2764. *s = sumf;
  2765. #endif
  2766. }
  2767. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2768. const int qk = QK8_1;
  2769. const int nb = n / qk;
  2770. assert(n % qk == 0);
  2771. const block_q4_1 * restrict x = vx;
  2772. const block_q8_1 * restrict y = vy;
  2773. // TODO: add WASM SIMD
  2774. #if defined(__ARM_NEON)
  2775. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2776. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2777. float summs = 0;
  2778. assert(nb % 2 == 0); // TODO: handle odd nb
  2779. for (int i = 0; i < nb; i += 2) {
  2780. const block_q4_1 * restrict x0 = &x[i + 0];
  2781. const block_q4_1 * restrict x1 = &x[i + 1];
  2782. const block_q8_1 * restrict y0 = &y[i + 0];
  2783. const block_q8_1 * restrict y1 = &y[i + 1];
  2784. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  2785. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2786. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2787. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2788. // 4-bit -> 8-bit
  2789. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2790. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2791. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2792. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2793. // load y
  2794. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2795. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2796. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2797. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2798. // dot product into int32x4_t
  2799. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  2800. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  2801. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2802. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2803. }
  2804. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  2805. #elif defined(__AVX2__) || defined(__AVX__)
  2806. // Initialize accumulator with zeros
  2807. __m256 acc = _mm256_setzero_ps();
  2808. float summs = 0;
  2809. // Main loop
  2810. for (int i = 0; i < nb; ++i) {
  2811. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  2812. const float d1 = y[i].d;
  2813. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2814. const __m256 d0v = _mm256_set1_ps( d0 );
  2815. const __m256 d1v = _mm256_set1_ps( d1 );
  2816. // Compute combined scales
  2817. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  2818. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  2819. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2820. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  2821. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  2822. // Accumulate d0*d1*x*y
  2823. #if defined(__AVX2__)
  2824. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  2825. #else
  2826. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  2827. #endif
  2828. }
  2829. *s = hsum_float_8(acc) + summs;
  2830. #elif defined(__riscv_v_intrinsic)
  2831. float sumf = 0.0;
  2832. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2833. for (int i = 0; i < nb; i++) {
  2834. // load elements
  2835. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2836. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2837. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2838. // mask and store lower part of x, and then upper part
  2839. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2840. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2841. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2842. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2843. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2844. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2845. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2846. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2847. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2848. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2849. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2850. }
  2851. *s = sumf;
  2852. #else
  2853. // scalar
  2854. float sumf = 0.0;
  2855. for (int i = 0; i < nb; i++) {
  2856. int sumi = 0;
  2857. for (int j = 0; j < qk/2; ++j) {
  2858. const int v0 = (x[i].qs[j] & 0x0F);
  2859. const int v1 = (x[i].qs[j] >> 4);
  2860. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2861. }
  2862. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2863. }
  2864. *s = sumf;
  2865. #endif
  2866. }
  2867. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2868. const int qk = QK8_0;
  2869. const int nb = n / qk;
  2870. assert(n % qk == 0);
  2871. assert(qk == QK5_0);
  2872. const block_q5_0 * restrict x = vx;
  2873. const block_q8_0 * restrict y = vy;
  2874. #if defined(__ARM_NEON)
  2875. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2876. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2877. uint32_t qh0;
  2878. uint32_t qh1;
  2879. uint64_t tmp0[4];
  2880. uint64_t tmp1[4];
  2881. assert(nb % 2 == 0); // TODO: handle odd nb
  2882. for (int i = 0; i < nb; i += 2) {
  2883. const block_q5_0 * restrict x0 = &x[i];
  2884. const block_q5_0 * restrict x1 = &x[i + 1];
  2885. const block_q8_0 * restrict y0 = &y[i];
  2886. const block_q8_0 * restrict y1 = &y[i + 1];
  2887. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2888. // extract the 5th bit via lookup table ((!b) << 4)
  2889. memcpy(&qh0, x0->qh, sizeof(qh0));
  2890. memcpy(&qh1, x1->qh, sizeof(qh1));
  2891. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  2892. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  2893. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  2894. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  2895. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  2896. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  2897. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  2898. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  2899. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2900. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2901. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2902. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2903. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2904. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2905. // 4-bit -> 8-bit
  2906. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2907. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2908. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2909. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2910. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2911. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  2912. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  2913. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  2914. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  2915. // load y
  2916. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2917. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2918. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2919. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2920. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2921. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2922. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2923. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2924. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2925. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2926. }
  2927. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2928. #elif defined(__wasm_simd128__)
  2929. v128_t sumv = wasm_f32x4_splat(0.0f);
  2930. uint32_t qh;
  2931. uint64_t tmp[4];
  2932. // TODO: check if unrolling this is better
  2933. for (int i = 0; i < nb; ++i) {
  2934. const block_q5_0 * restrict x0 = &x[i];
  2935. const block_q8_0 * restrict y0 = &y[i];
  2936. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2937. // extract the 5th bit
  2938. memcpy(&qh, x0->qh, sizeof(qh));
  2939. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  2940. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  2941. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  2942. tmp[3] = table_b2b_1[(qh >> 24) ];
  2943. const v128_t qhl = wasm_v128_load(tmp + 0);
  2944. const v128_t qhh = wasm_v128_load(tmp + 2);
  2945. const v128_t v0 = wasm_v128_load(x0->qs);
  2946. // 4-bit -> 8-bit
  2947. const v128_t v0l = wasm_v128_and (v0, m4b);
  2948. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2949. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2950. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  2951. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  2952. // load y
  2953. const v128_t v1l = wasm_v128_load(y0->qs);
  2954. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2955. // int8x16 -> int16x8
  2956. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2957. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2958. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2959. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2960. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2961. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2962. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2963. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2964. // dot product
  2965. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  2966. wasm_i32x4_add(
  2967. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2968. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2969. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2970. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2971. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  2972. }
  2973. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2974. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  2975. #elif defined(__AVX2__)
  2976. // Initialize accumulator with zeros
  2977. __m256 acc = _mm256_setzero_ps();
  2978. // Main loop
  2979. for (int i = 0; i < nb; i++) {
  2980. /* Compute combined scale for the block */
  2981. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2982. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2983. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2984. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  2985. bx = _mm256_or_si256(bx, bxhi);
  2986. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2987. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2988. /* Multiply q with scale and accumulate */
  2989. acc = _mm256_fmadd_ps(d, q, acc);
  2990. }
  2991. *s = hsum_float_8(acc);
  2992. #elif defined(__AVX__)
  2993. // Initialize accumulator with zeros
  2994. __m256 acc = _mm256_setzero_ps();
  2995. __m128i mask = _mm_set1_epi8((char)0xF0);
  2996. // Main loop
  2997. for (int i = 0; i < nb; i++) {
  2998. /* Compute combined scale for the block */
  2999. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3000. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3001. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3002. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3003. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3004. bxhil = _mm_andnot_si128(bxhil, mask);
  3005. bxhih = _mm_andnot_si128(bxhih, mask);
  3006. __m128i bxl = _mm256_castsi256_si128(bx);
  3007. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3008. bxl = _mm_or_si128(bxl, bxhil);
  3009. bxh = _mm_or_si128(bxh, bxhih);
  3010. bx = MM256_SET_M128I(bxh, bxl);
  3011. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3012. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3013. /* Multiply q with scale and accumulate */
  3014. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3015. }
  3016. *s = hsum_float_8(acc);
  3017. #elif defined(__riscv_v_intrinsic)
  3018. float sumf = 0.0;
  3019. uint32_t qh;
  3020. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3021. // These temporary registers are for masking and shift operations
  3022. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3023. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3024. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3025. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3026. for (int i = 0; i < nb; i++) {
  3027. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3028. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3029. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3030. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3031. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3032. // ((qh & (1u << (j + 16))) >> (j + 12));
  3033. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3034. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3035. // narrowing
  3036. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3037. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3038. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3039. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3040. // load
  3041. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3042. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3043. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3044. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3045. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3046. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3047. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3048. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3049. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3050. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3051. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3052. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3053. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3054. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3055. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3056. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3057. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3058. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3059. }
  3060. *s = sumf;
  3061. #else
  3062. // scalar
  3063. float sumf = 0.0;
  3064. for (int i = 0; i < nb; i++) {
  3065. uint32_t qh;
  3066. memcpy(&qh, x[i].qh, sizeof(qh));
  3067. int sumi = 0;
  3068. for (int j = 0; j < qk/2; ++j) {
  3069. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3070. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3071. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3072. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3073. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3074. }
  3075. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3076. }
  3077. *s = sumf;
  3078. #endif
  3079. }
  3080. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3081. const int qk = QK8_1;
  3082. const int nb = n / qk;
  3083. assert(n % qk == 0);
  3084. assert(qk == QK5_1);
  3085. const block_q5_1 * restrict x = vx;
  3086. const block_q8_1 * restrict y = vy;
  3087. #if defined(__ARM_NEON)
  3088. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3089. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3090. float summs0 = 0.0f;
  3091. float summs1 = 0.0f;
  3092. uint32_t qh0;
  3093. uint32_t qh1;
  3094. uint64_t tmp0[4];
  3095. uint64_t tmp1[4];
  3096. assert(nb % 2 == 0); // TODO: handle odd nb
  3097. for (int i = 0; i < nb; i += 2) {
  3098. const block_q5_1 * restrict x0 = &x[i];
  3099. const block_q5_1 * restrict x1 = &x[i + 1];
  3100. const block_q8_1 * restrict y0 = &y[i];
  3101. const block_q8_1 * restrict y1 = &y[i + 1];
  3102. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3103. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3104. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3105. // extract the 5th bit via lookup table ((b) << 4)
  3106. memcpy(&qh0, x0->qh, sizeof(qh0));
  3107. memcpy(&qh1, x1->qh, sizeof(qh1));
  3108. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3109. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3110. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3111. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3112. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3113. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3114. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3115. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3116. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3117. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3118. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3119. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3120. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3121. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3122. // 4-bit -> 8-bit
  3123. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3124. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3125. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3126. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3127. // add high bit
  3128. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3129. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3130. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3131. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3132. // load y
  3133. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3134. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3135. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3136. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3137. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3138. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3139. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3140. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3141. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3142. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3143. }
  3144. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3145. #elif defined(__wasm_simd128__)
  3146. v128_t sumv = wasm_f32x4_splat(0.0f);
  3147. float summs = 0.0f;
  3148. uint32_t qh;
  3149. uint64_t tmp[4];
  3150. // TODO: check if unrolling this is better
  3151. for (int i = 0; i < nb; ++i) {
  3152. const block_q5_1 * restrict x0 = &x[i];
  3153. const block_q8_1 * restrict y0 = &y[i];
  3154. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3155. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3156. // extract the 5th bit
  3157. memcpy(&qh, x0->qh, sizeof(qh));
  3158. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3159. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3160. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3161. tmp[3] = table_b2b_0[(qh >> 24) ];
  3162. const v128_t qhl = wasm_v128_load(tmp + 0);
  3163. const v128_t qhh = wasm_v128_load(tmp + 2);
  3164. const v128_t v0 = wasm_v128_load(x0->qs);
  3165. // 4-bit -> 8-bit
  3166. const v128_t v0l = wasm_v128_and (v0, m4b);
  3167. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3168. // add high bit
  3169. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3170. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3171. // load y
  3172. const v128_t v1l = wasm_v128_load(y0->qs);
  3173. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3174. // int8x16 -> int16x8
  3175. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3176. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3177. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3178. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3179. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3180. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3181. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3182. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3183. // dot product
  3184. sumv = wasm_f32x4_add(sumv,
  3185. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3186. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3187. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3188. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3189. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3190. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3191. }
  3192. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3193. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3194. #elif defined(__AVX2__)
  3195. // Initialize accumulator with zeros
  3196. __m256 acc = _mm256_setzero_ps();
  3197. float summs = 0.0f;
  3198. // Main loop
  3199. for (int i = 0; i < nb; i++) {
  3200. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3201. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3202. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3203. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3204. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3205. bx = _mm256_or_si256(bx, bxhi);
  3206. const __m256 dy = _mm256_set1_ps(y[i].d);
  3207. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3208. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3209. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3210. }
  3211. *s = hsum_float_8(acc) + summs;
  3212. #elif defined(__AVX__)
  3213. // Initialize accumulator with zeros
  3214. __m256 acc = _mm256_setzero_ps();
  3215. __m128i mask = _mm_set1_epi8(0x10);
  3216. float summs = 0.0f;
  3217. // Main loop
  3218. for (int i = 0; i < nb; i++) {
  3219. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3220. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3221. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3222. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3223. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3224. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3225. bxhil = _mm_and_si128(bxhil, mask);
  3226. bxhih = _mm_and_si128(bxhih, mask);
  3227. __m128i bxl = _mm256_castsi256_si128(bx);
  3228. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3229. bxl = _mm_or_si128(bxl, bxhil);
  3230. bxh = _mm_or_si128(bxh, bxhih);
  3231. bx = MM256_SET_M128I(bxh, bxl);
  3232. const __m256 dy = _mm256_set1_ps(y[i].d);
  3233. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3234. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3235. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3236. }
  3237. *s = hsum_float_8(acc) + summs;
  3238. #elif defined(__riscv_v_intrinsic)
  3239. float sumf = 0.0;
  3240. uint32_t qh;
  3241. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3242. // temporary registers for shift operations
  3243. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3244. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3245. for (int i = 0; i < nb; i++) {
  3246. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3247. // load qh
  3248. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3249. // ((qh >> (j + 0)) << 4) & 0x10;
  3250. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3251. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3252. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3253. // ((qh >> (j + 12)) ) & 0x10;
  3254. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3255. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3256. // narrowing
  3257. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3258. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3259. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3260. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3261. // load
  3262. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3263. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3264. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3265. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3266. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3267. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3268. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3269. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3270. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3271. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3272. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3273. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3274. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3275. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3276. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3277. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3278. }
  3279. *s = sumf;
  3280. #else
  3281. // scalar
  3282. float sumf = 0.0;
  3283. for (int i = 0; i < nb; i++) {
  3284. uint32_t qh;
  3285. memcpy(&qh, x[i].qh, sizeof(qh));
  3286. int sumi = 0;
  3287. for (int j = 0; j < qk/2; ++j) {
  3288. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3289. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3290. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3291. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3292. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3293. }
  3294. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3295. }
  3296. *s = sumf;
  3297. #endif
  3298. }
  3299. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3300. const int qk = QK8_0;
  3301. const int nb = n / qk;
  3302. assert(n % qk == 0);
  3303. const block_q8_0 * restrict x = vx;
  3304. const block_q8_0 * restrict y = vy;
  3305. #if defined(__ARM_NEON)
  3306. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3307. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3308. assert(nb % 2 == 0); // TODO: handle odd nb
  3309. for (int i = 0; i < nb; i += 2) {
  3310. const block_q8_0 * restrict x0 = &x[i + 0];
  3311. const block_q8_0 * restrict x1 = &x[i + 1];
  3312. const block_q8_0 * restrict y0 = &y[i + 0];
  3313. const block_q8_0 * restrict y1 = &y[i + 1];
  3314. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3315. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3316. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3317. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3318. // load y
  3319. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3320. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3321. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3322. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3323. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3324. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3325. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3326. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3327. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3328. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3329. }
  3330. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3331. #elif defined(__AVX2__) || defined(__AVX__)
  3332. // Initialize accumulator with zeros
  3333. __m256 acc = _mm256_setzero_ps();
  3334. // Main loop
  3335. for (int i = 0; i < nb; ++i) {
  3336. // Compute combined scale for the block
  3337. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3338. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3339. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3340. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3341. // Multiply q with scale and accumulate
  3342. #if defined(__AVX2__)
  3343. acc = _mm256_fmadd_ps( d, q, acc );
  3344. #else
  3345. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3346. #endif
  3347. }
  3348. *s = hsum_float_8(acc);
  3349. #elif defined(__riscv_v_intrinsic)
  3350. float sumf = 0.0;
  3351. size_t vl = __riscv_vsetvl_e8m1(qk);
  3352. for (int i = 0; i < nb; i++) {
  3353. // load elements
  3354. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3355. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3356. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3357. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3358. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3359. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3360. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3361. }
  3362. *s = sumf;
  3363. #else
  3364. // scalar
  3365. float sumf = 0.0;
  3366. for (int i = 0; i < nb; i++) {
  3367. int sumi = 0;
  3368. for (int j = 0; j < qk; j++) {
  3369. sumi += x[i].qs[j]*y[i].qs[j];
  3370. }
  3371. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3372. }
  3373. *s = sumf;
  3374. #endif
  3375. }
  3376. #if QK_K == 256
  3377. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3378. const block_q2_K * restrict x = vx;
  3379. const block_q8_K * restrict y = vy;
  3380. const int nb = n / QK_K;
  3381. #ifdef __ARM_NEON
  3382. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3383. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3384. const int32x4_t vzero = vdupq_n_s32(0);
  3385. ggml_int8x16x2_t q2bytes;
  3386. uint8_t aux[16];
  3387. float sum = 0;
  3388. for (int i = 0; i < nb; ++i) {
  3389. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3390. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3391. const uint8_t * restrict q2 = x[i].qs;
  3392. const int8_t * restrict q8 = y[i].qs;
  3393. const uint8_t * restrict sc = x[i].scales;
  3394. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3395. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3396. vst1q_u8(aux, scales);
  3397. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3398. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3399. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3400. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3401. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3402. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3403. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3404. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3405. int isum = 0;
  3406. int is = 0;
  3407. // We use this macro instead of a function call because for some reason
  3408. // the code runs 2-3% slower, even if the function is declared inline
  3409. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3410. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3411. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3412. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3413. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3414. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3415. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3416. MULTIPLY_ACCUM_WITH_SCALE((index));
  3417. for (int j = 0; j < QK_K/128; ++j) {
  3418. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3419. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3420. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3421. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3422. MULTIPLY_ACCUM_WITH_SCALE(0);
  3423. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3424. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3425. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3426. is += 8;
  3427. }
  3428. sum += d * isum;
  3429. }
  3430. *s = sum;
  3431. #elif defined __AVX2__
  3432. const __m256i m3 = _mm256_set1_epi8(3);
  3433. const __m128i m4 = _mm_set1_epi8(0xF);
  3434. __m256 acc = _mm256_setzero_ps();
  3435. for (int i = 0; i < nb; ++i) {
  3436. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3437. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3438. const uint8_t * restrict q2 = x[i].qs;
  3439. const int8_t * restrict q8 = y[i].qs;
  3440. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3441. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3442. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3443. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3444. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3445. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3446. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3447. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3448. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3449. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3450. __m256i sumi = _mm256_setzero_si256();
  3451. for (int j = 0; j < QK_K/128; ++j) {
  3452. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  3453. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3454. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3455. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3456. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3457. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  3458. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  3459. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  3460. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  3461. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3462. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3463. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  3464. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  3465. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  3466. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  3467. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  3468. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  3469. p0 = _mm256_add_epi32(p0, p1);
  3470. p2 = _mm256_add_epi32(p2, p3);
  3471. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  3472. }
  3473. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3474. }
  3475. *s = hsum_float_8(acc);
  3476. #elif defined __AVX__
  3477. const __m128i m3 = _mm_set1_epi8(0x3);
  3478. const __m128i m4 = _mm_set1_epi8(0xF);
  3479. const __m128i m2 = _mm_set1_epi8(0x2);
  3480. __m256 acc = _mm256_setzero_ps();
  3481. for (int i = 0; i < nb; ++i) {
  3482. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3483. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3484. const uint8_t * restrict q2 = x[i].qs;
  3485. const int8_t * restrict q8 = y[i].qs;
  3486. // load mins and scales from block_q2_K.scales[QK_K/16]
  3487. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3488. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  3489. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3490. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  3491. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  3492. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  3493. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  3494. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  3495. // sumf += -dmin * summs in 32bits*8
  3496. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  3497. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  3498. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  3499. const __m128i scales[2] = { scales_0, scales_1 };
  3500. __m128i sumi_0 = _mm_setzero_si128();
  3501. __m128i sumi_1 = _mm_setzero_si128();
  3502. for (int j = 0; j < QK_K/128; ++j) {
  3503. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  3504. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3505. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3506. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3507. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3508. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3509. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3510. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3511. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3512. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  3513. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3514. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3515. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3516. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3517. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3518. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3519. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  3520. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3521. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3522. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3523. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  3524. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  3525. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  3526. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  3527. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  3528. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  3529. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  3530. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  3531. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  3532. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  3533. __m128i shuffle = _mm_set1_epi16(0x0100);
  3534. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  3535. shuffle = _mm_add_epi16(shuffle, m2);
  3536. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  3537. shuffle = _mm_add_epi16(shuffle, m2);
  3538. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  3539. shuffle = _mm_add_epi16(shuffle, m2);
  3540. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  3541. shuffle = _mm_add_epi16(shuffle, m2);
  3542. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  3543. shuffle = _mm_add_epi16(shuffle, m2);
  3544. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  3545. shuffle = _mm_add_epi16(shuffle, m2);
  3546. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  3547. shuffle = _mm_add_epi16(shuffle, m2);
  3548. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  3549. p0 = _mm_add_epi32(p0, p1);
  3550. p2 = _mm_add_epi32(p2, p3);
  3551. p4 = _mm_add_epi32(p4, p5);
  3552. p6 = _mm_add_epi32(p6, p7);
  3553. // isum in 32bits*4*2
  3554. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  3555. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  3556. }
  3557. // sumf += dall * isum - dmin * summs in 32bits
  3558. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3559. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  3560. }
  3561. *s = hsum_float_8(acc);
  3562. #elif defined __riscv_v_intrinsic
  3563. float sumf = 0;
  3564. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3565. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  3566. for (int i = 0; i < nb; ++i) {
  3567. const uint8_t * q2 = x[i].qs;
  3568. const int8_t * q8 = y[i].qs;
  3569. const uint8_t * sc = x[i].scales;
  3570. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3571. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3572. size_t vl = 16;
  3573. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  3574. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  3575. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  3576. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  3577. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  3578. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  3579. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  3580. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3581. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  3582. vl = 32;
  3583. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3584. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  3585. uint8_t is=0;
  3586. int isum=0;
  3587. for (int j = 0; j < QK_K/128; ++j) {
  3588. // load Q2
  3589. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  3590. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  3591. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  3592. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  3593. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  3594. // duplicate scale elements for product
  3595. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  3596. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  3597. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  3598. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  3599. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  3600. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  3601. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  3602. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  3603. // load Q8
  3604. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  3605. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  3606. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  3607. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  3608. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  3609. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  3610. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  3611. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  3612. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  3613. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  3614. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  3615. q2+=32; q8+=128; is=8;
  3616. }
  3617. sumf += dall * isum;
  3618. }
  3619. *s = sumf;
  3620. #else
  3621. float sumf = 0;
  3622. for (int i = 0; i < nb; ++i) {
  3623. const uint8_t * q2 = x[i].qs;
  3624. const int8_t * q8 = y[i].qs;
  3625. const uint8_t * sc = x[i].scales;
  3626. int summs = 0;
  3627. for (int j = 0; j < 16; ++j) {
  3628. summs += y[i].bsums[j] * (sc[j] >> 4);
  3629. }
  3630. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3631. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3632. int isum = 0;
  3633. int is = 0;
  3634. int d;
  3635. for (int k = 0; k < QK_K/128; ++k) {
  3636. int shift = 0;
  3637. for (int j = 0; j < 4; ++j) {
  3638. d = sc[is++] & 0xF;
  3639. int isuml = 0;
  3640. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3641. isum += d * isuml;
  3642. d = sc[is++] & 0xF;
  3643. isuml = 0;
  3644. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3645. isum += d * isuml;
  3646. shift += 2;
  3647. q8 += 32;
  3648. }
  3649. q2 += 32;
  3650. }
  3651. sumf += dall * isum - dmin * summs;
  3652. }
  3653. *s = sumf;
  3654. #endif
  3655. }
  3656. #else
  3657. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3658. const block_q2_K * restrict x = vx;
  3659. const block_q8_K * restrict y = vy;
  3660. const int nb = n / QK_K;
  3661. #ifdef __ARM_NEON
  3662. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3663. const int32x4_t vzero = vdupq_n_s32(0);
  3664. ggml_int8x16x4_t q2bytes;
  3665. uint32_t aux32[2];
  3666. const uint8_t * scales = (const uint8_t *)aux32;
  3667. float sum = 0;
  3668. for (int i = 0; i < nb; ++i) {
  3669. const float d = y[i].d * (float)x[i].d;
  3670. const float dmin = -y[i].d * (float)x[i].dmin;
  3671. const uint8_t * restrict q2 = x[i].qs;
  3672. const int8_t * restrict q8 = y[i].qs;
  3673. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3674. aux32[0] = sc[0] & 0x0f0f0f0f;
  3675. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3676. 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]);
  3677. int isum1 = 0, isum2 = 0;
  3678. const uint8x16_t q2bits = vld1q_u8(q2);
  3679. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  3680. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  3681. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  3682. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  3683. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  3684. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  3685. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  3686. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  3687. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  3688. sum += d * (isum1 + isum2);
  3689. }
  3690. *s = sum;
  3691. #elif defined __AVX2__
  3692. const __m256i m3 = _mm256_set1_epi8(3);
  3693. __m256 acc = _mm256_setzero_ps();
  3694. uint32_t ud, um;
  3695. const uint8_t * restrict db = (const uint8_t *)&ud;
  3696. const uint8_t * restrict mb = (const uint8_t *)&um;
  3697. float summs = 0;
  3698. // TODO: optimize this
  3699. for (int i = 0; i < nb; ++i) {
  3700. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3701. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3702. const uint8_t * restrict q2 = x[i].qs;
  3703. const int8_t * restrict q8 = y[i].qs;
  3704. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3705. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3706. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3707. 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];
  3708. summs += dmin * smin;
  3709. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3710. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  3711. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  3712. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3713. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3714. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3715. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3716. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  3717. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  3718. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  3719. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  3720. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  3721. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  3722. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  3723. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  3724. }
  3725. *s = hsum_float_8(acc) + summs;
  3726. #elif defined __AVX__
  3727. const __m128i m3 = _mm_set1_epi8(3);
  3728. __m256 acc = _mm256_setzero_ps();
  3729. uint32_t ud, um;
  3730. const uint8_t * restrict db = (const uint8_t *)&ud;
  3731. const uint8_t * restrict mb = (const uint8_t *)&um;
  3732. float summs = 0;
  3733. // TODO: optimize this
  3734. for (int i = 0; i < nb; ++i) {
  3735. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3736. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3737. const uint8_t * restrict q2 = x[i].qs;
  3738. const int8_t * restrict q8 = y[i].qs;
  3739. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3740. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3741. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3742. 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];
  3743. summs += dmin * smin;
  3744. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3745. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3746. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3747. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3748. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3749. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3750. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3751. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  3752. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  3753. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  3754. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  3755. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  3756. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  3757. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  3758. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  3759. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  3760. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  3761. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  3762. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  3763. }
  3764. *s = hsum_float_8(acc) + summs;
  3765. #elif defined __riscv_v_intrinsic
  3766. uint32_t aux32[2];
  3767. const uint8_t * scales = (const uint8_t *)aux32;
  3768. float sumf = 0;
  3769. for (int i = 0; i < nb; ++i) {
  3770. const float d = y[i].d * (float)x[i].d;
  3771. const float dmin = -y[i].d * (float)x[i].dmin;
  3772. const uint8_t * restrict q2 = x[i].qs;
  3773. const int8_t * restrict q8 = y[i].qs;
  3774. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3775. aux32[0] = sc[0] & 0x0f0f0f0f;
  3776. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3777. 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]);
  3778. int isum1 = 0;
  3779. int isum2 = 0;
  3780. size_t vl = 16;
  3781. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  3782. // load Q2
  3783. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  3784. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  3785. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  3786. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  3787. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  3788. // load Q8, and take product with Q2
  3789. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3790. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3791. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3792. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3793. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  3794. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  3795. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  3796. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  3797. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  3798. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  3799. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  3800. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  3801. sumf += d * (isum1 + isum2);
  3802. }
  3803. *s = sumf;
  3804. #else
  3805. float sumf = 0;
  3806. int isum[4];
  3807. for (int i = 0; i < nb; ++i) {
  3808. const uint8_t * q2 = x[i].qs;
  3809. const int8_t * q8 = y[i].qs;
  3810. const uint8_t * sc = x[i].scales;
  3811. int summs = 0;
  3812. for (int j = 0; j < QK_K/16; ++j) {
  3813. summs += y[i].bsums[j] * (sc[j] >> 4);
  3814. }
  3815. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3816. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3817. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  3818. for (int l = 0; l < 16; ++l) {
  3819. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  3820. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  3821. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  3822. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  3823. }
  3824. for (int l = 0; l < 4; ++l) {
  3825. isum[l] *= (sc[l] & 0xF);
  3826. }
  3827. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  3828. }
  3829. *s = sumf;
  3830. #endif
  3831. }
  3832. #endif
  3833. #if QK_K == 256
  3834. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3835. assert(n % QK_K == 0);
  3836. const uint32_t kmask1 = 0x03030303;
  3837. const uint32_t kmask2 = 0x0f0f0f0f;
  3838. const block_q3_K * restrict x = vx;
  3839. const block_q8_K * restrict y = vy;
  3840. const int nb = n / QK_K;
  3841. #ifdef __ARM_NEON
  3842. uint32_t aux[3];
  3843. uint32_t utmp[4];
  3844. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3845. const int32x4_t vzero = vdupq_n_s32(0);
  3846. const uint8x16_t m0 = vdupq_n_u8(1);
  3847. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  3848. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  3849. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  3850. const int8_t m32 = 32;
  3851. ggml_int8x16x4_t q3bytes;
  3852. float sum = 0;
  3853. for (int i = 0; i < nb; ++i) {
  3854. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3855. const uint8_t * restrict q3 = x[i].qs;
  3856. const uint8_t * restrict qh = x[i].hmask;
  3857. const int8_t * restrict q8 = y[i].qs;
  3858. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  3859. ggml_uint8x16x4_t q3h;
  3860. int32_t isum = 0;
  3861. // Set up scales
  3862. memcpy(aux, x[i].scales, 12);
  3863. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3864. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3865. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3866. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3867. int8_t * scale = (int8_t *)utmp;
  3868. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  3869. for (int j = 0; j < QK_K/128; ++j) {
  3870. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  3871. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3872. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3873. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  3874. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  3875. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  3876. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  3877. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3878. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3879. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3880. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3881. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  3882. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  3883. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  3884. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  3885. scale += 4;
  3886. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  3887. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  3888. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  3889. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  3890. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3891. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3892. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3893. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3894. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  3895. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  3896. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  3897. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  3898. scale += 4;
  3899. if (j == 0) {
  3900. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  3901. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  3902. }
  3903. }
  3904. sum += d * isum;
  3905. }
  3906. *s = sum;
  3907. #elif defined __AVX2__
  3908. const __m256i m3 = _mm256_set1_epi8(3);
  3909. const __m256i mone = _mm256_set1_epi8(1);
  3910. const __m128i m32 = _mm_set1_epi8(32);
  3911. __m256 acc = _mm256_setzero_ps();
  3912. uint32_t aux[3];
  3913. for (int i = 0; i < nb; ++i) {
  3914. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3915. const uint8_t * restrict q3 = x[i].qs;
  3916. const int8_t * restrict q8 = y[i].qs;
  3917. // Set up scales
  3918. memcpy(aux, x[i].scales, 12);
  3919. __m128i scales128 = _mm_set_epi32(
  3920. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3921. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3922. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3923. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3924. scales128 = _mm_sub_epi8(scales128, m32);
  3925. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  3926. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3927. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3928. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3929. // high bit
  3930. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  3931. // integer accumulator
  3932. __m256i sumi = _mm256_setzero_si256();
  3933. int bit = 0;
  3934. int is = 0;
  3935. for (int j = 0; j < QK_K/128; ++j) {
  3936. // load low 2 bits
  3937. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  3938. // prepare low and high bits
  3939. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  3940. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3941. ++bit;
  3942. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  3943. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3944. ++bit;
  3945. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  3946. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3947. ++bit;
  3948. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  3949. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3950. ++bit;
  3951. // load Q8 quants
  3952. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3953. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3954. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3955. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3956. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3957. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3958. // and 2 if the high bit was set)
  3959. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3960. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3961. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  3962. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  3963. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3964. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3965. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  3966. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  3967. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3968. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3969. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  3970. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  3971. // multiply with scales
  3972. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  3973. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  3974. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  3975. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  3976. // accumulate
  3977. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3978. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  3979. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  3980. }
  3981. // multiply with block scale and accumulate
  3982. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3983. }
  3984. *s = hsum_float_8(acc);
  3985. #elif defined __AVX__
  3986. const __m128i m3 = _mm_set1_epi8(3);
  3987. const __m128i mone = _mm_set1_epi8(1);
  3988. const __m128i m32 = _mm_set1_epi8(32);
  3989. const __m128i m2 = _mm_set1_epi8(2);
  3990. __m256 acc = _mm256_setzero_ps();
  3991. const uint32_t *aux;
  3992. for (int i = 0; i < nb; ++i) {
  3993. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3994. const uint8_t * restrict q3 = x[i].qs;
  3995. const int8_t * restrict q8 = y[i].qs;
  3996. // Set up scales
  3997. aux = (const uint32_t *)x[i].scales;
  3998. __m128i scales128 = _mm_set_epi32(
  3999. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4000. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4001. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4002. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4003. scales128 = _mm_sub_epi8(scales128, m32);
  4004. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4005. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4006. const __m128i scales[2] = { scales_0, scales_1 };
  4007. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4008. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4009. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4010. // integer accumulator
  4011. __m128i sumi_0 = _mm_setzero_si128();
  4012. __m128i sumi_1 = _mm_setzero_si128();
  4013. for (int j = 0; j < QK_K/128; ++j) {
  4014. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4015. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4016. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4017. // prepare low and high bits
  4018. const int bit = j << 2;
  4019. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4020. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4021. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4022. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4023. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4024. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4025. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4026. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4027. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4028. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4029. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4030. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4031. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4032. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4033. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4034. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4035. // load Q8 quants from block_q8_K.qs[QK_K]
  4036. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4037. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4038. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4039. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4040. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4041. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4042. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4043. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4044. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4045. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4046. // and 2 if the high bit was set)
  4047. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4048. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4049. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4050. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4051. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4052. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4053. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4054. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4055. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4056. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4057. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4058. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4059. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4060. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4061. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4062. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4063. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4064. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4065. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4066. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4067. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4068. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4069. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4070. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4071. // multiply with scales
  4072. __m128i shuffle = _mm_set1_epi16(0x0100);
  4073. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4074. shuffle = _mm_add_epi16(shuffle, m2);
  4075. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4076. shuffle = _mm_add_epi16(shuffle, m2);
  4077. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4078. shuffle = _mm_add_epi16(shuffle, m2);
  4079. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4080. shuffle = _mm_add_epi16(shuffle, m2);
  4081. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4082. shuffle = _mm_add_epi16(shuffle, m2);
  4083. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4084. shuffle = _mm_add_epi16(shuffle, m2);
  4085. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4086. shuffle = _mm_add_epi16(shuffle, m2);
  4087. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4088. // accumulate
  4089. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4090. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4091. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4092. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4093. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4094. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4095. }
  4096. // multiply with block scale and accumulate
  4097. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4098. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4099. }
  4100. *s = hsum_float_8(acc);
  4101. #elif defined __riscv_v_intrinsic
  4102. uint32_t aux[3];
  4103. uint32_t utmp[4];
  4104. float sumf = 0;
  4105. for (int i = 0; i < nb; ++i) {
  4106. const uint8_t * restrict q3 = x[i].qs;
  4107. const uint8_t * restrict qh = x[i].hmask;
  4108. const int8_t * restrict q8 = y[i].qs;
  4109. memcpy(aux, x[i].scales, 12);
  4110. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4111. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4112. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4113. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4114. int8_t * scale = (int8_t *)utmp;
  4115. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4116. size_t vl = 32;
  4117. uint8_t m = 1;
  4118. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4119. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4120. int sum_t = 0;
  4121. for (int j = 0; j < QK_K; j += 128) {
  4122. vl = 32;
  4123. // load Q3
  4124. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4125. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4126. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4127. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4128. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4129. // compute mask for subtraction
  4130. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4131. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4132. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4133. m <<= 1;
  4134. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4135. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4136. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4137. m <<= 1;
  4138. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4139. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4140. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4141. m <<= 1;
  4142. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4143. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4144. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4145. m <<= 1;
  4146. // load Q8 and take product with Q3
  4147. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4148. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4149. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4150. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4151. vl = 16;
  4152. // retrieve lane to multiply with scale
  4153. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4154. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4155. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4156. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4157. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4158. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4159. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4160. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4161. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4162. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4163. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4164. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4165. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4166. q3 += 32; q8 += 128; scale += 8;
  4167. }
  4168. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4169. sumf += d*sum_t;
  4170. }
  4171. *s = sumf;
  4172. #else
  4173. // scalar version
  4174. // This function is written like this so the compiler can manage to vectorize most of it
  4175. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4176. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4177. // The ideal situation would be if we could just write the code once, and the compiler would
  4178. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4179. // write vectorized versions for AVX, ARM_NEON, etc.
  4180. int8_t aux8[QK_K];
  4181. int16_t aux16[8];
  4182. float sums [8];
  4183. int32_t aux32[8];
  4184. memset(sums, 0, 8*sizeof(float));
  4185. uint32_t auxs[4];
  4186. const int8_t * scales = (const int8_t*)auxs;
  4187. float sumf = 0;
  4188. for (int i = 0; i < nb; ++i) {
  4189. const uint8_t * restrict q3 = x[i].qs;
  4190. const uint8_t * restrict hm = x[i].hmask;
  4191. const int8_t * restrict q8 = y[i].qs;
  4192. memset(aux32, 0, 8*sizeof(int32_t));
  4193. int8_t * restrict a = aux8;
  4194. uint8_t m = 1;
  4195. for (int j = 0; j < QK_K; j += 128) {
  4196. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4197. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4198. a += 32; m <<= 1;
  4199. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4200. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4201. a += 32; m <<= 1;
  4202. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4203. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4204. a += 32; m <<= 1;
  4205. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4206. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4207. a += 32; m <<= 1;
  4208. q3 += 32;
  4209. }
  4210. a = aux8;
  4211. memcpy(auxs, x[i].scales, 12);
  4212. uint32_t tmp = auxs[2];
  4213. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4214. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4215. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4216. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4217. for (int j = 0; j < QK_K/16; ++j) {
  4218. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4219. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4220. q8 += 8; a += 8;
  4221. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4222. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4223. q8 += 8; a += 8;
  4224. }
  4225. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4226. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4227. }
  4228. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4229. *s = sumf;
  4230. #endif
  4231. }
  4232. #else
  4233. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4234. assert(n % QK_K == 0);
  4235. const block_q3_K * restrict x = vx;
  4236. const block_q8_K * restrict y = vy;
  4237. const int nb = n / QK_K;
  4238. #ifdef __ARM_NEON
  4239. const int32x4_t vzero = vdupq_n_s32(0);
  4240. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4241. const uint8x16_t mh = vdupq_n_u8(4);
  4242. ggml_int8x16x4_t q3bytes;
  4243. uint16_t aux16[2];
  4244. int8_t * scales = (int8_t *)aux16;
  4245. float sum = 0;
  4246. for (int i = 0; i < nb; ++i) {
  4247. ggml_uint8x16x4_t q3h;
  4248. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4249. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4250. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4251. const uint16_t a = *(const uint16_t *)x[i].scales;
  4252. aux16[0] = a & 0x0f0f;
  4253. aux16[1] = (a >> 4) & 0x0f0f;
  4254. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4255. 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]);
  4256. const float d = y[i].d * (float)x[i].d;
  4257. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4258. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4259. q3h.val[1] = vandq_u8(mh, htmp);
  4260. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4261. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4262. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4263. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4264. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4265. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4266. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4267. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4268. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4269. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4270. sum += d * isum;
  4271. }
  4272. *s = sum;
  4273. #elif defined __AVX2__
  4274. const __m256i m3 = _mm256_set1_epi8(3);
  4275. const __m256i m1 = _mm256_set1_epi8(1);
  4276. __m256 acc = _mm256_setzero_ps();
  4277. uint64_t aux64;
  4278. uint16_t aux16[2];
  4279. const int8_t * aux8 = (const int8_t *)aux16;
  4280. for (int i = 0; i < nb; ++i) {
  4281. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4282. const uint8_t * restrict q3 = x[i].qs;
  4283. const int8_t * restrict q8 = y[i].qs;
  4284. const uint16_t a = *(const uint16_t *)x[i].scales;
  4285. aux16[0] = a & 0x0f0f;
  4286. aux16[1] = (a >> 4) & 0x0f0f;
  4287. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4288. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4289. memcpy(&aux64, x[i].hmask, 8);
  4290. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4291. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4292. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4293. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4294. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4295. // load low 2 bits
  4296. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4297. // prepare low and high bits
  4298. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4299. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4300. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4301. // load Q8 quants
  4302. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4303. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4304. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4305. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4306. // and 2 if the high bit was set)
  4307. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4308. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4309. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4310. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4311. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4312. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4313. // multiply with scales
  4314. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4315. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4316. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4317. // multiply with block scale and accumulate
  4318. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4319. }
  4320. *s = hsum_float_8(acc);
  4321. #elif defined __AVX__
  4322. const __m128i m3 = _mm_set1_epi8(3);
  4323. const __m128i m1 = _mm_set1_epi8(1);
  4324. __m256 acc = _mm256_setzero_ps();
  4325. uint64_t aux64;
  4326. uint16_t aux16[2];
  4327. const int8_t * aux8 = (const int8_t *)aux16;
  4328. for (int i = 0; i < nb; ++i) {
  4329. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4330. const uint8_t * restrict q3 = x[i].qs;
  4331. const int8_t * restrict q8 = y[i].qs;
  4332. const uint16_t a = *(const uint16_t *)x[i].scales;
  4333. aux16[0] = a & 0x0f0f;
  4334. aux16[1] = (a >> 4) & 0x0f0f;
  4335. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4336. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4337. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4338. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4339. memcpy(&aux64, x[i].hmask, 8);
  4340. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4341. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4342. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4343. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4344. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4345. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4346. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4347. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4348. // load low 2 bits
  4349. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4350. // prepare low and high bits
  4351. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4352. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4353. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4354. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4355. // load Q8 quants
  4356. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4357. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4358. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4359. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4360. // and 2 if the high bit was set)
  4361. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4362. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4363. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4364. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4365. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4366. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4367. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4368. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4369. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4370. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4371. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4372. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4373. // multiply with scales
  4374. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4375. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4376. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4377. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4378. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4379. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4380. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4381. // multiply with block scale and accumulate
  4382. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4383. }
  4384. *s = hsum_float_8(acc);
  4385. #elif defined __riscv_v_intrinsic
  4386. uint16_t aux16[2];
  4387. int8_t * scales = (int8_t *)aux16;
  4388. float sumf = 0;
  4389. for (int i = 0; i < nb; ++i) {
  4390. const uint8_t * restrict q3 = x[i].qs;
  4391. const int8_t * restrict q8 = y[i].qs;
  4392. const uint16_t a = *(const uint16_t *)x[i].scales;
  4393. aux16[0] = a & 0x0f0f;
  4394. aux16[1] = (a >> 4) & 0x0f0f;
  4395. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4396. 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]);
  4397. const float d = y[i].d * (float)x[i].d;
  4398. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4399. // load qh
  4400. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4401. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4402. size_t vl = 16;
  4403. // extend and combine both qh_x1 and qh_x2
  4404. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4405. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4406. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4407. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4408. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4409. // load Q3
  4410. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4411. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4412. 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);
  4413. 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);
  4414. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4415. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4416. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4417. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4418. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4419. // load Q8 and take product with Q3
  4420. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4421. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4422. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4423. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4424. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4425. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4426. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4427. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4428. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4429. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4430. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4431. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4432. sumf += d * isum;
  4433. }
  4434. *s = sumf;
  4435. #else
  4436. int8_t aux8[QK_K];
  4437. int16_t aux16[8];
  4438. float sums [8];
  4439. int32_t aux32[8];
  4440. int32_t scales[4];
  4441. memset(sums, 0, 8*sizeof(float));
  4442. float sumf = 0;
  4443. for (int i = 0; i < nb; ++i) {
  4444. const uint8_t * restrict q3 = x[i].qs;
  4445. const uint8_t * restrict hm = x[i].hmask;
  4446. const int8_t * restrict q8 = y[i].qs;
  4447. int8_t * restrict a = aux8;
  4448. for (int l = 0; l < 8; ++l) {
  4449. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  4450. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  4451. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  4452. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  4453. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  4454. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  4455. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  4456. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  4457. }
  4458. scales[0] = (x[i].scales[0] & 0xF) - 8;
  4459. scales[1] = (x[i].scales[0] >> 4) - 8;
  4460. scales[2] = (x[i].scales[1] & 0xF) - 8;
  4461. scales[3] = (x[i].scales[1] >> 4) - 8;
  4462. memset(aux32, 0, 8*sizeof(int32_t));
  4463. for (int j = 0; j < QK_K/16; ++j) {
  4464. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4465. q8 += 8; a += 8;
  4466. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  4467. q8 += 8; a += 8;
  4468. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  4469. }
  4470. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4471. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4472. }
  4473. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4474. *s = sumf;
  4475. #endif
  4476. }
  4477. #endif
  4478. #if QK_K == 256
  4479. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4480. assert(n % QK_K == 0);
  4481. const block_q4_K * restrict x = vx;
  4482. const block_q8_K * restrict y = vy;
  4483. const int nb = n / QK_K;
  4484. static const uint32_t kmask1 = 0x3f3f3f3f;
  4485. static const uint32_t kmask2 = 0x0f0f0f0f;
  4486. static const uint32_t kmask3 = 0x03030303;
  4487. uint32_t utmp[4];
  4488. #ifdef __ARM_NEON
  4489. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4490. const int32x4_t mzero = vdupq_n_s32(0);
  4491. ggml_int8x16x2_t q4bytes;
  4492. ggml_int8x16x2_t q8bytes;
  4493. float sumf = 0;
  4494. for (int i = 0; i < nb; ++i) {
  4495. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4496. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4497. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4498. memcpy(utmp, x[i].scales, 12);
  4499. uint32x2_t mins8 = { 0 };
  4500. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  4501. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  4502. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4503. utmp[0] &= kmask1;
  4504. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  4505. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4506. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4507. sumf -= dmin * vaddvq_s32(prod);
  4508. const uint8_t * scales = (const uint8_t *)utmp;
  4509. const uint8_t * restrict q4 = x[i].qs;
  4510. const int8_t * restrict q8 = y[i].qs;
  4511. int32_t sumi1 = 0;
  4512. int32_t sumi2 = 0;
  4513. for (int j = 0; j < QK_K/64; ++j) {
  4514. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  4515. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4516. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4517. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4518. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4519. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  4520. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4521. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4522. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4523. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4524. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  4525. }
  4526. sumf += d * (sumi1 + sumi2);
  4527. }
  4528. *s = sumf;
  4529. #elif defined __AVX2__
  4530. const __m256i m4 = _mm256_set1_epi8(0xF);
  4531. __m256 acc = _mm256_setzero_ps();
  4532. __m128 acc_m = _mm_setzero_ps();
  4533. for (int i = 0; i < nb; ++i) {
  4534. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4535. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4536. memcpy(utmp, x[i].scales, 12);
  4537. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4538. const uint32_t uaux = utmp[1] & kmask1;
  4539. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4540. utmp[2] = uaux;
  4541. utmp[0] &= kmask1;
  4542. const uint8_t * restrict q4 = x[i].qs;
  4543. const int8_t * restrict q8 = y[i].qs;
  4544. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4545. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4546. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4547. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4548. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  4549. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4550. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4551. __m256i sumi = _mm256_setzero_si256();
  4552. for (int j = 0; j < QK_K/64; ++j) {
  4553. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4554. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4555. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  4556. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4557. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4558. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4559. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4560. p16l = _mm256_madd_epi16(scale_l, p16l);
  4561. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4562. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4563. p16h = _mm256_madd_epi16(scale_h, p16h);
  4564. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  4565. sumi = _mm256_add_epi32(sumi, sumj);
  4566. }
  4567. __m256 vd = _mm256_set1_ps(d);
  4568. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4569. }
  4570. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4571. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4572. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4573. #elif defined __AVX__
  4574. const __m128i m4 = _mm_set1_epi8(0xF);
  4575. const __m128i m2 = _mm_set1_epi8(0x2);
  4576. __m256 acc = _mm256_setzero_ps();
  4577. __m128 acc_m = _mm_setzero_ps();
  4578. for (int i = 0; i < nb; ++i) {
  4579. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4580. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4581. const uint8_t * restrict q4 = x[i].qs;
  4582. const int8_t * restrict q8 = y[i].qs;
  4583. memcpy(utmp, x[i].scales, 12);
  4584. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4585. const uint32_t uaux = utmp[1] & kmask1;
  4586. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4587. utmp[2] = uaux;
  4588. utmp[0] &= kmask1;
  4589. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4590. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4591. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4592. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4593. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4594. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4595. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4596. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  4597. __m128i sumi_0 = _mm_setzero_si128();
  4598. __m128i sumi_1 = _mm_setzero_si128();
  4599. __m128i shuffle = _mm_set1_epi16(0x0100);
  4600. for (int j = 0; j < QK_K/64; ++j) {
  4601. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  4602. shuffle = _mm_add_epi16(shuffle, m2);
  4603. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  4604. shuffle = _mm_add_epi16(shuffle, m2);
  4605. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4606. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  4607. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4608. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4609. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  4610. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4611. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4612. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  4613. p16l = _mm_madd_epi16(scale_l, p16l);
  4614. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  4615. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4616. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  4617. p16l = _mm_madd_epi16(scale_l, p16l);
  4618. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  4619. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4620. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  4621. p16h = _mm_madd_epi16(scale_h, p16h);
  4622. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  4623. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4624. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  4625. p16h = _mm_madd_epi16(scale_h, p16h);
  4626. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  4627. }
  4628. __m256 vd = _mm256_set1_ps(d);
  4629. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4630. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4631. }
  4632. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4633. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4634. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4635. #elif defined __riscv_v_intrinsic
  4636. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4637. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4638. float sumf = 0;
  4639. for (int i = 0; i < nb; ++i) {
  4640. size_t vl = 8;
  4641. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4642. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4643. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4644. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4645. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4646. memcpy(utmp, x[i].scales, 12);
  4647. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4648. const uint32_t uaux = utmp[1] & kmask1;
  4649. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4650. utmp[2] = uaux;
  4651. utmp[0] &= kmask1;
  4652. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4653. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4654. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4655. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4656. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4657. const uint8_t * restrict q4 = x[i].qs;
  4658. const int8_t * restrict q8 = y[i].qs;
  4659. vl = 32;
  4660. int32_t sum_1 = 0;
  4661. int32_t sum_2 = 0;
  4662. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4663. for (int j = 0; j < QK_K/64; ++j) {
  4664. // load Q4
  4665. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4666. // load Q8 and multiply it with lower Q4 nibble
  4667. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4668. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4669. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  4670. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  4671. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  4672. // load Q8 and multiply it with upper Q4 nibble
  4673. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4674. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4675. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  4676. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  4677. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  4678. q4 += 32; q8 += 64;
  4679. }
  4680. sumf += d*(sum_1 + sum_2);
  4681. }
  4682. *s = sumf;
  4683. #else
  4684. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4685. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4686. int8_t aux8[QK_K];
  4687. int16_t aux16[8];
  4688. float sums [8];
  4689. int32_t aux32[8];
  4690. memset(sums, 0, 8*sizeof(float));
  4691. float sumf = 0;
  4692. for (int i = 0; i < nb; ++i) {
  4693. const uint8_t * restrict q4 = x[i].qs;
  4694. const int8_t * restrict q8 = y[i].qs;
  4695. memset(aux32, 0, 8*sizeof(int32_t));
  4696. int8_t * restrict a = aux8;
  4697. for (int j = 0; j < QK_K/64; ++j) {
  4698. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4699. a += 32;
  4700. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4701. a += 32; q4 += 32;
  4702. }
  4703. memcpy(utmp, x[i].scales, 12);
  4704. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4705. const uint32_t uaux = utmp[1] & kmask1;
  4706. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4707. utmp[2] = uaux;
  4708. utmp[0] &= kmask1;
  4709. int sumi = 0;
  4710. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4711. a = aux8;
  4712. int is = 0;
  4713. for (int j = 0; j < QK_K/32; ++j) {
  4714. int32_t scale = scales[is++];
  4715. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4716. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4717. q8 += 8; a += 8;
  4718. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4719. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4720. q8 += 8; a += 8;
  4721. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4722. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4723. q8 += 8; a += 8;
  4724. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4725. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4726. q8 += 8; a += 8;
  4727. }
  4728. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4729. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4730. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4731. sumf -= dmin * sumi;
  4732. }
  4733. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4734. *s = sumf;
  4735. #endif
  4736. }
  4737. #else
  4738. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4739. assert(n % QK_K == 0);
  4740. const block_q4_K * restrict x = vx;
  4741. const block_q8_K * restrict y = vy;
  4742. const int nb = n / QK_K;
  4743. #ifdef __ARM_NEON
  4744. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4745. const int32x4_t mzero = vdupq_n_s32(0);
  4746. float sumf = 0;
  4747. ggml_int8x16x2_t q4bytes;
  4748. ggml_int8x16x4_t q8bytes;
  4749. float sum_mins = 0.f;
  4750. uint16_t aux16[2];
  4751. const uint8_t * restrict scales = (const uint8_t *)aux16;
  4752. for (int i = 0; i < nb; ++i) {
  4753. const uint8_t * restrict q4 = x[i].qs;
  4754. const int8_t * restrict q8 = y[i].qs;
  4755. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  4756. aux16[0] = a[0] & 0x0f0f;
  4757. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4758. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  4759. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  4760. const float d = y[i].d * (float)x[i].d[0];
  4761. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  4762. q8bytes = ggml_vld1q_s8_x4(q8);
  4763. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4764. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4765. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4766. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  4767. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4768. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4769. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  4770. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  4771. sumf += d * (sumi1 + sumi2);
  4772. }
  4773. *s = sumf - sum_mins;
  4774. #elif defined __AVX2__
  4775. const __m256i m4 = _mm256_set1_epi8(0xF);
  4776. __m256 acc = _mm256_setzero_ps();
  4777. float summs = 0;
  4778. uint16_t aux16[2];
  4779. const uint8_t * scales = (const uint8_t *)aux16;
  4780. for (int i = 0; i < nb; ++i) {
  4781. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4782. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4783. const __m256 vd = _mm256_set1_ps(d);
  4784. const uint16_t * a = (const uint16_t *)x[i].scales;
  4785. aux16[0] = a[0] & 0x0f0f;
  4786. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4787. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4788. const uint8_t * restrict q4 = x[i].qs;
  4789. const int8_t * restrict q8 = y[i].qs;
  4790. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4791. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4792. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4793. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4794. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  4795. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4796. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4797. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  4798. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  4799. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  4800. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  4801. }
  4802. *s = hsum_float_8(acc) - summs;
  4803. #elif defined __AVX__
  4804. const __m128i m4 = _mm_set1_epi8(0xF);
  4805. __m256 acc = _mm256_setzero_ps();
  4806. float summs = 0;
  4807. uint16_t aux16[2];
  4808. const uint8_t * scales = (const uint8_t *)aux16;
  4809. for (int i = 0; i < nb; ++i) {
  4810. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4811. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4812. const __m256 vd = _mm256_set1_ps(d);
  4813. const uint16_t * a = (const uint16_t *)x[i].scales;
  4814. aux16[0] = a[0] & 0x0f0f;
  4815. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4816. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4817. const uint8_t * restrict q4 = x[i].qs;
  4818. const int8_t * restrict q8 = y[i].qs;
  4819. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4820. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  4821. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  4822. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  4823. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  4824. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  4825. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  4826. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4827. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4828. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  4829. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  4830. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  4831. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  4832. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  4833. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  4834. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  4835. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  4836. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  4837. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  4838. }
  4839. *s = hsum_float_8(acc) - summs;
  4840. #elif defined __riscv_v_intrinsic
  4841. uint16_t s16[2];
  4842. const uint8_t * restrict scales = (const uint8_t *)s16;
  4843. float sumf = 0;
  4844. for (int i = 0; i < nb; ++i) {
  4845. const uint8_t * restrict q4 = x[i].qs;
  4846. const int8_t * restrict q8 = y[i].qs;
  4847. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4848. s16[0] = b[0] & 0x0f0f;
  4849. s16[1] = (b[0] >> 4) & 0x0f0f;
  4850. 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]));
  4851. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4852. size_t vl = 32;
  4853. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4854. // load Q4
  4855. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4856. // load Q8 and multiply it with lower Q4 nibble
  4857. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4858. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  4859. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  4860. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  4861. // load Q8 and multiply it with upper Q4 nibble
  4862. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4863. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4864. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  4865. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  4866. }
  4867. *s = sumf;
  4868. #else
  4869. uint8_t aux8[QK_K];
  4870. int16_t aux16[16];
  4871. float sums [8];
  4872. memset(sums, 0, 8*sizeof(float));
  4873. uint16_t s16[2];
  4874. const uint8_t * restrict scales = (const uint8_t *)s16;
  4875. float sumf = 0;
  4876. for (int i = 0; i < nb; ++i) {
  4877. const uint8_t * restrict q4 = x[i].qs;
  4878. const int8_t * restrict q8 = y[i].qs;
  4879. uint8_t * restrict a = aux8;
  4880. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  4881. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  4882. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4883. s16[0] = b[0] & 0x0f0f;
  4884. s16[1] = (b[0] >> 4) & 0x0f0f;
  4885. 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]));
  4886. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4887. for (int j = 0; j < QK_K/32; ++j) {
  4888. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  4889. q8 += 16; a += 16;
  4890. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  4891. q8 += 16; a += 16;
  4892. const float dl = d * scales[j];
  4893. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  4894. }
  4895. }
  4896. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4897. *s = sumf;
  4898. #endif
  4899. }
  4900. #endif
  4901. #if QK_K == 256
  4902. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4903. assert(n % QK_K == 0);
  4904. const block_q5_K * restrict x = vx;
  4905. const block_q8_K * restrict y = vy;
  4906. const int nb = n / QK_K;
  4907. static const uint32_t kmask1 = 0x3f3f3f3f;
  4908. static const uint32_t kmask2 = 0x0f0f0f0f;
  4909. static const uint32_t kmask3 = 0x03030303;
  4910. uint32_t utmp[4];
  4911. #ifdef __ARM_NEON
  4912. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4913. const uint8x16_t mone = vdupq_n_u8(1);
  4914. const uint8x16_t mtwo = vdupq_n_u8(2);
  4915. const int32x4_t mzero = vdupq_n_s32(0);
  4916. ggml_int8x16x4_t q5bytes;
  4917. float sumf = 0;
  4918. for (int i = 0; i < nb; ++i) {
  4919. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4920. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4921. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4922. memcpy(utmp, x[i].scales, 12);
  4923. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4924. const uint32_t uaux = utmp[1] & kmask1;
  4925. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4926. utmp[2] = uaux;
  4927. utmp[0] &= kmask1;
  4928. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  4929. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  4930. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4931. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4932. int32_t sumi_mins = vaddvq_s32(prod);
  4933. const uint8_t * scales = (const uint8_t *)utmp;
  4934. const uint8_t * restrict q5 = x[i].qs;
  4935. const uint8_t * restrict qh = x[i].qh;
  4936. const int8_t * restrict q8 = y[i].qs;
  4937. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4938. ggml_uint8x16x4_t q5h;
  4939. int32_t sumi = 0;
  4940. for (int j = 0; j < QK_K/64; ++j) {
  4941. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  4942. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  4943. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  4944. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  4945. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  4946. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  4947. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  4948. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  4949. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  4950. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  4951. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  4952. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  4953. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  4954. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  4955. }
  4956. sumf += d * sumi - dmin * sumi_mins;
  4957. }
  4958. *s = sumf;
  4959. #elif defined __AVX2__
  4960. const __m256i m4 = _mm256_set1_epi8(0xF);
  4961. const __m128i mzero = _mm_setzero_si128();
  4962. const __m256i mone = _mm256_set1_epi8(1);
  4963. __m256 acc = _mm256_setzero_ps();
  4964. float summs = 0.f;
  4965. for (int i = 0; i < nb; ++i) {
  4966. const uint8_t * restrict q5 = x[i].qs;
  4967. const int8_t * restrict q8 = y[i].qs;
  4968. #if QK_K == 256
  4969. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4970. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4971. memcpy(utmp, x[i].scales, 12);
  4972. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4973. const uint32_t uaux = utmp[1] & kmask1;
  4974. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4975. utmp[2] = uaux;
  4976. utmp[0] &= kmask1;
  4977. #else
  4978. // TODO
  4979. const float d = 0, dmin = 0;
  4980. #endif
  4981. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4982. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4983. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4984. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4985. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4986. summs += dmin * _mm_extract_epi32(hsum, 0);
  4987. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4988. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4989. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  4990. __m256i hmask = mone;
  4991. __m256i sumi = _mm256_setzero_si256();
  4992. int bit = 0;
  4993. for (int j = 0; j < QK_K/64; ++j) {
  4994. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4995. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4996. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  4997. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4998. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4999. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5000. hmask = _mm256_slli_epi16(hmask, 1);
  5001. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5002. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5003. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5004. hmask = _mm256_slli_epi16(hmask, 1);
  5005. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5006. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5007. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5008. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5009. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5010. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5011. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5012. }
  5013. __m256 vd = _mm256_set1_ps(d);
  5014. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5015. }
  5016. *s = hsum_float_8(acc) + summs;
  5017. #elif defined __AVX__
  5018. const __m128i m4 = _mm_set1_epi8(0xF);
  5019. const __m128i mzero = _mm_setzero_si128();
  5020. const __m128i mone = _mm_set1_epi8(1);
  5021. const __m128i m2 = _mm_set1_epi8(2);
  5022. __m256 acc = _mm256_setzero_ps();
  5023. float summs = 0.f;
  5024. for (int i = 0; i < nb; ++i) {
  5025. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5026. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5027. const uint8_t * restrict q5 = x[i].qs;
  5028. const int8_t * restrict q8 = y[i].qs;
  5029. memcpy(utmp, x[i].scales, 12);
  5030. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5031. const uint32_t uaux = utmp[1] & kmask1;
  5032. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5033. utmp[2] = uaux;
  5034. utmp[0] &= kmask1;
  5035. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5036. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5037. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5038. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5039. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5040. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5041. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5042. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5043. summs += dmin * _mm_extract_epi32(hsum, 0);
  5044. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5045. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5046. __m128i hmask = mone;
  5047. __m128i sumi_0 = _mm_setzero_si128();
  5048. __m128i sumi_1 = _mm_setzero_si128();
  5049. int bit = 0;
  5050. __m128i shuffle = _mm_set1_epi16(0x0100);
  5051. for (int j = 0; j < QK_K/64; ++j) {
  5052. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5053. shuffle = _mm_add_epi16(shuffle, m2);
  5054. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5055. shuffle = _mm_add_epi16(shuffle, m2);
  5056. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5057. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5058. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5059. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5060. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5061. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5062. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5063. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5064. hmask = _mm_slli_epi16(hmask, 1);
  5065. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5066. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5067. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5068. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5069. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5070. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5071. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5072. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5073. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5074. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5075. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5076. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5077. hmask = _mm_slli_epi16(hmask, 1);
  5078. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5079. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5080. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5081. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5082. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5083. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5084. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5085. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5086. }
  5087. __m256 vd = _mm256_set1_ps(d);
  5088. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5089. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5090. }
  5091. *s = hsum_float_8(acc) + summs;
  5092. #elif defined __riscv_v_intrinsic
  5093. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5094. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5095. float sumf = 0;
  5096. float sums = 0.0;
  5097. size_t vl;
  5098. for (int i = 0; i < nb; ++i) {
  5099. vl = 8;
  5100. const uint8_t * restrict q5 = x[i].qs;
  5101. const uint8_t * restrict hm = x[i].qh;
  5102. const int8_t * restrict q8 = y[i].qs;
  5103. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5104. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5105. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5106. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5107. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5108. memcpy(utmp, x[i].scales, 12);
  5109. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5110. const uint32_t uaux = utmp[1] & kmask1;
  5111. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5112. utmp[2] = uaux;
  5113. utmp[0] &= kmask1;
  5114. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5115. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5116. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5117. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5118. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5119. vl = 32;
  5120. int32_t aux32 = 0;
  5121. int is = 0;
  5122. uint8_t m = 1;
  5123. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5124. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5125. for (int j = 0; j < QK_K/64; ++j) {
  5126. // load Q5 and Q8
  5127. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5128. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5129. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5130. // compute mask for addition
  5131. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5132. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5133. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5134. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5135. m <<= 1;
  5136. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5137. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5138. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5139. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5140. m <<= 1;
  5141. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5142. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5143. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5144. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5145. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5146. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5147. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5148. q5 += 32; q8 += 64;
  5149. }
  5150. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5151. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5152. }
  5153. *s = sumf+sums;
  5154. #else
  5155. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5156. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5157. int8_t aux8[QK_K];
  5158. int16_t aux16[8];
  5159. float sums [8];
  5160. int32_t aux32[8];
  5161. memset(sums, 0, 8*sizeof(float));
  5162. float sumf = 0;
  5163. for (int i = 0; i < nb; ++i) {
  5164. const uint8_t * restrict q4 = x[i].qs;
  5165. const uint8_t * restrict hm = x[i].qh;
  5166. const int8_t * restrict q8 = y[i].qs;
  5167. memset(aux32, 0, 8*sizeof(int32_t));
  5168. int8_t * restrict a = aux8;
  5169. uint8_t m = 1;
  5170. for (int j = 0; j < QK_K/64; ++j) {
  5171. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5172. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5173. a += 32; m <<= 1;
  5174. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5175. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5176. a += 32; m <<= 1;
  5177. q4 += 32;
  5178. }
  5179. memcpy(utmp, x[i].scales, 12);
  5180. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5181. const uint32_t uaux = utmp[1] & kmask1;
  5182. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5183. utmp[2] = uaux;
  5184. utmp[0] &= kmask1;
  5185. int sumi = 0;
  5186. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5187. a = aux8;
  5188. int is = 0;
  5189. for (int j = 0; j < QK_K/32; ++j) {
  5190. int32_t scale = scales[is++];
  5191. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5192. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5193. q8 += 8; a += 8;
  5194. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5195. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5196. q8 += 8; a += 8;
  5197. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5198. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5199. q8 += 8; a += 8;
  5200. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5201. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5202. q8 += 8; a += 8;
  5203. }
  5204. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5205. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5206. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5207. sumf -= dmin * sumi;
  5208. }
  5209. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5210. *s = sumf;
  5211. #endif
  5212. }
  5213. #else
  5214. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5215. assert(n % QK_K == 0);
  5216. const block_q5_K * restrict x = vx;
  5217. const block_q8_K * restrict y = vy;
  5218. const int nb = n / QK_K;
  5219. #ifdef __ARM_NEON
  5220. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5221. const uint8x16_t mh = vdupq_n_u8(16);
  5222. const int32x4_t mzero = vdupq_n_s32(0);
  5223. ggml_int8x16x4_t q5bytes;
  5224. ggml_uint8x16x4_t q5h;
  5225. float sumf = 0;
  5226. for (int i = 0; i < nb; ++i) {
  5227. const float d = y[i].d * (float)x[i].d;
  5228. const int8_t * sc = x[i].scales;
  5229. const uint8_t * restrict q5 = x[i].qs;
  5230. const uint8_t * restrict qh = x[i].qh;
  5231. const int8_t * restrict q8 = y[i].qs;
  5232. const uint8x8_t qhbits = vld1_u8(qh);
  5233. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5234. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5235. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5236. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5237. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5238. q5h.val[2] = vbicq_u8(mh, htmp);
  5239. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5240. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5241. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5242. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5243. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5244. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5245. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5246. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5247. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5248. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5249. }
  5250. *s = sumf;
  5251. #elif defined __AVX2__
  5252. const __m256i m4 = _mm256_set1_epi8(0xF);
  5253. const __m256i mone = _mm256_set1_epi8(1);
  5254. __m256 acc = _mm256_setzero_ps();
  5255. for (int i = 0; i < nb; ++i) {
  5256. const uint8_t * restrict q5 = x[i].qs;
  5257. const int8_t * restrict q8 = y[i].qs;
  5258. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5259. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5260. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5261. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5262. int64_t aux64;
  5263. memcpy(&aux64, x[i].qh, 8);
  5264. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5265. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5266. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5267. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5268. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5269. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5270. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5271. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5272. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5273. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5274. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5275. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5276. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5277. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5278. }
  5279. *s = hsum_float_8(acc);
  5280. #elif defined __AVX__
  5281. const __m128i m4 = _mm_set1_epi8(0xF);
  5282. const __m128i mone = _mm_set1_epi8(1);
  5283. __m256 acc = _mm256_setzero_ps();
  5284. for (int i = 0; i < nb; ++i) {
  5285. const uint8_t * restrict q5 = x[i].qs;
  5286. const int8_t * restrict q8 = y[i].qs;
  5287. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5288. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5289. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5290. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5291. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5292. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5293. int64_t aux64;
  5294. memcpy(&aux64, x[i].qh, 8);
  5295. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5296. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5297. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5298. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5299. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5300. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5301. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5302. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5303. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5304. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5305. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5306. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5307. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5308. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5309. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5310. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5311. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5312. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5313. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5314. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5315. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5316. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5317. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5318. }
  5319. *s = hsum_float_8(acc);
  5320. #elif defined __riscv_v_intrinsic
  5321. float sumf = 0;
  5322. for (int i = 0; i < nb; ++i) {
  5323. const float d = y[i].d * (float)x[i].d;
  5324. const int8_t * sc = x[i].scales;
  5325. const uint8_t * restrict q5 = x[i].qs;
  5326. const uint8_t * restrict qh = x[i].qh;
  5327. const int8_t * restrict q8 = y[i].qs;
  5328. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5329. // load qh
  5330. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5331. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5332. size_t vl = 16;
  5333. // combine both qh_1 and qh_2
  5334. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5335. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5336. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5337. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5338. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5339. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5340. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5341. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5342. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5343. // load q5
  5344. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5345. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5346. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5347. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5348. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5349. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5350. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5351. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5352. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5353. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5354. // load Q8 and multiply it with Q5
  5355. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5356. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5357. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5358. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5359. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5360. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5361. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5362. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5363. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5364. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5365. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5366. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5367. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5368. }
  5369. *s = sumf;
  5370. #else
  5371. int8_t aux8[QK_K];
  5372. int16_t aux16[16];
  5373. float sums [8];
  5374. memset(sums, 0, 8*sizeof(float));
  5375. float sumf = 0;
  5376. for (int i = 0; i < nb; ++i) {
  5377. const uint8_t * restrict q4 = x[i].qs;
  5378. const uint8_t * restrict hm = x[i].qh;
  5379. const int8_t * restrict q8 = y[i].qs;
  5380. int8_t * restrict a = aux8;
  5381. for (int l = 0; l < 32; ++l) {
  5382. a[l+ 0] = q4[l] & 0xF;
  5383. a[l+32] = q4[l] >> 4;
  5384. }
  5385. for (int is = 0; is < 8; ++is) {
  5386. uint8_t m = 1 << is;
  5387. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5388. }
  5389. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5390. const int8_t * restrict sc = x[i].scales;
  5391. for (int j = 0; j < QK_K/16; ++j) {
  5392. const float dl = d * sc[j];
  5393. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5394. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5395. q8 += 16; a += 16;
  5396. }
  5397. }
  5398. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5399. *s = sumf;
  5400. #endif
  5401. }
  5402. #endif
  5403. #if QK_K == 256
  5404. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5405. assert(n % QK_K == 0);
  5406. const block_q6_K * restrict x = vx;
  5407. const block_q8_K * restrict y = vy;
  5408. const int nb = n / QK_K;
  5409. #ifdef __ARM_NEON
  5410. float sum = 0;
  5411. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5412. const int32x4_t vzero = vdupq_n_s32(0);
  5413. //const int8x16_t m32s = vdupq_n_s8(32);
  5414. const uint8x16_t mone = vdupq_n_u8(3);
  5415. ggml_int8x16x4_t q6bytes;
  5416. ggml_uint8x16x4_t q6h;
  5417. for (int i = 0; i < nb; ++i) {
  5418. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5419. const uint8_t * restrict q6 = x[i].ql;
  5420. const uint8_t * restrict qh = x[i].qh;
  5421. const int8_t * restrict q8 = y[i].qs;
  5422. const int8_t * restrict scale = x[i].scales;
  5423. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  5424. const int8x16_t scales = vld1q_s8(scale);
  5425. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  5426. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5427. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5428. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5429. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5430. int32_t isum_mins = vaddvq_s32(prod);
  5431. int32_t isum = 0;
  5432. for (int j = 0; j < QK_K/128; ++j) {
  5433. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  5434. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  5435. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5436. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5437. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5438. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5439. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5440. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5441. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5442. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5443. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5444. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5445. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5446. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5447. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  5448. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  5449. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  5450. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5451. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5452. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5453. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5454. scale += 4;
  5455. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5456. shifted = vshrq_n_u8(qhbits.val[0], 4);
  5457. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5458. shifted = vshrq_n_u8(qhbits.val[1], 4);
  5459. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5460. shifted = vshrq_n_u8(qhbits.val[0], 6);
  5461. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5462. shifted = vshrq_n_u8(qhbits.val[1], 6);
  5463. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5464. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  5465. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  5466. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  5467. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  5468. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  5469. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  5470. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  5471. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  5472. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5473. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5474. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5475. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5476. scale += 4;
  5477. }
  5478. //sum += isum * d_all * y[i].d;
  5479. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  5480. }
  5481. *s = sum;
  5482. #elif defined __AVX2__
  5483. const __m256i m4 = _mm256_set1_epi8(0xF);
  5484. const __m256i m2 = _mm256_set1_epi8(3);
  5485. const __m256i m32s = _mm256_set1_epi8(32);
  5486. __m256 acc = _mm256_setzero_ps();
  5487. for (int i = 0; i < nb; ++i) {
  5488. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5489. const uint8_t * restrict q4 = x[i].ql;
  5490. const uint8_t * restrict qh = x[i].qh;
  5491. const int8_t * restrict q8 = y[i].qs;
  5492. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5493. __m256i sumi = _mm256_setzero_si256();
  5494. int is = 0;
  5495. for (int j = 0; j < QK_K/128; ++j) {
  5496. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  5497. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  5498. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  5499. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  5500. is += 4;
  5501. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5502. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5503. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  5504. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  5505. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  5506. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  5507. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  5508. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5509. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  5510. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  5511. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  5512. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5513. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5514. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5515. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5516. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5517. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5518. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  5519. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  5520. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5521. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5522. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  5523. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  5524. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5525. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5526. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5527. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5528. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5529. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5530. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  5531. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  5532. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5533. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  5534. }
  5535. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5536. }
  5537. *s = hsum_float_8(acc);
  5538. #elif defined __AVX__
  5539. const __m128i m4 = _mm_set1_epi8(0xF);
  5540. const __m128i m3 = _mm_set1_epi8(3);
  5541. const __m128i m32s = _mm_set1_epi8(32);
  5542. const __m128i m2 = _mm_set1_epi8(2);
  5543. __m256 acc = _mm256_setzero_ps();
  5544. for (int i = 0; i < nb; ++i) {
  5545. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5546. const uint8_t * restrict q4 = x[i].ql;
  5547. const uint8_t * restrict qh = x[i].qh;
  5548. const int8_t * restrict q8 = y[i].qs;
  5549. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5550. __m128i sumi_0 = _mm_setzero_si128();
  5551. __m128i sumi_1 = _mm_setzero_si128();
  5552. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  5553. for (int j = 0; j < QK_K/128; ++j) {
  5554. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5555. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5556. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  5557. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  5558. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  5559. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  5560. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  5561. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  5562. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  5563. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  5564. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5565. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5566. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5567. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5568. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  5569. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  5570. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  5571. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  5572. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  5573. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  5574. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  5575. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  5576. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5577. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5578. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5579. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5580. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5581. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5582. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5583. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5584. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  5585. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  5586. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  5587. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  5588. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  5589. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  5590. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  5591. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  5592. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  5593. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  5594. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  5595. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  5596. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  5597. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  5598. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  5599. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  5600. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5601. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5602. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5603. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5604. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5605. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5606. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5607. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5608. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5609. shuffle = _mm_add_epi8(shuffle, m2);
  5610. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5611. shuffle = _mm_add_epi8(shuffle, m2);
  5612. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  5613. shuffle = _mm_add_epi8(shuffle, m2);
  5614. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  5615. shuffle = _mm_add_epi8(shuffle, m2);
  5616. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5617. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5618. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5619. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5620. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  5621. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  5622. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  5623. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  5624. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5625. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5626. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  5627. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  5628. }
  5629. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5630. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  5631. }
  5632. *s = hsum_float_8(acc);
  5633. #elif defined __riscv_v_intrinsic
  5634. float sumf = 0;
  5635. for (int i = 0; i < nb; ++i) {
  5636. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5637. const uint8_t * restrict q6 = x[i].ql;
  5638. const uint8_t * restrict qh = x[i].qh;
  5639. const int8_t * restrict q8 = y[i].qs;
  5640. const int8_t * restrict scale = x[i].scales;
  5641. size_t vl;
  5642. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5643. int sum_t = 0;
  5644. int is = 0;
  5645. for (int j = 0; j < QK_K/128; ++j) {
  5646. vl = 32;
  5647. // load qh
  5648. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  5649. // load Q6
  5650. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  5651. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  5652. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  5653. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  5654. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  5655. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  5656. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  5657. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  5658. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  5659. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  5660. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  5661. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  5662. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  5663. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  5664. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  5665. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  5666. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  5667. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  5668. // load Q8 and take product
  5669. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  5670. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5671. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  5672. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  5673. vl = 16;
  5674. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  5675. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  5676. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  5677. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  5678. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  5679. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  5680. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  5681. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  5682. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  5683. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  5684. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  5685. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  5686. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  5687. q6 += 64; qh += 32; q8 += 128; is=8;
  5688. }
  5689. sumf += d * sum_t;
  5690. }
  5691. *s = sumf;
  5692. #else
  5693. int8_t aux8[QK_K];
  5694. int16_t aux16[8];
  5695. float sums [8];
  5696. int32_t aux32[8];
  5697. memset(sums, 0, 8*sizeof(float));
  5698. float sumf = 0;
  5699. for (int i = 0; i < nb; ++i) {
  5700. const uint8_t * restrict q4 = x[i].ql;
  5701. const uint8_t * restrict qh = x[i].qh;
  5702. const int8_t * restrict q8 = y[i].qs;
  5703. memset(aux32, 0, 8*sizeof(int32_t));
  5704. int8_t * restrict a = aux8;
  5705. for (int j = 0; j < QK_K; j += 128) {
  5706. for (int l = 0; l < 32; ++l) {
  5707. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5708. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5709. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5710. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5711. }
  5712. a += 128;
  5713. q4 += 64;
  5714. qh += 32;
  5715. }
  5716. a = aux8;
  5717. int is = 0;
  5718. for (int j = 0; j < QK_K/16; ++j) {
  5719. int scale = x[i].scales[is++];
  5720. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5721. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5722. q8 += 8; a += 8;
  5723. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5724. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5725. q8 += 8; a += 8;
  5726. }
  5727. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5728. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5729. }
  5730. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5731. *s = sumf;
  5732. #endif
  5733. }
  5734. #else
  5735. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5736. assert(n % QK_K == 0);
  5737. const block_q6_K * restrict x = vx;
  5738. const block_q8_K * restrict y = vy;
  5739. const int nb = n / QK_K;
  5740. #ifdef __ARM_NEON
  5741. float sum = 0;
  5742. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5743. const int8x16_t m32s = vdupq_n_s8(32);
  5744. const int32x4_t vzero = vdupq_n_s32(0);
  5745. const uint8x16_t mone = vdupq_n_u8(3);
  5746. ggml_int8x16x4_t q6bytes;
  5747. ggml_uint8x16x4_t q6h;
  5748. for (int i = 0; i < nb; ++i) {
  5749. const float d_all = (float)x[i].d;
  5750. const uint8_t * restrict q6 = x[i].ql;
  5751. const uint8_t * restrict qh = x[i].qh;
  5752. const int8_t * restrict q8 = y[i].qs;
  5753. const int8_t * restrict scale = x[i].scales;
  5754. int32_t isum = 0;
  5755. uint8x16_t qhbits = vld1q_u8(qh);
  5756. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  5757. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5758. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  5759. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  5760. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5761. shifted = vshrq_n_u8(qhbits, 4);
  5762. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5763. shifted = vshrq_n_u8(qhbits, 6);
  5764. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5765. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5766. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5767. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  5768. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  5769. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5770. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5771. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5772. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5773. sum += isum * d_all * y[i].d;
  5774. }
  5775. *s = sum;
  5776. #elif defined __AVX2__
  5777. const __m256i m4 = _mm256_set1_epi8(0xF);
  5778. const __m256i m2 = _mm256_set1_epi8(3);
  5779. const __m256i m32s = _mm256_set1_epi8(32);
  5780. __m256 acc = _mm256_setzero_ps();
  5781. for (int i = 0; i < nb; ++i) {
  5782. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5783. const uint8_t * restrict q4 = x[i].ql;
  5784. const uint8_t * restrict qh = x[i].qh;
  5785. const int8_t * restrict q8 = y[i].qs;
  5786. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5787. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5788. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5789. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5790. __m256i sumi = _mm256_setzero_si256();
  5791. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5792. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5793. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5794. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5795. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  5796. 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);
  5797. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5798. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  5799. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5800. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5801. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5802. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5803. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5804. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5805. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5806. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5807. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5808. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5809. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5810. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5811. }
  5812. *s = hsum_float_8(acc);
  5813. #elif defined __AVX__
  5814. const __m128i m4 = _mm_set1_epi8(0xF);
  5815. const __m128i m2 = _mm_set1_epi8(3);
  5816. const __m128i m32s = _mm_set1_epi8(32);
  5817. __m256 acc = _mm256_setzero_ps();
  5818. for (int i = 0; i < nb; ++i) {
  5819. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5820. const uint8_t * restrict q4 = x[i].ql;
  5821. const uint8_t * restrict qh = x[i].qh;
  5822. const int8_t * restrict q8 = y[i].qs;
  5823. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5824. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5825. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5826. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5827. __m128i sumi_0 = _mm_setzero_si128();
  5828. __m128i sumi_1 = _mm_setzero_si128();
  5829. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5830. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5831. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5832. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5833. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  5834. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  5835. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  5836. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  5837. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  5838. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  5839. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  5840. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  5841. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5842. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5843. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  5844. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  5845. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  5846. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  5847. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5848. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5849. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5850. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5851. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5852. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5853. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5854. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5855. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5856. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5857. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5858. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5859. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5860. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5861. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  5862. }
  5863. *s = hsum_float_8(acc);
  5864. #elif defined __riscv_v_intrinsic
  5865. float sumf = 0;
  5866. for (int i = 0; i < nb; ++i) {
  5867. const float d_all = (float)x[i].d;
  5868. const uint8_t * restrict q6 = x[i].ql;
  5869. const uint8_t * restrict qh = x[i].qh;
  5870. const int8_t * restrict q8 = y[i].qs;
  5871. const int8_t * restrict scale = x[i].scales;
  5872. int32_t isum = 0;
  5873. size_t vl = 16;
  5874. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5875. // load Q6
  5876. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  5877. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  5878. // load qh
  5879. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  5880. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5881. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5882. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5883. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5884. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5885. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5886. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5887. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  5888. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  5889. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  5890. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  5891. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  5892. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  5893. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  5894. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  5895. // load Q8 and take product
  5896. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5897. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5898. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5899. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5900. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5901. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5902. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5903. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5904. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  5905. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  5906. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  5907. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  5908. sumf += isum * d_all * y[i].d;
  5909. }
  5910. *s = sumf;
  5911. #else
  5912. int8_t aux8[QK_K];
  5913. int16_t aux16[8];
  5914. float sums [8];
  5915. int32_t aux32[8];
  5916. memset(sums, 0, 8*sizeof(float));
  5917. float sumf = 0;
  5918. for (int i = 0; i < nb; ++i) {
  5919. const uint8_t * restrict q4 = x[i].ql;
  5920. const uint8_t * restrict qh = x[i].qh;
  5921. const int8_t * restrict q8 = y[i].qs;
  5922. memset(aux32, 0, 8*sizeof(int32_t));
  5923. int8_t * restrict a = aux8;
  5924. for (int l = 0; l < 16; ++l) {
  5925. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5926. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5927. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5928. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5929. }
  5930. int is = 0;
  5931. for (int j = 0; j < QK_K/16; ++j) {
  5932. int scale = x[i].scales[is++];
  5933. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5934. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5935. q8 += 8; a += 8;
  5936. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5937. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5938. q8 += 8; a += 8;
  5939. }
  5940. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5941. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5942. }
  5943. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5944. *s = sumf;
  5945. #endif
  5946. }
  5947. #endif
  5948. static const int8_t keven_signs_q2xs[1024] = {
  5949. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  5950. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  5951. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  5952. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  5953. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  5954. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  5955. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  5956. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  5957. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  5958. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  5959. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  5960. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  5961. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  5962. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  5963. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  5964. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  5965. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  5966. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  5967. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  5968. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  5969. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  5970. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  5971. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  5972. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  5973. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  5974. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  5975. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  5976. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  5977. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  5978. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  5979. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  5980. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  5981. };
  5982. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5983. assert(n % QK_K == 0);
  5984. const block_iq2_xxs * restrict x = vx;
  5985. const block_q8_K * restrict y = vy;
  5986. const int nb = n / QK_K;
  5987. #if defined(__ARM_NEON)
  5988. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  5989. uint32_t aux32[4];
  5990. const uint8_t * aux8 = (const uint8_t *)aux32;
  5991. ggml_int8x16x4_t q2u;
  5992. ggml_int8x16x4_t q2s;
  5993. ggml_int8x16x4_t q8b;
  5994. float sumf = 0;
  5995. for (int i = 0; i < nb; ++i) {
  5996. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5997. const uint16_t * restrict q2 = x[i].qs;
  5998. const int8_t * restrict q8 = y[i].qs;
  5999. float sumf1 = 0, sumf2 = 0;
  6000. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6001. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6002. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6003. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6004. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6005. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6006. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6007. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6008. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6009. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6010. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6011. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6012. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6013. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6014. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6015. 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]);
  6016. 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]);
  6017. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6018. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6019. }
  6020. sumf += d*(sumf1 + sumf2);
  6021. }
  6022. *s = 0.25f * sumf;
  6023. #elif defined(__AVX2__)
  6024. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6025. uint32_t aux32[4];
  6026. const uint8_t * aux8 = (const uint8_t *)aux32;
  6027. __m256 accumf = _mm256_setzero_ps();
  6028. for (int i = 0; i < nb; ++i) {
  6029. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6030. const uint16_t * restrict q2 = x[i].qs;
  6031. const int8_t * restrict q8 = y[i].qs;
  6032. __m256i sumi1 = _mm256_setzero_si256();
  6033. __m256i sumi2 = _mm256_setzero_si256();
  6034. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6035. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6036. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6037. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6038. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6039. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6040. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6041. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6042. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6043. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6044. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6045. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6046. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6047. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6048. const uint16_t ls1 = aux32[1] >> 28;
  6049. const uint16_t ls2 = aux32[3] >> 28;
  6050. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6051. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6052. sumi1 = _mm256_add_epi32(sumi1, p1);
  6053. sumi2 = _mm256_add_epi32(sumi2, p2);
  6054. }
  6055. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6056. }
  6057. *s = 0.125f * hsum_float_8(accumf);
  6058. #else
  6059. uint32_t aux32[2];
  6060. const uint8_t * aux8 = (const uint8_t *)aux32;
  6061. float sumf = 0.f;
  6062. for (int i = 0; i < nb; ++i) {
  6063. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6064. const uint16_t * restrict q2 = x[i].qs;
  6065. const int8_t * restrict q8 = y[i].qs;
  6066. int32_t bsum = 0;
  6067. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6068. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6069. q2 += 4;
  6070. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6071. int32_t sumi = 0;
  6072. for (int l = 0; l < 4; ++l) {
  6073. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6074. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6075. for (int j = 0; j < 8; ++j) {
  6076. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6077. }
  6078. q8 += 8;
  6079. }
  6080. bsum += sumi * ls;
  6081. }
  6082. sumf += d * bsum;
  6083. }
  6084. *s = 0.125f * sumf;
  6085. #endif
  6086. }
  6087. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6088. assert(n % QK_K == 0);
  6089. const block_iq2_xs * restrict x = vx;
  6090. const block_q8_K * restrict y = vy;
  6091. const int nb = n / QK_K;
  6092. #if defined(__ARM_NEON)
  6093. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6094. ggml_int8x16x4_t q2u;
  6095. ggml_int8x16x4_t q2s;
  6096. ggml_int8x16x4_t q8b;
  6097. int32x4x4_t scales32;
  6098. float sumf = 0;
  6099. for (int i = 0; i < nb; ++i) {
  6100. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6101. const uint16_t * restrict q2 = x[i].qs;
  6102. const int8_t * restrict q8 = y[i].qs;
  6103. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6104. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6105. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6106. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6107. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6108. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6109. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6110. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6111. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6112. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6113. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6114. int32x4_t sumi = vdupq_n_s32(0);
  6115. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6116. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6117. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6118. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6119. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6120. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6121. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6122. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6123. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6124. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6125. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6126. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6127. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6128. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6129. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6130. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6131. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6132. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6133. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6134. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6135. q2 += 8;
  6136. }
  6137. sumf += d*vaddvq_s32(sumi);
  6138. }
  6139. *s = 0.125f * sumf;
  6140. #elif defined(__AVX2__)
  6141. const __m128i m4 = _mm_set1_epi8(0xf);
  6142. const __m128i m1 = _mm_set1_epi8(1);
  6143. const __m128i m511 = _mm_set1_epi16(511);
  6144. const __m128i m127 = _mm_set1_epi16(127);
  6145. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6146. uint64_t aux64;
  6147. // somewhat hacky, but gives a significant boost in performance
  6148. __m128i aux_gindex, aux_sindex;
  6149. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6150. const uint16_t * sindex = (const uint16_t *)&aux_sindex;
  6151. __m256 accumf = _mm256_setzero_ps();
  6152. for (int i = 0; i < nb; ++i) {
  6153. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6154. const uint16_t * restrict q2 = x[i].qs;
  6155. const int8_t * restrict q8 = y[i].qs;
  6156. memcpy(&aux64, x[i].scales, 8);
  6157. __m128i stmp = _mm_set1_epi64x(aux64);
  6158. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6159. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6160. __m256i sumi1 = _mm256_setzero_si256();
  6161. __m256i sumi2 = _mm256_setzero_si256();
  6162. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6163. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6164. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6165. const __m128i q2_data = _mm_loadu_si128((const __m128i*)q2); q2 += 8;
  6166. aux_gindex = _mm_and_si128(q2_data, m511);
  6167. aux_sindex = _mm_and_si128(_mm_srli_epi16(q2_data, 9), m127);
  6168. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]], iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
  6169. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]], iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
  6170. const __m256i s2_1 = _mm256_set_epi64x(signs64[sindex[3]], signs64[sindex[2]], signs64[sindex[1]], signs64[sindex[0]]);
  6171. const __m256i s2_2 = _mm256_set_epi64x(signs64[sindex[7]], signs64[sindex[6]], signs64[sindex[5]], signs64[sindex[4]]);
  6172. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6173. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6174. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6175. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6176. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6177. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6178. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6179. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6180. }
  6181. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6182. }
  6183. *s = 0.125f * hsum_float_8(accumf);
  6184. #else
  6185. float sumf = 0.f;
  6186. for (int i = 0; i < nb; ++i) {
  6187. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6188. const uint16_t * restrict q2 = x[i].qs;
  6189. const uint8_t * restrict sc = x[i].scales;
  6190. const int8_t * restrict q8 = y[i].qs;
  6191. int32_t bsum = 0;
  6192. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6193. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6194. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6195. int32_t sumi = 0;
  6196. for (int l = 0; l < 2; ++l) {
  6197. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6198. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6199. for (int j = 0; j < 8; ++j) {
  6200. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6201. }
  6202. q8 += 8;
  6203. }
  6204. bsum += sumi * ls1;
  6205. sumi = 0;
  6206. for (int l = 2; l < 4; ++l) {
  6207. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6208. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6209. for (int j = 0; j < 8; ++j) {
  6210. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6211. }
  6212. q8 += 8;
  6213. }
  6214. bsum += sumi * ls2;
  6215. q2 += 4;
  6216. }
  6217. sumf += d * bsum;
  6218. }
  6219. *s = 0.125f * sumf;
  6220. #endif
  6221. }
  6222. // ================================ IQ2 quantization =============================================
  6223. typedef struct {
  6224. uint64_t * grid;
  6225. int * map;
  6226. uint16_t * neighbours;
  6227. } iq2_entry_t;
  6228. static iq2_entry_t iq2_data[2] = {
  6229. {NULL, NULL, NULL},
  6230. {NULL, NULL, NULL},
  6231. };
  6232. static inline int iq2_data_index(int grid_size) {
  6233. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  6234. return grid_size == 256 ? 0 : 1;
  6235. }
  6236. static int iq2_compare_func(const void * left, const void * right) {
  6237. const int * l = (const int *)left;
  6238. const int * r = (const int *)right;
  6239. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  6240. }
  6241. static void q2xs_init_impl(int grid_size) {
  6242. const int gindex = iq2_data_index(grid_size);
  6243. if (iq2_data[gindex].grid) {
  6244. return;
  6245. }
  6246. static const uint16_t kgrid_256[256] = {
  6247. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  6248. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  6249. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  6250. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  6251. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  6252. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  6253. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  6254. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  6255. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  6256. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  6257. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  6258. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  6259. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  6260. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  6261. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  6262. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  6263. };
  6264. static const uint16_t kgrid_512[512] = {
  6265. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  6266. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  6267. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  6268. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  6269. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  6270. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  6271. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  6272. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  6273. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  6274. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  6275. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  6276. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  6277. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  6278. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  6279. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  6280. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  6281. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  6282. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  6283. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  6284. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  6285. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  6286. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  6287. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  6288. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  6289. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  6290. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  6291. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  6292. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  6293. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  6294. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  6295. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  6296. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  6297. };
  6298. const int kmap_size = 43692;
  6299. const int nwant = 2;
  6300. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  6301. uint64_t * kgrid_q2xs;
  6302. int * kmap_q2xs;
  6303. uint16_t * kneighbors_q2xs;
  6304. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  6305. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  6306. for (int k = 0; k < grid_size; ++k) {
  6307. int8_t * pos = (int8_t *)(the_grid + k);
  6308. for (int i = 0; i < 8; ++i) {
  6309. int l = (kgrid[k] >> 2*i) & 0x3;
  6310. pos[i] = 2*l + 1;
  6311. }
  6312. }
  6313. kgrid_q2xs = the_grid;
  6314. iq2_data[gindex].grid = the_grid;
  6315. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  6316. iq2_data[gindex].map = kmap_q2xs;
  6317. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  6318. uint64_t aux64;
  6319. uint8_t * aux8 = (uint8_t *)&aux64;
  6320. for (int i = 0; i < grid_size; ++i) {
  6321. aux64 = kgrid_q2xs[i];
  6322. uint16_t index = 0;
  6323. for (int k=0; k<8; ++k) {
  6324. uint16_t q = (aux8[k] - 1)/2;
  6325. index |= (q << 2*k);
  6326. }
  6327. kmap_q2xs[index] = i;
  6328. }
  6329. int8_t pos[8];
  6330. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  6331. int num_neighbors = 0, num_not_in_map = 0;
  6332. for (int i = 0; i < kmap_size; ++i) {
  6333. if (kmap_q2xs[i] >= 0) continue;
  6334. ++num_not_in_map;
  6335. for (int k = 0; k < 8; ++k) {
  6336. int l = (i >> 2*k) & 0x3;
  6337. pos[k] = 2*l + 1;
  6338. }
  6339. for (int j = 0; j < grid_size; ++j) {
  6340. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6341. int d2 = 0;
  6342. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6343. dist2[2*j+0] = d2;
  6344. dist2[2*j+1] = j;
  6345. }
  6346. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6347. int n = 0; int d2 = dist2[0];
  6348. int nhave = 1;
  6349. for (int j = 0; j < grid_size; ++j) {
  6350. if (dist2[2*j] > d2) {
  6351. if (nhave == nwant) break;
  6352. d2 = dist2[2*j];
  6353. ++nhave;
  6354. }
  6355. ++n;
  6356. }
  6357. num_neighbors += n;
  6358. }
  6359. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  6360. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  6361. iq2_data[gindex].neighbours = kneighbors_q2xs;
  6362. int counter = 0;
  6363. for (int i = 0; i < kmap_size; ++i) {
  6364. if (kmap_q2xs[i] >= 0) continue;
  6365. for (int k = 0; k < 8; ++k) {
  6366. int l = (i >> 2*k) & 0x3;
  6367. pos[k] = 2*l + 1;
  6368. }
  6369. for (int j = 0; j < grid_size; ++j) {
  6370. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6371. int d2 = 0;
  6372. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6373. dist2[2*j+0] = d2;
  6374. dist2[2*j+1] = j;
  6375. }
  6376. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6377. kmap_q2xs[i] = -(counter + 1);
  6378. int d2 = dist2[0];
  6379. uint16_t * start = &kneighbors_q2xs[counter++];
  6380. int n = 0, nhave = 1;
  6381. for (int j = 0; j < grid_size; ++j) {
  6382. if (dist2[2*j] > d2) {
  6383. if (nhave == nwant) break;
  6384. d2 = dist2[2*j];
  6385. ++nhave;
  6386. }
  6387. kneighbors_q2xs[counter++] = dist2[2*j+1];
  6388. ++n;
  6389. }
  6390. *start = n;
  6391. }
  6392. free(dist2);
  6393. }
  6394. void ggml_init_iq2_quantization(enum ggml_type type) {
  6395. if (type == GGML_TYPE_IQ2_XXS) {
  6396. q2xs_init_impl(256);
  6397. }
  6398. else if (type == GGML_TYPE_IQ2_XS) {
  6399. q2xs_init_impl(512);
  6400. }
  6401. else {
  6402. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6403. }
  6404. }
  6405. static void q2xs_deinit_impl(int grid_size) {
  6406. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  6407. const int gindex = iq2_data_index(grid_size);
  6408. if (iq2_data[gindex].grid) {
  6409. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  6410. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  6411. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  6412. }
  6413. }
  6414. void ggml_deinit_iq2_quantization(enum ggml_type type) {
  6415. if (type == GGML_TYPE_IQ2_XXS) {
  6416. q2xs_deinit_impl(256);
  6417. }
  6418. else if (type == GGML_TYPE_IQ2_XS) {
  6419. q2xs_deinit_impl(512);
  6420. }
  6421. else {
  6422. fprintf(stderr, "======================== Why are you calling %s with type %d?\n", __func__, (int)type);
  6423. }
  6424. }
  6425. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  6426. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  6427. int num_neighbors = neighbours[0];
  6428. GGML_ASSERT(num_neighbors > 0);
  6429. float best_d2 = FLT_MAX;
  6430. int grid_index = -1;
  6431. for (int j = 1; j <= num_neighbors; ++j) {
  6432. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  6433. float d2 = 0;
  6434. for (int i = 0; i < 8; ++i) {
  6435. float q = pg[i];
  6436. float diff = scale*q - xval[i];
  6437. d2 += weight[i]*diff*diff;
  6438. }
  6439. if (d2 < best_d2) {
  6440. best_d2 = d2; grid_index = neighbours[j];
  6441. }
  6442. }
  6443. GGML_ASSERT(grid_index >= 0);
  6444. const int8_t * pg = (const int8_t *)(grid + grid_index);
  6445. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  6446. return grid_index;
  6447. }
  6448. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  6449. const int gindex = iq2_data_index(256);
  6450. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  6451. const int * kmap_q2xs = iq2_data[gindex].map;
  6452. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  6453. GGML_ASSERT(quant_weights);
  6454. GGML_ASSERT(kgrid_q2xs);
  6455. GGML_ASSERT(kmap_q2xs);
  6456. GGML_ASSERT(kneighbors_q2xs);
  6457. GGML_ASSERT(n%QK_K == 0);
  6458. const int kMaxQ = 3;
  6459. const int nbl = n/256;
  6460. block_iq2_xxs * y = vy;
  6461. float scales[QK_K/32];
  6462. float weight[32];
  6463. float xval[32];
  6464. int8_t L[32];
  6465. int8_t Laux[32];
  6466. float waux[32];
  6467. bool is_on_grid[4];
  6468. bool is_on_grid_aux[4];
  6469. uint8_t block_signs[4];
  6470. uint32_t q2[2*(QK_K/32)];
  6471. for (int ibl = 0; ibl < nbl; ++ibl) {
  6472. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  6473. memset(q2, 0, QK_K/4);
  6474. float max_scale = 0;
  6475. const float * xbl = x + QK_K*ibl;
  6476. float sumx2 = 0;
  6477. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  6478. float sigma2 = sumx2/QK_K;
  6479. for (int ib = 0; ib < QK_K/32; ++ib) {
  6480. const float * xb = xbl + 32*ib;
  6481. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  6482. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  6483. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  6484. for (int k = 0; k < 4; ++k) {
  6485. int nflip = 0;
  6486. uint8_t s = 0;
  6487. for (int i = 0; i < 8; ++i) {
  6488. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  6489. else {
  6490. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  6491. }
  6492. }
  6493. if (nflip%2) {
  6494. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  6495. for (int i = 1; i < 8; ++i) {
  6496. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  6497. if (ax < min) {
  6498. min = ax; imin = i;
  6499. }
  6500. }
  6501. xval[8*k+imin] = -xval[8*k+imin];
  6502. s ^= (1 << imin);
  6503. }
  6504. block_signs[k] = s & 127;
  6505. }
  6506. float max = xval[0];
  6507. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  6508. if (!max) {
  6509. scales[ib] = 0;
  6510. memset(L, 0, 32);
  6511. continue;
  6512. }
  6513. float best = 0;
  6514. float scale = max/(2*kMaxQ-1);
  6515. for (int is = -9; is <= 9; ++is) {
  6516. float id = (2*kMaxQ-1+is*0.1f)/max;
  6517. float this_scale = 1/id;
  6518. for (int k = 0; k < 4; ++k) {
  6519. for (int i = 0; i < 8; ++i) {
  6520. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6521. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  6522. }
  6523. uint16_t u = 0;
  6524. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  6525. int grid_index = kmap_q2xs[u];
  6526. is_on_grid_aux[k] = true;
  6527. if (grid_index < 0) {
  6528. is_on_grid_aux[k] = false;
  6529. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6530. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  6531. }
  6532. }
  6533. float sumqx = 0, sumq2 = 0;
  6534. for (int i = 0; i < 32; ++i) {
  6535. float w = weight[i];
  6536. float q = 2*Laux[i] + 1;
  6537. sumqx += w*xval[i]*q;
  6538. sumq2 += w*q*q;
  6539. }
  6540. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  6541. scale = sumqx/sumq2; best = scale*sumqx;
  6542. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  6543. for (int k = 0; k < 4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  6544. }
  6545. }
  6546. int n_not_ongrid = 0;
  6547. for (int k = 0; k < 4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  6548. if (n_not_ongrid > 0 && scale > 0) {
  6549. float id = 1/scale;
  6550. for (int k = 0; k < 4; ++k) {
  6551. if (is_on_grid[k]) continue;
  6552. uint16_t u = 0;
  6553. for (int i = 0; i < 8; ++i) {
  6554. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6555. l = MAX(0, MIN(kMaxQ-1, l));
  6556. u |= (l << 2*i);
  6557. }
  6558. int grid_index = kmap_q2xs[u];
  6559. if (grid_index < 0) {
  6560. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6561. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  6562. }
  6563. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  6564. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  6565. }
  6566. float sumqx = 0, sumq2 = 0;
  6567. for (int i = 0; i < 32; ++i) {
  6568. float w = weight[i];
  6569. float q = 2*L[i] + 1;
  6570. sumqx += w*xval[i]*q;
  6571. sumq2 += w*q*q;
  6572. }
  6573. if (sumq2 > 0) scale = sumqx/sumq2;
  6574. }
  6575. if (scale < 0) {
  6576. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  6577. // and correspondingly flip quant signs.
  6578. scale = -scale;
  6579. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  6580. }
  6581. for (int k = 0; k < 4; ++k) {
  6582. uint16_t u = 0;
  6583. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  6584. int grid_index = kmap_q2xs[u];
  6585. if (grid_index < 0) {
  6586. printf("Oops: found point %u not on grid:", u);
  6587. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  6588. printf("\n");
  6589. GGML_ASSERT(false);
  6590. }
  6591. q2[2*ib+0] |= (grid_index << 8*k);
  6592. q2[2*ib+1] |= (block_signs[k] << 7*k);
  6593. }
  6594. GGML_ASSERT(scale >= 0);
  6595. scales[ib] = scale;
  6596. max_scale = MAX(max_scale, scale);
  6597. }
  6598. if (!max_scale) {
  6599. memset(y[ibl].qs, 0, QK_K/4);
  6600. continue;
  6601. }
  6602. float d = max_scale/31;
  6603. y[ibl].d = GGML_FP32_TO_FP16(d);
  6604. float id = 1/d;
  6605. float sumqx = 0, sumq2 = 0;
  6606. for (int ib = 0; ib < QK_K/32; ++ib) {
  6607. int l = nearest_int(0.5f*(id*scales[ib]-1));
  6608. l = MAX(0, MIN(15, l));
  6609. q2[2*ib+1] |= ((uint32_t)l << 28);
  6610. const float * xb = xbl + 32*ib;
  6611. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  6612. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  6613. const uint8_t * aux8 = (const uint8_t *)(q2 + 2*ib);
  6614. const float db = d * (1 + 2*l);
  6615. uint32_t u = 0;
  6616. for (int k = 0; k < 4; ++k) {
  6617. const int8_t * signs = keven_signs_q2xs + 8*((q2[2*ib+1] >> 7*k) & 127);
  6618. const float * xk = xb + 8*k;
  6619. const float * wk = weight + 8*k;
  6620. const uint8_t * grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  6621. float best_mse = 0; int best_index = aux8[k];
  6622. for (int j = 0; j < 8; ++j) {
  6623. float diff = db * grid[j] * signs[j] - xk[j];
  6624. best_mse += wk[j] * diff * diff;
  6625. }
  6626. for (int idx = 0; idx < 256; ++idx) {
  6627. grid = (const uint8_t *)(kgrid_q2xs + idx);
  6628. float mse = 0;
  6629. for (int j = 0; j < 8; ++j) {
  6630. float diff = db * grid[j] * signs[j] - xk[j];
  6631. mse += wk[j] * diff * diff;
  6632. }
  6633. if (mse < best_mse) {
  6634. best_mse = mse; best_index = idx;
  6635. }
  6636. }
  6637. u |= (best_index << 8*k);
  6638. grid = (const uint8_t *)(kgrid_q2xs + best_index);
  6639. //grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  6640. for (int j = 0; j < 8; ++j) {
  6641. float q = db * grid[j] * signs[j];
  6642. sumqx += wk[j] * q * xk[j];
  6643. sumq2 += wk[j] * q * q;
  6644. }
  6645. }
  6646. q2[2*ib] = u;
  6647. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  6648. }
  6649. memcpy(y[ibl].qs, q2, QK_K/4);
  6650. }
  6651. }
  6652. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  6653. const int gindex = iq2_data_index(512);
  6654. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  6655. const int * kmap_q2xs = iq2_data[gindex].map;
  6656. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  6657. GGML_ASSERT(quant_weights);
  6658. GGML_ASSERT(kmap_q2xs);
  6659. GGML_ASSERT(kgrid_q2xs);
  6660. GGML_ASSERT(kneighbors_q2xs);
  6661. GGML_ASSERT(n%QK_K == 0);
  6662. const int kMaxQ = 3;
  6663. const int nbl = n/256;
  6664. block_iq2_xs * y = vy;
  6665. float scales[QK_K/16];
  6666. float weight[16];
  6667. float xval[16];
  6668. int8_t L[16];
  6669. int8_t Laux[16];
  6670. float waux[16];
  6671. bool is_on_grid[2];
  6672. bool is_on_grid_aux[2];
  6673. uint8_t block_signs[2];
  6674. uint16_t q2[2*(QK_K/16)];
  6675. for (int ibl = 0; ibl < nbl; ++ibl) {
  6676. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  6677. memset(q2, 0, QK_K/4);
  6678. memset(y[ibl].scales, 0, QK_K/32);
  6679. float max_scale = 0;
  6680. const float * xbl = x + QK_K*ibl;
  6681. float sumx2 = 0;
  6682. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  6683. float sigma2 = sumx2/QK_K;
  6684. for (int ib = 0; ib < QK_K/16; ++ib) {
  6685. const float * xb = xbl + 16*ib;
  6686. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  6687. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  6688. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  6689. for (int k = 0; k < 2; ++k) {
  6690. int nflip = 0;
  6691. uint8_t s = 0;
  6692. for (int i = 0; i < 8; ++i) {
  6693. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  6694. else {
  6695. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  6696. }
  6697. }
  6698. if (nflip%2) {
  6699. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  6700. for (int i = 1; i < 8; ++i) {
  6701. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  6702. if (ax < min) {
  6703. min = ax; imin = i;
  6704. }
  6705. }
  6706. xval[8*k+imin] = -xval[8*k+imin];
  6707. s ^= (1 << imin);
  6708. }
  6709. block_signs[k] = s & 127;
  6710. }
  6711. float max = xval[0];
  6712. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  6713. if (!max) {
  6714. scales[ib] = 0;
  6715. memset(L, 0, 16);
  6716. continue;
  6717. }
  6718. float best = 0;
  6719. float scale = max/(2*kMaxQ-1);
  6720. is_on_grid[0] = is_on_grid[1] = true;
  6721. for (int is = -9; is <= 9; ++is) {
  6722. float id = (2*kMaxQ-1+is*0.1f)/max;
  6723. float this_scale = 1/id;
  6724. for (int k = 0; k < 2; ++k) {
  6725. for (int i = 0; i < 8; ++i) {
  6726. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6727. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  6728. }
  6729. uint16_t u = 0;
  6730. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  6731. int grid_index = kmap_q2xs[u];
  6732. is_on_grid_aux[k] = true;
  6733. if (grid_index < 0) {
  6734. is_on_grid_aux[k] = false;
  6735. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6736. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  6737. }
  6738. }
  6739. float sumqx = 0, sumq2 = 0;
  6740. for (int i = 0; i < 16; ++i) {
  6741. float w = weight[i];
  6742. float q = 2*Laux[i] + 1;
  6743. sumqx += w*xval[i]*q;
  6744. sumq2 += w*q*q;
  6745. }
  6746. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  6747. scale = sumqx/sumq2; best = scale*sumqx;
  6748. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  6749. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  6750. }
  6751. }
  6752. int n_not_ongrid = 0;
  6753. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  6754. if (n_not_ongrid > 0 && scale > 0) {
  6755. float id = 1/scale;
  6756. for (int k = 0; k < 2; ++k) {
  6757. if (is_on_grid[k]) continue;
  6758. uint16_t u = 0;
  6759. for (int i = 0; i < 8; ++i) {
  6760. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  6761. l = MAX(0, MIN(kMaxQ-1, l));
  6762. u |= (l << 2*i);
  6763. L[8*k + i] = l;
  6764. }
  6765. int grid_index = kmap_q2xs[u];
  6766. if (grid_index < 0) {
  6767. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  6768. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  6769. }
  6770. }
  6771. float sumqx = 0, sumq2 = 0;
  6772. for (int i = 0; i < 16; ++i) {
  6773. float w = weight[i];
  6774. float q = 2*L[i] + 1;
  6775. sumqx += w*xval[i]*q;
  6776. sumq2 += w*q*q;
  6777. }
  6778. if (sumq2 > 0) scale = sumqx/sumq2;
  6779. }
  6780. if (scale < 0) {
  6781. scale = -scale;
  6782. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  6783. }
  6784. for (int k = 0; k < 2; ++k) {
  6785. uint16_t u = 0;
  6786. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  6787. int grid_index = kmap_q2xs[u];
  6788. if (grid_index < 0) {
  6789. printf("Oops: found point %u not on grid:", u);
  6790. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  6791. printf("\n");
  6792. GGML_ASSERT(false);
  6793. }
  6794. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  6795. }
  6796. GGML_ASSERT(scale >= 0);
  6797. scales[ib] = scale;
  6798. max_scale = MAX(max_scale, scale);
  6799. }
  6800. if (!max_scale) {
  6801. memset(y[ibl].qs, 0, QK_K/4);
  6802. continue;
  6803. }
  6804. float d = max_scale/31;
  6805. y[ibl].d = GGML_FP32_TO_FP16(d);
  6806. float id = 1/d;
  6807. for (int ib = 0; ib < QK_K/16; ++ib) {
  6808. int l = nearest_int(0.5f*(id*scales[ib]-1));
  6809. l = MAX(0, MIN(15, l));
  6810. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  6811. else y[ibl].scales[ib/2] |= (l << 4);
  6812. }
  6813. memcpy(y[ibl].qs, q2, QK_K/4);
  6814. }
  6815. }
  6816. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  6817. (void)hist;
  6818. GGML_ASSERT(n_per_row%QK_K == 0);
  6819. int nblock = n_per_row/QK_K;
  6820. char * qrow = (char *)dst;
  6821. for (int row = 0; row < nrow; ++row) {
  6822. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  6823. src += n_per_row;
  6824. qrow += nblock*sizeof(block_iq2_xxs);
  6825. }
  6826. return nrow * nblock * sizeof(block_iq2_xxs);
  6827. }
  6828. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  6829. (void)hist;
  6830. GGML_ASSERT(n_per_row%QK_K == 0);
  6831. int nblock = n_per_row/QK_K;
  6832. char * qrow = (char *)dst;
  6833. for (int row = 0; row < nrow; ++row) {
  6834. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  6835. src += n_per_row;
  6836. qrow += nblock*sizeof(block_iq2_xs);
  6837. }
  6838. return nrow * nblock * sizeof(block_iq2_xs);
  6839. }