ggml-quants.c 390 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827
  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. const float * restrict qw) {
  983. float max = 0;
  984. float amax = 0;
  985. for (int i = 0; i < n; ++i) {
  986. float ax = fabsf(x[i]);
  987. if (ax > amax) { amax = ax; max = x[i]; }
  988. }
  989. if (amax < 1e-30f) { // all zero
  990. for (int i = 0; i < n; ++i) {
  991. L[i] = 0;
  992. }
  993. return 0.f;
  994. }
  995. float iscale = -nmax / max;
  996. if (rmse_type == 0) {
  997. for (int i = 0; i < n; ++i) {
  998. int l = nearest_int(iscale * x[i]);
  999. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1000. }
  1001. return 1/iscale;
  1002. }
  1003. bool return_early = false;
  1004. if (rmse_type < 0) {
  1005. rmse_type = -rmse_type;
  1006. return_early = true;
  1007. }
  1008. float sumlx = 0;
  1009. float suml2 = 0;
  1010. #ifdef HAVE_BUGGY_APPLE_LINKER
  1011. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1012. for (volatile int i = 0; i < n; ++i) {
  1013. #else
  1014. for (int i = 0; i < n; ++i) {
  1015. #endif
  1016. int l = nearest_int(iscale * x[i]);
  1017. l = MAX(-nmax, MIN(nmax-1, l));
  1018. L[i] = l + nmax;
  1019. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1020. sumlx += w*x[i]*l;
  1021. suml2 += w*l*l;
  1022. }
  1023. float scale = sumlx/suml2;
  1024. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1025. float best = scale * sumlx;
  1026. for (int is = -9; is <= 9; ++is) {
  1027. if (is == 0) {
  1028. continue;
  1029. }
  1030. iscale = -(nmax + 0.1f*is) / max;
  1031. sumlx = suml2 = 0;
  1032. for (int i = 0; i < n; ++i) {
  1033. int l = nearest_int(iscale * x[i]);
  1034. l = MAX(-nmax, MIN(nmax-1, l));
  1035. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1036. sumlx += w*x[i]*l;
  1037. suml2 += w*l*l;
  1038. }
  1039. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1040. for (int i = 0; i < n; ++i) {
  1041. int l = nearest_int(iscale * x[i]);
  1042. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1043. }
  1044. scale = sumlx/suml2; best = scale*sumlx;
  1045. }
  1046. }
  1047. return scale;
  1048. }
  1049. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1050. float max = 0;
  1051. float amax = 0;
  1052. for (int i = 0; i < n; ++i) {
  1053. float ax = fabsf(x[i]);
  1054. if (ax > amax) { amax = ax; max = x[i]; }
  1055. }
  1056. if (!amax) { // all zero
  1057. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1058. return 0.f;
  1059. }
  1060. float iscale = -nmax / max;
  1061. if (do_rmse) {
  1062. float sumlx = 0;
  1063. float suml2 = 0;
  1064. for (int i = 0; i < n; ++i) {
  1065. int l = nearest_int(iscale * x[i]);
  1066. l = MAX(-nmax, MIN(nmax-1, l));
  1067. L[i] = l;
  1068. float w = x[i]*x[i];
  1069. sumlx += w*x[i]*l;
  1070. suml2 += w*l*l;
  1071. }
  1072. for (int itry = 0; itry < 5; ++itry) {
  1073. int n_changed = 0;
  1074. for (int i = 0; i < n; ++i) {
  1075. float w = x[i]*x[i];
  1076. float slx = sumlx - w*x[i]*L[i];
  1077. if (slx > 0) {
  1078. float sl2 = suml2 - w*L[i]*L[i];
  1079. int new_l = nearest_int(x[i] * sl2 / slx);
  1080. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1081. if (new_l != L[i]) {
  1082. slx += w*x[i]*new_l;
  1083. sl2 += w*new_l*new_l;
  1084. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1085. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1086. ++n_changed;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. if (!n_changed) {
  1092. break;
  1093. }
  1094. }
  1095. for (int i = 0; i < n; ++i) {
  1096. L[i] += nmax;
  1097. }
  1098. return sumlx / suml2;
  1099. }
  1100. for (int i = 0; i < n; ++i) {
  1101. int l = nearest_int(iscale * x[i]);
  1102. l = MAX(-nmax, MIN(nmax-1, l));
  1103. L[i] = l + nmax;
  1104. }
  1105. return 1/iscale;
  1106. }
  1107. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1108. int ntry, float alpha) {
  1109. float min = x[0];
  1110. float max = x[0];
  1111. for (int i = 1; i < n; ++i) {
  1112. if (x[i] < min) min = x[i];
  1113. if (x[i] > max) max = x[i];
  1114. }
  1115. if (max == min) {
  1116. for (int i = 0; i < n; ++i) L[i] = 0;
  1117. *the_min = 0;
  1118. return 0.f;
  1119. }
  1120. if (min > 0) min = 0;
  1121. float iscale = nmax/(max - min);
  1122. float scale = 1/iscale;
  1123. for (int itry = 0; itry < ntry; ++itry) {
  1124. float sumlx = 0; int suml2 = 0;
  1125. bool did_change = false;
  1126. for (int i = 0; i < n; ++i) {
  1127. int l = nearest_int(iscale*(x[i] - min));
  1128. l = MAX(0, MIN(nmax, l));
  1129. if (l != L[i]) {
  1130. L[i] = l;
  1131. did_change = true;
  1132. }
  1133. sumlx += (x[i] - min)*l;
  1134. suml2 += l*l;
  1135. }
  1136. scale = sumlx/suml2;
  1137. float sum = 0;
  1138. for (int i = 0; i < n; ++i) {
  1139. sum += x[i] - scale*L[i];
  1140. }
  1141. min = alpha*min + (1 - alpha)*sum/n;
  1142. if (min > 0) min = 0;
  1143. iscale = 1/scale;
  1144. if (!did_change) break;
  1145. }
  1146. *the_min = -min;
  1147. return scale;
  1148. }
  1149. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1150. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1151. float rmin, float rdelta, int nstep, bool use_mad) {
  1152. float min = x[0];
  1153. float max = x[0];
  1154. float sum_w = weights[0];
  1155. float sum_x = sum_w * x[0];
  1156. #ifdef HAVE_BUGGY_APPLE_LINKER
  1157. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1158. for (volatile int i = 1; i < n; ++i) {
  1159. #else
  1160. for (int i = 1; i < n; ++i) {
  1161. #endif
  1162. if (x[i] < min) min = x[i];
  1163. if (x[i] > max) max = x[i];
  1164. float w = weights[i];
  1165. sum_w += w;
  1166. sum_x += w * x[i];
  1167. }
  1168. if (min > 0) min = 0;
  1169. if (max == min) {
  1170. for (int i = 0; i < n; ++i) L[i] = 0;
  1171. *the_min = -min;
  1172. return 0.f;
  1173. }
  1174. float iscale = nmax/(max - min);
  1175. float scale = 1/iscale;
  1176. float best_mad = 0;
  1177. for (int i = 0; i < n; ++i) {
  1178. int l = nearest_int(iscale*(x[i] - min));
  1179. L[i] = MAX(0, MIN(nmax, l));
  1180. float diff = scale * L[i] + min - x[i];
  1181. diff = use_mad ? fabsf(diff) : diff * diff;
  1182. float w = weights[i];
  1183. best_mad += w * diff;
  1184. }
  1185. if (nstep < 1) {
  1186. *the_min = -min;
  1187. return scale;
  1188. }
  1189. for (int is = 0; is <= nstep; ++is) {
  1190. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1191. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1192. for (int i = 0; i < n; ++i) {
  1193. int l = nearest_int(iscale*(x[i] - min));
  1194. l = MAX(0, MIN(nmax, l));
  1195. Laux[i] = l;
  1196. float w = weights[i];
  1197. sum_l += w*l;
  1198. sum_l2 += w*l*l;
  1199. sum_xl += w*l*x[i];
  1200. }
  1201. float D = sum_w * sum_l2 - sum_l * sum_l;
  1202. if (D > 0) {
  1203. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1204. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1205. if (this_min > 0) {
  1206. this_min = 0;
  1207. this_scale = sum_xl / sum_l2;
  1208. }
  1209. float mad = 0;
  1210. for (int i = 0; i < n; ++i) {
  1211. float diff = this_scale * Laux[i] + this_min - x[i];
  1212. diff = use_mad ? fabsf(diff) : diff * diff;
  1213. float w = weights[i];
  1214. mad += w * diff;
  1215. }
  1216. if (mad < best_mad) {
  1217. for (int i = 0; i < n; ++i) {
  1218. L[i] = Laux[i];
  1219. }
  1220. best_mad = mad;
  1221. scale = this_scale;
  1222. min = this_min;
  1223. }
  1224. }
  1225. }
  1226. *the_min = -min;
  1227. return scale;
  1228. }
  1229. #if QK_K == 256
  1230. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1231. if (j < 4) {
  1232. *d = q[j] & 63; *m = q[j + 4] & 63;
  1233. } else {
  1234. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1235. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1236. }
  1237. }
  1238. #endif
  1239. //========================- 2-bit (de)-quantization
  1240. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1241. assert(k % QK_K == 0);
  1242. const int nb = k / QK_K;
  1243. uint8_t L[QK_K];
  1244. uint8_t Laux[16];
  1245. float weights[16];
  1246. float mins[QK_K/16];
  1247. float scales[QK_K/16];
  1248. const float q4scale = 15.f;
  1249. for (int i = 0; i < nb; i++) {
  1250. float max_scale = 0; // as we are deducting the min, scales are always positive
  1251. float max_min = 0;
  1252. for (int j = 0; j < QK_K/16; ++j) {
  1253. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1254. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1255. float scale = scales[j];
  1256. if (scale > max_scale) {
  1257. max_scale = scale;
  1258. }
  1259. float min = mins[j];
  1260. if (min > max_min) {
  1261. max_min = min;
  1262. }
  1263. }
  1264. if (max_scale > 0) {
  1265. float iscale = q4scale/max_scale;
  1266. for (int j = 0; j < QK_K/16; ++j) {
  1267. int l = nearest_int(iscale*scales[j]);
  1268. y[i].scales[j] = l;
  1269. }
  1270. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1271. } else {
  1272. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1273. y[i].d = GGML_FP32_TO_FP16(0.f);
  1274. }
  1275. if (max_min > 0) {
  1276. float iscale = q4scale/max_min;
  1277. for (int j = 0; j < QK_K/16; ++j) {
  1278. int l = nearest_int(iscale*mins[j]);
  1279. y[i].scales[j] |= (l << 4);
  1280. }
  1281. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1282. } else {
  1283. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1284. }
  1285. for (int j = 0; j < QK_K/16; ++j) {
  1286. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1287. if (!d) continue;
  1288. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1289. for (int ii = 0; ii < 16; ++ii) {
  1290. int l = nearest_int((x[16*j + ii] + dm)/d);
  1291. l = MAX(0, MIN(3, l));
  1292. L[16*j + ii] = l;
  1293. }
  1294. }
  1295. #if QK_K == 256
  1296. for (int j = 0; j < QK_K; j += 128) {
  1297. for (int l = 0; l < 32; ++l) {
  1298. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1299. }
  1300. }
  1301. #else
  1302. for (int l = 0; l < 16; ++l) {
  1303. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1304. }
  1305. #endif
  1306. x += QK_K;
  1307. }
  1308. }
  1309. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1310. assert(k % QK_K == 0);
  1311. const int nb = k / QK_K;
  1312. for (int i = 0; i < nb; i++) {
  1313. const float d = GGML_FP16_TO_FP32(x[i].d);
  1314. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1315. const uint8_t * q = x[i].qs;
  1316. #if QK_K == 256
  1317. int is = 0;
  1318. float dl, ml;
  1319. for (int n = 0; n < QK_K; n += 128) {
  1320. int shift = 0;
  1321. for (int j = 0; j < 4; ++j) {
  1322. uint8_t sc = x[i].scales[is++];
  1323. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1324. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1325. sc = x[i].scales[is++];
  1326. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1327. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1328. shift += 2;
  1329. }
  1330. q += 32;
  1331. }
  1332. #else
  1333. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1334. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1335. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1336. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1337. for (int l = 0; l < 16; ++l) {
  1338. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1339. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1340. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1341. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1342. }
  1343. y += QK_K;
  1344. #endif
  1345. }
  1346. }
  1347. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1348. quantize_row_q2_K_reference(x, vy, k);
  1349. }
  1350. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1351. (void)hist; // TODO: collect histograms
  1352. for (int j = 0; j < n; j += k) {
  1353. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1354. quantize_row_q2_K_reference(src + j, y, k);
  1355. }
  1356. return (n/QK_K*sizeof(block_q2_K));
  1357. }
  1358. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1359. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1360. float rmin, float rdelta, int nstep, bool use_mad) {
  1361. float min = x[0];
  1362. float max = x[0];
  1363. float sum_w = weights ? weights[0] : x[0]*x[0];
  1364. float sum_x = sum_w * x[0];
  1365. #ifdef HAVE_BUGGY_APPLE_LINKER
  1366. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1367. for (volatile int i = 1; i < n; ++i) {
  1368. #else
  1369. for (int i = 1; i < n; ++i) {
  1370. #endif
  1371. if (x[i] < min) min = x[i];
  1372. if (x[i] > max) max = x[i];
  1373. float w = weights ? weights[i] : x[i]*x[i];
  1374. sum_w += w;
  1375. sum_x += w * x[i];
  1376. }
  1377. if (min > 0) {
  1378. min = 0;
  1379. }
  1380. if (max <= min) {
  1381. memset(L, 0, n);
  1382. *the_min = -min;
  1383. return 0.f;
  1384. }
  1385. float iscale = nmax/(max - min);
  1386. float scale = 1/iscale;
  1387. float best_mad = 0;
  1388. for (int i = 0; i < n; ++i) {
  1389. int l = nearest_int(iscale*(x[i] - min));
  1390. L[i] = MAX(0, MIN(nmax, l));
  1391. float diff = scale * L[i] + min - x[i];
  1392. diff = use_mad ? fabsf(diff) : diff*diff;
  1393. float w = weights ? weights[i] : x[i]*x[i];
  1394. best_mad += w * diff;
  1395. }
  1396. if (nstep < 1) {
  1397. *the_min = -min;
  1398. return scale;
  1399. }
  1400. for (int is = 0; is <= nstep; ++is) {
  1401. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1402. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1403. for (int i = 0; i < n; ++i) {
  1404. int l = nearest_int(iscale*(x[i] - min));
  1405. l = MAX(0, MIN(nmax, l));
  1406. Laux[i] = l;
  1407. float w = weights ? weights[i] : x[i]*x[i];
  1408. sum_l += w*l;
  1409. sum_l2 += w*l*l;
  1410. sum_xl += w*l*x[i];
  1411. }
  1412. float D = sum_w * sum_l2 - sum_l * sum_l;
  1413. if (D > 0) {
  1414. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1415. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1416. if (this_min > 0) {
  1417. this_min = 0;
  1418. this_scale = sum_xl / sum_l2;
  1419. }
  1420. float mad = 0;
  1421. for (int i = 0; i < n; ++i) {
  1422. float diff = this_scale * Laux[i] + this_min - x[i];
  1423. diff = use_mad ? fabsf(diff) : diff*diff;
  1424. float w = weights ? weights[i] : x[i]*x[i];
  1425. mad += w * diff;
  1426. }
  1427. if (mad < best_mad) {
  1428. for (int i = 0; i < n; ++i) {
  1429. L[i] = Laux[i];
  1430. }
  1431. best_mad = mad;
  1432. scale = this_scale;
  1433. min = this_min;
  1434. }
  1435. }
  1436. }
  1437. *the_min = -min;
  1438. return scale;
  1439. }
  1440. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1441. float max = 0;
  1442. for (int i = 0; i < n; ++i) {
  1443. max = MAX(max, x[i]);
  1444. }
  1445. if (!max) { // all zero
  1446. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1447. return 0.f;
  1448. }
  1449. float iscale = nmax / max;
  1450. for (int i = 0; i < n; ++i) {
  1451. L[i] = nearest_int(iscale * x[i]);
  1452. }
  1453. float scale = 1/iscale;
  1454. float best_mse = 0;
  1455. for (int i = 0; i < n; ++i) {
  1456. float diff = x[i] - scale*L[i];
  1457. float w = quant_weights[i];
  1458. best_mse += w*diff*diff;
  1459. }
  1460. for (int is = -4; is <= 4; ++is) {
  1461. if (is == 0) continue;
  1462. float iscale_is = (0.1f*is + nmax)/max;
  1463. float scale_is = 1/iscale_is;
  1464. float mse = 0;
  1465. for (int i = 0; i < n; ++i) {
  1466. int l = nearest_int(iscale_is*x[i]);
  1467. l = MIN(nmax, l);
  1468. float diff = x[i] - scale_is*l;
  1469. float w = quant_weights[i];
  1470. mse += w*diff*diff;
  1471. }
  1472. if (mse < best_mse) {
  1473. best_mse = mse;
  1474. iscale = iscale_is;
  1475. }
  1476. }
  1477. float sumlx = 0;
  1478. float suml2 = 0;
  1479. for (int i = 0; i < n; ++i) {
  1480. int l = nearest_int(iscale * x[i]);
  1481. l = MIN(nmax, l);
  1482. L[i] = l;
  1483. float w = quant_weights[i];
  1484. sumlx += w*x[i]*l;
  1485. suml2 += w*l*l;
  1486. }
  1487. for (int itry = 0; itry < 5; ++itry) {
  1488. int n_changed = 0;
  1489. for (int i = 0; i < n; ++i) {
  1490. float w = quant_weights[i];
  1491. float slx = sumlx - w*x[i]*L[i];
  1492. float sl2 = suml2 - w*L[i]*L[i];
  1493. if (slx > 0 && sl2 > 0) {
  1494. int new_l = nearest_int(x[i] * sl2 / slx);
  1495. new_l = MIN(nmax, new_l);
  1496. if (new_l != L[i]) {
  1497. slx += w*x[i]*new_l;
  1498. sl2 += w*new_l*new_l;
  1499. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1500. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1501. ++n_changed;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. if (!n_changed) {
  1507. break;
  1508. }
  1509. }
  1510. return sumlx / suml2;
  1511. }
  1512. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1513. GGML_ASSERT(quant_weights);
  1514. assert(k % QK_K == 0);
  1515. const int nb = k / QK_K;
  1516. const bool requantize = true;
  1517. uint8_t L[QK_K];
  1518. uint8_t Laux[16];
  1519. float mins[QK_K/16];
  1520. float scales[QK_K/16];
  1521. float sw[QK_K/16];
  1522. float weight[QK_K/16];
  1523. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1524. for (int i = 0; i < nb; i++) {
  1525. memset(sw, 0, QK_K/16*sizeof(float));
  1526. float sumx2 = 0;
  1527. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1528. float sigma2 = sumx2/QK_K;
  1529. for (int j = 0; j < QK_K/16; ++j) {
  1530. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1531. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1532. for (int l = 0; l < 16; ++l) sw[j] += weight[l];
  1533. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1534. }
  1535. float dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1536. float mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1537. y[i].d = GGML_FP32_TO_FP16(dm);
  1538. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1539. dm = GGML_FP16_TO_FP32(y[i].d);
  1540. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1541. for (int j = 0; j < QK_K/16; ++j) {
  1542. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1543. }
  1544. if (requantize) {
  1545. for (int j = 0; j < QK_K/16; ++j) {
  1546. const float d = dm * (y[i].scales[j] & 0xF);
  1547. if (!d) continue;
  1548. const float m = mm * (y[i].scales[j] >> 4);
  1549. for (int ii = 0; ii < 16; ++ii) {
  1550. int l = nearest_int((x[16*j + ii] + m)/d);
  1551. l = MAX(0, MIN(3, l));
  1552. L[16*j + ii] = l;
  1553. }
  1554. }
  1555. }
  1556. #if QK_K == 256
  1557. for (int j = 0; j < QK_K; j += 128) {
  1558. for (int l = 0; l < 32; ++l) {
  1559. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1560. }
  1561. }
  1562. #else
  1563. for (int l = 0; l < 16; ++l) {
  1564. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1565. }
  1566. #endif
  1567. x += QK_K;
  1568. }
  1569. }
  1570. size_t quantize_q2_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1571. (void)hist;
  1572. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1573. if (!quant_weights) {
  1574. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1575. }
  1576. else {
  1577. char * qrow = (char *)dst;
  1578. for (int row = 0; row < nrow; ++row) {
  1579. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1580. src += n_per_row;
  1581. qrow += row_size;
  1582. }
  1583. }
  1584. return nrow * row_size;
  1585. }
  1586. //========================= 3-bit (de)-quantization
  1587. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1588. assert(k % QK_K == 0);
  1589. const int nb = k / QK_K;
  1590. int8_t L[QK_K];
  1591. float scales[QK_K / 16];
  1592. for (int i = 0; i < nb; i++) {
  1593. float max_scale = 0;
  1594. float amax = 0;
  1595. for (int j = 0; j < QK_K/16; ++j) {
  1596. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1597. float scale = fabsf(scales[j]);
  1598. if (scale > amax) {
  1599. amax = scale; max_scale = scales[j];
  1600. }
  1601. }
  1602. #if QK_K == 256
  1603. memset(y[i].scales, 0, 12);
  1604. if (max_scale) {
  1605. float iscale = -32.f/max_scale;
  1606. for (int j = 0; j < QK_K/16; ++j) {
  1607. int8_t l = nearest_int(iscale*scales[j]);
  1608. l = MAX(-32, MIN(31, l)) + 32;
  1609. if (j < 8) {
  1610. y[i].scales[j] = l & 0xF;
  1611. } else {
  1612. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1613. }
  1614. l >>= 4;
  1615. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1616. }
  1617. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1618. } else {
  1619. y[i].d = GGML_FP32_TO_FP16(0.f);
  1620. }
  1621. int8_t sc;
  1622. for (int j = 0; j < QK_K/16; ++j) {
  1623. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1624. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1625. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1626. if (!d) {
  1627. continue;
  1628. }
  1629. for (int ii = 0; ii < 16; ++ii) {
  1630. int l = nearest_int(x[16*j + ii]/d);
  1631. l = MAX(-4, MIN(3, l));
  1632. L[16*j + ii] = l + 4;
  1633. }
  1634. }
  1635. #else
  1636. if (max_scale) {
  1637. float iscale = -8.f/max_scale;
  1638. for (int j = 0; j < QK_K/16; j+=2) {
  1639. int l1 = nearest_int(iscale*scales[j]);
  1640. l1 = 8 + MAX(-8, MIN(7, l1));
  1641. int l2 = nearest_int(iscale*scales[j+1]);
  1642. l2 = 8 + MAX(-8, MIN(7, l2));
  1643. y[i].scales[j/2] = l1 | (l2 << 4);
  1644. }
  1645. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1646. } else {
  1647. for (int j = 0; j < QK_K/16; j+=2) {
  1648. y[i].scales[j/2] = 0;
  1649. }
  1650. y[i].d = GGML_FP32_TO_FP16(0.f);
  1651. }
  1652. for (int j = 0; j < QK_K/16; ++j) {
  1653. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1654. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1655. if (!d) {
  1656. continue;
  1657. }
  1658. for (int ii = 0; ii < 16; ++ii) {
  1659. int l = nearest_int(x[16*j + ii]/d);
  1660. l = MAX(-4, MIN(3, l));
  1661. L[16*j + ii] = l + 4;
  1662. }
  1663. }
  1664. #endif
  1665. memset(y[i].hmask, 0, QK_K/8);
  1666. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1667. int m = 0;
  1668. uint8_t hm = 1;
  1669. for (int j = 0; j < QK_K; ++j) {
  1670. if (L[j] > 3) {
  1671. y[i].hmask[m] |= hm;
  1672. L[j] -= 4;
  1673. }
  1674. if (++m == QK_K/8) {
  1675. m = 0; hm <<= 1;
  1676. }
  1677. }
  1678. #if QK_K == 256
  1679. for (int j = 0; j < QK_K; j += 128) {
  1680. for (int l = 0; l < 32; ++l) {
  1681. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1682. }
  1683. }
  1684. #else
  1685. for (int l = 0; l < 16; ++l) {
  1686. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1687. }
  1688. #endif
  1689. x += QK_K;
  1690. }
  1691. }
  1692. #if QK_K == 256
  1693. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1694. assert(k % QK_K == 0);
  1695. const int nb = k / QK_K;
  1696. const uint32_t kmask1 = 0x03030303;
  1697. const uint32_t kmask2 = 0x0f0f0f0f;
  1698. uint32_t aux[4];
  1699. const int8_t * scales = (const int8_t*)aux;
  1700. for (int i = 0; i < nb; i++) {
  1701. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1702. const uint8_t * restrict q = x[i].qs;
  1703. const uint8_t * restrict hm = x[i].hmask;
  1704. uint8_t m = 1;
  1705. memcpy(aux, x[i].scales, 12);
  1706. uint32_t tmp = aux[2];
  1707. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1708. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1709. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1710. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1711. int is = 0;
  1712. float dl;
  1713. for (int n = 0; n < QK_K; n += 128) {
  1714. int shift = 0;
  1715. for (int j = 0; j < 4; ++j) {
  1716. dl = d_all * (scales[is++] - 32);
  1717. for (int l = 0; l < 16; ++l) {
  1718. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1719. }
  1720. dl = d_all * (scales[is++] - 32);
  1721. for (int l = 0; l < 16; ++l) {
  1722. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1723. }
  1724. shift += 2;
  1725. m <<= 1;
  1726. }
  1727. q += 32;
  1728. }
  1729. }
  1730. }
  1731. #else
  1732. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1733. assert(k % QK_K == 0);
  1734. assert(QK_K == 64);
  1735. const int nb = k / QK_K;
  1736. for (int i = 0; i < nb; i++) {
  1737. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1738. const uint8_t * restrict q = x[i].qs;
  1739. const uint8_t * restrict hm = x[i].hmask;
  1740. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1741. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1742. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1743. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1744. for (int l=0; l<8; ++l) {
  1745. uint8_t h = hm[l];
  1746. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1747. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1748. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1749. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1750. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1751. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1752. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1753. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1754. }
  1755. y += QK_K;
  1756. }
  1757. }
  1758. #endif
  1759. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1760. quantize_row_q3_K_reference(x, vy, k);
  1761. }
  1762. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1763. (void)hist; // TODO: collect histograms
  1764. for (int j = 0; j < n; j += k) {
  1765. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1766. quantize_row_q3_K_reference(src + j, y, k);
  1767. }
  1768. return (n/QK_K*sizeof(block_q3_K));
  1769. }
  1770. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1771. #if QK_K != 256
  1772. (void)quant_weights;
  1773. quantize_row_q3_K_reference(x, y, n_per_row);
  1774. #else
  1775. assert(n_per_row % QK_K == 0);
  1776. const int nb = n_per_row / QK_K;
  1777. int8_t L[QK_K];
  1778. float scales[QK_K / 16];
  1779. float weight[16];
  1780. float sw[QK_K / 16];
  1781. int8_t Ls[QK_K / 16];
  1782. for (int i = 0; i < nb; i++) {
  1783. float sumx2 = 0;
  1784. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1785. float sigma2 = 2*sumx2/QK_K;
  1786. for (int j = 0; j < QK_K/16; ++j) {
  1787. if (quant_weights) {
  1788. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1789. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1790. } else {
  1791. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1792. }
  1793. float sumw = 0;
  1794. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1795. sw[j] = sumw;
  1796. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1797. }
  1798. memset(y[i].scales, 0, 12);
  1799. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1800. for (int j = 0; j < QK_K/16; ++j) {
  1801. int l = Ls[j];
  1802. if (j < 8) {
  1803. y[i].scales[j] = l & 0xF;
  1804. } else {
  1805. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1806. }
  1807. l >>= 4;
  1808. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1809. }
  1810. y[i].d = GGML_FP32_TO_FP16(d_block);
  1811. int8_t sc;
  1812. for (int j = 0; j < QK_K/16; ++j) {
  1813. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1814. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1815. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1816. if (!d) {
  1817. continue;
  1818. }
  1819. for (int ii = 0; ii < 16; ++ii) {
  1820. int l = nearest_int(x[16*j + ii]/d);
  1821. l = MAX(-4, MIN(3, l));
  1822. L[16*j + ii] = l + 4;
  1823. }
  1824. }
  1825. memset(y[i].hmask, 0, QK_K/8);
  1826. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1827. int m = 0;
  1828. uint8_t hm = 1;
  1829. for (int j = 0; j < QK_K; ++j) {
  1830. if (L[j] > 3) {
  1831. y[i].hmask[m] |= hm;
  1832. L[j] -= 4;
  1833. }
  1834. if (++m == QK_K/8) {
  1835. m = 0; hm <<= 1;
  1836. }
  1837. }
  1838. for (int j = 0; j < QK_K; j += 128) {
  1839. for (int l = 0; l < 32; ++l) {
  1840. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1841. }
  1842. }
  1843. x += QK_K;
  1844. }
  1845. #endif
  1846. }
  1847. size_t quantize_q3_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1848. (void)hist;
  1849. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1850. if (!quant_weights) {
  1851. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1852. }
  1853. else {
  1854. char * qrow = (char *)dst;
  1855. for (int row = 0; row < nrow; ++row) {
  1856. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1857. src += n_per_row;
  1858. qrow += row_size;
  1859. }
  1860. }
  1861. return nrow * row_size;
  1862. }
  1863. // ====================== 4-bit (de)-quantization
  1864. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1865. assert(k % QK_K == 0);
  1866. const int nb = k / QK_K;
  1867. uint8_t L[QK_K];
  1868. uint8_t Laux[32];
  1869. float weights[32];
  1870. float mins[QK_K/32];
  1871. float scales[QK_K/32];
  1872. for (int i = 0; i < nb; i++) {
  1873. float max_scale = 0; // as we are deducting the min, scales are always positive
  1874. float max_min = 0;
  1875. for (int j = 0; j < QK_K/32; ++j) {
  1876. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1877. float sum_x2 = 0;
  1878. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1879. float av_x = sqrtf(sum_x2/32);
  1880. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1881. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1882. float scale = scales[j];
  1883. if (scale > max_scale) {
  1884. max_scale = scale;
  1885. }
  1886. float min = mins[j];
  1887. if (min > max_min) {
  1888. max_min = min;
  1889. }
  1890. }
  1891. #if QK_K == 256
  1892. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1893. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1894. for (int j = 0; j < QK_K/32; ++j) {
  1895. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1896. uint8_t lm = nearest_int(inv_min*mins[j]);
  1897. ls = MIN(63, ls);
  1898. lm = MIN(63, lm);
  1899. if (j < 4) {
  1900. y[i].scales[j] = ls;
  1901. y[i].scales[j+4] = lm;
  1902. } else {
  1903. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1904. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1905. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1906. }
  1907. }
  1908. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1909. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1910. uint8_t sc, m;
  1911. for (int j = 0; j < QK_K/32; ++j) {
  1912. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1913. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1914. if (!d) continue;
  1915. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1916. for (int ii = 0; ii < 32; ++ii) {
  1917. int l = nearest_int((x[32*j + ii] + dm)/d);
  1918. l = MAX(0, MIN(15, l));
  1919. L[32*j + ii] = l;
  1920. }
  1921. }
  1922. #else
  1923. const float s_factor = 15.f;
  1924. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1925. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1926. int d1 = nearest_int(inv_scale*scales[0]);
  1927. int m1 = nearest_int(inv_min*mins[0]);
  1928. int d2 = nearest_int(inv_scale*scales[1]);
  1929. int m2 = nearest_int(inv_min*mins[1]);
  1930. y[i].scales[0] = d1 | (m1 << 4);
  1931. y[i].scales[1] = d2 | (m2 << 4);
  1932. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1933. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1934. float sumlx = 0;
  1935. int suml2 = 0;
  1936. for (int j = 0; j < QK_K/32; ++j) {
  1937. const uint8_t sd = y[i].scales[j] & 0xF;
  1938. const uint8_t sm = y[i].scales[j] >> 4;
  1939. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1940. if (!d) continue;
  1941. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1942. for (int ii = 0; ii < 32; ++ii) {
  1943. int l = nearest_int((x[32*j + ii] + m)/d);
  1944. l = MAX(0, MIN(15, l));
  1945. L[32*j + ii] = l;
  1946. sumlx += (x[32*j + ii] + m)*l*sd;
  1947. suml2 += l*l*sd*sd;
  1948. }
  1949. }
  1950. if (suml2) {
  1951. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1952. }
  1953. #endif
  1954. uint8_t * q = y[i].qs;
  1955. for (int j = 0; j < QK_K; j += 64) {
  1956. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1957. q += 32;
  1958. }
  1959. x += QK_K;
  1960. }
  1961. }
  1962. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1963. assert(k % QK_K == 0);
  1964. const int nb = k / QK_K;
  1965. for (int i = 0; i < nb; i++) {
  1966. const uint8_t * q = x[i].qs;
  1967. #if QK_K == 256
  1968. const float d = GGML_FP16_TO_FP32(x[i].d);
  1969. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1970. int is = 0;
  1971. uint8_t sc, m;
  1972. for (int j = 0; j < QK_K; j += 64) {
  1973. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1974. const float d1 = d * sc; const float m1 = min * m;
  1975. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1976. const float d2 = d * sc; const float m2 = min * m;
  1977. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1978. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1979. q += 32; is += 2;
  1980. }
  1981. #else
  1982. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1983. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1984. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1985. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1986. for (int l = 0; l < 32; ++l) {
  1987. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1988. y[l+32] = d2 * (q[l] >> 4) - m2;
  1989. }
  1990. y += QK_K;
  1991. #endif
  1992. }
  1993. }
  1994. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1995. assert(k % QK_K == 0);
  1996. block_q4_K * restrict y = vy;
  1997. quantize_row_q4_K_reference(x, y, k);
  1998. }
  1999. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2000. assert(k % QK_K == 0);
  2001. (void)hist; // TODO: collect histograms
  2002. for (int j = 0; j < n; j += k) {
  2003. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  2004. quantize_row_q4_K_reference(src + j, y, k);
  2005. }
  2006. return (n/QK_K*sizeof(block_q4_K));
  2007. }
  2008. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2009. #if QK_K != 256
  2010. (void)quant_weights;
  2011. quantize_row_q4_K_reference(x, y, n_per_row);
  2012. #else
  2013. assert(n_per_row % QK_K == 0);
  2014. const int nb = n_per_row / QK_K;
  2015. uint8_t L[QK_K];
  2016. uint8_t Laux[32];
  2017. uint8_t Ls[QK_K/32];
  2018. uint8_t Lm[QK_K/32];
  2019. float weights[32];
  2020. float sw[QK_K/32];
  2021. float mins[QK_K/32];
  2022. float scales[QK_K/32];
  2023. for (int i = 0; i < nb; i++) {
  2024. float sum_x2 = 0;
  2025. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2026. float sigma2 = 2*sum_x2/QK_K;
  2027. float av_x = sqrtf(sigma2);
  2028. for (int j = 0; j < QK_K/32; ++j) {
  2029. if (quant_weights) {
  2030. const float * qw = quant_weights + QK_K*i + 32*j;
  2031. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2032. } else {
  2033. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2034. }
  2035. float sumw = 0;
  2036. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2037. sw[j] = sumw;
  2038. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2039. }
  2040. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2041. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2042. for (int j = 0; j < QK_K/32; ++j) {
  2043. uint8_t ls = Ls[j];
  2044. uint8_t lm = Lm[j];
  2045. if (j < 4) {
  2046. y[i].scales[j] = ls;
  2047. y[i].scales[j+4] = lm;
  2048. } else {
  2049. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2050. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2051. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2052. }
  2053. }
  2054. y[i].d = GGML_FP32_TO_FP16(d_block);
  2055. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2056. uint8_t sc, m;
  2057. for (int j = 0; j < QK_K/32; ++j) {
  2058. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2059. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2060. if (!d) continue;
  2061. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2062. for (int ii = 0; ii < 32; ++ii) {
  2063. int l = nearest_int((x[32*j + ii] + dm)/d);
  2064. l = MAX(0, MIN(15, l));
  2065. L[32*j + ii] = l;
  2066. }
  2067. }
  2068. uint8_t * q = y[i].qs;
  2069. for (int j = 0; j < QK_K; j += 64) {
  2070. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2071. q += 32;
  2072. }
  2073. x += QK_K;
  2074. }
  2075. #endif
  2076. }
  2077. size_t quantize_q4_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2078. (void)hist;
  2079. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2080. if (!quant_weights) {
  2081. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2082. }
  2083. else {
  2084. char * qrow = (char *)dst;
  2085. for (int row = 0; row < nrow; ++row) {
  2086. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2087. src += n_per_row;
  2088. qrow += row_size;
  2089. }
  2090. }
  2091. return nrow * row_size;
  2092. }
  2093. // ====================== 5-bit (de)-quantization
  2094. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2095. assert(k % QK_K == 0);
  2096. const int nb = k / QK_K;
  2097. #if QK_K == 256
  2098. uint8_t L[QK_K];
  2099. float mins[QK_K/32];
  2100. float scales[QK_K/32];
  2101. float weights[32];
  2102. uint8_t Laux[32];
  2103. #else
  2104. int8_t L[QK_K];
  2105. float scales[QK_K/16];
  2106. #endif
  2107. for (int i = 0; i < nb; i++) {
  2108. #if QK_K == 256
  2109. float max_scale = 0; // as we are deducting the min, scales are always positive
  2110. float max_min = 0;
  2111. for (int j = 0; j < QK_K/32; ++j) {
  2112. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2113. float sum_x2 = 0;
  2114. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2115. float av_x = sqrtf(sum_x2/32);
  2116. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2117. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2118. float scale = scales[j];
  2119. if (scale > max_scale) {
  2120. max_scale = scale;
  2121. }
  2122. float min = mins[j];
  2123. if (min > max_min) {
  2124. max_min = min;
  2125. }
  2126. }
  2127. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2128. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2129. for (int j = 0; j < QK_K/32; ++j) {
  2130. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2131. uint8_t lm = nearest_int(inv_min*mins[j]);
  2132. ls = MIN(63, ls);
  2133. lm = MIN(63, lm);
  2134. if (j < 4) {
  2135. y[i].scales[j] = ls;
  2136. y[i].scales[j+4] = lm;
  2137. } else {
  2138. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2139. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2140. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2141. }
  2142. }
  2143. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2144. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2145. uint8_t sc, m;
  2146. for (int j = 0; j < QK_K/32; ++j) {
  2147. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2148. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2149. if (!d) continue;
  2150. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2151. for (int ii = 0; ii < 32; ++ii) {
  2152. int l = nearest_int((x[32*j + ii] + dm)/d);
  2153. l = MAX(0, MIN(31, l));
  2154. L[32*j + ii] = l;
  2155. }
  2156. }
  2157. uint8_t * restrict qh = y[i].qh;
  2158. uint8_t * restrict ql = y[i].qs;
  2159. memset(qh, 0, QK_K/8);
  2160. uint8_t m1 = 1, m2 = 2;
  2161. for (int n = 0; n < QK_K; n += 64) {
  2162. for (int j = 0; j < 32; ++j) {
  2163. int l1 = L[n + j];
  2164. if (l1 > 15) {
  2165. l1 -= 16; qh[j] |= m1;
  2166. }
  2167. int l2 = L[n + j + 32];
  2168. if (l2 > 15) {
  2169. l2 -= 16; qh[j] |= m2;
  2170. }
  2171. ql[j] = l1 | (l2 << 4);
  2172. }
  2173. m1 <<= 2; m2 <<= 2;
  2174. ql += 32;
  2175. }
  2176. #else
  2177. float max_scale = 0, amax = 0;
  2178. for (int j = 0; j < QK_K/16; ++j) {
  2179. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2180. float abs_scale = fabsf(scales[j]);
  2181. if (abs_scale > amax) {
  2182. amax = abs_scale;
  2183. max_scale = scales[j];
  2184. }
  2185. }
  2186. float iscale = -128.f/max_scale;
  2187. for (int j = 0; j < QK_K/16; ++j) {
  2188. int l = nearest_int(iscale*scales[j]);
  2189. y[i].scales[j] = MAX(-128, MIN(127, l));
  2190. }
  2191. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2192. for (int j = 0; j < QK_K/16; ++j) {
  2193. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2194. if (!d) continue;
  2195. for (int ii = 0; ii < 16; ++ii) {
  2196. int l = nearest_int(x[16*j + ii]/d);
  2197. l = MAX(-16, MIN(15, l));
  2198. L[16*j + ii] = l + 16;
  2199. }
  2200. }
  2201. uint8_t * restrict qh = y[i].qh;
  2202. uint8_t * restrict ql = y[i].qs;
  2203. memset(qh, 0, QK_K/8);
  2204. for (int j = 0; j < 32; ++j) {
  2205. int jm = j%8;
  2206. int is = j/8;
  2207. int l1 = L[j];
  2208. if (l1 > 15) {
  2209. l1 -= 16; qh[jm] |= (1 << is);
  2210. }
  2211. int l2 = L[j + 32];
  2212. if (l2 > 15) {
  2213. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2214. }
  2215. ql[j] = l1 | (l2 << 4);
  2216. }
  2217. #endif
  2218. x += QK_K;
  2219. }
  2220. }
  2221. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2222. assert(k % QK_K == 0);
  2223. const int nb = k / QK_K;
  2224. for (int i = 0; i < nb; i++) {
  2225. const uint8_t * ql = x[i].qs;
  2226. const uint8_t * qh = x[i].qh;
  2227. #if QK_K == 256
  2228. const float d = GGML_FP16_TO_FP32(x[i].d);
  2229. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2230. int is = 0;
  2231. uint8_t sc, m;
  2232. uint8_t u1 = 1, u2 = 2;
  2233. for (int j = 0; j < QK_K; j += 64) {
  2234. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2235. const float d1 = d * sc; const float m1 = min * m;
  2236. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2237. const float d2 = d * sc; const float m2 = min * m;
  2238. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2239. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2240. ql += 32; is += 2;
  2241. u1 <<= 2; u2 <<= 2;
  2242. }
  2243. #else
  2244. float d = GGML_FP16_TO_FP32(x[i].d);
  2245. const int8_t * restrict s = x[i].scales;
  2246. for (int l = 0; l < 8; ++l) {
  2247. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2248. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2249. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2250. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2251. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2252. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2253. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2254. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2255. }
  2256. y += QK_K;
  2257. #endif
  2258. }
  2259. }
  2260. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2261. assert(k % QK_K == 0);
  2262. block_q5_K * restrict y = vy;
  2263. quantize_row_q5_K_reference(x, y, k);
  2264. }
  2265. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2266. assert(k % QK_K == 0);
  2267. (void)hist; // TODO: collect histograms
  2268. for (int j = 0; j < n; j += k) {
  2269. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  2270. quantize_row_q5_K_reference(src + j, y, k);
  2271. }
  2272. return (n/QK_K*sizeof(block_q5_K));
  2273. }
  2274. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2275. #if QK_K != 256
  2276. (void)quant_weights;
  2277. quantize_row_q5_K_reference(x, y, n_per_row);
  2278. #else
  2279. assert(n_per_row % QK_K == 0);
  2280. const int nb = n_per_row / QK_K;
  2281. uint8_t L[QK_K];
  2282. uint8_t Laux[32];
  2283. uint8_t Ls[QK_K/32];
  2284. uint8_t Lm[QK_K/32];
  2285. float mins[QK_K/32];
  2286. float scales[QK_K/32];
  2287. float sw[QK_K/32];
  2288. float weights[32];
  2289. for (int i = 0; i < nb; i++) {
  2290. float sum_x2 = 0;
  2291. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2292. float sigma2 = 2*sum_x2/QK_K;
  2293. float av_x = sqrtf(sigma2);
  2294. for (int j = 0; j < QK_K/32; ++j) {
  2295. if (quant_weights) {
  2296. const float * qw = quant_weights + QK_K*i + 32*j;
  2297. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2298. } else {
  2299. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2300. }
  2301. float sumw = 0;
  2302. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2303. sw[j] = sumw;
  2304. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2305. }
  2306. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2307. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2308. for (int j = 0; j < QK_K/32; ++j) {
  2309. uint8_t ls = Ls[j];
  2310. uint8_t lm = Lm[j];
  2311. ls = MIN(63, ls);
  2312. lm = MIN(63, lm);
  2313. if (j < 4) {
  2314. y[i].scales[j] = ls;
  2315. y[i].scales[j+4] = lm;
  2316. } else {
  2317. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2318. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2319. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2320. }
  2321. }
  2322. y[i].d = GGML_FP32_TO_FP16(d_block);
  2323. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2324. uint8_t sc, m;
  2325. for (int j = 0; j < QK_K/32; ++j) {
  2326. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2327. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2328. if (!d) continue;
  2329. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2330. for (int ii = 0; ii < 32; ++ii) {
  2331. int l = nearest_int((x[32*j + ii] + dm)/d);
  2332. l = MAX(0, MIN(31, l));
  2333. L[32*j + ii] = l;
  2334. }
  2335. }
  2336. uint8_t * restrict qh = y[i].qh;
  2337. uint8_t * restrict ql = y[i].qs;
  2338. memset(qh, 0, QK_K/8);
  2339. uint8_t m1 = 1, m2 = 2;
  2340. for (int n = 0; n < QK_K; n += 64) {
  2341. for (int j = 0; j < 32; ++j) {
  2342. int l1 = L[n + j];
  2343. if (l1 > 15) {
  2344. l1 -= 16; qh[j] |= m1;
  2345. }
  2346. int l2 = L[n + j + 32];
  2347. if (l2 > 15) {
  2348. l2 -= 16; qh[j] |= m2;
  2349. }
  2350. ql[j] = l1 | (l2 << 4);
  2351. }
  2352. m1 <<= 2; m2 <<= 2;
  2353. ql += 32;
  2354. }
  2355. x += QK_K;
  2356. }
  2357. #endif
  2358. }
  2359. size_t quantize_q5_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2360. (void)hist;
  2361. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2362. if (!quant_weights) {
  2363. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2364. }
  2365. else {
  2366. char * qrow = (char *)dst;
  2367. for (int row = 0; row < nrow; ++row) {
  2368. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2369. src += n_per_row;
  2370. qrow += row_size;
  2371. }
  2372. }
  2373. return nrow * row_size;
  2374. }
  2375. // ====================== 6-bit (de)-quantization
  2376. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2377. assert(k % QK_K == 0);
  2378. const int nb = k / QK_K;
  2379. int8_t L[QK_K];
  2380. float scales[QK_K/16];
  2381. for (int i = 0; i < nb; i++) {
  2382. float max_scale = 0;
  2383. float max_abs_scale = 0;
  2384. for (int ib = 0; ib < QK_K/16; ++ib) {
  2385. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2386. scales[ib] = scale;
  2387. const float abs_scale = fabsf(scale);
  2388. if (abs_scale > max_abs_scale) {
  2389. max_abs_scale = abs_scale;
  2390. max_scale = scale;
  2391. }
  2392. }
  2393. if (!max_abs_scale) {
  2394. memset(&y[i], 0, sizeof(block_q6_K));
  2395. y[i].d = GGML_FP32_TO_FP16(0.f);
  2396. x += QK_K;
  2397. continue;
  2398. }
  2399. float iscale = -128.f/max_scale;
  2400. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2401. for (int ib = 0; ib < QK_K/16; ++ib) {
  2402. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2403. }
  2404. for (int j = 0; j < QK_K/16; ++j) {
  2405. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2406. if (!d) {
  2407. continue;
  2408. }
  2409. for (int ii = 0; ii < 16; ++ii) {
  2410. int l = nearest_int(x[16*j + ii]/d);
  2411. l = MAX(-32, MIN(31, l));
  2412. L[16*j + ii] = l + 32;
  2413. }
  2414. }
  2415. uint8_t * restrict ql = y[i].ql;
  2416. uint8_t * restrict qh = y[i].qh;
  2417. #if QK_K == 256
  2418. for (int j = 0; j < QK_K; j += 128) {
  2419. for (int l = 0; l < 32; ++l) {
  2420. const uint8_t q1 = L[j + l + 0] & 0xF;
  2421. const uint8_t q2 = L[j + l + 32] & 0xF;
  2422. const uint8_t q3 = L[j + l + 64] & 0xF;
  2423. const uint8_t q4 = L[j + l + 96] & 0xF;
  2424. ql[l+ 0] = q1 | (q3 << 4);
  2425. ql[l+32] = q2 | (q4 << 4);
  2426. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2427. }
  2428. ql += 64;
  2429. qh += 32;
  2430. }
  2431. #else
  2432. for (int l = 0; l < 32; ++l) {
  2433. const uint8_t q1 = L[l + 0] & 0xF;
  2434. const uint8_t q2 = L[l + 32] & 0xF;
  2435. ql[l] = q1 | (q2 << 4);
  2436. }
  2437. for (int l = 0; l < 16; ++l) {
  2438. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2439. }
  2440. #endif
  2441. x += QK_K;
  2442. }
  2443. }
  2444. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2445. assert(k % QK_K == 0);
  2446. const int nb = k / QK_K;
  2447. for (int i = 0; i < nb; i++) {
  2448. const float d = GGML_FP16_TO_FP32(x[i].d);
  2449. const uint8_t * restrict ql = x[i].ql;
  2450. const uint8_t * restrict qh = x[i].qh;
  2451. const int8_t * restrict sc = x[i].scales;
  2452. #if QK_K == 256
  2453. for (int n = 0; n < QK_K; n += 128) {
  2454. for (int l = 0; l < 32; ++l) {
  2455. int is = l/16;
  2456. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2457. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2458. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2459. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2460. y[l + 0] = d * sc[is + 0] * q1;
  2461. y[l + 32] = d * sc[is + 2] * q2;
  2462. y[l + 64] = d * sc[is + 4] * q3;
  2463. y[l + 96] = d * sc[is + 6] * q4;
  2464. }
  2465. y += 128;
  2466. ql += 64;
  2467. qh += 32;
  2468. sc += 8;
  2469. }
  2470. #else
  2471. for (int l = 0; l < 16; ++l) {
  2472. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2473. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2474. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2475. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2476. y[l+ 0] = d * sc[0] * q1;
  2477. y[l+16] = d * sc[1] * q2;
  2478. y[l+32] = d * sc[2] * q3;
  2479. y[l+48] = d * sc[3] * q4;
  2480. }
  2481. y += 64;
  2482. #endif
  2483. }
  2484. }
  2485. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2486. assert(k % QK_K == 0);
  2487. block_q6_K * restrict y = vy;
  2488. quantize_row_q6_K_reference(x, y, k);
  2489. }
  2490. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  2491. assert(k % QK_K == 0);
  2492. (void)hist; // TODO: collect histograms
  2493. for (int j = 0; j < n; j += k) {
  2494. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  2495. quantize_row_q6_K_reference(src + j, y, k);
  2496. }
  2497. return (n/QK_K*sizeof(block_q6_K));
  2498. }
  2499. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2500. #if QK_K != 256
  2501. (void)quant_weights;
  2502. quantize_row_q6_K_reference(x, y, n_per_row);
  2503. #else
  2504. assert(n_per_row % QK_K == 0);
  2505. const int nb = n_per_row / QK_K;
  2506. int8_t L[QK_K];
  2507. float scales[QK_K/16];
  2508. //float weights[16];
  2509. for (int i = 0; i < nb; i++) {
  2510. //float sum_x2 = 0;
  2511. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2512. //float sigma2 = sum_x2/QK_K;
  2513. float max_scale = 0;
  2514. float max_abs_scale = 0;
  2515. for (int ib = 0; ib < QK_K/16; ++ib) {
  2516. float scale;
  2517. if (quant_weights) {
  2518. const float * qw = quant_weights + QK_K*i + 16*ib;
  2519. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2520. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2521. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2522. } else {
  2523. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2524. }
  2525. scales[ib] = scale;
  2526. const float abs_scale = fabsf(scale);
  2527. if (abs_scale > max_abs_scale) {
  2528. max_abs_scale = abs_scale;
  2529. max_scale = scale;
  2530. }
  2531. }
  2532. if (!max_abs_scale) {
  2533. memset(&y[i], 0, sizeof(block_q6_K));
  2534. y[i].d = GGML_FP32_TO_FP16(0.f);
  2535. x += QK_K;
  2536. continue;
  2537. }
  2538. float iscale = -128.f/max_scale;
  2539. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2540. for (int ib = 0; ib < QK_K/16; ++ib) {
  2541. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2542. }
  2543. for (int j = 0; j < QK_K/16; ++j) {
  2544. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2545. if (!d) {
  2546. continue;
  2547. }
  2548. for (int ii = 0; ii < 16; ++ii) {
  2549. int l = nearest_int(x[16*j + ii]/d);
  2550. l = MAX(-32, MIN(31, l));
  2551. L[16*j + ii] = l + 32;
  2552. }
  2553. }
  2554. uint8_t * restrict ql = y[i].ql;
  2555. uint8_t * restrict qh = y[i].qh;
  2556. for (int j = 0; j < QK_K; j += 128) {
  2557. for (int l = 0; l < 32; ++l) {
  2558. const uint8_t q1 = L[j + l + 0] & 0xF;
  2559. const uint8_t q2 = L[j + l + 32] & 0xF;
  2560. const uint8_t q3 = L[j + l + 64] & 0xF;
  2561. const uint8_t q4 = L[j + l + 96] & 0xF;
  2562. ql[l+ 0] = q1 | (q3 << 4);
  2563. ql[l+32] = q2 | (q4 << 4);
  2564. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2565. }
  2566. ql += 64;
  2567. qh += 32;
  2568. }
  2569. x += QK_K;
  2570. }
  2571. #endif
  2572. }
  2573. size_t quantize_q6_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2574. (void)hist;
  2575. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2576. if (!quant_weights) {
  2577. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2578. }
  2579. else {
  2580. char * qrow = (char *)dst;
  2581. for (int row = 0; row < nrow; ++row) {
  2582. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2583. src += n_per_row;
  2584. qrow += row_size;
  2585. }
  2586. }
  2587. return nrow * row_size;
  2588. }
  2589. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2590. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2591. if (!quant_weights) {
  2592. quantize_row_q4_0_reference(x, y, n_per_row);
  2593. return;
  2594. }
  2595. float weight[QK4_0];
  2596. int8_t L[QK4_0];
  2597. float sum_x2 = 0;
  2598. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2599. float sigma2 = sum_x2/n_per_row;
  2600. const int nb = n_per_row/QK4_0;
  2601. for (int ib = 0; ib < nb; ++ib) {
  2602. const float * xb = x + QK4_0 * ib;
  2603. const float * qw = quant_weights + QK4_0 * ib;
  2604. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2605. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2606. y[ib].d = GGML_FP32_TO_FP16(d);
  2607. for (int j = 0; j < 16; ++j) {
  2608. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2609. }
  2610. }
  2611. }
  2612. size_t quantize_q4_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2613. if (!quant_weights) {
  2614. return ggml_quantize_q4_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2615. }
  2616. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2617. char * qrow = (char *)dst;
  2618. for (int row = 0; row < nrow; ++row) {
  2619. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2620. src += n_per_row;
  2621. qrow += row_size;
  2622. }
  2623. return nrow * row_size;
  2624. }
  2625. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2626. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2627. if (!quant_weights) {
  2628. quantize_row_q4_1_reference(x, y, n_per_row);
  2629. return;
  2630. }
  2631. float weight[QK4_1];
  2632. uint8_t L[QK4_1], Laux[QK4_1];
  2633. float sum_x2 = 0;
  2634. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2635. float sigma2 = sum_x2/n_per_row;
  2636. const int nb = n_per_row/QK4_1;
  2637. for (int ib = 0; ib < nb; ++ib) {
  2638. const float * xb = x + QK4_1 * ib;
  2639. const float * qw = quant_weights + QK4_1 * ib;
  2640. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2641. float min;
  2642. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2643. y[ib].d = GGML_FP32_TO_FP16(d);
  2644. y[ib].m = GGML_FP32_TO_FP16(-min);
  2645. for (int j = 0; j < 16; ++j) {
  2646. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2647. }
  2648. }
  2649. }
  2650. size_t quantize_q4_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2651. if (!quant_weights) {
  2652. return ggml_quantize_q4_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2653. }
  2654. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2655. char * qrow = (char *)dst;
  2656. for (int row = 0; row < nrow; ++row) {
  2657. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2658. src += n_per_row;
  2659. qrow += row_size;
  2660. }
  2661. return nrow * row_size;
  2662. }
  2663. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2664. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2665. if (!quant_weights) {
  2666. quantize_row_q5_0_reference(x, y, n_per_row);
  2667. return;
  2668. }
  2669. float weight[QK5_0];
  2670. int8_t L[QK5_0];
  2671. float sum_x2 = 0;
  2672. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2673. float sigma2 = sum_x2/n_per_row;
  2674. const int nb = n_per_row/QK5_0;
  2675. for (int ib = 0; ib < nb; ++ib) {
  2676. const float * xb = x + QK5_0 * ib;
  2677. const float * qw = quant_weights + QK5_0 * ib;
  2678. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2679. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2680. y[ib].d = GGML_FP32_TO_FP16(d);
  2681. uint32_t qh = 0;
  2682. for (int j = 0; j < 16; ++j) {
  2683. const uint8_t xi0 = L[j];
  2684. const uint8_t xi1 = L[j+16];
  2685. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2686. // get the 5-th bit and store it in qh at the right position
  2687. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2688. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2689. }
  2690. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2691. }
  2692. }
  2693. size_t quantize_q5_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2694. if (!quant_weights) {
  2695. return ggml_quantize_q5_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2696. }
  2697. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2698. char * qrow = (char *)dst;
  2699. for (int row = 0; row < nrow; ++row) {
  2700. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2701. src += n_per_row;
  2702. qrow += row_size;
  2703. }
  2704. return nrow * row_size;
  2705. }
  2706. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2707. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2708. if (!quant_weights) {
  2709. quantize_row_q5_1_reference(x, y, n_per_row);
  2710. return;
  2711. }
  2712. float weight[QK5_1];
  2713. uint8_t L[QK5_1], Laux[QK5_1];
  2714. float sum_x2 = 0;
  2715. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2716. float sigma2 = sum_x2/n_per_row;
  2717. const int nb = n_per_row/QK5_1;
  2718. for (int ib = 0; ib < nb; ++ib) {
  2719. const float * xb = x + QK5_1 * ib;
  2720. const float * qw = quant_weights + QK5_1 * ib;
  2721. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2722. float min;
  2723. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2724. y[ib].d = GGML_FP32_TO_FP16(d);
  2725. y[ib].m = GGML_FP32_TO_FP16(-min);
  2726. uint32_t qh = 0;
  2727. for (int j = 0; j < 16; ++j) {
  2728. const uint8_t xi0 = L[j];
  2729. const uint8_t xi1 = L[j+16];
  2730. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2731. // get the 5-th bit and store it in qh at the right position
  2732. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2733. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2734. }
  2735. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2736. }
  2737. }
  2738. size_t quantize_q5_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2739. if (!quant_weights) {
  2740. return ggml_quantize_q5_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2741. }
  2742. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2743. char * qrow = (char *)dst;
  2744. for (int row = 0; row < nrow; ++row) {
  2745. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2746. src += n_per_row;
  2747. qrow += row_size;
  2748. }
  2749. return nrow * row_size;
  2750. }
  2751. // ====================== "True" 2-bit (de)-quantization
  2752. static const uint64_t iq2xxs_grid[256] = {
  2753. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2754. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2755. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2756. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2757. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2758. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2759. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2760. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2761. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2762. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2763. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2764. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2765. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2766. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2767. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2768. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2769. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2770. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2771. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2772. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2773. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2774. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2775. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2776. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2777. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2778. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2779. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2780. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2781. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2782. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2783. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2784. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2785. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2786. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2787. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2788. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2789. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2790. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2791. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2792. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2793. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2794. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2795. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2796. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2797. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2798. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2799. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2800. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2801. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2802. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2803. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2804. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2805. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2806. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2807. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2808. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2809. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2810. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2811. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2812. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2813. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2814. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2815. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2816. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2817. };
  2818. static const uint64_t iq2xs_grid[512] = {
  2819. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2820. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2821. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2822. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2823. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2824. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2825. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2826. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2827. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2828. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2829. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2830. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2831. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2832. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2833. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2834. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2835. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2836. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2837. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2838. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2839. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2840. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2841. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2842. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2843. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2844. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2845. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2846. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2847. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2848. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2849. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2850. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2851. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2852. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2853. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2854. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2855. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2856. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2857. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2858. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2859. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2860. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2861. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2862. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2863. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2864. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2865. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2866. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2867. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2868. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2869. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2870. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2871. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2872. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2873. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2874. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2875. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2876. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2877. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2878. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2879. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2880. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2881. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2882. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2883. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2884. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2885. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2886. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2887. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2888. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2889. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2890. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2891. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2892. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2893. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2894. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2895. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2896. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2897. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2898. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2899. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2900. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2901. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2902. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2903. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2904. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2905. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2906. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2907. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2908. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2909. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2910. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2911. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2912. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2913. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2914. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2915. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2916. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2917. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2918. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2919. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2920. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2921. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2922. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2923. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2924. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2925. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2926. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2927. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2928. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2929. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2930. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2931. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2932. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2933. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2934. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2935. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2936. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2937. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2938. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2939. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2940. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2941. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2942. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2943. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2944. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2945. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2946. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2947. };
  2948. static const uint32_t iq3xxs_grid[256] = {
  2949. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  2950. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  2951. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  2952. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  2953. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  2954. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  2955. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  2956. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  2957. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  2958. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  2959. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  2960. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  2961. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  2962. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  2963. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  2964. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  2965. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  2966. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  2967. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  2968. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  2969. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  2970. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  2971. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  2972. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  2973. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  2974. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  2975. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  2976. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  2977. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  2978. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  2979. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  2980. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  2981. };
  2982. static const uint8_t ksigns_iq2xs[128] = {
  2983. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2984. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2985. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2986. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2987. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2988. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2989. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2990. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2991. };
  2992. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2993. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2994. assert(k % QK_K == 0);
  2995. const int nb = k / QK_K;
  2996. uint32_t aux32[2];
  2997. const uint8_t * aux8 = (const uint8_t *)aux32;
  2998. for (int i = 0; i < nb; i++) {
  2999. const float d = GGML_FP16_TO_FP32(x[i].d);
  3000. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3001. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  3002. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  3003. for (int l = 0; l < 4; ++l) {
  3004. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3005. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  3006. for (int j = 0; j < 8; ++j) {
  3007. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3008. }
  3009. y += 8;
  3010. }
  3011. }
  3012. }
  3013. }
  3014. // ====================== 2.3125 bpw (de)-quantization
  3015. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  3016. assert(k % QK_K == 0);
  3017. const int nb = k / QK_K;
  3018. float db[2];
  3019. for (int i = 0; i < nb; i++) {
  3020. const float d = GGML_FP16_TO_FP32(x[i].d);
  3021. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3022. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  3023. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  3024. for (int l = 0; l < 4; ++l) {
  3025. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  3026. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  3027. for (int j = 0; j < 8; ++j) {
  3028. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3029. }
  3030. y += 8;
  3031. }
  3032. }
  3033. }
  3034. }
  3035. // ====================== 3.0625 bpw (de)-quantization
  3036. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  3037. assert(k % QK_K == 0);
  3038. const int nb = k / QK_K;
  3039. uint32_t aux32;
  3040. for (int i = 0; i < nb; i++) {
  3041. const float d = GGML_FP16_TO_FP32(x[i].d);
  3042. const uint8_t * qs = x[i].qs;
  3043. const uint8_t * scales_and_signs = qs + QK_K/4;
  3044. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3045. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  3046. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  3047. for (int l = 0; l < 4; ++l) {
  3048. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  3049. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  3050. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  3051. for (int j = 0; j < 4; ++j) {
  3052. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  3053. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  3054. }
  3055. y += 8;
  3056. }
  3057. qs += 8;
  3058. }
  3059. }
  3060. }
  3061. //===================================== Q8_K ==============================================
  3062. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3063. assert(k % QK_K == 0);
  3064. const int nb = k / QK_K;
  3065. for (int i = 0; i < nb; i++) {
  3066. float max = 0;
  3067. float amax = 0;
  3068. for (int j = 0; j < QK_K; ++j) {
  3069. float ax = fabsf(x[j]);
  3070. if (ax > amax) {
  3071. amax = ax; max = x[j];
  3072. }
  3073. }
  3074. if (!amax) {
  3075. y[i].d = 0;
  3076. memset(y[i].qs, 0, QK_K);
  3077. x += QK_K;
  3078. continue;
  3079. }
  3080. //const float iscale = -128.f/max;
  3081. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3082. const float iscale = -127.f/max;
  3083. for (int j = 0; j < QK_K; ++j) {
  3084. int v = nearest_int(iscale*x[j]);
  3085. y[i].qs[j] = MIN(127, v);
  3086. }
  3087. for (int j = 0; j < QK_K/16; ++j) {
  3088. int sum = 0;
  3089. for (int ii = 0; ii < 16; ++ii) {
  3090. sum += y[i].qs[j*16 + ii];
  3091. }
  3092. y[i].bsums[j] = sum;
  3093. }
  3094. y[i].d = 1/iscale;
  3095. x += QK_K;
  3096. }
  3097. }
  3098. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3099. assert(k % QK_K == 0);
  3100. const int nb = k / QK_K;
  3101. for (int i = 0; i < nb; i++) {
  3102. for (int j = 0; j < QK_K; ++j) {
  3103. *y++ = x[i].d * x[i].qs[j];
  3104. }
  3105. }
  3106. }
  3107. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3108. quantize_row_q8_K_reference(x, y, k);
  3109. }
  3110. //===================================== Dot ptoducts =================================
  3111. //
  3112. // Helper functions
  3113. //
  3114. #if __AVX__ || __AVX2__ || __AVX512F__
  3115. // shuffles to pick the required scales in dot products
  3116. static inline __m256i get_scale_shuffle_q3k(int i) {
  3117. static const uint8_t k_shuffle[128] = {
  3118. 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,
  3119. 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,
  3120. 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,
  3121. 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,
  3122. };
  3123. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3124. }
  3125. static inline __m256i get_scale_shuffle_k4(int i) {
  3126. static const uint8_t k_shuffle[256] = {
  3127. 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,
  3128. 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,
  3129. 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,
  3130. 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,
  3131. 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,
  3132. 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,
  3133. 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,
  3134. 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
  3135. };
  3136. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3137. }
  3138. static inline __m128i get_scale_shuffle(int i) {
  3139. static const uint8_t k_shuffle[128] = {
  3140. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3141. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3142. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3143. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3144. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3145. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3146. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3147. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3148. };
  3149. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3150. }
  3151. #endif
  3152. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3153. const int qk = QK8_0;
  3154. const int nb = n / qk;
  3155. assert(n % qk == 0);
  3156. const block_q4_0 * restrict x = vx;
  3157. const block_q8_0 * restrict y = vy;
  3158. #if defined(__ARM_NEON)
  3159. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3160. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3161. assert(nb % 2 == 0); // TODO: handle odd nb
  3162. for (int i = 0; i < nb; i += 2) {
  3163. const block_q4_0 * restrict x0 = &x[i + 0];
  3164. const block_q4_0 * restrict x1 = &x[i + 1];
  3165. const block_q8_0 * restrict y0 = &y[i + 0];
  3166. const block_q8_0 * restrict y1 = &y[i + 1];
  3167. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3168. const int8x16_t s8b = vdupq_n_s8(0x8);
  3169. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3170. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3171. // 4-bit -> 8-bit
  3172. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3173. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3174. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3175. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3176. // sub 8
  3177. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3178. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3179. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3180. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3181. // load y
  3182. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3183. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3184. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3185. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3186. // dot product into int32x4_t
  3187. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3188. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3189. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3190. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3191. }
  3192. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3193. #elif defined(__AVX2__)
  3194. // Initialize accumulator with zeros
  3195. __m256 acc = _mm256_setzero_ps();
  3196. // Main loop
  3197. for (int i = 0; i < nb; ++i) {
  3198. /* Compute combined scale for the block */
  3199. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3200. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3201. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3202. const __m256i off = _mm256_set1_epi8( 8 );
  3203. bx = _mm256_sub_epi8( bx, off );
  3204. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3205. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3206. /* Multiply q with scale and accumulate */
  3207. acc = _mm256_fmadd_ps( d, q, acc );
  3208. }
  3209. *s = hsum_float_8(acc);
  3210. #elif defined(__AVX__)
  3211. // Initialize accumulator with zeros
  3212. __m256 acc = _mm256_setzero_ps();
  3213. // Main loop
  3214. for (int i = 0; i < nb; ++i) {
  3215. // Compute combined scale for the block
  3216. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3217. const __m128i lowMask = _mm_set1_epi8(0xF);
  3218. const __m128i off = _mm_set1_epi8(8);
  3219. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3220. __m128i bx = _mm_and_si128(lowMask, tmp);
  3221. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  3222. bx = _mm_sub_epi8(bx, off);
  3223. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  3224. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3225. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3226. bx = _mm_sub_epi8(bx, off);
  3227. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  3228. // Convert int32_t to float
  3229. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3230. // Apply the scale, and accumulate
  3231. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3232. }
  3233. *s = hsum_float_8(acc);
  3234. #elif defined(__SSSE3__)
  3235. // set constants
  3236. const __m128i lowMask = _mm_set1_epi8(0xF);
  3237. const __m128i off = _mm_set1_epi8(8);
  3238. // Initialize accumulator with zeros
  3239. __m128 acc_0 = _mm_setzero_ps();
  3240. __m128 acc_1 = _mm_setzero_ps();
  3241. __m128 acc_2 = _mm_setzero_ps();
  3242. __m128 acc_3 = _mm_setzero_ps();
  3243. // First round without accumulation
  3244. {
  3245. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3246. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3247. // Compute combined scale for the block 0 and 1
  3248. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3249. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3250. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3251. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3252. bx_0 = _mm_sub_epi8(bx_0, off);
  3253. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3254. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3255. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3256. bx_1 = _mm_sub_epi8(bx_1, off);
  3257. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3258. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3259. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3260. // Compute combined scale for the block 2 and 3
  3261. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3262. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3263. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3264. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3265. bx_2 = _mm_sub_epi8(bx_2, off);
  3266. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3267. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3268. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3269. bx_3 = _mm_sub_epi8(bx_3, off);
  3270. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3271. // Convert int32_t to float
  3272. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3273. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3274. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3275. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3276. // Apply the scale
  3277. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3278. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3279. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3280. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3281. }
  3282. assert(nb % 2 == 0); // TODO: handle odd nb
  3283. // Main loop
  3284. for (int i = 2; i < nb; i+=2) {
  3285. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3286. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3287. // Compute combined scale for the block 0 and 1
  3288. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3289. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3290. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3291. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3292. bx_0 = _mm_sub_epi8(bx_0, off);
  3293. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3294. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3295. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3296. bx_1 = _mm_sub_epi8(bx_1, off);
  3297. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3298. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3299. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3300. // Compute combined scale for the block 2 and 3
  3301. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3302. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3303. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3304. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3305. bx_2 = _mm_sub_epi8(bx_2, off);
  3306. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3307. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3308. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3309. bx_3 = _mm_sub_epi8(bx_3, off);
  3310. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3311. // Convert int32_t to float
  3312. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3313. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3314. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3315. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3316. // Apply the scale
  3317. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3318. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3319. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3320. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3321. // Acummulate
  3322. acc_0 = _mm_add_ps(p0_d, acc_0);
  3323. acc_1 = _mm_add_ps(p1_d, acc_1);
  3324. acc_2 = _mm_add_ps(p2_d, acc_2);
  3325. acc_3 = _mm_add_ps(p3_d, acc_3);
  3326. }
  3327. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3328. #elif defined(__riscv_v_intrinsic)
  3329. float sumf = 0.0;
  3330. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3331. for (int i = 0; i < nb; i++) {
  3332. // load elements
  3333. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3334. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3335. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3336. // mask and store lower part of x, and then upper part
  3337. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3338. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3339. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3340. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3341. // subtract offset
  3342. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3343. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3344. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3345. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3346. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3347. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3348. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3349. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3350. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3351. }
  3352. *s = sumf;
  3353. #else
  3354. // scalar
  3355. float sumf = 0.0;
  3356. for (int i = 0; i < nb; i++) {
  3357. int sumi = 0;
  3358. for (int j = 0; j < qk/2; ++j) {
  3359. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3360. const int v1 = (x[i].qs[j] >> 4) - 8;
  3361. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3362. }
  3363. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3364. }
  3365. *s = sumf;
  3366. #endif
  3367. }
  3368. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3369. const int qk = QK8_1;
  3370. const int nb = n / qk;
  3371. assert(n % qk == 0);
  3372. const block_q4_1 * restrict x = vx;
  3373. const block_q8_1 * restrict y = vy;
  3374. // TODO: add WASM SIMD
  3375. #if defined(__ARM_NEON)
  3376. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3377. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3378. float summs = 0;
  3379. assert(nb % 2 == 0); // TODO: handle odd nb
  3380. for (int i = 0; i < nb; i += 2) {
  3381. const block_q4_1 * restrict x0 = &x[i + 0];
  3382. const block_q4_1 * restrict x1 = &x[i + 1];
  3383. const block_q8_1 * restrict y0 = &y[i + 0];
  3384. const block_q8_1 * restrict y1 = &y[i + 1];
  3385. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3386. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3387. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3388. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3389. // 4-bit -> 8-bit
  3390. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3391. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3392. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3393. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3394. // load y
  3395. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3396. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3397. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3398. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3399. // dot product into int32x4_t
  3400. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3401. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3402. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3403. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3404. }
  3405. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3406. #elif defined(__AVX2__) || defined(__AVX__)
  3407. // Initialize accumulator with zeros
  3408. __m256 acc = _mm256_setzero_ps();
  3409. float summs = 0;
  3410. // Main loop
  3411. for (int i = 0; i < nb; ++i) {
  3412. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3413. const float d1 = y[i].d;
  3414. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3415. const __m256 d0v = _mm256_set1_ps( d0 );
  3416. const __m256 d1v = _mm256_set1_ps( d1 );
  3417. // Compute combined scales
  3418. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3419. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3420. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3421. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3422. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  3423. // Accumulate d0*d1*x*y
  3424. #if defined(__AVX2__)
  3425. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3426. #else
  3427. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3428. #endif
  3429. }
  3430. *s = hsum_float_8(acc) + summs;
  3431. #elif defined(__riscv_v_intrinsic)
  3432. float sumf = 0.0;
  3433. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3434. for (int i = 0; i < nb; i++) {
  3435. // load elements
  3436. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3437. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3438. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3439. // mask and store lower part of x, and then upper part
  3440. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3441. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3442. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3443. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3444. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3445. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3446. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3447. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3448. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3449. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3450. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3451. }
  3452. *s = sumf;
  3453. #else
  3454. // scalar
  3455. float sumf = 0.0;
  3456. for (int i = 0; i < nb; i++) {
  3457. int sumi = 0;
  3458. for (int j = 0; j < qk/2; ++j) {
  3459. const int v0 = (x[i].qs[j] & 0x0F);
  3460. const int v1 = (x[i].qs[j] >> 4);
  3461. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3462. }
  3463. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3464. }
  3465. *s = sumf;
  3466. #endif
  3467. }
  3468. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3469. const int qk = QK8_0;
  3470. const int nb = n / qk;
  3471. assert(n % qk == 0);
  3472. assert(qk == QK5_0);
  3473. const block_q5_0 * restrict x = vx;
  3474. const block_q8_0 * restrict y = vy;
  3475. #if defined(__ARM_NEON)
  3476. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3477. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3478. uint32_t qh0;
  3479. uint32_t qh1;
  3480. uint64_t tmp0[4];
  3481. uint64_t tmp1[4];
  3482. assert(nb % 2 == 0); // TODO: handle odd nb
  3483. for (int i = 0; i < nb; i += 2) {
  3484. const block_q5_0 * restrict x0 = &x[i];
  3485. const block_q5_0 * restrict x1 = &x[i + 1];
  3486. const block_q8_0 * restrict y0 = &y[i];
  3487. const block_q8_0 * restrict y1 = &y[i + 1];
  3488. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3489. // extract the 5th bit via lookup table ((!b) << 4)
  3490. memcpy(&qh0, x0->qh, sizeof(qh0));
  3491. memcpy(&qh1, x1->qh, sizeof(qh1));
  3492. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3493. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3494. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3495. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3496. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3497. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3498. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3499. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3500. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3501. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3502. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3503. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3504. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3505. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3506. // 4-bit -> 8-bit
  3507. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3508. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3509. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3510. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3511. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3512. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3513. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3514. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3515. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3516. // load y
  3517. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3518. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3519. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3520. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3521. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3522. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3523. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3524. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3525. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3526. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3527. }
  3528. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3529. #elif defined(__wasm_simd128__)
  3530. v128_t sumv = wasm_f32x4_splat(0.0f);
  3531. uint32_t qh;
  3532. uint64_t tmp[4];
  3533. // TODO: check if unrolling this is better
  3534. for (int i = 0; i < nb; ++i) {
  3535. const block_q5_0 * restrict x0 = &x[i];
  3536. const block_q8_0 * restrict y0 = &y[i];
  3537. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3538. // extract the 5th bit
  3539. memcpy(&qh, x0->qh, sizeof(qh));
  3540. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3541. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3542. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3543. tmp[3] = table_b2b_1[(qh >> 24) ];
  3544. const v128_t qhl = wasm_v128_load(tmp + 0);
  3545. const v128_t qhh = wasm_v128_load(tmp + 2);
  3546. const v128_t v0 = wasm_v128_load(x0->qs);
  3547. // 4-bit -> 8-bit
  3548. const v128_t v0l = wasm_v128_and (v0, m4b);
  3549. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3550. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3551. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3552. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3553. // load y
  3554. const v128_t v1l = wasm_v128_load(y0->qs);
  3555. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3556. // int8x16 -> int16x8
  3557. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3558. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3559. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3560. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3561. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3562. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3563. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3564. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3565. // dot product
  3566. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3567. wasm_i32x4_add(
  3568. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3569. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3570. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3571. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3572. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3573. }
  3574. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3575. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3576. #elif defined(__AVX2__)
  3577. // Initialize accumulator with zeros
  3578. __m256 acc = _mm256_setzero_ps();
  3579. // Main loop
  3580. for (int i = 0; i < nb; i++) {
  3581. /* Compute combined scale for the block */
  3582. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3583. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3584. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3585. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3586. bx = _mm256_or_si256(bx, bxhi);
  3587. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3588. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3589. /* Multiply q with scale and accumulate */
  3590. acc = _mm256_fmadd_ps(d, q, acc);
  3591. }
  3592. *s = hsum_float_8(acc);
  3593. #elif defined(__AVX__)
  3594. // Initialize accumulator with zeros
  3595. __m256 acc = _mm256_setzero_ps();
  3596. __m128i mask = _mm_set1_epi8((char)0xF0);
  3597. // Main loop
  3598. for (int i = 0; i < nb; i++) {
  3599. /* Compute combined scale for the block */
  3600. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3601. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3602. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3603. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3604. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3605. bxhil = _mm_andnot_si128(bxhil, mask);
  3606. bxhih = _mm_andnot_si128(bxhih, mask);
  3607. __m128i bxl = _mm256_castsi256_si128(bx);
  3608. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3609. bxl = _mm_or_si128(bxl, bxhil);
  3610. bxh = _mm_or_si128(bxh, bxhih);
  3611. bx = MM256_SET_M128I(bxh, bxl);
  3612. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3613. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3614. /* Multiply q with scale and accumulate */
  3615. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3616. }
  3617. *s = hsum_float_8(acc);
  3618. #elif defined(__riscv_v_intrinsic)
  3619. float sumf = 0.0;
  3620. uint32_t qh;
  3621. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3622. // These temporary registers are for masking and shift operations
  3623. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3624. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3625. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3626. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3627. for (int i = 0; i < nb; i++) {
  3628. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3629. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3630. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3631. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3632. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3633. // ((qh & (1u << (j + 16))) >> (j + 12));
  3634. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3635. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3636. // narrowing
  3637. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3638. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3639. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3640. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3641. // load
  3642. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3643. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3644. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3645. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3646. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3647. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3648. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3649. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3650. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3651. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3652. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3653. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3654. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3655. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3656. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3657. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3658. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3659. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3660. }
  3661. *s = sumf;
  3662. #else
  3663. // scalar
  3664. float sumf = 0.0;
  3665. for (int i = 0; i < nb; i++) {
  3666. uint32_t qh;
  3667. memcpy(&qh, x[i].qh, sizeof(qh));
  3668. int sumi = 0;
  3669. for (int j = 0; j < qk/2; ++j) {
  3670. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3671. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3672. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3673. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3674. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3675. }
  3676. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3677. }
  3678. *s = sumf;
  3679. #endif
  3680. }
  3681. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3682. const int qk = QK8_1;
  3683. const int nb = n / qk;
  3684. assert(n % qk == 0);
  3685. assert(qk == QK5_1);
  3686. const block_q5_1 * restrict x = vx;
  3687. const block_q8_1 * restrict y = vy;
  3688. #if defined(__ARM_NEON)
  3689. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3690. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3691. float summs0 = 0.0f;
  3692. float summs1 = 0.0f;
  3693. uint32_t qh0;
  3694. uint32_t qh1;
  3695. uint64_t tmp0[4];
  3696. uint64_t tmp1[4];
  3697. assert(nb % 2 == 0); // TODO: handle odd nb
  3698. for (int i = 0; i < nb; i += 2) {
  3699. const block_q5_1 * restrict x0 = &x[i];
  3700. const block_q5_1 * restrict x1 = &x[i + 1];
  3701. const block_q8_1 * restrict y0 = &y[i];
  3702. const block_q8_1 * restrict y1 = &y[i + 1];
  3703. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3704. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3705. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3706. // extract the 5th bit via lookup table ((b) << 4)
  3707. memcpy(&qh0, x0->qh, sizeof(qh0));
  3708. memcpy(&qh1, x1->qh, sizeof(qh1));
  3709. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3710. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3711. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3712. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3713. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3714. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3715. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3716. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3717. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3718. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3719. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3720. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3721. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3722. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3723. // 4-bit -> 8-bit
  3724. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3725. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3726. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3727. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3728. // add high bit
  3729. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3730. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3731. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3732. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3733. // load y
  3734. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3735. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3736. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3737. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3738. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3739. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3740. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3741. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3742. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3743. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3744. }
  3745. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3746. #elif defined(__wasm_simd128__)
  3747. v128_t sumv = wasm_f32x4_splat(0.0f);
  3748. float summs = 0.0f;
  3749. uint32_t qh;
  3750. uint64_t tmp[4];
  3751. // TODO: check if unrolling this is better
  3752. for (int i = 0; i < nb; ++i) {
  3753. const block_q5_1 * restrict x0 = &x[i];
  3754. const block_q8_1 * restrict y0 = &y[i];
  3755. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3756. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3757. // extract the 5th bit
  3758. memcpy(&qh, x0->qh, sizeof(qh));
  3759. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3760. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3761. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3762. tmp[3] = table_b2b_0[(qh >> 24) ];
  3763. const v128_t qhl = wasm_v128_load(tmp + 0);
  3764. const v128_t qhh = wasm_v128_load(tmp + 2);
  3765. const v128_t v0 = wasm_v128_load(x0->qs);
  3766. // 4-bit -> 8-bit
  3767. const v128_t v0l = wasm_v128_and (v0, m4b);
  3768. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3769. // add high bit
  3770. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3771. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3772. // load y
  3773. const v128_t v1l = wasm_v128_load(y0->qs);
  3774. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3775. // int8x16 -> int16x8
  3776. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3777. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3778. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3779. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3780. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3781. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3782. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3783. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3784. // dot product
  3785. sumv = wasm_f32x4_add(sumv,
  3786. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3787. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3788. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3789. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3790. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3791. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3792. }
  3793. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3794. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3795. #elif defined(__AVX2__)
  3796. // Initialize accumulator with zeros
  3797. __m256 acc = _mm256_setzero_ps();
  3798. float summs = 0.0f;
  3799. // Main loop
  3800. for (int i = 0; i < nb; i++) {
  3801. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3802. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3803. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3804. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3805. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3806. bx = _mm256_or_si256(bx, bxhi);
  3807. const __m256 dy = _mm256_set1_ps(y[i].d);
  3808. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3809. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3810. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3811. }
  3812. *s = hsum_float_8(acc) + summs;
  3813. #elif defined(__AVX__)
  3814. // Initialize accumulator with zeros
  3815. __m256 acc = _mm256_setzero_ps();
  3816. __m128i mask = _mm_set1_epi8(0x10);
  3817. float summs = 0.0f;
  3818. // Main loop
  3819. for (int i = 0; i < nb; i++) {
  3820. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3821. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3822. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3823. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3824. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3825. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3826. bxhil = _mm_and_si128(bxhil, mask);
  3827. bxhih = _mm_and_si128(bxhih, mask);
  3828. __m128i bxl = _mm256_castsi256_si128(bx);
  3829. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3830. bxl = _mm_or_si128(bxl, bxhil);
  3831. bxh = _mm_or_si128(bxh, bxhih);
  3832. bx = MM256_SET_M128I(bxh, bxl);
  3833. const __m256 dy = _mm256_set1_ps(y[i].d);
  3834. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3835. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3836. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3837. }
  3838. *s = hsum_float_8(acc) + summs;
  3839. #elif defined(__riscv_v_intrinsic)
  3840. float sumf = 0.0;
  3841. uint32_t qh;
  3842. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3843. // temporary registers for shift operations
  3844. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3845. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3846. for (int i = 0; i < nb; i++) {
  3847. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3848. // load qh
  3849. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3850. // ((qh >> (j + 0)) << 4) & 0x10;
  3851. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3852. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3853. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3854. // ((qh >> (j + 12)) ) & 0x10;
  3855. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3856. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3857. // narrowing
  3858. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3859. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3860. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3861. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3862. // load
  3863. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3864. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3865. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3866. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3867. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3868. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3869. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3870. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3871. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3872. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3873. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3874. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3875. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3876. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3877. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3878. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3879. }
  3880. *s = sumf;
  3881. #else
  3882. // scalar
  3883. float sumf = 0.0;
  3884. for (int i = 0; i < nb; i++) {
  3885. uint32_t qh;
  3886. memcpy(&qh, x[i].qh, sizeof(qh));
  3887. int sumi = 0;
  3888. for (int j = 0; j < qk/2; ++j) {
  3889. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3890. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3891. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3892. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3893. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3894. }
  3895. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3896. }
  3897. *s = sumf;
  3898. #endif
  3899. }
  3900. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3901. const int qk = QK8_0;
  3902. const int nb = n / qk;
  3903. assert(n % qk == 0);
  3904. const block_q8_0 * restrict x = vx;
  3905. const block_q8_0 * restrict y = vy;
  3906. #if defined(__ARM_NEON)
  3907. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3908. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3909. assert(nb % 2 == 0); // TODO: handle odd nb
  3910. for (int i = 0; i < nb; i += 2) {
  3911. const block_q8_0 * restrict x0 = &x[i + 0];
  3912. const block_q8_0 * restrict x1 = &x[i + 1];
  3913. const block_q8_0 * restrict y0 = &y[i + 0];
  3914. const block_q8_0 * restrict y1 = &y[i + 1];
  3915. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3916. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3917. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3918. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3919. // load y
  3920. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3921. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3922. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3923. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3924. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3925. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3926. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3927. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3928. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3929. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3930. }
  3931. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3932. #elif defined(__AVX2__) || defined(__AVX__)
  3933. // Initialize accumulator with zeros
  3934. __m256 acc = _mm256_setzero_ps();
  3935. // Main loop
  3936. for (int i = 0; i < nb; ++i) {
  3937. // Compute combined scale for the block
  3938. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3939. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3940. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3941. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3942. // Multiply q with scale and accumulate
  3943. #if defined(__AVX2__)
  3944. acc = _mm256_fmadd_ps( d, q, acc );
  3945. #else
  3946. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3947. #endif
  3948. }
  3949. *s = hsum_float_8(acc);
  3950. #elif defined(__riscv_v_intrinsic)
  3951. float sumf = 0.0;
  3952. size_t vl = __riscv_vsetvl_e8m1(qk);
  3953. for (int i = 0; i < nb; i++) {
  3954. // load elements
  3955. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3956. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3957. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3958. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3959. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3960. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3961. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3962. }
  3963. *s = sumf;
  3964. #else
  3965. // scalar
  3966. float sumf = 0.0;
  3967. for (int i = 0; i < nb; i++) {
  3968. int sumi = 0;
  3969. for (int j = 0; j < qk; j++) {
  3970. sumi += x[i].qs[j]*y[i].qs[j];
  3971. }
  3972. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3973. }
  3974. *s = sumf;
  3975. #endif
  3976. }
  3977. #if QK_K == 256
  3978. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3979. const block_q2_K * restrict x = vx;
  3980. const block_q8_K * restrict y = vy;
  3981. const int nb = n / QK_K;
  3982. #ifdef __ARM_NEON
  3983. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3984. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3985. const int32x4_t vzero = vdupq_n_s32(0);
  3986. ggml_int8x16x2_t q2bytes;
  3987. uint8_t aux[16];
  3988. float sum = 0;
  3989. for (int i = 0; i < nb; ++i) {
  3990. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3991. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3992. const uint8_t * restrict q2 = x[i].qs;
  3993. const int8_t * restrict q8 = y[i].qs;
  3994. const uint8_t * restrict sc = x[i].scales;
  3995. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3996. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3997. vst1q_u8(aux, scales);
  3998. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3999. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4000. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4001. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4002. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4003. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4004. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4005. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4006. int isum = 0;
  4007. int is = 0;
  4008. // We use this macro instead of a function call because for some reason
  4009. // the code runs 2-3% slower, even if the function is declared inline
  4010. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4011. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4012. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4013. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4014. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4015. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4016. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4017. MULTIPLY_ACCUM_WITH_SCALE((index));
  4018. for (int j = 0; j < QK_K/128; ++j) {
  4019. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4020. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4021. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4022. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4023. MULTIPLY_ACCUM_WITH_SCALE(0);
  4024. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4025. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4026. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4027. is += 8;
  4028. }
  4029. sum += d * isum;
  4030. }
  4031. *s = sum;
  4032. #elif defined __AVX2__
  4033. const __m256i m3 = _mm256_set1_epi8(3);
  4034. const __m128i m4 = _mm_set1_epi8(0xF);
  4035. __m256 acc = _mm256_setzero_ps();
  4036. for (int i = 0; i < nb; ++i) {
  4037. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4038. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4039. const uint8_t * restrict q2 = x[i].qs;
  4040. const int8_t * restrict q8 = y[i].qs;
  4041. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4042. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4043. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4044. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4045. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4046. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4047. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4048. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4049. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4050. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4051. __m256i sumi = _mm256_setzero_si256();
  4052. for (int j = 0; j < QK_K/128; ++j) {
  4053. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4054. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4055. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4056. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4057. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4058. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4059. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4060. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4061. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4062. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4063. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4064. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4065. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4066. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4067. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4068. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4069. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4070. p0 = _mm256_add_epi32(p0, p1);
  4071. p2 = _mm256_add_epi32(p2, p3);
  4072. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4073. }
  4074. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4075. }
  4076. *s = hsum_float_8(acc);
  4077. #elif defined __AVX__
  4078. const __m128i m3 = _mm_set1_epi8(0x3);
  4079. const __m128i m4 = _mm_set1_epi8(0xF);
  4080. const __m128i m2 = _mm_set1_epi8(0x2);
  4081. __m256 acc = _mm256_setzero_ps();
  4082. for (int i = 0; i < nb; ++i) {
  4083. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4084. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4085. const uint8_t * restrict q2 = x[i].qs;
  4086. const int8_t * restrict q8 = y[i].qs;
  4087. // load mins and scales from block_q2_K.scales[QK_K/16]
  4088. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4089. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4090. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4091. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4092. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4093. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4094. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4095. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4096. // sumf += -dmin * summs in 32bits*8
  4097. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4098. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4099. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4100. const __m128i scales[2] = { scales_0, scales_1 };
  4101. __m128i sumi_0 = _mm_setzero_si128();
  4102. __m128i sumi_1 = _mm_setzero_si128();
  4103. for (int j = 0; j < QK_K/128; ++j) {
  4104. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4105. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4106. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4107. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4108. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4109. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4110. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4111. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4112. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4113. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4114. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4115. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4116. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4117. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4118. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4119. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4120. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4121. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4122. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4123. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4124. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4125. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4126. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4127. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4128. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4129. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4130. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4131. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4132. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4133. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4134. __m128i shuffle = _mm_set1_epi16(0x0100);
  4135. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4136. shuffle = _mm_add_epi16(shuffle, m2);
  4137. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4138. shuffle = _mm_add_epi16(shuffle, m2);
  4139. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4140. shuffle = _mm_add_epi16(shuffle, m2);
  4141. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4142. shuffle = _mm_add_epi16(shuffle, m2);
  4143. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4144. shuffle = _mm_add_epi16(shuffle, m2);
  4145. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4146. shuffle = _mm_add_epi16(shuffle, m2);
  4147. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4148. shuffle = _mm_add_epi16(shuffle, m2);
  4149. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4150. p0 = _mm_add_epi32(p0, p1);
  4151. p2 = _mm_add_epi32(p2, p3);
  4152. p4 = _mm_add_epi32(p4, p5);
  4153. p6 = _mm_add_epi32(p6, p7);
  4154. // isum in 32bits*4*2
  4155. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4156. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4157. }
  4158. // sumf += dall * isum - dmin * summs in 32bits
  4159. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4160. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4161. }
  4162. *s = hsum_float_8(acc);
  4163. #elif defined __riscv_v_intrinsic
  4164. float sumf = 0;
  4165. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4166. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4167. for (int i = 0; i < nb; ++i) {
  4168. const uint8_t * q2 = x[i].qs;
  4169. const int8_t * q8 = y[i].qs;
  4170. const uint8_t * sc = x[i].scales;
  4171. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4172. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4173. size_t vl = 16;
  4174. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4175. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4176. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4177. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4178. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4179. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4180. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4181. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4182. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4183. vl = 32;
  4184. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4185. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4186. uint8_t is=0;
  4187. int isum=0;
  4188. for (int j = 0; j < QK_K/128; ++j) {
  4189. // load Q2
  4190. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4191. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4192. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4193. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4194. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4195. // duplicate scale elements for product
  4196. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4197. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4198. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4199. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4200. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4201. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4202. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4203. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4204. // load Q8
  4205. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4206. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4207. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4208. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4209. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4210. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4211. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4212. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4213. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4214. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4215. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4216. q2+=32; q8+=128; is=8;
  4217. }
  4218. sumf += dall * isum;
  4219. }
  4220. *s = sumf;
  4221. #else
  4222. float sumf = 0;
  4223. for (int i = 0; i < nb; ++i) {
  4224. const uint8_t * q2 = x[i].qs;
  4225. const int8_t * q8 = y[i].qs;
  4226. const uint8_t * sc = x[i].scales;
  4227. int summs = 0;
  4228. for (int j = 0; j < 16; ++j) {
  4229. summs += y[i].bsums[j] * (sc[j] >> 4);
  4230. }
  4231. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4232. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4233. int isum = 0;
  4234. int is = 0;
  4235. int d;
  4236. for (int k = 0; k < QK_K/128; ++k) {
  4237. int shift = 0;
  4238. for (int j = 0; j < 4; ++j) {
  4239. d = sc[is++] & 0xF;
  4240. int isuml = 0;
  4241. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4242. isum += d * isuml;
  4243. d = sc[is++] & 0xF;
  4244. isuml = 0;
  4245. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4246. isum += d * isuml;
  4247. shift += 2;
  4248. q8 += 32;
  4249. }
  4250. q2 += 32;
  4251. }
  4252. sumf += dall * isum - dmin * summs;
  4253. }
  4254. *s = sumf;
  4255. #endif
  4256. }
  4257. #else
  4258. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4259. const block_q2_K * restrict x = vx;
  4260. const block_q8_K * restrict y = vy;
  4261. const int nb = n / QK_K;
  4262. #ifdef __ARM_NEON
  4263. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4264. const int32x4_t vzero = vdupq_n_s32(0);
  4265. ggml_int8x16x4_t q2bytes;
  4266. uint32_t aux32[2];
  4267. const uint8_t * scales = (const uint8_t *)aux32;
  4268. float sum = 0;
  4269. for (int i = 0; i < nb; ++i) {
  4270. const float d = y[i].d * (float)x[i].d;
  4271. const float dmin = -y[i].d * (float)x[i].dmin;
  4272. const uint8_t * restrict q2 = x[i].qs;
  4273. const int8_t * restrict q8 = y[i].qs;
  4274. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4275. aux32[0] = sc[0] & 0x0f0f0f0f;
  4276. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4277. 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]);
  4278. int isum1 = 0, isum2 = 0;
  4279. const uint8x16_t q2bits = vld1q_u8(q2);
  4280. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4281. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4282. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4283. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4284. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4285. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4286. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4287. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4288. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4289. sum += d * (isum1 + isum2);
  4290. }
  4291. *s = sum;
  4292. #elif defined __AVX2__
  4293. const __m256i m3 = _mm256_set1_epi8(3);
  4294. __m256 acc = _mm256_setzero_ps();
  4295. uint32_t ud, um;
  4296. const uint8_t * restrict db = (const uint8_t *)&ud;
  4297. const uint8_t * restrict mb = (const uint8_t *)&um;
  4298. float summs = 0;
  4299. // TODO: optimize this
  4300. for (int i = 0; i < nb; ++i) {
  4301. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4302. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4303. const uint8_t * restrict q2 = x[i].qs;
  4304. const int8_t * restrict q8 = y[i].qs;
  4305. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4306. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4307. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4308. 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];
  4309. summs += dmin * smin;
  4310. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4311. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4312. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4313. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4314. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4315. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4316. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4317. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4318. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4319. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4320. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4321. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4322. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4323. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4324. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4325. }
  4326. *s = hsum_float_8(acc) + summs;
  4327. #elif defined __AVX__
  4328. const __m128i m3 = _mm_set1_epi8(3);
  4329. __m256 acc = _mm256_setzero_ps();
  4330. uint32_t ud, um;
  4331. const uint8_t * restrict db = (const uint8_t *)&ud;
  4332. const uint8_t * restrict mb = (const uint8_t *)&um;
  4333. float summs = 0;
  4334. // TODO: optimize this
  4335. for (int i = 0; i < nb; ++i) {
  4336. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4337. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4338. const uint8_t * restrict q2 = x[i].qs;
  4339. const int8_t * restrict q8 = y[i].qs;
  4340. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4341. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4342. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4343. 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];
  4344. summs += dmin * smin;
  4345. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4346. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4347. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4348. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4349. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4350. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4351. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4352. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4353. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4354. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4355. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4356. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4357. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4358. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4359. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4360. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4361. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4362. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4363. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4364. }
  4365. *s = hsum_float_8(acc) + summs;
  4366. #elif defined __riscv_v_intrinsic
  4367. uint32_t aux32[2];
  4368. const uint8_t * scales = (const uint8_t *)aux32;
  4369. float sumf = 0;
  4370. for (int i = 0; i < nb; ++i) {
  4371. const float d = y[i].d * (float)x[i].d;
  4372. const float dmin = -y[i].d * (float)x[i].dmin;
  4373. const uint8_t * restrict q2 = x[i].qs;
  4374. const int8_t * restrict q8 = y[i].qs;
  4375. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4376. aux32[0] = sc[0] & 0x0f0f0f0f;
  4377. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4378. 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]);
  4379. int isum1 = 0;
  4380. int isum2 = 0;
  4381. size_t vl = 16;
  4382. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4383. // load Q2
  4384. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4385. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4386. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4387. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4388. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4389. // load Q8, and take product with Q2
  4390. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4391. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4392. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4393. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4394. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4395. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4396. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4397. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4398. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4399. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4400. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4401. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4402. sumf += d * (isum1 + isum2);
  4403. }
  4404. *s = sumf;
  4405. #else
  4406. float sumf = 0;
  4407. int isum[4];
  4408. for (int i = 0; i < nb; ++i) {
  4409. const uint8_t * q2 = x[i].qs;
  4410. const int8_t * q8 = y[i].qs;
  4411. const uint8_t * sc = x[i].scales;
  4412. int summs = 0;
  4413. for (int j = 0; j < QK_K/16; ++j) {
  4414. summs += y[i].bsums[j] * (sc[j] >> 4);
  4415. }
  4416. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4417. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4418. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  4419. for (int l = 0; l < 16; ++l) {
  4420. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4421. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4422. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4423. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4424. }
  4425. for (int l = 0; l < 4; ++l) {
  4426. isum[l] *= (sc[l] & 0xF);
  4427. }
  4428. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4429. }
  4430. *s = sumf;
  4431. #endif
  4432. }
  4433. #endif
  4434. #if QK_K == 256
  4435. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4436. assert(n % QK_K == 0);
  4437. const uint32_t kmask1 = 0x03030303;
  4438. const uint32_t kmask2 = 0x0f0f0f0f;
  4439. const block_q3_K * restrict x = vx;
  4440. const block_q8_K * restrict y = vy;
  4441. const int nb = n / QK_K;
  4442. #ifdef __ARM_NEON
  4443. uint32_t aux[3];
  4444. uint32_t utmp[4];
  4445. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4446. const int32x4_t vzero = vdupq_n_s32(0);
  4447. const uint8x16_t m0 = vdupq_n_u8(1);
  4448. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4449. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4450. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4451. const int8_t m32 = 32;
  4452. ggml_int8x16x4_t q3bytes;
  4453. float sum = 0;
  4454. for (int i = 0; i < nb; ++i) {
  4455. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4456. const uint8_t * restrict q3 = x[i].qs;
  4457. const uint8_t * restrict qh = x[i].hmask;
  4458. const int8_t * restrict q8 = y[i].qs;
  4459. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4460. ggml_uint8x16x4_t q3h;
  4461. int32_t isum = 0;
  4462. // Set up scales
  4463. memcpy(aux, x[i].scales, 12);
  4464. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4465. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4466. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4467. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4468. int8_t * scale = (int8_t *)utmp;
  4469. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4470. for (int j = 0; j < QK_K/128; ++j) {
  4471. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4472. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4473. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4474. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4475. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4476. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4477. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4478. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4479. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4480. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4481. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4482. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4483. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4484. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4485. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4486. scale += 4;
  4487. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4488. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4489. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4490. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4491. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4492. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4493. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4494. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4495. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4496. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4497. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4498. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4499. scale += 4;
  4500. if (j == 0) {
  4501. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4502. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4503. }
  4504. }
  4505. sum += d * isum;
  4506. }
  4507. *s = sum;
  4508. #elif defined __AVX2__
  4509. const __m256i m3 = _mm256_set1_epi8(3);
  4510. const __m256i mone = _mm256_set1_epi8(1);
  4511. const __m128i m32 = _mm_set1_epi8(32);
  4512. __m256 acc = _mm256_setzero_ps();
  4513. uint32_t aux[3];
  4514. for (int i = 0; i < nb; ++i) {
  4515. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4516. const uint8_t * restrict q3 = x[i].qs;
  4517. const int8_t * restrict q8 = y[i].qs;
  4518. // Set up scales
  4519. memcpy(aux, x[i].scales, 12);
  4520. __m128i scales128 = _mm_set_epi32(
  4521. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4522. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4523. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4524. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4525. scales128 = _mm_sub_epi8(scales128, m32);
  4526. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4527. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4528. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4529. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4530. // high bit
  4531. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4532. // integer accumulator
  4533. __m256i sumi = _mm256_setzero_si256();
  4534. int bit = 0;
  4535. int is = 0;
  4536. for (int j = 0; j < QK_K/128; ++j) {
  4537. // load low 2 bits
  4538. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4539. // prepare low and high bits
  4540. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4541. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4542. ++bit;
  4543. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4544. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4545. ++bit;
  4546. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4547. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4548. ++bit;
  4549. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4550. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4551. ++bit;
  4552. // load Q8 quants
  4553. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4554. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4555. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4556. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4557. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4558. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4559. // and 2 if the high bit was set)
  4560. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4561. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4562. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4563. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4564. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4565. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4566. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4567. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4568. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4569. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4570. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4571. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4572. // multiply with scales
  4573. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4574. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4575. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4576. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4577. // accumulate
  4578. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4579. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4580. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4581. }
  4582. // multiply with block scale and accumulate
  4583. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4584. }
  4585. *s = hsum_float_8(acc);
  4586. #elif defined __AVX__
  4587. const __m128i m3 = _mm_set1_epi8(3);
  4588. const __m128i mone = _mm_set1_epi8(1);
  4589. const __m128i m32 = _mm_set1_epi8(32);
  4590. const __m128i m2 = _mm_set1_epi8(2);
  4591. __m256 acc = _mm256_setzero_ps();
  4592. const uint32_t *aux;
  4593. for (int i = 0; i < nb; ++i) {
  4594. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4595. const uint8_t * restrict q3 = x[i].qs;
  4596. const int8_t * restrict q8 = y[i].qs;
  4597. // Set up scales
  4598. aux = (const uint32_t *)x[i].scales;
  4599. __m128i scales128 = _mm_set_epi32(
  4600. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4601. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4602. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4603. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4604. scales128 = _mm_sub_epi8(scales128, m32);
  4605. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4606. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4607. const __m128i scales[2] = { scales_0, scales_1 };
  4608. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4609. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4610. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4611. // integer accumulator
  4612. __m128i sumi_0 = _mm_setzero_si128();
  4613. __m128i sumi_1 = _mm_setzero_si128();
  4614. for (int j = 0; j < QK_K/128; ++j) {
  4615. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4616. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4617. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4618. // prepare low and high bits
  4619. const int bit = j << 2;
  4620. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4621. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4622. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4623. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4624. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4625. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4626. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4627. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4628. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4629. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4630. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4631. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4632. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4633. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4634. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4635. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4636. // load Q8 quants from block_q8_K.qs[QK_K]
  4637. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4638. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4639. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4640. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4641. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4642. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4643. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4644. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4645. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4646. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4647. // and 2 if the high bit was set)
  4648. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4649. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4650. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4651. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4652. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4653. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4654. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4655. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4656. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4657. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4658. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4659. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4660. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4661. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4662. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4663. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4664. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4665. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4666. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4667. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4668. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4669. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4670. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4671. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4672. // multiply with scales
  4673. __m128i shuffle = _mm_set1_epi16(0x0100);
  4674. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4675. shuffle = _mm_add_epi16(shuffle, m2);
  4676. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4677. shuffle = _mm_add_epi16(shuffle, m2);
  4678. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4679. shuffle = _mm_add_epi16(shuffle, m2);
  4680. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4681. shuffle = _mm_add_epi16(shuffle, m2);
  4682. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4683. shuffle = _mm_add_epi16(shuffle, m2);
  4684. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4685. shuffle = _mm_add_epi16(shuffle, m2);
  4686. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4687. shuffle = _mm_add_epi16(shuffle, m2);
  4688. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4689. // accumulate
  4690. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4691. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4692. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4693. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4694. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4695. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4696. }
  4697. // multiply with block scale and accumulate
  4698. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4699. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4700. }
  4701. *s = hsum_float_8(acc);
  4702. #elif defined __riscv_v_intrinsic
  4703. uint32_t aux[3];
  4704. uint32_t utmp[4];
  4705. float sumf = 0;
  4706. for (int i = 0; i < nb; ++i) {
  4707. const uint8_t * restrict q3 = x[i].qs;
  4708. const uint8_t * restrict qh = x[i].hmask;
  4709. const int8_t * restrict q8 = y[i].qs;
  4710. memcpy(aux, x[i].scales, 12);
  4711. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4712. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4713. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4714. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4715. int8_t * scale = (int8_t *)utmp;
  4716. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4717. size_t vl = 32;
  4718. uint8_t m = 1;
  4719. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4720. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4721. int sum_t = 0;
  4722. for (int j = 0; j < QK_K; j += 128) {
  4723. vl = 32;
  4724. // load Q3
  4725. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4726. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4727. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4728. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4729. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4730. // compute mask for subtraction
  4731. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4732. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4733. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4734. m <<= 1;
  4735. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4736. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4737. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4738. m <<= 1;
  4739. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4740. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4741. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4742. m <<= 1;
  4743. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4744. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4745. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4746. m <<= 1;
  4747. // load Q8 and take product with Q3
  4748. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4749. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4750. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4751. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4752. vl = 16;
  4753. // retrieve lane to multiply with scale
  4754. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4755. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4756. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4757. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4758. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4759. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4760. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4761. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4762. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4763. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4764. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4765. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4766. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4767. q3 += 32; q8 += 128; scale += 8;
  4768. }
  4769. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4770. sumf += d*sum_t;
  4771. }
  4772. *s = sumf;
  4773. #else
  4774. // scalar version
  4775. // This function is written like this so the compiler can manage to vectorize most of it
  4776. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4777. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4778. // The ideal situation would be if we could just write the code once, and the compiler would
  4779. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4780. // write vectorized versions for AVX, ARM_NEON, etc.
  4781. int8_t aux8[QK_K];
  4782. int16_t aux16[8];
  4783. float sums [8];
  4784. int32_t aux32[8];
  4785. memset(sums, 0, 8*sizeof(float));
  4786. uint32_t auxs[4];
  4787. const int8_t * scales = (const int8_t*)auxs;
  4788. float sumf = 0;
  4789. for (int i = 0; i < nb; ++i) {
  4790. const uint8_t * restrict q3 = x[i].qs;
  4791. const uint8_t * restrict hm = x[i].hmask;
  4792. const int8_t * restrict q8 = y[i].qs;
  4793. memset(aux32, 0, 8*sizeof(int32_t));
  4794. int8_t * restrict a = aux8;
  4795. uint8_t m = 1;
  4796. for (int j = 0; j < QK_K; j += 128) {
  4797. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4798. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4799. a += 32; m <<= 1;
  4800. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4801. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4802. a += 32; m <<= 1;
  4803. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4804. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4805. a += 32; m <<= 1;
  4806. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4807. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4808. a += 32; m <<= 1;
  4809. q3 += 32;
  4810. }
  4811. a = aux8;
  4812. memcpy(auxs, x[i].scales, 12);
  4813. uint32_t tmp = auxs[2];
  4814. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4815. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4816. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4817. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4818. for (int j = 0; j < QK_K/16; ++j) {
  4819. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4820. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4821. q8 += 8; a += 8;
  4822. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4823. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4824. q8 += 8; a += 8;
  4825. }
  4826. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4827. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4828. }
  4829. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4830. *s = sumf;
  4831. #endif
  4832. }
  4833. #else
  4834. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4835. assert(n % QK_K == 0);
  4836. const block_q3_K * restrict x = vx;
  4837. const block_q8_K * restrict y = vy;
  4838. const int nb = n / QK_K;
  4839. #ifdef __ARM_NEON
  4840. const int32x4_t vzero = vdupq_n_s32(0);
  4841. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4842. const uint8x16_t mh = vdupq_n_u8(4);
  4843. ggml_int8x16x4_t q3bytes;
  4844. uint16_t aux16[2];
  4845. int8_t * scales = (int8_t *)aux16;
  4846. float sum = 0;
  4847. for (int i = 0; i < nb; ++i) {
  4848. ggml_uint8x16x4_t q3h;
  4849. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4850. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4851. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4852. const uint16_t a = *(const uint16_t *)x[i].scales;
  4853. aux16[0] = a & 0x0f0f;
  4854. aux16[1] = (a >> 4) & 0x0f0f;
  4855. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4856. 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]);
  4857. const float d = y[i].d * (float)x[i].d;
  4858. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4859. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4860. q3h.val[1] = vandq_u8(mh, htmp);
  4861. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4862. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4863. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4864. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4865. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4866. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4867. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4868. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4869. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4870. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4871. sum += d * isum;
  4872. }
  4873. *s = sum;
  4874. #elif defined __AVX2__
  4875. const __m256i m3 = _mm256_set1_epi8(3);
  4876. const __m256i m1 = _mm256_set1_epi8(1);
  4877. __m256 acc = _mm256_setzero_ps();
  4878. uint64_t aux64;
  4879. uint16_t aux16[2];
  4880. const int8_t * aux8 = (const int8_t *)aux16;
  4881. for (int i = 0; i < nb; ++i) {
  4882. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4883. const uint8_t * restrict q3 = x[i].qs;
  4884. const int8_t * restrict q8 = y[i].qs;
  4885. const uint16_t a = *(const uint16_t *)x[i].scales;
  4886. aux16[0] = a & 0x0f0f;
  4887. aux16[1] = (a >> 4) & 0x0f0f;
  4888. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4889. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4890. memcpy(&aux64, x[i].hmask, 8);
  4891. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4892. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4893. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4894. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4895. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4896. // load low 2 bits
  4897. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4898. // prepare low and high bits
  4899. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4900. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4901. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4902. // load Q8 quants
  4903. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4904. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4905. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4906. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4907. // and 2 if the high bit was set)
  4908. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4909. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4910. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4911. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4912. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4913. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4914. // multiply with scales
  4915. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4916. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4917. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4918. // multiply with block scale and accumulate
  4919. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4920. }
  4921. *s = hsum_float_8(acc);
  4922. #elif defined __AVX__
  4923. const __m128i m3 = _mm_set1_epi8(3);
  4924. const __m128i m1 = _mm_set1_epi8(1);
  4925. __m256 acc = _mm256_setzero_ps();
  4926. uint64_t aux64;
  4927. uint16_t aux16[2];
  4928. const int8_t * aux8 = (const int8_t *)aux16;
  4929. for (int i = 0; i < nb; ++i) {
  4930. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4931. const uint8_t * restrict q3 = x[i].qs;
  4932. const int8_t * restrict q8 = y[i].qs;
  4933. const uint16_t a = *(const uint16_t *)x[i].scales;
  4934. aux16[0] = a & 0x0f0f;
  4935. aux16[1] = (a >> 4) & 0x0f0f;
  4936. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4937. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4938. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4939. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4940. memcpy(&aux64, x[i].hmask, 8);
  4941. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4942. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4943. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4944. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4945. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4946. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4947. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4948. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4949. // load low 2 bits
  4950. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4951. // prepare low and high bits
  4952. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4953. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4954. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4955. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4956. // load Q8 quants
  4957. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4958. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4959. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4960. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4961. // and 2 if the high bit was set)
  4962. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4963. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4964. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4965. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4966. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4967. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4968. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4969. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4970. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4971. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4972. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4973. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4974. // multiply with scales
  4975. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4976. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4977. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4978. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4979. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4980. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4981. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4982. // multiply with block scale and accumulate
  4983. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4984. }
  4985. *s = hsum_float_8(acc);
  4986. #elif defined __riscv_v_intrinsic
  4987. uint16_t aux16[2];
  4988. int8_t * scales = (int8_t *)aux16;
  4989. float sumf = 0;
  4990. for (int i = 0; i < nb; ++i) {
  4991. const uint8_t * restrict q3 = x[i].qs;
  4992. const int8_t * restrict q8 = y[i].qs;
  4993. const uint16_t a = *(const uint16_t *)x[i].scales;
  4994. aux16[0] = a & 0x0f0f;
  4995. aux16[1] = (a >> 4) & 0x0f0f;
  4996. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4997. 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]);
  4998. const float d = y[i].d * (float)x[i].d;
  4999. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5000. // load qh
  5001. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5002. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5003. size_t vl = 16;
  5004. // extend and combine both qh_x1 and qh_x2
  5005. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5006. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5007. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5008. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5009. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5010. // load Q3
  5011. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5012. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5013. 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);
  5014. 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);
  5015. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5016. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5017. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5018. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5019. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5020. // load Q8 and take product with Q3
  5021. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5022. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5023. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5024. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5025. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5026. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5027. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5028. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5029. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5030. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5031. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5032. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5033. sumf += d * isum;
  5034. }
  5035. *s = sumf;
  5036. #else
  5037. int8_t aux8[QK_K];
  5038. int16_t aux16[8];
  5039. float sums [8];
  5040. int32_t aux32[8];
  5041. int32_t scales[4];
  5042. memset(sums, 0, 8*sizeof(float));
  5043. float sumf = 0;
  5044. for (int i = 0; i < nb; ++i) {
  5045. const uint8_t * restrict q3 = x[i].qs;
  5046. const uint8_t * restrict hm = x[i].hmask;
  5047. const int8_t * restrict q8 = y[i].qs;
  5048. int8_t * restrict a = aux8;
  5049. for (int l = 0; l < 8; ++l) {
  5050. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5051. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5052. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5053. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5054. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5055. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5056. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5057. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5058. }
  5059. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5060. scales[1] = (x[i].scales[0] >> 4) - 8;
  5061. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5062. scales[3] = (x[i].scales[1] >> 4) - 8;
  5063. memset(aux32, 0, 8*sizeof(int32_t));
  5064. for (int j = 0; j < QK_K/16; ++j) {
  5065. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5066. q8 += 8; a += 8;
  5067. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5068. q8 += 8; a += 8;
  5069. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5070. }
  5071. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5072. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5073. }
  5074. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5075. *s = sumf;
  5076. #endif
  5077. }
  5078. #endif
  5079. #if QK_K == 256
  5080. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5081. assert(n % QK_K == 0);
  5082. const block_q4_K * restrict x = vx;
  5083. const block_q8_K * restrict y = vy;
  5084. const int nb = n / QK_K;
  5085. static const uint32_t kmask1 = 0x3f3f3f3f;
  5086. static const uint32_t kmask2 = 0x0f0f0f0f;
  5087. static const uint32_t kmask3 = 0x03030303;
  5088. uint32_t utmp[4];
  5089. #ifdef __ARM_NEON
  5090. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5091. const int32x4_t mzero = vdupq_n_s32(0);
  5092. ggml_int8x16x2_t q4bytes;
  5093. ggml_int8x16x2_t q8bytes;
  5094. float sumf = 0;
  5095. for (int i = 0; i < nb; ++i) {
  5096. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5097. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5098. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5099. memcpy(utmp, x[i].scales, 12);
  5100. uint32x2_t mins8 = { 0 };
  5101. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5102. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5103. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5104. utmp[0] &= kmask1;
  5105. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5106. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5107. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5108. sumf -= dmin * vaddvq_s32(prod);
  5109. const uint8_t * scales = (const uint8_t *)utmp;
  5110. const uint8_t * restrict q4 = x[i].qs;
  5111. const int8_t * restrict q8 = y[i].qs;
  5112. int32_t sumi1 = 0;
  5113. int32_t sumi2 = 0;
  5114. for (int j = 0; j < QK_K/64; ++j) {
  5115. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5116. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5117. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5118. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5119. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5120. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5121. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5122. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5123. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5124. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5125. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5126. }
  5127. sumf += d * (sumi1 + sumi2);
  5128. }
  5129. *s = sumf;
  5130. #elif defined __AVX2__
  5131. const __m256i m4 = _mm256_set1_epi8(0xF);
  5132. __m256 acc = _mm256_setzero_ps();
  5133. __m128 acc_m = _mm_setzero_ps();
  5134. for (int i = 0; i < nb; ++i) {
  5135. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5136. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5137. memcpy(utmp, x[i].scales, 12);
  5138. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5139. const uint32_t uaux = utmp[1] & kmask1;
  5140. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5141. utmp[2] = uaux;
  5142. utmp[0] &= kmask1;
  5143. const uint8_t * restrict q4 = x[i].qs;
  5144. const int8_t * restrict q8 = y[i].qs;
  5145. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5146. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5147. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5148. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5149. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5150. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5151. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5152. __m256i sumi = _mm256_setzero_si256();
  5153. for (int j = 0; j < QK_K/64; ++j) {
  5154. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5155. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5156. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5157. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5158. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5159. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5160. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5161. p16l = _mm256_madd_epi16(scale_l, p16l);
  5162. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5163. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5164. p16h = _mm256_madd_epi16(scale_h, p16h);
  5165. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5166. sumi = _mm256_add_epi32(sumi, sumj);
  5167. }
  5168. __m256 vd = _mm256_set1_ps(d);
  5169. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5170. }
  5171. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5172. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5173. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5174. #elif defined __AVX__
  5175. const __m128i m4 = _mm_set1_epi8(0xF);
  5176. const __m128i m2 = _mm_set1_epi8(0x2);
  5177. __m256 acc = _mm256_setzero_ps();
  5178. __m128 acc_m = _mm_setzero_ps();
  5179. for (int i = 0; i < nb; ++i) {
  5180. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5181. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5182. const uint8_t * restrict q4 = x[i].qs;
  5183. const int8_t * restrict q8 = y[i].qs;
  5184. memcpy(utmp, x[i].scales, 12);
  5185. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5186. const uint32_t uaux = utmp[1] & kmask1;
  5187. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5188. utmp[2] = uaux;
  5189. utmp[0] &= kmask1;
  5190. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5191. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5192. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5193. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5194. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5195. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5196. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5197. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5198. __m128i sumi_0 = _mm_setzero_si128();
  5199. __m128i sumi_1 = _mm_setzero_si128();
  5200. __m128i shuffle = _mm_set1_epi16(0x0100);
  5201. for (int j = 0; j < QK_K/64; ++j) {
  5202. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5203. shuffle = _mm_add_epi16(shuffle, m2);
  5204. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5205. shuffle = _mm_add_epi16(shuffle, m2);
  5206. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5207. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5208. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5209. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5210. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5211. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5212. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5213. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5214. p16l = _mm_madd_epi16(scale_l, p16l);
  5215. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5216. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5217. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5218. p16l = _mm_madd_epi16(scale_l, p16l);
  5219. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5220. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5221. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5222. p16h = _mm_madd_epi16(scale_h, p16h);
  5223. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5224. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5225. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5226. p16h = _mm_madd_epi16(scale_h, p16h);
  5227. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5228. }
  5229. __m256 vd = _mm256_set1_ps(d);
  5230. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5231. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5232. }
  5233. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5234. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5235. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5236. #elif defined __riscv_v_intrinsic
  5237. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5238. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5239. float sumf = 0;
  5240. for (int i = 0; i < nb; ++i) {
  5241. size_t vl = 8;
  5242. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5243. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5244. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5245. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5246. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5247. memcpy(utmp, x[i].scales, 12);
  5248. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5249. const uint32_t uaux = utmp[1] & kmask1;
  5250. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5251. utmp[2] = uaux;
  5252. utmp[0] &= kmask1;
  5253. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5254. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5255. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5256. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5257. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5258. const uint8_t * restrict q4 = x[i].qs;
  5259. const int8_t * restrict q8 = y[i].qs;
  5260. vl = 32;
  5261. int32_t sum_1 = 0;
  5262. int32_t sum_2 = 0;
  5263. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5264. for (int j = 0; j < QK_K/64; ++j) {
  5265. // load Q4
  5266. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5267. // load Q8 and multiply it with lower Q4 nibble
  5268. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5269. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5270. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5271. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5272. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5273. // load Q8 and multiply it with upper Q4 nibble
  5274. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5275. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5276. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5277. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5278. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5279. q4 += 32; q8 += 64;
  5280. }
  5281. sumf += d*(sum_1 + sum_2);
  5282. }
  5283. *s = sumf;
  5284. #else
  5285. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5286. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5287. int8_t aux8[QK_K];
  5288. int16_t aux16[8];
  5289. float sums [8];
  5290. int32_t aux32[8];
  5291. memset(sums, 0, 8*sizeof(float));
  5292. float sumf = 0;
  5293. for (int i = 0; i < nb; ++i) {
  5294. const uint8_t * restrict q4 = x[i].qs;
  5295. const int8_t * restrict q8 = y[i].qs;
  5296. memset(aux32, 0, 8*sizeof(int32_t));
  5297. int8_t * restrict a = aux8;
  5298. for (int j = 0; j < QK_K/64; ++j) {
  5299. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5300. a += 32;
  5301. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5302. a += 32; q4 += 32;
  5303. }
  5304. memcpy(utmp, x[i].scales, 12);
  5305. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5306. const uint32_t uaux = utmp[1] & kmask1;
  5307. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5308. utmp[2] = uaux;
  5309. utmp[0] &= kmask1;
  5310. int sumi = 0;
  5311. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5312. a = aux8;
  5313. int is = 0;
  5314. for (int j = 0; j < QK_K/32; ++j) {
  5315. int32_t scale = scales[is++];
  5316. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5317. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5318. q8 += 8; a += 8;
  5319. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5320. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5321. q8 += 8; a += 8;
  5322. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5323. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5324. q8 += 8; a += 8;
  5325. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5326. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5327. q8 += 8; a += 8;
  5328. }
  5329. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5330. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5331. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5332. sumf -= dmin * sumi;
  5333. }
  5334. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5335. *s = sumf;
  5336. #endif
  5337. }
  5338. #else
  5339. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5340. assert(n % QK_K == 0);
  5341. const block_q4_K * restrict x = vx;
  5342. const block_q8_K * restrict y = vy;
  5343. const int nb = n / QK_K;
  5344. #ifdef __ARM_NEON
  5345. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5346. const int32x4_t mzero = vdupq_n_s32(0);
  5347. float sumf = 0;
  5348. ggml_int8x16x2_t q4bytes;
  5349. ggml_int8x16x4_t q8bytes;
  5350. float sum_mins = 0.f;
  5351. uint16_t aux16[2];
  5352. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5353. for (int i = 0; i < nb; ++i) {
  5354. const uint8_t * restrict q4 = x[i].qs;
  5355. const int8_t * restrict q8 = y[i].qs;
  5356. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5357. aux16[0] = a[0] & 0x0f0f;
  5358. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5359. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5360. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  5361. const float d = y[i].d * (float)x[i].d[0];
  5362. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5363. q8bytes = ggml_vld1q_s8_x4(q8);
  5364. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5365. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5366. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5367. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5368. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5369. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5370. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5371. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5372. sumf += d * (sumi1 + sumi2);
  5373. }
  5374. *s = sumf - sum_mins;
  5375. #elif defined __AVX2__
  5376. const __m256i m4 = _mm256_set1_epi8(0xF);
  5377. __m256 acc = _mm256_setzero_ps();
  5378. float summs = 0;
  5379. uint16_t aux16[2];
  5380. const uint8_t * scales = (const uint8_t *)aux16;
  5381. for (int i = 0; i < nb; ++i) {
  5382. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5383. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5384. const __m256 vd = _mm256_set1_ps(d);
  5385. const uint16_t * a = (const uint16_t *)x[i].scales;
  5386. aux16[0] = a[0] & 0x0f0f;
  5387. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5388. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5389. const uint8_t * restrict q4 = x[i].qs;
  5390. const int8_t * restrict q8 = y[i].qs;
  5391. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5392. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5393. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5394. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5395. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5396. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5397. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5398. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5399. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5400. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5401. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5402. }
  5403. *s = hsum_float_8(acc) - summs;
  5404. #elif defined __AVX__
  5405. const __m128i m4 = _mm_set1_epi8(0xF);
  5406. __m256 acc = _mm256_setzero_ps();
  5407. float summs = 0;
  5408. uint16_t aux16[2];
  5409. const uint8_t * scales = (const uint8_t *)aux16;
  5410. for (int i = 0; i < nb; ++i) {
  5411. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5412. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5413. const __m256 vd = _mm256_set1_ps(d);
  5414. const uint16_t * a = (const uint16_t *)x[i].scales;
  5415. aux16[0] = a[0] & 0x0f0f;
  5416. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5417. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5418. const uint8_t * restrict q4 = x[i].qs;
  5419. const int8_t * restrict q8 = y[i].qs;
  5420. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5421. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5422. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5423. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5424. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5425. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5426. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5427. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5428. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5429. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5430. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5431. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5432. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5433. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5434. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5435. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5436. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5437. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5438. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5439. }
  5440. *s = hsum_float_8(acc) - summs;
  5441. #elif defined __riscv_v_intrinsic
  5442. uint16_t s16[2];
  5443. const uint8_t * restrict scales = (const uint8_t *)s16;
  5444. float sumf = 0;
  5445. for (int i = 0; i < nb; ++i) {
  5446. const uint8_t * restrict q4 = x[i].qs;
  5447. const int8_t * restrict q8 = y[i].qs;
  5448. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5449. s16[0] = b[0] & 0x0f0f;
  5450. s16[1] = (b[0] >> 4) & 0x0f0f;
  5451. 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]));
  5452. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5453. size_t vl = 32;
  5454. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5455. // load Q4
  5456. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5457. // load Q8 and multiply it with lower Q4 nibble
  5458. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5459. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5460. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5461. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5462. // load Q8 and multiply it with upper Q4 nibble
  5463. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5464. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5465. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5466. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5467. }
  5468. *s = sumf;
  5469. #else
  5470. uint8_t aux8[QK_K];
  5471. int16_t aux16[16];
  5472. float sums [8];
  5473. memset(sums, 0, 8*sizeof(float));
  5474. uint16_t s16[2];
  5475. const uint8_t * restrict scales = (const uint8_t *)s16;
  5476. float sumf = 0;
  5477. for (int i = 0; i < nb; ++i) {
  5478. const uint8_t * restrict q4 = x[i].qs;
  5479. const int8_t * restrict q8 = y[i].qs;
  5480. uint8_t * restrict a = aux8;
  5481. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5482. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5483. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5484. s16[0] = b[0] & 0x0f0f;
  5485. s16[1] = (b[0] >> 4) & 0x0f0f;
  5486. 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]));
  5487. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5488. for (int j = 0; j < QK_K/32; ++j) {
  5489. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5490. q8 += 16; a += 16;
  5491. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5492. q8 += 16; a += 16;
  5493. const float dl = d * scales[j];
  5494. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5495. }
  5496. }
  5497. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5498. *s = sumf;
  5499. #endif
  5500. }
  5501. #endif
  5502. #if QK_K == 256
  5503. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5504. assert(n % QK_K == 0);
  5505. const block_q5_K * restrict x = vx;
  5506. const block_q8_K * restrict y = vy;
  5507. const int nb = n / QK_K;
  5508. static const uint32_t kmask1 = 0x3f3f3f3f;
  5509. static const uint32_t kmask2 = 0x0f0f0f0f;
  5510. static const uint32_t kmask3 = 0x03030303;
  5511. uint32_t utmp[4];
  5512. #ifdef __ARM_NEON
  5513. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5514. const uint8x16_t mone = vdupq_n_u8(1);
  5515. const uint8x16_t mtwo = vdupq_n_u8(2);
  5516. const int32x4_t mzero = vdupq_n_s32(0);
  5517. ggml_int8x16x4_t q5bytes;
  5518. float sumf = 0;
  5519. for (int i = 0; i < nb; ++i) {
  5520. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5521. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5522. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5523. memcpy(utmp, x[i].scales, 12);
  5524. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5525. const uint32_t uaux = utmp[1] & kmask1;
  5526. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5527. utmp[2] = uaux;
  5528. utmp[0] &= kmask1;
  5529. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5530. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5531. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5532. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5533. int32_t sumi_mins = vaddvq_s32(prod);
  5534. const uint8_t * scales = (const uint8_t *)utmp;
  5535. const uint8_t * restrict q5 = x[i].qs;
  5536. const uint8_t * restrict qh = x[i].qh;
  5537. const int8_t * restrict q8 = y[i].qs;
  5538. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5539. ggml_uint8x16x4_t q5h;
  5540. int32_t sumi = 0;
  5541. for (int j = 0; j < QK_K/64; ++j) {
  5542. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5543. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5544. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5545. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5546. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5547. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5548. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5549. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5550. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5551. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5552. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5553. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5554. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5555. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5556. }
  5557. sumf += d * sumi - dmin * sumi_mins;
  5558. }
  5559. *s = sumf;
  5560. #elif defined __AVX2__
  5561. const __m256i m4 = _mm256_set1_epi8(0xF);
  5562. const __m128i mzero = _mm_setzero_si128();
  5563. const __m256i mone = _mm256_set1_epi8(1);
  5564. __m256 acc = _mm256_setzero_ps();
  5565. float summs = 0.f;
  5566. for (int i = 0; i < nb; ++i) {
  5567. const uint8_t * restrict q5 = x[i].qs;
  5568. const int8_t * restrict q8 = y[i].qs;
  5569. #if QK_K == 256
  5570. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5571. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5572. memcpy(utmp, x[i].scales, 12);
  5573. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5574. const uint32_t uaux = utmp[1] & kmask1;
  5575. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5576. utmp[2] = uaux;
  5577. utmp[0] &= kmask1;
  5578. #else
  5579. // TODO
  5580. const float d = 0, dmin = 0;
  5581. #endif
  5582. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5583. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5584. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5585. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5586. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5587. summs += dmin * _mm_extract_epi32(hsum, 0);
  5588. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5589. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5590. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5591. __m256i hmask = mone;
  5592. __m256i sumi = _mm256_setzero_si256();
  5593. int bit = 0;
  5594. for (int j = 0; j < QK_K/64; ++j) {
  5595. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5596. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5597. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5598. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5599. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5600. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5601. hmask = _mm256_slli_epi16(hmask, 1);
  5602. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5603. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5604. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5605. hmask = _mm256_slli_epi16(hmask, 1);
  5606. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5607. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5608. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5609. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5610. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5611. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5612. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5613. }
  5614. __m256 vd = _mm256_set1_ps(d);
  5615. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5616. }
  5617. *s = hsum_float_8(acc) + summs;
  5618. #elif defined __AVX__
  5619. const __m128i m4 = _mm_set1_epi8(0xF);
  5620. const __m128i mzero = _mm_setzero_si128();
  5621. const __m128i mone = _mm_set1_epi8(1);
  5622. const __m128i m2 = _mm_set1_epi8(2);
  5623. __m256 acc = _mm256_setzero_ps();
  5624. float summs = 0.f;
  5625. for (int i = 0; i < nb; ++i) {
  5626. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5627. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5628. const uint8_t * restrict q5 = x[i].qs;
  5629. const int8_t * restrict q8 = y[i].qs;
  5630. memcpy(utmp, x[i].scales, 12);
  5631. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5632. const uint32_t uaux = utmp[1] & kmask1;
  5633. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5634. utmp[2] = uaux;
  5635. utmp[0] &= kmask1;
  5636. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5637. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5638. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5639. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5640. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5641. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5642. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5643. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5644. summs += dmin * _mm_extract_epi32(hsum, 0);
  5645. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5646. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5647. __m128i hmask = mone;
  5648. __m128i sumi_0 = _mm_setzero_si128();
  5649. __m128i sumi_1 = _mm_setzero_si128();
  5650. int bit = 0;
  5651. __m128i shuffle = _mm_set1_epi16(0x0100);
  5652. for (int j = 0; j < QK_K/64; ++j) {
  5653. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5654. shuffle = _mm_add_epi16(shuffle, m2);
  5655. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5656. shuffle = _mm_add_epi16(shuffle, m2);
  5657. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5658. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5659. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5660. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5661. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5662. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5663. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5664. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5665. hmask = _mm_slli_epi16(hmask, 1);
  5666. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5667. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5668. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5669. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5670. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5671. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5672. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5673. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5674. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5675. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5676. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5677. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5678. hmask = _mm_slli_epi16(hmask, 1);
  5679. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5680. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5681. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5682. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5683. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5684. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5685. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5686. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5687. }
  5688. __m256 vd = _mm256_set1_ps(d);
  5689. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5690. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5691. }
  5692. *s = hsum_float_8(acc) + summs;
  5693. #elif defined __riscv_v_intrinsic
  5694. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5695. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5696. float sumf = 0;
  5697. float sums = 0.0;
  5698. size_t vl;
  5699. for (int i = 0; i < nb; ++i) {
  5700. vl = 8;
  5701. const uint8_t * restrict q5 = x[i].qs;
  5702. const uint8_t * restrict hm = x[i].qh;
  5703. const int8_t * restrict q8 = y[i].qs;
  5704. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5705. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5706. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5707. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5708. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5709. memcpy(utmp, x[i].scales, 12);
  5710. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5711. const uint32_t uaux = utmp[1] & kmask1;
  5712. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5713. utmp[2] = uaux;
  5714. utmp[0] &= kmask1;
  5715. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5716. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5717. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5718. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5719. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5720. vl = 32;
  5721. int32_t aux32 = 0;
  5722. int is = 0;
  5723. uint8_t m = 1;
  5724. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5725. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5726. for (int j = 0; j < QK_K/64; ++j) {
  5727. // load Q5 and Q8
  5728. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5729. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5730. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5731. // compute mask for addition
  5732. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5733. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5734. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5735. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5736. m <<= 1;
  5737. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5738. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5739. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5740. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5741. m <<= 1;
  5742. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5743. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5744. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5745. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5746. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5747. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5748. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5749. q5 += 32; q8 += 64;
  5750. }
  5751. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5752. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5753. }
  5754. *s = sumf+sums;
  5755. #else
  5756. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5757. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5758. int8_t aux8[QK_K];
  5759. int16_t aux16[8];
  5760. float sums [8];
  5761. int32_t aux32[8];
  5762. memset(sums, 0, 8*sizeof(float));
  5763. float sumf = 0;
  5764. for (int i = 0; i < nb; ++i) {
  5765. const uint8_t * restrict q4 = x[i].qs;
  5766. const uint8_t * restrict hm = x[i].qh;
  5767. const int8_t * restrict q8 = y[i].qs;
  5768. memset(aux32, 0, 8*sizeof(int32_t));
  5769. int8_t * restrict a = aux8;
  5770. uint8_t m = 1;
  5771. for (int j = 0; j < QK_K/64; ++j) {
  5772. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5773. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5774. a += 32; m <<= 1;
  5775. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5776. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5777. a += 32; m <<= 1;
  5778. q4 += 32;
  5779. }
  5780. memcpy(utmp, x[i].scales, 12);
  5781. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5782. const uint32_t uaux = utmp[1] & kmask1;
  5783. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5784. utmp[2] = uaux;
  5785. utmp[0] &= kmask1;
  5786. int sumi = 0;
  5787. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5788. a = aux8;
  5789. int is = 0;
  5790. for (int j = 0; j < QK_K/32; ++j) {
  5791. int32_t scale = scales[is++];
  5792. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5793. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5794. q8 += 8; a += 8;
  5795. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5796. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5797. q8 += 8; a += 8;
  5798. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5799. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5800. q8 += 8; a += 8;
  5801. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5802. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5803. q8 += 8; a += 8;
  5804. }
  5805. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5806. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5807. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5808. sumf -= dmin * sumi;
  5809. }
  5810. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5811. *s = sumf;
  5812. #endif
  5813. }
  5814. #else
  5815. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5816. assert(n % QK_K == 0);
  5817. const block_q5_K * restrict x = vx;
  5818. const block_q8_K * restrict y = vy;
  5819. const int nb = n / QK_K;
  5820. #ifdef __ARM_NEON
  5821. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5822. const uint8x16_t mh = vdupq_n_u8(16);
  5823. const int32x4_t mzero = vdupq_n_s32(0);
  5824. ggml_int8x16x4_t q5bytes;
  5825. ggml_uint8x16x4_t q5h;
  5826. float sumf = 0;
  5827. for (int i = 0; i < nb; ++i) {
  5828. const float d = y[i].d * (float)x[i].d;
  5829. const int8_t * sc = x[i].scales;
  5830. const uint8_t * restrict q5 = x[i].qs;
  5831. const uint8_t * restrict qh = x[i].qh;
  5832. const int8_t * restrict q8 = y[i].qs;
  5833. const uint8x8_t qhbits = vld1_u8(qh);
  5834. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5835. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5836. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5837. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5838. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5839. q5h.val[2] = vbicq_u8(mh, htmp);
  5840. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5841. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5842. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5843. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5844. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5845. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5846. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5847. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5848. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5849. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5850. }
  5851. *s = sumf;
  5852. #elif defined __AVX2__
  5853. const __m256i m4 = _mm256_set1_epi8(0xF);
  5854. const __m256i mone = _mm256_set1_epi8(1);
  5855. __m256 acc = _mm256_setzero_ps();
  5856. for (int i = 0; i < nb; ++i) {
  5857. const uint8_t * restrict q5 = x[i].qs;
  5858. const int8_t * restrict q8 = y[i].qs;
  5859. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5860. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5861. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5862. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5863. int64_t aux64;
  5864. memcpy(&aux64, x[i].qh, 8);
  5865. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5866. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5867. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5868. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5869. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5870. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5871. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5872. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5873. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5874. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5875. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5876. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5877. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5878. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5879. }
  5880. *s = hsum_float_8(acc);
  5881. #elif defined __AVX__
  5882. const __m128i m4 = _mm_set1_epi8(0xF);
  5883. const __m128i mone = _mm_set1_epi8(1);
  5884. __m256 acc = _mm256_setzero_ps();
  5885. for (int i = 0; i < nb; ++i) {
  5886. const uint8_t * restrict q5 = x[i].qs;
  5887. const int8_t * restrict q8 = y[i].qs;
  5888. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5889. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5890. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5891. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5892. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5893. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5894. int64_t aux64;
  5895. memcpy(&aux64, x[i].qh, 8);
  5896. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5897. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5898. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5899. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5900. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5901. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5902. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5903. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5904. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5905. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5906. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5907. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5908. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5909. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5910. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5911. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5912. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5913. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5914. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5915. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5916. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5917. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5918. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5919. }
  5920. *s = hsum_float_8(acc);
  5921. #elif defined __riscv_v_intrinsic
  5922. float sumf = 0;
  5923. for (int i = 0; i < nb; ++i) {
  5924. const float d = y[i].d * (float)x[i].d;
  5925. const int8_t * sc = x[i].scales;
  5926. const uint8_t * restrict q5 = x[i].qs;
  5927. const uint8_t * restrict qh = x[i].qh;
  5928. const int8_t * restrict q8 = y[i].qs;
  5929. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5930. // load qh
  5931. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5932. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5933. size_t vl = 16;
  5934. // combine both qh_1 and qh_2
  5935. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5936. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5937. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5938. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5939. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5940. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5941. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5942. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5943. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5944. // load q5
  5945. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5946. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5947. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5948. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5949. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5950. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5951. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5952. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5953. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5954. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5955. // load Q8 and multiply it with Q5
  5956. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5957. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5958. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5959. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5960. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5961. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5962. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5963. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5964. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5965. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5966. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5967. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5968. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5969. }
  5970. *s = sumf;
  5971. #else
  5972. int8_t aux8[QK_K];
  5973. int16_t aux16[16];
  5974. float sums [8];
  5975. memset(sums, 0, 8*sizeof(float));
  5976. float sumf = 0;
  5977. for (int i = 0; i < nb; ++i) {
  5978. const uint8_t * restrict q4 = x[i].qs;
  5979. const uint8_t * restrict hm = x[i].qh;
  5980. const int8_t * restrict q8 = y[i].qs;
  5981. int8_t * restrict a = aux8;
  5982. for (int l = 0; l < 32; ++l) {
  5983. a[l+ 0] = q4[l] & 0xF;
  5984. a[l+32] = q4[l] >> 4;
  5985. }
  5986. for (int is = 0; is < 8; ++is) {
  5987. uint8_t m = 1 << is;
  5988. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5989. }
  5990. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5991. const int8_t * restrict sc = x[i].scales;
  5992. for (int j = 0; j < QK_K/16; ++j) {
  5993. const float dl = d * sc[j];
  5994. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5995. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5996. q8 += 16; a += 16;
  5997. }
  5998. }
  5999. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6000. *s = sumf;
  6001. #endif
  6002. }
  6003. #endif
  6004. #if QK_K == 256
  6005. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6006. assert(n % QK_K == 0);
  6007. const block_q6_K * restrict x = vx;
  6008. const block_q8_K * restrict y = vy;
  6009. const int nb = n / QK_K;
  6010. #ifdef __ARM_NEON
  6011. float sum = 0;
  6012. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6013. const int32x4_t vzero = vdupq_n_s32(0);
  6014. //const int8x16_t m32s = vdupq_n_s8(32);
  6015. const uint8x16_t mone = vdupq_n_u8(3);
  6016. ggml_int8x16x4_t q6bytes;
  6017. ggml_uint8x16x4_t q6h;
  6018. for (int i = 0; i < nb; ++i) {
  6019. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6020. const uint8_t * restrict q6 = x[i].ql;
  6021. const uint8_t * restrict qh = x[i].qh;
  6022. const int8_t * restrict q8 = y[i].qs;
  6023. const int8_t * restrict scale = x[i].scales;
  6024. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6025. const int8x16_t scales = vld1q_s8(scale);
  6026. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6027. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6028. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6029. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6030. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6031. int32_t isum_mins = vaddvq_s32(prod);
  6032. int32_t isum = 0;
  6033. for (int j = 0; j < QK_K/128; ++j) {
  6034. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6035. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6036. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6037. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6038. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6039. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6040. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6041. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6042. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6043. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6044. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6045. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6046. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6047. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6048. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6049. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6050. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6051. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6052. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6053. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6054. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6055. scale += 4;
  6056. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6057. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6058. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6059. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6060. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6061. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6062. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6063. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6064. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6065. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6066. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6067. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6068. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6069. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6070. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6071. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6072. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6073. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6074. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6075. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6076. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6077. scale += 4;
  6078. }
  6079. //sum += isum * d_all * y[i].d;
  6080. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6081. }
  6082. *s = sum;
  6083. #elif defined __AVX2__
  6084. const __m256i m4 = _mm256_set1_epi8(0xF);
  6085. const __m256i m2 = _mm256_set1_epi8(3);
  6086. const __m256i m32s = _mm256_set1_epi8(32);
  6087. __m256 acc = _mm256_setzero_ps();
  6088. for (int i = 0; i < nb; ++i) {
  6089. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6090. const uint8_t * restrict q4 = x[i].ql;
  6091. const uint8_t * restrict qh = x[i].qh;
  6092. const int8_t * restrict q8 = y[i].qs;
  6093. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6094. __m256i sumi = _mm256_setzero_si256();
  6095. int is = 0;
  6096. for (int j = 0; j < QK_K/128; ++j) {
  6097. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6098. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6099. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6100. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6101. is += 4;
  6102. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6103. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6104. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6105. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6106. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6107. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6108. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6109. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6110. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6111. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6112. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6113. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6114. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6115. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6116. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6117. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6118. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6119. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6120. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6121. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6122. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6123. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6124. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6125. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6126. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6127. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6128. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6129. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6130. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6131. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6132. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6133. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6134. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6135. }
  6136. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6137. }
  6138. *s = hsum_float_8(acc);
  6139. #elif defined __AVX__
  6140. const __m128i m4 = _mm_set1_epi8(0xF);
  6141. const __m128i m3 = _mm_set1_epi8(3);
  6142. const __m128i m32s = _mm_set1_epi8(32);
  6143. const __m128i m2 = _mm_set1_epi8(2);
  6144. __m256 acc = _mm256_setzero_ps();
  6145. for (int i = 0; i < nb; ++i) {
  6146. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6147. const uint8_t * restrict q4 = x[i].ql;
  6148. const uint8_t * restrict qh = x[i].qh;
  6149. const int8_t * restrict q8 = y[i].qs;
  6150. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6151. __m128i sumi_0 = _mm_setzero_si128();
  6152. __m128i sumi_1 = _mm_setzero_si128();
  6153. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6154. for (int j = 0; j < QK_K/128; ++j) {
  6155. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6156. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6157. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6158. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6159. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6160. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6161. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6162. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6163. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6164. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6165. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6166. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6167. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6168. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6169. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6170. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6171. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6172. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6173. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6174. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6175. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6176. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6177. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6178. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6179. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6180. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6181. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6182. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6183. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6184. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6185. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6186. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6187. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6188. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6189. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6190. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6191. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6192. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6193. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6194. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6195. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6196. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6197. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6198. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6199. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6200. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6201. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6202. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6203. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6204. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6205. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6206. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6207. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6208. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6209. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6210. shuffle = _mm_add_epi8(shuffle, m2);
  6211. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6212. shuffle = _mm_add_epi8(shuffle, m2);
  6213. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6214. shuffle = _mm_add_epi8(shuffle, m2);
  6215. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6216. shuffle = _mm_add_epi8(shuffle, m2);
  6217. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6218. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6219. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6220. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6221. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6222. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6223. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6224. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6225. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6226. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6227. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6228. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6229. }
  6230. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6231. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6232. }
  6233. *s = hsum_float_8(acc);
  6234. #elif defined __riscv_v_intrinsic
  6235. float sumf = 0;
  6236. for (int i = 0; i < nb; ++i) {
  6237. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6238. const uint8_t * restrict q6 = x[i].ql;
  6239. const uint8_t * restrict qh = x[i].qh;
  6240. const int8_t * restrict q8 = y[i].qs;
  6241. const int8_t * restrict scale = x[i].scales;
  6242. size_t vl;
  6243. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6244. int sum_t = 0;
  6245. int is = 0;
  6246. for (int j = 0; j < QK_K/128; ++j) {
  6247. vl = 32;
  6248. // load qh
  6249. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6250. // load Q6
  6251. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6252. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6253. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6254. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6255. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6256. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6257. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6258. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6259. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6260. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6261. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6262. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6263. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6264. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6265. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6266. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6267. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6268. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6269. // load Q8 and take product
  6270. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6271. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6272. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6273. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6274. vl = 16;
  6275. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6276. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6277. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6278. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6279. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6280. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6281. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6282. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6283. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6284. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6285. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6286. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6287. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6288. q6 += 64; qh += 32; q8 += 128; is=8;
  6289. }
  6290. sumf += d * sum_t;
  6291. }
  6292. *s = sumf;
  6293. #else
  6294. int8_t aux8[QK_K];
  6295. int16_t aux16[8];
  6296. float sums [8];
  6297. int32_t aux32[8];
  6298. memset(sums, 0, 8*sizeof(float));
  6299. float sumf = 0;
  6300. for (int i = 0; i < nb; ++i) {
  6301. const uint8_t * restrict q4 = x[i].ql;
  6302. const uint8_t * restrict qh = x[i].qh;
  6303. const int8_t * restrict q8 = y[i].qs;
  6304. memset(aux32, 0, 8*sizeof(int32_t));
  6305. int8_t * restrict a = aux8;
  6306. for (int j = 0; j < QK_K; j += 128) {
  6307. for (int l = 0; l < 32; ++l) {
  6308. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6309. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6310. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6311. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6312. }
  6313. a += 128;
  6314. q4 += 64;
  6315. qh += 32;
  6316. }
  6317. a = aux8;
  6318. int is = 0;
  6319. for (int j = 0; j < QK_K/16; ++j) {
  6320. int scale = x[i].scales[is++];
  6321. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6322. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6323. q8 += 8; a += 8;
  6324. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6325. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6326. q8 += 8; a += 8;
  6327. }
  6328. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6329. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6330. }
  6331. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6332. *s = sumf;
  6333. #endif
  6334. }
  6335. #else
  6336. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6337. assert(n % QK_K == 0);
  6338. const block_q6_K * restrict x = vx;
  6339. const block_q8_K * restrict y = vy;
  6340. const int nb = n / QK_K;
  6341. #ifdef __ARM_NEON
  6342. float sum = 0;
  6343. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6344. const int8x16_t m32s = vdupq_n_s8(32);
  6345. const int32x4_t vzero = vdupq_n_s32(0);
  6346. const uint8x16_t mone = vdupq_n_u8(3);
  6347. ggml_int8x16x4_t q6bytes;
  6348. ggml_uint8x16x4_t q6h;
  6349. for (int i = 0; i < nb; ++i) {
  6350. const float d_all = (float)x[i].d;
  6351. const uint8_t * restrict q6 = x[i].ql;
  6352. const uint8_t * restrict qh = x[i].qh;
  6353. const int8_t * restrict q8 = y[i].qs;
  6354. const int8_t * restrict scale = x[i].scales;
  6355. int32_t isum = 0;
  6356. uint8x16_t qhbits = vld1q_u8(qh);
  6357. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6358. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6359. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6360. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6361. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6362. shifted = vshrq_n_u8(qhbits, 4);
  6363. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6364. shifted = vshrq_n_u8(qhbits, 6);
  6365. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6366. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6367. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6368. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6369. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6370. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6371. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6372. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6373. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6374. sum += isum * d_all * y[i].d;
  6375. }
  6376. *s = sum;
  6377. #elif defined __AVX2__
  6378. const __m256i m4 = _mm256_set1_epi8(0xF);
  6379. const __m256i m2 = _mm256_set1_epi8(3);
  6380. const __m256i m32s = _mm256_set1_epi8(32);
  6381. __m256 acc = _mm256_setzero_ps();
  6382. for (int i = 0; i < nb; ++i) {
  6383. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6384. const uint8_t * restrict q4 = x[i].ql;
  6385. const uint8_t * restrict qh = x[i].qh;
  6386. const int8_t * restrict q8 = y[i].qs;
  6387. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6388. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6389. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6390. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6391. __m256i sumi = _mm256_setzero_si256();
  6392. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6393. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6394. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6395. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6396. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6397. 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);
  6398. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6399. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6400. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6401. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6402. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6403. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6404. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6405. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6406. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6407. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6408. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6409. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6410. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6411. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6412. }
  6413. *s = hsum_float_8(acc);
  6414. #elif defined __AVX__
  6415. const __m128i m4 = _mm_set1_epi8(0xF);
  6416. const __m128i m2 = _mm_set1_epi8(3);
  6417. const __m128i m32s = _mm_set1_epi8(32);
  6418. __m256 acc = _mm256_setzero_ps();
  6419. for (int i = 0; i < nb; ++i) {
  6420. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6421. const uint8_t * restrict q4 = x[i].ql;
  6422. const uint8_t * restrict qh = x[i].qh;
  6423. const int8_t * restrict q8 = y[i].qs;
  6424. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6425. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6426. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6427. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6428. __m128i sumi_0 = _mm_setzero_si128();
  6429. __m128i sumi_1 = _mm_setzero_si128();
  6430. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6431. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6432. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6433. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6434. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6435. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6436. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6437. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6438. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6439. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6440. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6441. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6442. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6443. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6444. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6445. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6446. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6447. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6448. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6449. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6450. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6451. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6452. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6453. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6454. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6455. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6456. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6457. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6458. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6459. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6460. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6461. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6462. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6463. }
  6464. *s = hsum_float_8(acc);
  6465. #elif defined __riscv_v_intrinsic
  6466. float sumf = 0;
  6467. for (int i = 0; i < nb; ++i) {
  6468. const float d_all = (float)x[i].d;
  6469. const uint8_t * restrict q6 = x[i].ql;
  6470. const uint8_t * restrict qh = x[i].qh;
  6471. const int8_t * restrict q8 = y[i].qs;
  6472. const int8_t * restrict scale = x[i].scales;
  6473. int32_t isum = 0;
  6474. size_t vl = 16;
  6475. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6476. // load Q6
  6477. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6478. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6479. // load qh
  6480. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6481. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6482. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6483. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6484. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6485. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6486. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6487. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6488. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6489. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6490. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6491. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6492. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6493. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6494. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6495. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6496. // load Q8 and take product
  6497. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6498. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6499. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6500. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6501. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6502. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6503. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6504. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6505. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6506. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6507. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6508. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6509. sumf += isum * d_all * y[i].d;
  6510. }
  6511. *s = sumf;
  6512. #else
  6513. int8_t aux8[QK_K];
  6514. int16_t aux16[8];
  6515. float sums [8];
  6516. int32_t aux32[8];
  6517. memset(sums, 0, 8*sizeof(float));
  6518. float sumf = 0;
  6519. for (int i = 0; i < nb; ++i) {
  6520. const uint8_t * restrict q4 = x[i].ql;
  6521. const uint8_t * restrict qh = x[i].qh;
  6522. const int8_t * restrict q8 = y[i].qs;
  6523. memset(aux32, 0, 8*sizeof(int32_t));
  6524. int8_t * restrict a = aux8;
  6525. for (int l = 0; l < 16; ++l) {
  6526. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6527. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6528. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6529. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6530. }
  6531. int is = 0;
  6532. for (int j = 0; j < QK_K/16; ++j) {
  6533. int scale = x[i].scales[is++];
  6534. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6535. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6536. q8 += 8; a += 8;
  6537. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6538. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6539. q8 += 8; a += 8;
  6540. }
  6541. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6542. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6543. }
  6544. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6545. *s = sumf;
  6546. #endif
  6547. }
  6548. #endif
  6549. static const int8_t keven_signs_q2xs[1024] = {
  6550. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6551. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6552. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6553. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6554. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6555. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6556. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6557. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6558. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6559. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6560. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6561. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6562. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6563. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6564. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6565. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6566. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6567. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6568. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6569. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6570. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6571. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6572. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6573. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6574. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6575. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6576. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6577. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6578. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6579. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6580. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6581. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6582. };
  6583. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6584. assert(n % QK_K == 0);
  6585. const block_iq2_xxs * restrict x = vx;
  6586. const block_q8_K * restrict y = vy;
  6587. const int nb = n / QK_K;
  6588. #if defined(__ARM_NEON)
  6589. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6590. uint32_t aux32[4];
  6591. const uint8_t * aux8 = (const uint8_t *)aux32;
  6592. ggml_int8x16x4_t q2u;
  6593. ggml_int8x16x4_t q2s;
  6594. ggml_int8x16x4_t q8b;
  6595. float sumf = 0;
  6596. for (int i = 0; i < nb; ++i) {
  6597. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6598. const uint16_t * restrict q2 = x[i].qs;
  6599. const int8_t * restrict q8 = y[i].qs;
  6600. float sumf1 = 0, sumf2 = 0;
  6601. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6602. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6603. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6604. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6605. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6606. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6607. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6608. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6609. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6610. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6611. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6612. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6613. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6614. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6615. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6616. 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]);
  6617. 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]);
  6618. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6619. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6620. }
  6621. sumf += d*(sumf1 + sumf2);
  6622. }
  6623. *s = 0.25f * sumf;
  6624. #elif defined(__AVX2__)
  6625. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6626. uint32_t aux32[4];
  6627. const uint8_t * aux8 = (const uint8_t *)aux32;
  6628. __m256 accumf = _mm256_setzero_ps();
  6629. for (int i = 0; i < nb; ++i) {
  6630. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6631. const uint16_t * restrict q2 = x[i].qs;
  6632. const int8_t * restrict q8 = y[i].qs;
  6633. __m256i sumi1 = _mm256_setzero_si256();
  6634. __m256i sumi2 = _mm256_setzero_si256();
  6635. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6636. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6637. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6638. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6639. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6640. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6641. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6642. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6643. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6644. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6645. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6646. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6647. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6648. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6649. const uint16_t ls1 = aux32[1] >> 28;
  6650. const uint16_t ls2 = aux32[3] >> 28;
  6651. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6652. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6653. sumi1 = _mm256_add_epi32(sumi1, p1);
  6654. sumi2 = _mm256_add_epi32(sumi2, p2);
  6655. }
  6656. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6657. }
  6658. *s = 0.125f * hsum_float_8(accumf);
  6659. #else
  6660. uint32_t aux32[2];
  6661. const uint8_t * aux8 = (const uint8_t *)aux32;
  6662. float sumf = 0.f;
  6663. for (int i = 0; i < nb; ++i) {
  6664. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6665. const uint16_t * restrict q2 = x[i].qs;
  6666. const int8_t * restrict q8 = y[i].qs;
  6667. int32_t bsum = 0;
  6668. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6669. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6670. q2 += 4;
  6671. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6672. int32_t sumi = 0;
  6673. for (int l = 0; l < 4; ++l) {
  6674. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6675. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6676. for (int j = 0; j < 8; ++j) {
  6677. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6678. }
  6679. q8 += 8;
  6680. }
  6681. bsum += sumi * ls;
  6682. }
  6683. sumf += d * bsum;
  6684. }
  6685. *s = 0.125f * sumf;
  6686. #endif
  6687. }
  6688. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6689. assert(n % QK_K == 0);
  6690. const block_iq2_xs * restrict x = vx;
  6691. const block_q8_K * restrict y = vy;
  6692. const int nb = n / QK_K;
  6693. #if defined(__ARM_NEON)
  6694. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6695. ggml_int8x16x4_t q2u;
  6696. ggml_int8x16x4_t q2s;
  6697. ggml_int8x16x4_t q8b;
  6698. int32x4x4_t scales32;
  6699. float sumf = 0;
  6700. for (int i = 0; i < nb; ++i) {
  6701. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6702. const uint16_t * restrict q2 = x[i].qs;
  6703. const int8_t * restrict q8 = y[i].qs;
  6704. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6705. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6706. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6707. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6708. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6709. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6710. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6711. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6712. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6713. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6714. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6715. int32x4_t sumi = vdupq_n_s32(0);
  6716. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6717. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6718. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6719. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6720. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6721. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6722. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6723. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6724. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6725. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6726. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6727. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6728. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6729. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6730. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6731. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6732. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6733. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6734. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6735. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6736. q2 += 8;
  6737. }
  6738. sumf += d*vaddvq_s32(sumi);
  6739. }
  6740. *s = 0.125f * sumf;
  6741. #elif defined(__AVX2__)
  6742. const __m128i m4 = _mm_set1_epi8(0xf);
  6743. const __m128i m1 = _mm_set1_epi8(1);
  6744. const __m256i m511 = _mm256_set1_epi16(511);
  6745. const __m256i mone = _mm256_set1_epi8(1);
  6746. static const uint8_t k_bit_helper[32] = {
  6747. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6748. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6749. };
  6750. static const char block_sign_shuffle_mask_1[32] = {
  6751. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6752. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6753. };
  6754. static const char block_sign_shuffle_mask_2[32] = {
  6755. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6756. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6757. };
  6758. static const uint8_t bit_selector_mask_bytes[32] = {
  6759. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6760. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6761. };
  6762. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6763. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6764. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6765. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6766. uint64_t aux64;
  6767. // somewhat hacky, but gives a significant boost in performance
  6768. __m256i aux_gindex;
  6769. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6770. __m256 accumf = _mm256_setzero_ps();
  6771. for (int i = 0; i < nb; ++i) {
  6772. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6773. const uint16_t * restrict q2 = x[i].qs;
  6774. const int8_t * restrict q8 = y[i].qs;
  6775. memcpy(&aux64, x[i].scales, 8);
  6776. __m128i stmp = _mm_set1_epi64x(aux64);
  6777. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6778. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6779. __m256i sumi1 = _mm256_setzero_si256();
  6780. __m256i sumi2 = _mm256_setzero_si256();
  6781. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6782. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6783. aux_gindex = _mm256_and_si256(q2_data, m511);
  6784. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6785. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6786. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6787. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6788. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6789. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6790. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6791. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6792. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6793. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6794. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6795. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6796. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6797. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6798. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6799. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6800. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6801. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6802. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6803. const __m256i full_signs_1 = _mm256_set_m128i(full_signs_l, full_signs_l);
  6804. const __m256i full_signs_2 = _mm256_set_m128i(full_signs_h, full_signs_h);
  6805. __m256i signs;
  6806. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6807. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6808. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6809. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6810. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6811. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6812. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6813. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6814. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6815. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6816. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6817. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6818. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6819. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6820. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6821. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6822. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6823. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6824. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6825. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6826. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6827. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6828. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6829. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  6830. }
  6831. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6832. }
  6833. *s = 0.125f * hsum_float_8(accumf);
  6834. #else
  6835. float sumf = 0.f;
  6836. for (int i = 0; i < nb; ++i) {
  6837. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6838. const uint16_t * restrict q2 = x[i].qs;
  6839. const uint8_t * restrict sc = x[i].scales;
  6840. const int8_t * restrict q8 = y[i].qs;
  6841. int32_t bsum = 0;
  6842. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6843. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6844. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6845. int32_t sumi = 0;
  6846. for (int l = 0; l < 2; ++l) {
  6847. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6848. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6849. for (int j = 0; j < 8; ++j) {
  6850. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6851. }
  6852. q8 += 8;
  6853. }
  6854. bsum += sumi * ls1;
  6855. sumi = 0;
  6856. for (int l = 2; l < 4; ++l) {
  6857. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6858. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6859. for (int j = 0; j < 8; ++j) {
  6860. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6861. }
  6862. q8 += 8;
  6863. }
  6864. bsum += sumi * ls2;
  6865. q2 += 4;
  6866. }
  6867. sumf += d * bsum;
  6868. }
  6869. *s = 0.125f * sumf;
  6870. #endif
  6871. }
  6872. // TODO
  6873. void ggml_vec_dot_iq3_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6874. assert(n % QK_K == 0);
  6875. const block_iq3_xxs * restrict x = vx;
  6876. const block_q8_K * restrict y = vy;
  6877. const int nb = n / QK_K;
  6878. #if defined(__ARM_NEON)
  6879. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6880. uint32_t aux32[2];
  6881. ggml_int8x16x4_t q3s;
  6882. ggml_int8x16x4_t q8b;
  6883. float sumf = 0;
  6884. for (int i = 0; i < nb; ++i) {
  6885. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6886. const uint8_t * restrict q3 = x[i].qs;
  6887. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6888. const int8_t * restrict q8 = y[i].qs;
  6889. float sumf1 = 0, sumf2 = 0;
  6890. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6891. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6892. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  6893. const uint32x4_t aux32x4_0 = {iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]};
  6894. const uint32x4_t aux32x4_1 = {iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]};
  6895. const uint32x4_t aux32x4_2 = {iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]};
  6896. const uint32x4_t aux32x4_3 = {iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]};
  6897. q3 += 16;
  6898. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  6899. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  6900. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6901. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6902. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  6903. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  6904. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  6905. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  6906. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  6907. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  6908. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  6909. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  6910. }
  6911. sumf += d*(sumf1 + sumf2);
  6912. }
  6913. *s = 0.5f * sumf;
  6914. #elif defined(__AVX2__)
  6915. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6916. uint32_t aux32[2];
  6917. __m256 accumf = _mm256_setzero_ps();
  6918. for (int i = 0; i < nb; ++i) {
  6919. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6920. const uint8_t * restrict q3 = x[i].qs;
  6921. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6922. const int8_t * restrict q8 = y[i].qs;
  6923. __m256i sumi1 = _mm256_setzero_si256();
  6924. __m256i sumi2 = _mm256_setzero_si256();
  6925. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6926. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6927. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6928. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  6929. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  6930. q3 += 8;
  6931. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  6932. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  6933. q3 += 8;
  6934. memcpy(aux32, gas, 8); gas += 8;
  6935. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  6936. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  6937. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6938. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6939. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6940. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6941. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6942. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6943. const uint16_t ls1 = aux32[0] >> 28;
  6944. const uint16_t ls2 = aux32[1] >> 28;
  6945. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6946. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6947. sumi1 = _mm256_add_epi32(sumi1, p1);
  6948. sumi2 = _mm256_add_epi32(sumi2, p2);
  6949. }
  6950. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6951. }
  6952. *s = 0.25f * hsum_float_8(accumf);
  6953. #else
  6954. uint32_t aux32;
  6955. float sumf = 0.f;
  6956. for (int i = 0; i < nb; ++i) {
  6957. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6958. const uint8_t * restrict q3 = x[i].qs;
  6959. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6960. const int8_t * restrict q8 = y[i].qs;
  6961. int32_t bsum = 0;
  6962. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6963. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  6964. const uint32_t ls = 2*(aux32 >> 28) + 1;
  6965. int32_t sumi = 0;
  6966. for (int l = 0; l < 4; ++l) {
  6967. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  6968. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  6969. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  6970. for (int j = 0; j < 4; ++j) {
  6971. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  6972. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  6973. }
  6974. q8 += 8;
  6975. }
  6976. q3 += 8;
  6977. bsum += sumi * ls;
  6978. }
  6979. sumf += d * bsum;
  6980. }
  6981. *s = 0.25f * sumf;
  6982. #endif
  6983. }
  6984. // ================================ IQ2 quantization =============================================
  6985. typedef struct {
  6986. uint64_t * grid;
  6987. int * map;
  6988. uint16_t * neighbours;
  6989. } iq2_entry_t;
  6990. static iq2_entry_t iq2_data[2] = {
  6991. {NULL, NULL, NULL},
  6992. {NULL, NULL, NULL},
  6993. };
  6994. static inline int iq2_data_index(int grid_size) {
  6995. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  6996. return grid_size == 256 ? 0 : 1;
  6997. }
  6998. static int iq2_compare_func(const void * left, const void * right) {
  6999. const int * l = (const int *)left;
  7000. const int * r = (const int *)right;
  7001. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7002. }
  7003. void iq2xs_init_impl(int grid_size) {
  7004. const int gindex = iq2_data_index(grid_size);
  7005. if (iq2_data[gindex].grid) {
  7006. return;
  7007. }
  7008. static const uint16_t kgrid_256[256] = {
  7009. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7010. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7011. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7012. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7013. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7014. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7015. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7016. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7017. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7018. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7019. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7020. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7021. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7022. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7023. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7024. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7025. };
  7026. static const uint16_t kgrid_512[512] = {
  7027. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7028. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7029. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7030. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7031. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7032. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7033. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7034. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7035. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7036. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7037. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7038. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7039. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7040. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7041. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7042. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7043. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7044. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7045. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7046. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7047. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7048. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7049. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7050. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7051. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7052. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7053. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7054. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7055. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7056. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7057. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7058. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7059. };
  7060. const int kmap_size = 43692;
  7061. const int nwant = 2;
  7062. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  7063. uint64_t * kgrid_q2xs;
  7064. int * kmap_q2xs;
  7065. uint16_t * kneighbors_q2xs;
  7066. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  7067. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  7068. for (int k = 0; k < grid_size; ++k) {
  7069. int8_t * pos = (int8_t *)(the_grid + k);
  7070. for (int i = 0; i < 8; ++i) {
  7071. int l = (kgrid[k] >> 2*i) & 0x3;
  7072. pos[i] = 2*l + 1;
  7073. }
  7074. }
  7075. kgrid_q2xs = the_grid;
  7076. iq2_data[gindex].grid = the_grid;
  7077. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  7078. iq2_data[gindex].map = kmap_q2xs;
  7079. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  7080. uint64_t aux64;
  7081. uint8_t * aux8 = (uint8_t *)&aux64;
  7082. for (int i = 0; i < grid_size; ++i) {
  7083. aux64 = kgrid_q2xs[i];
  7084. uint16_t index = 0;
  7085. for (int k=0; k<8; ++k) {
  7086. uint16_t q = (aux8[k] - 1)/2;
  7087. index |= (q << 2*k);
  7088. }
  7089. kmap_q2xs[index] = i;
  7090. }
  7091. int8_t pos[8];
  7092. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  7093. int num_neighbors = 0, num_not_in_map = 0;
  7094. for (int i = 0; i < kmap_size; ++i) {
  7095. if (kmap_q2xs[i] >= 0) continue;
  7096. ++num_not_in_map;
  7097. for (int k = 0; k < 8; ++k) {
  7098. int l = (i >> 2*k) & 0x3;
  7099. pos[k] = 2*l + 1;
  7100. }
  7101. for (int j = 0; j < grid_size; ++j) {
  7102. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  7103. int d2 = 0;
  7104. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7105. dist2[2*j+0] = d2;
  7106. dist2[2*j+1] = j;
  7107. }
  7108. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  7109. int n = 0; int d2 = dist2[0];
  7110. int nhave = 1;
  7111. for (int j = 0; j < grid_size; ++j) {
  7112. if (dist2[2*j] > d2) {
  7113. if (nhave == nwant) break;
  7114. d2 = dist2[2*j];
  7115. ++nhave;
  7116. }
  7117. ++n;
  7118. }
  7119. num_neighbors += n;
  7120. }
  7121. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  7122. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  7123. iq2_data[gindex].neighbours = kneighbors_q2xs;
  7124. int counter = 0;
  7125. for (int i = 0; i < kmap_size; ++i) {
  7126. if (kmap_q2xs[i] >= 0) continue;
  7127. for (int k = 0; k < 8; ++k) {
  7128. int l = (i >> 2*k) & 0x3;
  7129. pos[k] = 2*l + 1;
  7130. }
  7131. for (int j = 0; j < grid_size; ++j) {
  7132. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  7133. int d2 = 0;
  7134. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7135. dist2[2*j+0] = d2;
  7136. dist2[2*j+1] = j;
  7137. }
  7138. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  7139. kmap_q2xs[i] = -(counter + 1);
  7140. int d2 = dist2[0];
  7141. uint16_t * start = &kneighbors_q2xs[counter++];
  7142. int n = 0, nhave = 1;
  7143. for (int j = 0; j < grid_size; ++j) {
  7144. if (dist2[2*j] > d2) {
  7145. if (nhave == nwant) break;
  7146. d2 = dist2[2*j];
  7147. ++nhave;
  7148. }
  7149. kneighbors_q2xs[counter++] = dist2[2*j+1];
  7150. ++n;
  7151. }
  7152. *start = n;
  7153. }
  7154. free(dist2);
  7155. }
  7156. void iq2xs_free_impl(int grid_size) {
  7157. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  7158. const int gindex = iq2_data_index(grid_size);
  7159. if (iq2_data[gindex].grid) {
  7160. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  7161. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  7162. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  7163. }
  7164. }
  7165. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  7166. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  7167. int num_neighbors = neighbours[0];
  7168. GGML_ASSERT(num_neighbors > 0);
  7169. float best_d2 = FLT_MAX;
  7170. int grid_index = -1;
  7171. for (int j = 1; j <= num_neighbors; ++j) {
  7172. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  7173. float d2 = 0;
  7174. for (int i = 0; i < 8; ++i) {
  7175. float q = pg[i];
  7176. float diff = scale*q - xval[i];
  7177. d2 += weight[i]*diff*diff;
  7178. }
  7179. if (d2 < best_d2) {
  7180. best_d2 = d2; grid_index = neighbours[j];
  7181. }
  7182. }
  7183. GGML_ASSERT(grid_index >= 0);
  7184. const int8_t * pg = (const int8_t *)(grid + grid_index);
  7185. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  7186. return grid_index;
  7187. }
  7188. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7189. const int gindex = iq2_data_index(256);
  7190. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7191. const int * kmap_q2xs = iq2_data[gindex].map;
  7192. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7193. GGML_ASSERT(quant_weights && "missing quantization weights");
  7194. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  7195. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  7196. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  7197. GGML_ASSERT(n%QK_K == 0);
  7198. const int kMaxQ = 3;
  7199. const int nbl = n/256;
  7200. block_iq2_xxs * y = vy;
  7201. float scales[QK_K/32];
  7202. float weight[32];
  7203. float xval[32];
  7204. int8_t L[32];
  7205. int8_t Laux[32];
  7206. float waux[32];
  7207. uint8_t block_signs[4];
  7208. uint32_t q2[2*(QK_K/32)];
  7209. for (int ibl = 0; ibl < nbl; ++ibl) {
  7210. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7211. memset(q2, 0, QK_K/4);
  7212. float max_scale = 0;
  7213. const float * xbl = x + QK_K*ibl;
  7214. float sumx2 = 0;
  7215. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7216. float sigma2 = sumx2/QK_K;
  7217. for (int ib = 0; ib < QK_K/32; ++ib) {
  7218. const float * xb = xbl + 32*ib;
  7219. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7220. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7221. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7222. for (int k = 0; k < 4; ++k) {
  7223. int nflip = 0;
  7224. uint8_t s = 0;
  7225. for (int i = 0; i < 8; ++i) {
  7226. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7227. else {
  7228. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7229. }
  7230. }
  7231. if (nflip%2) {
  7232. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7233. for (int i = 1; i < 8; ++i) {
  7234. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7235. if (ax < min) {
  7236. min = ax; imin = i;
  7237. }
  7238. }
  7239. xval[8*k+imin] = -xval[8*k+imin];
  7240. s ^= (1 << imin);
  7241. }
  7242. block_signs[k] = s & 127;
  7243. }
  7244. float max = xval[0];
  7245. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7246. if (!max) {
  7247. scales[ib] = 0;
  7248. memset(L, 0, 32);
  7249. continue;
  7250. }
  7251. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  7252. float eff_max = scale*kMaxQ;
  7253. float best = 0;
  7254. for (int is = -6; is <= 6; ++is) {
  7255. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  7256. float this_scale = 1/id;
  7257. for (int k = 0; k < 4; ++k) {
  7258. for (int i = 0; i < 8; ++i) {
  7259. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7260. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7261. }
  7262. uint16_t u = 0;
  7263. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7264. int grid_index = kmap_q2xs[u];
  7265. if (grid_index < 0) {
  7266. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7267. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7268. }
  7269. }
  7270. float sumqx = 0, sumq2 = 0;
  7271. for (int i = 0; i < 32; ++i) {
  7272. float w = weight[i];
  7273. float q = 2*Laux[i] + 1;
  7274. sumqx += w*xval[i]*q;
  7275. sumq2 += w*q*q;
  7276. }
  7277. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7278. scale = sumqx/sumq2; best = scale*sumqx;
  7279. memcpy(L, Laux, 32);
  7280. }
  7281. }
  7282. if (scale > 0) {
  7283. float id = 1/scale;
  7284. for (int k = 0; k < 4; ++k) {
  7285. uint16_t u = 0;
  7286. for (int i = 0; i < 8; ++i) {
  7287. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7288. l = MAX(0, MIN(kMaxQ-1, l));
  7289. u |= (l << 2*i);
  7290. }
  7291. int grid_index = kmap_q2xs[u];
  7292. if (grid_index < 0) {
  7293. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7294. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7295. }
  7296. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  7297. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  7298. }
  7299. float sumqx = 0, sumq2 = 0;
  7300. for (int i = 0; i < 32; ++i) {
  7301. float w = weight[i];
  7302. float q = 2*L[i] + 1;
  7303. sumqx += w*xval[i]*q;
  7304. sumq2 += w*q*q;
  7305. }
  7306. if (sumq2 > 0) scale = sumqx/sumq2;
  7307. }
  7308. if (scale < 0) {
  7309. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7310. // and correspondingly flip quant signs.
  7311. scale = -scale;
  7312. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7313. }
  7314. for (int k = 0; k < 4; ++k) {
  7315. uint16_t u = 0;
  7316. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7317. int grid_index = kmap_q2xs[u];
  7318. if (grid_index < 0) {
  7319. printf("Oops: found point %u not on grid:", u);
  7320. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7321. printf("\n");
  7322. GGML_ASSERT(false);
  7323. }
  7324. q2[2*ib+0] |= (grid_index << 8*k);
  7325. q2[2*ib+1] |= (block_signs[k] << 7*k);
  7326. }
  7327. GGML_ASSERT(scale >= 0);
  7328. scales[ib] = scale;
  7329. max_scale = MAX(max_scale, scale);
  7330. }
  7331. if (!max_scale) {
  7332. memset(y[ibl].qs, 0, QK_K/4);
  7333. continue;
  7334. }
  7335. float d = max_scale/31;
  7336. y[ibl].d = GGML_FP32_TO_FP16(d);
  7337. float id = 1/d;
  7338. for (int ib = 0; ib < QK_K/32; ++ib) {
  7339. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7340. l = MAX(0, MIN(15, l));
  7341. q2[2*ib+1] |= ((uint32_t)l << 28);
  7342. }
  7343. memcpy(y[ibl].qs, q2, QK_K/4);
  7344. }
  7345. }
  7346. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7347. const int gindex = iq2_data_index(512);
  7348. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7349. const int * kmap_q2xs = iq2_data[gindex].map;
  7350. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7351. GGML_ASSERT(quant_weights && "missing quantization weights");
  7352. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  7353. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  7354. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  7355. GGML_ASSERT(n%QK_K == 0);
  7356. const int kMaxQ = 3;
  7357. const int nbl = n/256;
  7358. block_iq2_xs * y = vy;
  7359. float scales[QK_K/16];
  7360. float weight[16];
  7361. float xval[16];
  7362. int8_t L[16];
  7363. int8_t Laux[16];
  7364. float waux[16];
  7365. bool is_on_grid[2];
  7366. bool is_on_grid_aux[2];
  7367. uint8_t block_signs[2];
  7368. uint16_t q2[2*(QK_K/16)];
  7369. for (int ibl = 0; ibl < nbl; ++ibl) {
  7370. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7371. memset(q2, 0, QK_K/4);
  7372. memset(y[ibl].scales, 0, QK_K/32);
  7373. float max_scale = 0;
  7374. const float * xbl = x + QK_K*ibl;
  7375. float sumx2 = 0;
  7376. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7377. float sigma2 = sumx2/QK_K;
  7378. for (int ib = 0; ib < QK_K/16; ++ib) {
  7379. const float * xb = xbl + 16*ib;
  7380. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  7381. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7382. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  7383. for (int k = 0; k < 2; ++k) {
  7384. int nflip = 0;
  7385. uint8_t s = 0;
  7386. for (int i = 0; i < 8; ++i) {
  7387. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7388. else {
  7389. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7390. }
  7391. }
  7392. if (nflip%2) {
  7393. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7394. for (int i = 1; i < 8; ++i) {
  7395. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7396. if (ax < min) {
  7397. min = ax; imin = i;
  7398. }
  7399. }
  7400. xval[8*k+imin] = -xval[8*k+imin];
  7401. s ^= (1 << imin);
  7402. }
  7403. block_signs[k] = s & 127;
  7404. }
  7405. float max = xval[0];
  7406. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  7407. if (!max) {
  7408. scales[ib] = 0;
  7409. memset(L, 0, 16);
  7410. continue;
  7411. }
  7412. float best = 0;
  7413. float scale = max/(2*kMaxQ-1);
  7414. is_on_grid[0] = is_on_grid[1] = true;
  7415. for (int is = -9; is <= 9; ++is) {
  7416. float id = (2*kMaxQ-1+is*0.1f)/max;
  7417. float this_scale = 1/id;
  7418. for (int k = 0; k < 2; ++k) {
  7419. for (int i = 0; i < 8; ++i) {
  7420. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7421. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7422. }
  7423. uint16_t u = 0;
  7424. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7425. int grid_index = kmap_q2xs[u];
  7426. is_on_grid_aux[k] = true;
  7427. if (grid_index < 0) {
  7428. is_on_grid_aux[k] = false;
  7429. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7430. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7431. }
  7432. }
  7433. float sumqx = 0, sumq2 = 0;
  7434. for (int i = 0; i < 16; ++i) {
  7435. float w = weight[i];
  7436. float q = 2*Laux[i] + 1;
  7437. sumqx += w*xval[i]*q;
  7438. sumq2 += w*q*q;
  7439. }
  7440. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7441. scale = sumqx/sumq2; best = scale*sumqx;
  7442. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  7443. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7444. }
  7445. }
  7446. int n_not_ongrid = 0;
  7447. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7448. if (n_not_ongrid > 0 && scale > 0) {
  7449. float id = 1/scale;
  7450. for (int k = 0; k < 2; ++k) {
  7451. if (is_on_grid[k]) continue;
  7452. uint16_t u = 0;
  7453. for (int i = 0; i < 8; ++i) {
  7454. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7455. l = MAX(0, MIN(kMaxQ-1, l));
  7456. u |= (l << 2*i);
  7457. L[8*k + i] = l;
  7458. }
  7459. int grid_index = kmap_q2xs[u];
  7460. if (grid_index < 0) {
  7461. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7462. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7463. }
  7464. }
  7465. float sumqx = 0, sumq2 = 0;
  7466. for (int i = 0; i < 16; ++i) {
  7467. float w = weight[i];
  7468. float q = 2*L[i] + 1;
  7469. sumqx += w*xval[i]*q;
  7470. sumq2 += w*q*q;
  7471. }
  7472. if (sumq2 > 0) scale = sumqx/sumq2;
  7473. }
  7474. if (scale < 0) {
  7475. scale = -scale;
  7476. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7477. }
  7478. for (int k = 0; k < 2; ++k) {
  7479. uint16_t u = 0;
  7480. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7481. int grid_index = kmap_q2xs[u];
  7482. if (grid_index < 0) {
  7483. printf("Oops: found point %u not on grid:", u);
  7484. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7485. printf("\n");
  7486. GGML_ASSERT(false);
  7487. }
  7488. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  7489. }
  7490. GGML_ASSERT(scale >= 0);
  7491. scales[ib] = scale;
  7492. max_scale = MAX(max_scale, scale);
  7493. }
  7494. if (!max_scale) {
  7495. memset(y[ibl].qs, 0, QK_K/4);
  7496. continue;
  7497. }
  7498. float d = max_scale/31;
  7499. y[ibl].d = GGML_FP32_TO_FP16(d);
  7500. float id = 1/d;
  7501. for (int ib = 0; ib < QK_K/16; ++ib) {
  7502. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7503. l = MAX(0, MIN(15, l));
  7504. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  7505. else y[ibl].scales[ib/2] |= (l << 4);
  7506. }
  7507. memcpy(y[ibl].qs, q2, QK_K/4);
  7508. }
  7509. }
  7510. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7511. (void)hist;
  7512. GGML_ASSERT(n_per_row%QK_K == 0);
  7513. int nblock = n_per_row/QK_K;
  7514. char * qrow = (char *)dst;
  7515. for (int row = 0; row < nrow; ++row) {
  7516. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  7517. src += n_per_row;
  7518. qrow += nblock*sizeof(block_iq2_xxs);
  7519. }
  7520. return nrow * nblock * sizeof(block_iq2_xxs);
  7521. }
  7522. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7523. (void)hist;
  7524. GGML_ASSERT(n_per_row%QK_K == 0);
  7525. int nblock = n_per_row/QK_K;
  7526. char * qrow = (char *)dst;
  7527. for (int row = 0; row < nrow; ++row) {
  7528. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  7529. src += n_per_row;
  7530. qrow += nblock*sizeof(block_iq2_xs);
  7531. }
  7532. return nrow * nblock * sizeof(block_iq2_xs);
  7533. }
  7534. //
  7535. // ============================================= 3-bit using D4 lattice
  7536. //
  7537. typedef struct {
  7538. uint32_t * grid;
  7539. int * map;
  7540. uint16_t * neighbours;
  7541. } iq3_entry_t;
  7542. static iq3_entry_t iq3_data[1] = {
  7543. {NULL, NULL, NULL},
  7544. };
  7545. static inline int iq3_data_index(int grid_size) {
  7546. (void)grid_size;
  7547. GGML_ASSERT(grid_size == 256);
  7548. return 0;
  7549. }
  7550. static int iq3_compare_func(const void * left, const void * right) {
  7551. const int * l = (const int *)left;
  7552. const int * r = (const int *)right;
  7553. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7554. }
  7555. void iq3xs_init_impl(int grid_size) {
  7556. const int gindex = iq3_data_index(grid_size);
  7557. if (iq3_data[gindex].grid) {
  7558. return;
  7559. }
  7560. static const uint16_t kgrid_256[256] = {
  7561. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  7562. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  7563. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  7564. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  7565. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  7566. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  7567. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  7568. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  7569. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  7570. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  7571. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  7572. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  7573. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  7574. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  7575. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  7576. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  7577. };
  7578. const int kmap_size = 4096;
  7579. const int nwant = 2;
  7580. const uint16_t * kgrid = kgrid_256;
  7581. uint32_t * kgrid_q3xs;
  7582. int * kmap_q3xs;
  7583. uint16_t * kneighbors_q3xs;
  7584. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  7585. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  7586. for (int k = 0; k < grid_size; ++k) {
  7587. int8_t * pos = (int8_t *)(the_grid + k);
  7588. for (int i = 0; i < 4; ++i) {
  7589. int l = (kgrid[k] >> 3*i) & 0x7;
  7590. pos[i] = 2*l + 1;
  7591. }
  7592. }
  7593. kgrid_q3xs = the_grid;
  7594. iq3_data[gindex].grid = the_grid;
  7595. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  7596. iq3_data[gindex].map = kmap_q3xs;
  7597. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  7598. uint32_t aux32;
  7599. uint8_t * aux8 = (uint8_t *)&aux32;
  7600. for (int i = 0; i < grid_size; ++i) {
  7601. aux32 = kgrid_q3xs[i];
  7602. uint16_t index = 0;
  7603. for (int k=0; k<4; ++k) {
  7604. uint16_t q = (aux8[k] - 1)/2;
  7605. index |= (q << 3*k);
  7606. }
  7607. kmap_q3xs[index] = i;
  7608. }
  7609. int8_t pos[4];
  7610. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  7611. int num_neighbors = 0, num_not_in_map = 0;
  7612. for (int i = 0; i < kmap_size; ++i) {
  7613. if (kmap_q3xs[i] >= 0) continue;
  7614. ++num_not_in_map;
  7615. for (int k = 0; k < 4; ++k) {
  7616. int l = (i >> 3*k) & 0x7;
  7617. pos[k] = 2*l + 1;
  7618. }
  7619. for (int j = 0; j < grid_size; ++j) {
  7620. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  7621. int d2 = 0;
  7622. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7623. dist2[2*j+0] = d2;
  7624. dist2[2*j+1] = j;
  7625. }
  7626. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  7627. int n = 0; int d2 = dist2[0];
  7628. int nhave = 1;
  7629. for (int j = 0; j < grid_size; ++j) {
  7630. if (dist2[2*j] > d2) {
  7631. if (nhave == nwant) break;
  7632. d2 = dist2[2*j];
  7633. ++nhave;
  7634. }
  7635. ++n;
  7636. }
  7637. num_neighbors += n;
  7638. }
  7639. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  7640. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  7641. iq3_data[gindex].neighbours = kneighbors_q3xs;
  7642. int counter = 0;
  7643. for (int i = 0; i < kmap_size; ++i) {
  7644. if (kmap_q3xs[i] >= 0) continue;
  7645. for (int k = 0; k < 4; ++k) {
  7646. int l = (i >> 3*k) & 0x7;
  7647. pos[k] = 2*l + 1;
  7648. }
  7649. for (int j = 0; j < grid_size; ++j) {
  7650. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  7651. int d2 = 0;
  7652. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7653. dist2[2*j+0] = d2;
  7654. dist2[2*j+1] = j;
  7655. }
  7656. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  7657. kmap_q3xs[i] = -(counter + 1);
  7658. int d2 = dist2[0];
  7659. uint16_t * start = &kneighbors_q3xs[counter++];
  7660. int n = 0, nhave = 1;
  7661. for (int j = 0; j < grid_size; ++j) {
  7662. if (dist2[2*j] > d2) {
  7663. if (nhave == nwant) break;
  7664. d2 = dist2[2*j];
  7665. ++nhave;
  7666. }
  7667. kneighbors_q3xs[counter++] = dist2[2*j+1];
  7668. ++n;
  7669. }
  7670. *start = n;
  7671. }
  7672. free(dist2);
  7673. }
  7674. void iq3xs_free_impl(int grid_size) {
  7675. GGML_ASSERT(grid_size == 256);
  7676. const int gindex = iq3_data_index(grid_size);
  7677. if (iq3_data[gindex].grid) {
  7678. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  7679. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  7680. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  7681. }
  7682. }
  7683. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  7684. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  7685. int num_neighbors = neighbours[0];
  7686. GGML_ASSERT(num_neighbors > 0);
  7687. float best_d2 = FLT_MAX;
  7688. int grid_index = -1;
  7689. for (int j = 1; j <= num_neighbors; ++j) {
  7690. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  7691. float d2 = 0;
  7692. for (int i = 0; i < 4; ++i) {
  7693. float q = pg[i];
  7694. float diff = scale*q - xval[i];
  7695. d2 += weight[i]*diff*diff;
  7696. }
  7697. if (d2 < best_d2) {
  7698. best_d2 = d2; grid_index = neighbours[j];
  7699. }
  7700. }
  7701. GGML_ASSERT(grid_index >= 0);
  7702. const int8_t * pg = (const int8_t *)(grid + grid_index);
  7703. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  7704. return grid_index;
  7705. }
  7706. static void quantize_row_iq3_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7707. const int gindex = iq3_data_index(256);
  7708. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  7709. const int * kmap_q3xs = iq3_data[gindex].map;
  7710. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  7711. //GGML_ASSERT(quant_weights && "missing quantization weights");
  7712. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  7713. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  7714. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  7715. GGML_ASSERT(n%QK_K == 0);
  7716. const int kMaxQ = 8;
  7717. const int nbl = n/256;
  7718. block_iq3_xxs * y = vy;
  7719. float scales[QK_K/32];
  7720. float weight[32];
  7721. float xval[32];
  7722. int8_t L[32];
  7723. int8_t Laux[32];
  7724. float waux[32];
  7725. bool is_on_grid[8];
  7726. bool is_on_grid_aux[8];
  7727. uint8_t block_signs[8];
  7728. uint8_t q3[3*(QK_K/8)];
  7729. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  7730. for (int ibl = 0; ibl < nbl; ++ibl) {
  7731. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7732. memset(q3, 0, 3*QK_K/8);
  7733. float max_scale = 0;
  7734. const float * xbl = x + QK_K*ibl;
  7735. float sumx2 = 0;
  7736. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7737. float sigma2 = sumx2/QK_K;
  7738. for (int ib = 0; ib < QK_K/32; ++ib) {
  7739. const float * xb = xbl + 32*ib;
  7740. if (quant_weights) {
  7741. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7742. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7743. } else {
  7744. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  7745. }
  7746. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7747. for (int k = 0; k < 4; ++k) {
  7748. int nflip = 0;
  7749. uint8_t s = 0;
  7750. for (int i = 0; i < 8; ++i) {
  7751. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7752. else {
  7753. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7754. }
  7755. }
  7756. if (nflip%2) {
  7757. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7758. for (int i = 1; i < 8; ++i) {
  7759. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7760. if (ax < min) {
  7761. min = ax; imin = i;
  7762. }
  7763. }
  7764. xval[8*k+imin] = -xval[8*k+imin];
  7765. s ^= (1 << imin);
  7766. }
  7767. block_signs[k] = s & 127;
  7768. }
  7769. float max = xval[0];
  7770. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7771. if (!max) {
  7772. scales[ib] = 0;
  7773. memset(L, 0, 32);
  7774. continue;
  7775. }
  7776. float best = 0;
  7777. float scale = max/(2*kMaxQ-1);
  7778. for (int is = -15; is <= 15; ++is) {
  7779. float id = (2*kMaxQ-1+is*0.2f)/max;
  7780. float this_scale = 1/id;
  7781. for (int k = 0; k < 8; ++k) {
  7782. for (int i = 0; i < 4; ++i) {
  7783. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  7784. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7785. }
  7786. uint16_t u = 0;
  7787. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  7788. int grid_index = kmap_q3xs[u];
  7789. is_on_grid_aux[k] = true;
  7790. if (grid_index < 0) {
  7791. is_on_grid_aux[k] = false;
  7792. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  7793. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  7794. }
  7795. }
  7796. float sumqx = 0, sumq2 = 0;
  7797. for (int i = 0; i < 32; ++i) {
  7798. float w = weight[i];
  7799. float q = 2*Laux[i] + 1;
  7800. sumqx += w*xval[i]*q;
  7801. sumq2 += w*q*q;
  7802. }
  7803. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7804. scale = sumqx/sumq2; best = scale*sumqx;
  7805. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  7806. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7807. }
  7808. }
  7809. int n_not_ongrid = 0;
  7810. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7811. if (n_not_ongrid > 0 && scale > 0) {
  7812. float id = 1/scale;
  7813. for (int k = 0; k < 8; ++k) {
  7814. if (is_on_grid[k]) continue;
  7815. uint16_t u = 0;
  7816. for (int i = 0; i < 4; ++i) {
  7817. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  7818. l = MAX(0, MIN(kMaxQ-1, l));
  7819. u |= (l << 3*i);
  7820. }
  7821. int grid_index = kmap_q3xs[u];
  7822. if (grid_index < 0) {
  7823. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  7824. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  7825. }
  7826. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  7827. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  7828. }
  7829. float sumqx = 0, sumq2 = 0;
  7830. for (int i = 0; i < 32; ++i) {
  7831. float w = weight[i];
  7832. float q = 2*L[i] + 1;
  7833. sumqx += w*xval[i]*q;
  7834. sumq2 += w*q*q;
  7835. }
  7836. if (sumq2 > 0) scale = sumqx/sumq2;
  7837. }
  7838. if (scale < 0) {
  7839. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7840. // and correspondingly flip quant signs.
  7841. scale = -scale;
  7842. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7843. }
  7844. for (int k = 0; k < 8; ++k) {
  7845. uint16_t u = 0;
  7846. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  7847. int grid_index = kmap_q3xs[u];
  7848. if (grid_index < 0) {
  7849. printf("Oops: found point %u not on grid:", u);
  7850. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  7851. printf("\n");
  7852. GGML_ASSERT(false);
  7853. }
  7854. q3[8*ib+k] = grid_index;
  7855. }
  7856. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  7857. GGML_ASSERT(scale >= 0);
  7858. scales[ib] = scale;
  7859. max_scale = MAX(max_scale, scale);
  7860. }
  7861. if (!max_scale) {
  7862. memset(y[ibl].qs, 0, 3*QK_K/8);
  7863. continue;
  7864. }
  7865. float d = max_scale/31;
  7866. y[ibl].d = GGML_FP32_TO_FP16(d);
  7867. float id = 1/d;
  7868. float sumqx = 0, sumq2 = 0;
  7869. for (int ib = 0; ib < QK_K/32; ++ib) {
  7870. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7871. l = MAX(0, MIN(15, l));
  7872. scales_and_signs[ib] |= ((uint32_t)l << 28);
  7873. if (false) {
  7874. const float * xb = xbl + 32*ib;
  7875. if (quant_weights) {
  7876. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7877. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7878. } else {
  7879. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  7880. }
  7881. const float db = 0.25f * d * (1 + 2*l);
  7882. for (int k = 0; k < 8; ++k) {
  7883. const int8_t * signs = keven_signs_q2xs + 8*((scales_and_signs[ib] >> 7*(k/2)) & 127) + 4*(k%2);
  7884. const float * xk = xb + 4*k;
  7885. const float * wk = weight + 4*k;
  7886. //const uint8_t * grid = (const uint8_t *)(kgrid_q3xs + q3[8*ib+k]);
  7887. const uint8_t * grid = (const uint8_t *)(iq3xxs_grid + q3[8*ib+k]);
  7888. float best_mse = 0; int best_index = q3[8*ib+k];
  7889. for (int j = 0; j < 4; ++j) {
  7890. float diff = db * grid[j] * signs[j] - xk[j];
  7891. best_mse += wk[j] * diff * diff;
  7892. }
  7893. for (int idx = 0; idx < 256; ++idx) {
  7894. //grid = (const uint8_t *)(kgrid_q3xs + idx);
  7895. grid = (const uint8_t *)(iq3xxs_grid + idx);
  7896. float mse = 0;
  7897. for (int j = 0; j < 4; ++j) {
  7898. float diff = db * grid[j] * signs[j] - xk[j];
  7899. mse += wk[j] * diff * diff;
  7900. }
  7901. if (mse < best_mse) {
  7902. best_mse = mse; best_index = idx;
  7903. }
  7904. }
  7905. q3[8*ib+k] = best_index;
  7906. //grid = (const uint8_t *)(kgrid_q3xs + best_index);
  7907. grid = (const uint8_t *)(iq3xxs_grid + best_index);
  7908. for (int j = 0; j < 4; ++j) {
  7909. float q = db * grid[j] * signs[j];
  7910. sumqx += wk[j] * q * xk[j];
  7911. sumq2 += wk[j] * q * q;
  7912. }
  7913. }
  7914. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  7915. }
  7916. }
  7917. memcpy(y[ibl].qs, q3, 3*QK_K/8);
  7918. }
  7919. }
  7920. size_t quantize_iq3_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7921. (void)hist;
  7922. GGML_ASSERT(n_per_row%QK_K == 0);
  7923. int nblock = n_per_row/QK_K;
  7924. char * qrow = (char *)dst;
  7925. for (int row = 0; row < nrow; ++row) {
  7926. quantize_row_iq3_xxs_impl(src, qrow, n_per_row, quant_weights);
  7927. src += n_per_row;
  7928. qrow += nblock*sizeof(block_iq3_xxs);
  7929. }
  7930. return nrow * nblock * sizeof(block_iq3_xxs);
  7931. }
  7932. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  7933. assert(k % QK_K == 0);
  7934. block_iq3_xxs * restrict y = vy;
  7935. quantize_row_iq3_xxs_reference(x, y, k);
  7936. }
  7937. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  7938. assert(k % QK_K == 0);
  7939. quantize_row_iq3_xxs_impl(x, y, k, NULL);
  7940. }